Commit 34cb0d69 authored by catch's avatar catch
Browse files

Issue #3010132 by Krzysztof Domański, dhirendra.mishra, longwave, joachim,...

Issue #3010132 by Krzysztof Domański, dhirendra.mishra, longwave, joachim, jhedstrom, timmillwood, Berdir, catch: NodeCreationTrait::createNode() doesn't work in kernel tests without the Filter module

(cherry picked from commit 0fca91f8)
parent 4dd0b453
Loading
Loading
Loading
Loading
+21 −14
Original line number Diff line number Diff line
@@ -51,7 +51,8 @@ public function getNodeByTitle($title, $reset = FALSE) {
   *       'type' => 'article',
   *     ));
   *   @endcode
   *   The following defaults are provided:
   *   The following defaults are provided, if the node has the field in
   *   question:
   *   - body: Random string using the default filter format:
   *     @code
   *       $values['body'][0] = array(
@@ -69,32 +70,38 @@ public function getNodeByTitle($title, $reset = FALSE) {
  protected function createNode(array $values = []) {
    // Populate defaults array.
    $values += [
      'body'      => [
        [
          'value' => $this->randomMachineName(32),
          'format' => filter_default_format(),
        ],
      ],
      '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) {
        $values['uid'] = $user->id();
        $uid = $user->id();
      }
      elseif (method_exists($this, 'setUpCurrentUser')) {
        /** @var \Drupal\user\UserInterface $user */
        $user = $this->setUpCurrentUser();
        $values['uid'] = $user->id();
        $uid = $user->id();
      }
      else {
        $values['uid'] = 0;
        $uid = 0;
      }
      $node->set('uid', $uid);
    }

    $node = Node::create($values);
    $node->save();

    return $node;