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 0fca91f8e6)
merge-requests/2976/head
catch 2022-11-14 11:58:17 +00:00
parent a663463d32
commit 58e26bd5d7
1 changed files with 21 additions and 14 deletions

View File

@ -51,7 +51,8 @@ trait NodeCreationTrait {
* '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 @@ trait NodeCreationTrait {
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;