Problem/Motivation
Drupal\config\Tests\ConfigSchemaTestBase has a fabulous testing facility to ensure config schema completeness in its checkValue() method. This would be amazing to be reused as a debugging tool. Eg. #2266427: Add more info on schema validity to configuration inspector for debugging could use this code to summarize no/full/partial schema validity for configuration as well as collect data granular enough to help with debugging and solving config schema issues much faster.
Proposed resolution
Abstract the logic from checkValue() out so that contrib can use this. Instead of directly throwing failures from it, collect the errors and let the implementor handle them as needed.
Remaining tasks
Agree on approach.
Implement.
User interface changes
None.
API changes
No. API addition with new schema testing functionality instead.
Comment | File | Size | Author |
---|---|---|---|
#33 | 2270815-diff-21-33.txt | 708 bytes | vijaycs85 |
#33 | 2270815-config-schema-trait-33.patch | 11.13 KB | vijaycs85 |
Comments
Comment #1
Gábor HojtsyPostponing #2264179: Clarify use of property/undefined and add an ignore type in configuration schema because that is RTBC and touches around the same code.
Comment #2
Jose Reyero CreditAttribution: Jose Reyero commentedThe related issue has been committed, so moving back to active
Comment #3
Jose Reyero CreditAttribution: Jose Reyero commentedAfter giving a try to this one, this is what I've found:
a) There's no simple way to implement some generic data validation in Drupal core. We need some generic validation for it to be reused in tests and in contrib modules. The Drupal generic validation way is now using Symfony validation constraints and I'm afraid implementing such complex thing would be out of scope for D8 atm.
b) Yes, we can just move these methods to somewhere else but that would be a very limited validation and doesn't even use PrimitiveTypeConstraintValidator which is what would take properly implementing a minimal reusable validation of TypedData elements. It is not good enough to validate generic configuration data, though it gets the job done for this single test.
Thus just moving this around for it to be reusable will be more harm than good if others are tempted to use it for implementing schema validation. My recommendation is we close this one as "won't fix", and think about something better (proper validation) for contrib modules.
FYI I'll be giving a try to some properly built config schema validation in a contrib module, not for Drupal core though.
References (Proper Drupal 8 reusable validation):
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Validatio...
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Validatio...
Comment #4
Gábor HojtsyHere is a quick approach of this using a trait that contrib can use as well. Not sure how safe the property names are then, may need to be rejiggered a bit. Also not really tested, looking forward to testbot results. No need to override fail() anymore since we collect all the errors in an array instead. This will be useful for contrib to map to the actual config and show actual places where errors are found.
Comment #6
YesCT CreditAttribution: YesCT commentedComment #7
Gábor HojtsyUgh, PHP error.
Comment #9
Jose Reyero CreditAttribution: Jose Reyero commentedI still think that code is useless for anything else than testing. If you want full schema validation (using typed data validation, etc..) take a look at this module, https://drupal.org/sandbox/reyero/config_schema
Comment #10
Gábor HojtsyIf this is useless, then core is dumb. A slight variation of the same code is used to typecast configuration, find translatable stuff, etc. So if we are to build something more powerful in contrib than what we did in core is pointless. We'll obviously not be able to ensure any particular contrib would be used on all D8 sites (there is not even Views in contrib to try and use ctools as a "trojan horse"). It also does not seem to be possible for that contrib to override all of those dumb implementations in core. So if we think that core is inadequate for extension, then that should be fixed there not with a contrib replacement. I firmly believe that we should help developers comply with core with the same tool that core would use to ensure their compliance, which is this exact code. If this code is dumb, then it needs to be improved not sidestepped as this code will be used by most sites unless your module successfully becomes the next ctools that is to overcome core issues for an extremely popular set of contrib modules.
Comment #11
Gábor HojtsyHelps to return an empty array when no errors are found.
Comment #12
tim.plunkettI found this code very useful, and opened #2274211: ConfigSchemaTestBase should be a trait to do the same thing.
Comment #13
Gábor Hojtsy@tim.plunkett: woot, wanna help review this? My goal was to use this as a schema review/debugging tool at #2266427: Add more info on schema validity to configuration inspector for debugging.
Comment #14
alexpottWe should move all the functionality into the trait or maybe have another test trait that uses the new schema trait and then the classes that used to extend form ConfigSchemaTestBase can extend from KernelTestBase or WebTestBase as appropriate. We already discussed the calling of assertion in traits for testing in IRC and decided it was okay.
Comment #15
Gábor Hojtsy@alexpott: the only way to reuse this as a general schema vs. config checker is if there is no assertions thrown / test environment assumed in the trait. Do you suggest a trait that extends another trait for the test? :D
Comment #16
alexpottYep
Comment #17
Gábor Hojtsy@tim.plunkett: what do you think about this?
Comment #18
tim.plunkettThis seems like a good idea to me:
But wouldn't that require #2260053: Make test helper traits explicitly define expected TestBase methods first?
Comment #19
vijaycs85Here is an update, though it depends on #2260053: Make test helper traits explicitly define expected TestBase methods, no fatal.
Comment #20
tim.plunkettContains \Drupal
This is not a class, its a trait now
Can be array|bool, and the description should have FALSE not False.
Now that these aren't just assertion messages, should we translate them?
// @todo Since
, and the second line should be indented 2 spacesComment #21
vijaycs85Thanks for the review @tim.plunkett. Here is an update on each item:
#20.1 - Fixed
#20.2 - Fixed
#20.3 - Fixed
#20.4 - Not fixed - as per #500866: [META] remove t() from assert message, we don't t() assert messages.
#20.5 - Fixed
Comment #22
tim.plunkettLooks good, thanks!
Comment #23
Gábor HojtsyAll right, let's do this and we can still refine this later based on our findings in #2183983: Find hidden configuration schema issues and similar places.
Wanna keep track of this in D8MI since we plan to use this code to figure out how the rest of the Drupal configs fair against their schema and if there are missing pieces for translation. So adding D8MI tags.
Comment #24
nvinayvinay CreditAttribution: nvinayvinay commentedI have added the code to create instance of trait using phpunit built in function getObjectForTrait
Comment #26
Gábor Hojtsy@nvinayvinay: please post an interdiff and please make your patch against the latest HEAD. Also fixing your tags change.
Comment #27
vijaycs85Thanks for the patch @nvinayvinay, but I don't see why are we getting getObjectForTrait while you can directly use them in your test class. If you still fee it is an improvement, please apply patch in #21 and make your changes on top of it.
Setting status back to review for the patch in #21.
Comment #28
vijaycs85As per #22 it is ready to go.(Cleanly applies to current HEAD)...
Comment #29
vijaycs85Re uploading the same patch in #21
Comment #31
vijaycs8529: 2270815-config-schema-trait-21.patch queued for re-testing.
Comment #33
vijaycs85Reroll...
Comment #34
vijaycs85As per #22 it is ready to go.
Comment #36
alexpottCommitted 463fd53 and pushed to 8.x. Thanks!
Comment #37
vijaycs85yay, thanks!
Comment #38
tim.plunkettSee #2287223: Use KernelTestBase for config schema tests where possible for an easy follow-up.
Comment #39
alexpottSee #2287193: checkConfigSchema() can fail to report errors for an essential followup