diff --git a/drupal_test_case.php b/drupal_test_case.php index caa79e6..dc9c8cb 100644 --- a/drupal_test_case.php +++ b/drupal_test_case.php @@ -9,10 +9,12 @@ */ class DrupalTestCase extends WebTestCase { var $_content; - var $_cleanupModules = array(); - var $_cleanupVariables = array(); - var $_cleanupUsers = array(); - var $_cleanupRoles = array(); + var $_cleanupModules = array(); + var $_cleanupVariables = array(); + var $_cleanupUsers = array(); + var $_cleanupRoles = array(); + var $_cleanupNodes = array(); + var $_cleanupContentTypes = array(); function DrupalTestCase($label = NULL) { @@ -26,6 +28,91 @@ class DrupalTestCase extends WebTestCase { } /** + * Creates a node based on default settings. + * + * @param settings An array of settings to change from the defaults, in the form of 'body' => 'Hello, world!' + */ + function drupalCreateNode($settings = array()) { + + // Populate defaults array + $defaults = array( + 'body' => $this->randomName(32), + 'title' => $this->randomName(8), + 'comment' => 2, + 'changed' => time(), + 'format' => 1, + 'moderate' => 0, + 'promote' => 0, + 'revision' => 1, + 'log' => '', + 'status' => 1, + 'sticky' => 0, + 'type' => 'page', + 'revisions' => NULL, + 'taxonomy' => NULL, + ); + $defaults['teaser'] = $defaults['body']; + // If we already have a node, we use the original node's created time, and this + $defaults['date'] = format_date($defaults['created'], 'custom', 'Y-m-d H:i:s O'); + + if (empty($settings['uid'])) { + global $user; + $defaults['uid'] = $user->uid; + } + $node = ($settings + $defaults); + $node = (object)$node; + + node_save($node); + + // small hack to link revisions to our test user + db_query('UPDATE {node_revisions} SET uid = %d WHERE vid = %d', $node->uid, $node->vid); + $this->_cleanupNodes[] = $node->nid; + return $node; + } + + /** + * Creates a custom content type based on default settings. + * + * @param settings An array of settings to change from the defaults, in the form of 'type' => 'foo' + */ + function drupalCreateContentType($settings = array()) { + // find a non-existent random type name. + do { + $name = strtolower($this->randomName(3, 'type_')); + } while (node_get_types('type', $name)); + + // Populate defaults array + $defaults = array( + 'type' => $name, + 'name' => $name, + 'description' => '', + 'help' => '', + 'min_word_count' => 0, + 'title_label' => 'Title', + 'body_label' => 'Body', + 'has_title' => 1, + 'has_body' => 1, + ); + // imposed values for a custom type + $forced = array( + 'orig_type' => '', + 'old_type' => '', + 'module' => 'node', + 'custom' => 1, + 'modified' => 1, + 'locked' => 0, + ); + $type = $forced + $settings + $defaults; + $type = (object)$type; + + node_type_save($type); + node_types_rebuild(); + + $this->_cleanupContentTypes[] = $type->type; + return $type; + } + + /** * @abstract Checks to see if we need to send * a http-auth header to authenticate * when browsing a site. @@ -353,6 +440,12 @@ class DrupalTestCase extends WebTestCase { } $this->_cleanupVariables = array(); + //delete nodes + foreach ($this->_cleanupNodes as $nid) { + node_delete($nid); + } + $this->_cleanupNodes = array(); + while (sizeof($this->_cleanupRoles) > 0) { $rid = array_pop($this->_cleanupRoles); db_query("DELETE FROM {role} WHERE rid = %d", $rid); @@ -368,6 +461,12 @@ class DrupalTestCase extends WebTestCase { } user_delete(array(), $uid); } + + //delete content types + foreach ($this->_cleanupContentTypes as $type) { + node_type_delete($type); + } + $this->_cleanupContentTypes = array(); //Output drupal warnings and messages into assert messages $drupal_msgs = drupal_get_messages(); diff --git a/drupal_unit_tests.php b/drupal_unit_tests.php index c2826cb..1fdeda3 100644 --- a/drupal_unit_tests.php +++ b/drupal_unit_tests.php @@ -87,9 +87,6 @@ class DrupalUnitTests extends DrupalTestSuite { $info = $test->get_info(); $groups[$info['group']][] = $test; } - else { - $groups[$class][] = $test; - } } /**