From 0fca91f8e6d8bdc8786bf19fae6dedb43a29c89f Mon Sep 17 00:00:00 2001 From: catch <catch56@gmail.com> Date: Mon, 14 Nov 2022 11:58:17 +0000 Subject: [PATCH] =?UTF-8?q?Issue=20#3010132=20by=20Krzysztof=20Doma=C5=84s?= =?UTF-8?q?ki,=20dhirendra.mishra,=20longwave,=20joachim,=20jhedstrom,=20t?= =?UTF-8?q?immillwood,=20Berdir,=20catch:=20NodeCreationTrait::createNode(?= =?UTF-8?q?)=20doesn't=20work=20in=20kernel=20tests=20without=20the=20Filt?= =?UTF-8?q?er=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/src/Traits/NodeCreationTrait.php | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/core/modules/node/tests/src/Traits/NodeCreationTrait.php b/core/modules/node/tests/src/Traits/NodeCreationTrait.php index bec9f2009ae3..fe71eff2036d 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; -- GitLab