Problem/Motivation

The NodeCreationTrait::createNode method is very handy for writing tests. When used in a Kernel test though, it throws an error that isn't necessarily intuitive to workaround if the filter module isn't present:

Error: Call to undefined function Drupal\Tests\node\Traits\filter_default_format()

even if one figures out the module should be installed, they then need to install its config or this error will occur:

Error: Call to a member function on null

Proposed resolution

The trait could conditionally check if the module is enabled, and if so, proceed as it does now, and otherwise not try to set the `body` field, which kernel tests might not need.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#34 interdiff.3010132.32-34.txt707 byteslongwave
#34 3010132-34.patch2.19 KBlongwave
#32 interdiff.3010132.27-32.txt2.21 KBjoachim
#32 3010132-32.drupal.NodeCreationTraitcreateNode-doesnt-work-in-kernel-tests-without-the-Filter-module.patch2.15 KBjoachim
#27 3010132-27.patch2.19 KBdhirendra.mishra
#27 interdiff_20_27.txt2.03 KBdhirendra.mishra
#25 interdiff_20_25.txt1.97 KBdhirendra.mishra
#25 3010132-25.patch2.25 KBdhirendra.mishra
#20 3010132-20.patch1.59 KBAnonymous (not verified)
#18 3010132-18.patch1.61 KBKrzysztof Domański
#16 core_3010132-16.patch1.57 KBKrzysztof Domański
#9 interdiff-8-9.txt1.14 KBKrzysztof Domański
#9 core_3010132-9.patch1.46 KBKrzysztof Domański
#8 interdiff-6-8.txt743 bytesKrzysztof Domański
#8 core_3010132-8.patch1.5 KBKrzysztof Domański
#6 core_3010132-6.patch1.44 KBKrzysztof Domański
#3 interdiff-2-3.txt637 bytesKrzysztof Domański
#3 core_3010132-3.patch1.16 KBKrzysztof Domański
#2 3010132-02.patch1.14 KBjhedstrom
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhedstrom created an issue. See original summary.

jhedstrom’s picture

Status: Active » Needs review
FileSize
1.14 KB

This gets around the first issue, but will still fail on a kernel test if the filter module is present, but no filter formats exist (eg, the default config for the module hasn't been imported).

Krzysztof Domański’s picture

This patch also checks if filter formats exist.

Berdir’s picture

I guess this code actually predates body being a configurable field anyway.

What about actually checking whether the body field even exists for the given node type, and if it doesn't, we don't have to bother with setting a value for it.

jibran’s picture

Title: Allow NodeCreationTrait::createNode() to work without the Filter module » NodeCreationTrait::createNode() doesn't work without the Filter module
Category: Feature request » Bug report
Status: Needs review » Needs work

What about actually checking whether the body field even exists for the given node type, and if it doesn't, we don't have to bother with setting a value for it.

I agree let's do this.

Krzysztof Domański’s picture

Status: Needs work » Needs review
FileSize
1.44 KB

If the body field exists for the given node type and the filter module are present, set the body field.

Status: Needs review » Needs work

The last submitted patch, 6: core_3010132-6.patch, failed testing. View results

Krzysztof Domański’s picture

Krzysztof Domański’s picture

Status: Needs work » Needs review
FileSize
1.46 KB
1.14 KB

Let's install field and filter modules and set the body if the body field exists for the given node type and the filter formats are present.

jhedstrom’s picture

+++ b/core/modules/node/tests/src/Traits/NodeCreationTrait.php
@@ -67,16 +68,25 @@ public function getNodeByTitle($title, $reset = FALSE) {
+    \Drupal::service('module_installer')->install(['field', 'filter']);

I wouldn't expect a module to be installed when calling nodeCreate. I think I prefer the approach in #8.

Krzysztof Domański’s picture

The current version for review: #8. Ignore the others.

timmillwood’s picture

Status: Needs review » Needs work

I agree modules shouldn't be installed when calling createNode.

#8 is still installing a module, field. Can we do this without installing field module?

Krzysztof Domański’s picture

Can we do this without installing field module?

We need field module to check if the body field exists.

See #6 and interdiff-6-8.txt.

timmillwood’s picture

Can we assume that if field module is not installed, then the body field doesn't exist?

Krzysztof Domański’s picture

Assigned: Unassigned » Krzysztof Domański

Makes sense. Thanks @timmillwood

Krzysztof Domański’s picture

Assigned: Krzysztof Domański » Unassigned
Status: Needs work » Needs review
FileSize
1.57 KB

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Krzysztof Domański’s picture

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community
Anonymous’s picture

Re-rolled for 8.8.x-dev.

larowlan’s picture

I think this is a good DX win...but

node requires text » text requires filter » filter provides default input formats.

So in reality, we're only talking about kernel tests here right? Because functional tests should get all of this by default?

jhedstrom’s picture

Title: NodeCreationTrait::createNode() doesn't work without the Filter module » NodeCreationTrait::createNode() doesn't work in kernel tests without the Filter module

So in reality, we're only talking about kernel tests here right?

Indeed, this is an issue specific to Kernel tests. As things currently stand all those modules mentioned above need to be manually enabled and have their configurations installed in order to use this trait at all in a kernel test.

I've updated the title for clarity.

catch’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/node/tests/src/Traits/NodeCreationTrait.php
    @@ -69,16 +70,27 @@ public function getNodeByTitle($title, $reset = FALSE) {
    +    // are present, set the body field.
    

    nit: This should be 'is present'.

  2. +++ b/core/modules/node/tests/src/Traits/NodeCreationTrait.php
    @@ -69,16 +70,27 @@ public function getNodeByTitle($title, $reset = FALSE) {
    +      $field = FieldConfig::loadByName('node', $values['type'], 'body');
    

    Instead of loading the field config, could we first create the Node without the field values, then check hasField() on $node? Then set the values before calling $node->save()?

dhirendra.mishra’s picture

Assigned: Unassigned » dhirendra.mishra

Working on it

dhirendra.mishra’s picture

Assigned: dhirendra.mishra » Unassigned
Status: Needs work » Needs review
FileSize
2.25 KB
1.97 KB

Please review the patch uploaded.

Status: Needs review » Needs work

The last submitted patch, 25: 3010132-25.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

dhirendra.mishra’s picture

Status: Needs work » Needs review
FileSize
2.03 KB
2.19 KB

Here is the updated patch.Kindly review it

Status: Needs review » Needs work

The last submitted patch, 27: 3010132-27.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

joachim’s picture

Status: Needs review » Needs work

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

idebr’s picture

Status: Needs review » Reviewed & tested by the community

Bumped into this while working on a Kernel test today. Patch works as expected.

  • catch committed fa13b06 on 10.0.x
    Issue #3010132 by Krzysztof Domański, dhirendra.mishra, longwave,...
  • catch committed 0fca91f on 10.1.x
    Issue #3010132 by Krzysztof Domański, dhirendra.mishra, longwave,...
  • catch committed 34cb0d6 on 9.4.x
    Issue #3010132 by Krzysztof Domański, dhirendra.mishra, longwave,...
  • catch committed 58e26bd on 9.5.x
    Issue #3010132 by Krzysztof Domański, dhirendra.mishra, longwave,...
catch’s picture

Version: 10.1.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.1.x and cherry-picked back through to 9.4.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.