protected function WebTestBase::drupalCreateNode

Creates a node based on default settings.

Parameters

array $settings: (optional) An associative array of settings for the node, as used in entity_create(). Override the defaults by specifying the key and value in the array, for example:

$this
  ->drupalCreateNode(array(
  'title' => t('Hello, world!'),
  'type' => 'article',
));

The following defaults are provided:

  • body: Random string using the default filter format:
$settings['body'][LANGUAGE_NOT_SPECIFIED][0] = array(
  'value' => $this
    ->randomName(32),
  'format' => filter_default_format(),
);
  • title: Random string.
  • comment: COMMENT_NODE_OPEN.
  • changed: REQUEST_TIME.
  • promote: NODE_NOT_PROMOTED.
  • log: Empty string.
  • status: NODE_PUBLISHED.
  • sticky: NODE_NOT_STICKY.
  • type: 'page'.
  • langcode: LANGUAGE_NOT_SPECIFIED. (If a 'langcode' key is provided in the array, this language code will also be used for a randomly generated body field for that language, and the body for LANGUAGE_NOT_SPECIFIED will remain empty.)
  • uid: The currently logged in user, or the user running test.
  • revision: 1. (Backwards-compatible binary flag indicating whether a new revision should be created; use 1 to specify a new revision.)

Return value

Drupal\node\Node The created node entity.

147 calls to WebTestBase::drupalCreateNode()
ApiDataTest::setUp in drupal/core/modules/views/lib/Drupal/views/Tests/Field/ApiDataTest.php
Sets up a Drupal site for running functional and integration tests.
ArgumentStringTest::testGlossary in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/ArgumentStringTest.php
Tests the glossary feature.
BasicTest::testViewsWizardAndListing in drupal/core/modules/views/lib/Drupal/views/Tests/Wizard/BasicTest.php
BreadcrumbTest::testBreadCrumbs in drupal/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php
Tests breadcrumbs on node and administrative paths.
BulkFormTest::testBulkForm in drupal/core/modules/action/lib/Drupal/action/Tests/BulkFormTest.php
Tests the bulk form.

... See full list

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php, line 226
Definition of Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

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

  // Populate defaults array.
  $settings += array(
    'body' => array(
      LANGUAGE_NOT_SPECIFIED => array(
        array(),
      ),
    ),
    'title' => $this
      ->randomName(8),
    'changed' => REQUEST_TIME,
    'promote' => NODE_NOT_PROMOTED,
    'revision' => 1,
    'log' => '',
    'status' => NODE_PUBLISHED,
    'sticky' => NODE_NOT_STICKY,
    'type' => 'page',
    'langcode' => LANGUAGE_NOT_SPECIFIED,
  );

  // Add in comment settings for nodes.
  if (module_exists('comment')) {
    $settings += array(
      'comment' => COMMENT_NODE_OPEN,
    );
  }

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

  // Merge body field value and format separately.
  $body = array(
    'value' => $this
      ->randomName(32),
    'format' => filter_default_format(),
  );
  if (empty($settings['body'][$settings['langcode']])) {
    $settings['body'][$settings['langcode']][0] = array();
  }
  $settings['body'][$settings['langcode']][0] += $body;
  $node = entity_create('node', $settings);
  if (!empty($settings['revision'])) {
    $node
      ->setNewRevision();
  }
  $node
    ->save();

  // Small hack to link revisions to our test user.
  db_update('node_revision')
    ->fields(array(
    'uid' => $node->uid,
  ))
    ->condition('vid', $node->vid)
    ->execute();
  return $node;
}