You are here

trait NodeCreationTrait in Drupal 8

Same name in this branch
  1. 8 core/modules/simpletest/src/NodeCreationTrait.php \Drupal\simpletest\NodeCreationTrait
  2. 8 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait
Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait
  2. 10 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait

Provides methods to create node based on default settings.

This trait is meant to be used only by test classes.

Hierarchy

30 files declare their use of NodeCreationTrait
BigPipeRegressionTest.php in core/modules/big_pipe/tests/src/FunctionalJavascript/BigPipeRegressionTest.php
BlockExposedFilterAJAXTest.php in core/modules/views/tests/src/FunctionalJavascript/BlockExposedFilterAJAXTest.php
BrowserTestBase.php in core/tests/Drupal/Tests/BrowserTestBase.php
ClickSortingAJAXTest.php in core/modules/views/tests/src/FunctionalJavascript/ClickSortingAJAXTest.php
CommentFieldNameTest.php in core/modules/comment/tests/src/Kernel/Views/CommentFieldNameTest.php

... See full list

File

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

Namespace

Drupal\Tests\node\Traits
View source
trait NodeCreationTrait {

  /**
   * Get a node from the database based on its title.
   *
   * @param string|\Drupal\Component\Render\MarkupInterface $title
   *   A node title, usually generated by $this->randomMachineName().
   * @param $reset
   *   (optional) Whether to reset the entity cache.
   *
   * @return \Drupal\node\NodeInterface
   *   A node entity matching $title.
   */
  public function getNodeByTitle($title, $reset = FALSE) {
    if ($reset) {
      \Drupal::entityTypeManager()
        ->getStorage('node')
        ->resetCache();
    }

    // Cast MarkupInterface objects to string.
    $title = (string) $title;
    $nodes = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->loadByProperties([
      'title' => $title,
    ]);

    // Load the first node returned from the database.
    $returned_node = reset($nodes);
    return $returned_node;
  }

  /**
   * Creates a node based on default settings.
   *
   * @param 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:
   *
   *   @code
   *     $this->drupalCreateNode(array(
   *       'title' => t('Hello, world!'),
   *       'type' => 'article',
   *     ));
   *   @endcode
   *   The following defaults are provided:
   *   - body: Random string using the default filter format:
   *     @code
   *       $values['body'][0] = array(
   *         'value' => $this->randomMachineName(32),
   *         'format' => filter_default_format(),
   *       );
   *     @endcode
   *   - title: Random string.
   *   - type: 'page'.
   *   - uid: The currently logged in user, or anonymous.
   *
   * @return \Drupal\node\NodeInterface
   *   The created node entity.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeCreationTrait::createNode protected function Creates a node based on default settings.
NodeCreationTrait::getNodeByTitle public function Get a node from the database based on its title.