You are here

protected function NodeCreationTrait::createNode in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::createNode()
  2. 9 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::createNode()

Creates a node based on default settings.

Parameters

array $values: (optional) An associative array of values for the node, as used in creation of entity. 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:
$values['body'][0] = array(
  'value' => $this
    ->randomMachineName(32),
  'format' => filter_default_format(),
);
  • title: Random string.
  • type: 'page'.
  • uid: The currently logged in user, or anonymous.

Return value

\Drupal\node\NodeInterface The created node entity.

61 calls to NodeCreationTrait::createNode()
BasicTest::testViewsWizardAndListing in core/modules/views/tests/src/Functional/Wizard/BasicTest.php
BreadcrumbFrontCacheContextsTest::setUp in core/modules/system/tests/src/Functional/Menu/BreadcrumbFrontCacheContextsTest.php
BreadcrumbTest::testBreadCrumbs in core/modules/system/tests/src/Functional/Menu/BreadcrumbTest.php
Tests breadcrumbs on node and administrative paths.
BulkFormAccessTest::testNodeDeleteAccess in core/modules/node/tests/src/Functional/Views/BulkFormAccessTest.php
Tests if nodes that may not be deleted, can not be deleted in bulk.
BulkFormAccessTest::testNodeEditAccess in core/modules/node/tests/src/Functional/Views/BulkFormAccessTest.php
Tests if nodes that may not be edited, can not be edited in bulk.

... See full list

File

core/modules/node/tests/src/Traits/NodeCreationTrait.php, line 69

Class

NodeCreationTrait
Provides methods to create node based on default settings.

Namespace

Drupal\Tests\node\Traits

Code

protected function createNode(array $values = []) {

  // Populate defaults array.
  $values += [
    'body' => [
      [
        'value' => $this
          ->randomMachineName(32),
        'format' => filter_default_format(),
      ],
    ],
    'title' => $this
      ->randomMachineName(8),
    'type' => 'page',
  ];
  if (!array_key_exists('uid', $values)) {
    $user = User::load(\Drupal::currentUser()
      ->id());
    if ($user) {
      $values['uid'] = $user
        ->id();
    }
    elseif (method_exists($this, 'setUpCurrentUser')) {

      /** @var \Drupal\user\UserInterface $user */
      $user = $this
        ->setUpCurrentUser();
      $values['uid'] = $user
        ->id();
    }
    else {
      $values['uid'] = 0;
    }
  }
  $node = Node::create($values);
  $node
    ->save();
  return $node;
}