Problem/Motivation
Problem #1: \Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase::testRequiredPropertyValuesMissing() attempts to blindly set all required properties to NULL and assert a validation error. But if the property being set is typed (i.e enforced at a language level) then setting it to NULL makes the test fail with a TypeError and no way to reuse the base class without overriding the whole method.
Problem #2: Attempting to fix Problem #1 uncovered an issue where ConfigEntityValidationTestBase::testRequiredPropertyValuesMissing() and ConfigEntityValidationTestBase::getPropertiesWithOptionalValues() assume that all label properties have the required_label type. If it validates a property with the optional label type, it will issue a "This value should not be blank" validation error. This was found when trying to implement the suggestion in #6 of validating the config_test entity which has an optional label.
Steps to reproduce
Create a config entity with a typed property e.g.
protected string $something
Attempt to add validation tests using \Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase
see testRequiredPropertyValuesMissing blow up with a TypeError
Proposed resolution
- Catch
TypeErrorand move on, validation is provided at the language level. - Check the schema definition of an entity's label, whether it is an optional
labelorrequired_label, instead of treating all labels as required.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3526908
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
annmarysruthy commentedComment #5
smustgrave commentedComment #6
larowlanNice one @annmarysruthy - I think it in terms of adding a test, making one of the properties like weight on
\Drupal\config_test\Entity\ConfigTesttyped and adding a config validation test for that entity-type would demonstrate the issue.Comment #8
dcam commentedWriting the test was WAY more of a pain than it should have been.
First off, I uncovered two additional bugs in this test! The class contains two mentions of the
required_labeldata type, including where it says:Except it doesn't actually check for
required_labeldata. In both places it just assumes that all labels are required. So if you're validating a config entity that isn't required, likeconfig_test, then the test fails. I couldn't simply change theconfig_testlabel's data type torequired_labeleither. When I did a bunch of tests started failing.To solve this problem I updated
::testRequiredPropertyValuesMissing()and::getPropertiesWithOptionalValues()to verify that a label is required before treating it that way.Secondly, the
weightcouldn't simply be given aninttype (see #6). Tests started failing when I did that. I tried other solutions like assigning types to other untyped properties and adding another property. You can see the commit log above. Everything I tried broke tests. It turned out that adding the type toweightbroke the least amount of tests, just one. It was theConfigEntityAdapterTestwhich tried to assign a string value to the weight to trigger validation errors. Of course, with the new type this can't be done at the PHP level. I changed the adapter test trigger a violation on the untypedstyleproperty instead. I don't think this is important to the test, but please double-check.Comment #9
smustgrave commentedCan we update the summary some. Proposed solution means just catching the exception and moving on. But seems we are doing more then that.
Comment #10
dcam commentedSure thing. I didn't think about it the other day. I was ready to move on to something else after hours of frustrating debugging.
Comment #11
smustgrave commentedWhy is the typehint needed in core/modules/config/tests/config_test/src/Entity/ConfigTest.php ?
Comment #12
dcam commentedThis was done at the suggestion of @larowlan in comment #6. And it's at the core of what this issue was originally created about. From the issue summary:
In order to test this problem we need to have a typed property.
Comment #13
smustgrave commentedThanks for clarifying that!
Believe all feedback here has been addressed.
Comment #14
longwaveI've been annoyed by the original issue before where typed properties can't be used, and nice find on all these additional edge cases!
Backported to 11.3.x as an eligible tests only bug fix.
Committed and pushed fac47b829ca to 11.x and e764a353476 to 11.3.x. Thanks!