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
Comment | File | Size | Author |
---|---|---|---|
#34 | interdiff.3010132.32-34.txt | 707 bytes | longwave |
#34 | 3010132-34.patch | 2.19 KB | longwave |
|
Comments
Comment #2
jhedstromThis 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).
Comment #3
Krzysztof DomańskiThis patch also checks if filter formats exist.
Comment #4
BerdirI 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.
Comment #5
jibranI agree let's do this.
Comment #6
Krzysztof DomańskiIf the body field exists for the given node type and the filter module are present, set the body field.
Comment #8
Krzysztof DomańskiComment #9
Krzysztof DomańskiLet'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.
Comment #10
jhedstromI wouldn't expect a module to be installed when calling
nodeCreate
. I think I prefer the approach in #8.Comment #11
Krzysztof DomańskiThe current version for review: #8. Ignore the others.
Comment #12
timmillwoodI agree modules shouldn't be installed when calling createNode.
#8 is still installing a module, field. Can we do this without installing field module?
Comment #13
Krzysztof DomańskiWe need field module to check if the body field exists.
See #6 and interdiff-6-8.txt.
Comment #14
timmillwoodCan we assume that if field module is not installed, then the body field doesn't exist?
Comment #15
Krzysztof DomańskiMakes sense. Thanks @timmillwood
Comment #16
Krzysztof DomańskiComment #18
Krzysztof DomańskiRe-roll
Comment #19
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #20
Anonymous (not verified) CreditAttribution: Anonymous commentedRe-rolled for 8.8.x-dev.
Comment #21
larowlanI 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?
Comment #22
jhedstromIndeed, 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.
Comment #23
catchnit: This should be 'is present'.
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()?
Comment #24
dhirendra.mishra CreditAttribution: dhirendra.mishra at Valuebound for Valuebound commentedWorking on it
Comment #25
dhirendra.mishra CreditAttribution: dhirendra.mishra at Valuebound for Valuebound commentedPlease review the patch uploaded.
Comment #27
dhirendra.mishra CreditAttribution: dhirendra.mishra at Valuebound for Valuebound commentedHere is the updated patch.Kindly review it
Comment #32
joachim CreditAttribution: joachim as a volunteer commentedFixed the test failure & tweaked the docs.
Comment #34
longwaveWe should only set up a random body if one wasn't provided.
Comment #39
idebr CreditAttribution: idebr at iO commentedBumped into this while working on a Kernel test today. Patch works as expected.
Comment #41
catchCommitted/pushed to 10.1.x and cherry-picked back through to 9.4.x, thanks!