You are here

function DrupalTestCase::drupalCreateNode in SimpleTest 6

Creates a node based on default settings.

Parameters

settings An array of settings to change from the defaults, in the form of 'body' => 'Hello, world!':

4 calls to DrupalTestCase::drupalCreateNode()
ActionsContentTest::testActionsContent in tests/content_actions.test
Various tests, all in one function to assure they happen in the right order.
CommentModuleTestCase::setUp in tests/comment_module.test
NodeRevisionsTest::prepareRevisions in tests/node_revisions.test
Setup function used by tests. Creates a node with three revisions.
PageViewTest::testPageView in tests/page_view.test

File

./drupal_test_case.php, line 35

Class

DrupalTestCase
Test case for typical Drupal tests. Extends WebTestCase for comfortable browser usage but also implements all UnitTestCase methods, I wish WebTestCase would do this.

Code

function drupalCreateNode($settings = []) {

  // 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;
}