function NodeCreationTrait::createNode

Same name in other branches
  1. 9 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::createNode()
  2. 8.9.x core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::createNode()
  3. 10 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([
    'title' => t('Hello, world!'),
    'type' => 'article',
]);

The following defaults are provided, if the node has the field in question:

  • body: Random string using the default filter format:
$values['body'][0] = [
    '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.

335 calls to NodeCreationTrait::createNode()
AnnouncementsCacheTest::testDynamicPageCache in core/modules/announcements_feed/tests/src/Functional/AnnouncementsCacheTest.php
Tests dynamic page cache.
ArgumentDefaultTest::testArgumentDefaultNode in core/modules/views/tests/src/Functional/Plugin/ArgumentDefaultTest.php
Tests node default argument.
BasicTest::testViewsWizardAndListing in core/modules/views/tests/src/Functional/Wizard/BasicTest.php
BlockExposedFilterAJAXTest::setUp in core/modules/views/tests/src/FunctionalJavascript/BlockExposedFilterAJAXTest.php
BlockExposedFilterAJAXTest::testExposedFilteringAndReset in core/modules/views/tests/src/FunctionalJavascript/BlockExposedFilterAJAXTest.php
Tests if exposed filtering and reset works with a views block and ajax.

... See full list

File

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

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 += [
        'title' => $this->randomMachineName(8),
        'type' => 'page',
    ];
    // Create node object.
    $node = Node::create($values);
    // If the node has a field named 'body', we assume it's a body field and
    // that the filter module is present.
    if (!array_key_exists('body', $values) && $node->hasField('body')) {
        $body = [
            'value' => $this->randomMachineName(32),
            'format' => filter_default_format(),
        ];
        $node->set('body', $body);
    }
    if (!array_key_exists('uid', $values)) {
        $user = User::load(\Drupal::currentUser()->id());
        if ($user) {
            $uid = $user->id();
        }
        elseif (method_exists($this, 'setUpCurrentUser')) {
            
            /** @var \Drupal\user\UserInterface $user */
            $user = $this->setUpCurrentUser();
            $uid = $user->id();
        }
        else {
            $uid = 0;
        }
        $node->set('uid', $uid);
    }
    $node->save();
    return $node;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.