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'][0] = array(
  'value' => $this
    ->randomName(32),
  'format' => filter_default_format(),
);

Return value

\Drupal\node\Plugin\Core\Entity\Node The created node entity.

172 calls to WebTestBase::drupalCreateNode()
ApiDataTest::setUp in drupal/core/modules/field/lib/Drupal/field/Tests/Views/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 238
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(
      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::LANGCODE_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.
  $settings['body'][0] += array(
    'value' => $this
      ->randomName(32),
    'format' => filter_default_format(),
  );
  $node = entity_create('node', $settings);
  if (!empty($settings['revision'])) {
    $node
      ->setNewRevision();
  }
  $node
    ->save();
  return $node;
}