You are here

protected function DrupalWebTestCase::drupalCreateNode in SimpleTest 6.2

Same name and namespace in other branches
  1. 7.2 drupal_web_test_case.php \DrupalWebTestCase::drupalCreateNode()
  2. 7 drupal_web_test_case.php \DrupalWebTestCase::drupalCreateNode()

Creates a node based on default settings.

Parameters

$settings: An associative array of settings to change from the defaults, keys are node properties, for example 'title' => 'Hello, world!'.

Return value

Created node object.

File

./drupal_web_test_case.php, line 855

Class

DrupalWebTestCase
Test case for typical Drupal tests.

Code

protected function drupalCreateNode($settings = array()) {

  // Populate defaults array.
  $settings += array(
    'body' => $this
      ->randomName(32),
    'format' => FILTER_FORMAT_DEFAULT,
    'title' => $this
      ->randomName(8),
    'comment' => 2,
    'changed' => time(),
    'moderate' => 0,
    'promote' => 0,
    'revision' => 1,
    'log' => '',
    'status' => 1,
    'sticky' => 0,
    'type' => 'page',
    'revisions' => NULL,
    'language' => '',
  );

  // Use the original node's created time for existing nodes.
  if (isset($settings['created']) && !isset($settings['date'])) {
    $settings['date'] = format_date($settings['created'], 'custom', 'Y-m-d H:i:s O');
  }

  // If the node's user uid is not specified manually, use the currently
  // logged in user if available, or else the user running the test.
  if (!isset($settings['uid'])) {
    if ($this->loggedInUser) {
      $settings['uid'] = $this->loggedInUser->uid;
    }
    else {
      global $user;
      $settings['uid'] = $user->uid;
    }
  }
  $node = (object) $settings;
  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);
  return $node;
}