diff --git a/core/modules/node/tests/src/Traits/NodeCreationTrait.php b/core/modules/node/tests/src/Traits/NodeCreationTrait.php index bec9f2009ae3bd613ba6a7e01da14f336f23a65d..fe71eff2036dce069eccd6f0c08417da63d314aa 100644 --- a/core/modules/node/tests/src/Traits/NodeCreationTrait.php +++ b/core/modules/node/tests/src/Traits/NodeCreationTrait.php @@ -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', + '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;