Problem/Motivation
Once #2920309: Add experimental module for Help Topics is committed, as part of getting it stable on #3027054: Help Topics module roadmap: the path to beta and stable, we need to add better test coverage. Here is an inventory of tests that we have vs. tests that we need:
a) The only Unit test we currently have is in core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php -- which is a test for the HelpTopicDiscovery class.
b) In patch Other classes that we don't have unit tests for:
- HelpTopicPluginManager [but its functionality is being tested extensively in HelpTopicTest, and we have a unit test for Discovery, so I think it is OK]
- HelpTopicTwig [in patch]
- HelpTopicTwigLoader [in patch]
c) Functional tests we have:
- HelpTopicTranslationTest: Tests translation of a help topic title, and of something in the body of a help topic
- HelpTopicTest:
-- verifies that the Topics section is on admin/help, that various links/titles are there and as expected, and that they are in the right order
-- verifies that topics go away when you uninstall a module/theme
-- tests the plugin manager alter hook
-- tests the various ways that plugins can be discovered, since #3072519: Help Topics discovery cannot be decorated easily
-- tests topic bi-directional relationships, and topics with "related" topics that don't exist
-- tests correct access to view topics
-- tests breadcrumbs
d) In patch Functional tests missing: cache tags on admin/help and on an individual topic page. What we should test for:
- I noticed that the HelpTopicSection plugin should (and doesn't) have a line in its getCacheMetadata() method saying:
$this->cacheableMetadata->addCacheableDependency($this->pluginManager);
- Looking at our current plugin structure... The individual topics are read from Twig files, so they (appropriately) only have a cache tag of "core.extension". Same with the plugin manager. So, we should test that on admin/help and on individual topic pages, that cache tag is present. See
https://api.drupal.org/api/drupal/core%21tests%21Drupal%21Tests%21Browse...
which has a deprecated method from AssertLegacyTrait :
https://api.drupal.org/api/drupal/core%21tests%21Drupal%21FunctionalTest...
which says to use a line like
$this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'core.extension');
So, we need to add this to the existing help topics tests, on both admin/help and a topic page.
- The only other cache thing I think we should test for is the language cache context. You can test for this (again on both admin/help and a topic page) by doing:
$this->assertSession()->responseHeaderContains('X-Drupal-Cache-Contexts', 'languages:language_interface');
e) Topic syntax tests and functional tests for topic discovery are being handled on:
#3066512: Add checks for syntax and display of help topic Twig template files
Proposed resolution
Add the missing tests. Could be done in child issues if this gets too big.
Remaining tasks
Review and commit.
User interface changes
None.
API changes
None.
Data model changes
None.
Release notes snippet
None needed, this just adds tests.
| Comment | File | Size | Author |
|---|---|---|---|
| #37 | 3037228-37.patch | 10.85 KB | andypost |
| #35 | interdiff.txt | 1.15 KB | andypost |
| #33 | interdiff.txt | 3.07 KB | andypost |
| #30 | 3037228-30.patch | 10.75 KB | jhodgdon |
| #30 | interdiff.txt | 2.3 KB | jhodgdon |
Comments
Comment #3
jhodgdonOn #3041924-52: [META] Convert hook_help() module overview text to topics, andypost suggested we should add tests for the syntax/content of help topics. My response on the next comment:
That sounds like a good idea.
The current tests for the module overviews from hook_help test for certain content, like the appearance of the module name. But I don't think we can/should test for content in help topics, because they're not supposed to be module-oriented. I also don't think we should require each module to have at least one topic file, because there may be circumstances where a particular module's files are put elsewhere (like a group of modules that share topics or putting them in the core space), or where a particular module doesn't need help because its tasks are described elsewhere.
So... for templates that do exist, I think we could write a test. Here's the sample template file (linked in the issue summary of the other issue): https://www.drupal.org/files/issues/2019-04-08/sample.html_.twig_.txt
Here are some things I think we could maybe test for:
- meta tag for the title (label) must be present (help_topic:label)
- allowed meta tags: help_topic:label, help_topic:top_level, help_topic:related -- there must not be any others [although maybe someone would write a subclass of the plugin manager that would use other meta tags?]
- there must not be an H1 tag
- if there is an h3 tag, there must be an h2 tag; if there is an h4, there must be an h3; etc. (proper hierarchy of headings)
- can we test for HTML syntax validation, once it's rendered from Twig?
- is it possible to test whether all of the text in the file is actually wrapped in translation commands {% trans }?
Comment #4
andypostAlready faced with "label required" in #3055317-6: Convert history, statistics, tracker module hook_help() to topic(s) trying to add stub test
Comment #5
jhodgdonYes, we at least need a test that tries to load all the help topics and display them, and makes sure they have a title! Glad you are working on this!
Comment #6
andypostTranslation also needs some testing plan
Comment #7
jhodgdonYes, translation already has another issue.
Comment #8
jhodgdonThe other issue for translation is not actually about testing. So... Adding translation and also the ideas from #3 to the issue summary as things we can possibly test.
Comment #9
jhodgdonWe already have HelpTopicTranslationTest actually, so we don't need to add more testing for translation (the Roadmap also lists this task as done). Unit tests and cache tests were the two areas identified by the core maintainers as needing attention, and then we added the other ideas about having tests to validate new topics (which I still think are a good idea).
Comment #10
scott_euser commentedUpdating issue summary as we have a test for "meta tag for the title (label) must be present (help_topic:label)" in
Drupal\Tests\help_topics\Unit\HelpTopicDiscoveryTest::testDiscoveryExceptionMissingLabelMetaTag()Comment #11
scott_euser commentedHmm, switching back as maybe we are intending to actually test each discovered core help_topic?
Comment #12
scott_euser commentedHere this patch adds a test which checks all core help topics to ensure there are no exceptions thrown. This will help ensure that any new topics from other issues (like the typo in https://www.drupal.org/project/drupal/issues/3055055#comment-13109805) will cause this test to fail.
This does not yet cover h1-h6 requirement of issue so leaving as 'needs work'.
Comment #13
scott_euser commentedTest now covers ensuring all core help topics have a valid heading 1 - 6 structure.
Comment #14
jhodgdonThanks for your interest in this issue, and the patches!
A few notes:
a) It seems like these sorts of tests should maybe not be based on BrowserTestBase unless that is really necessary? If we are checking the structure of Twig templates, I don't think we need a browser? Probably could be a Kernel test? See https://api.drupal.org/api/drupal/core%21core.api.php/group/testing/8.8.x
So probably you would need to add a new test file rather than adding to this one.
b) Also you have added some coding standards problems to the file, so you'll need to remove those problems. To see those, click through to the search results, and you'll see where it says there are coding standards problems.
c) You should not do your own help topic discovery. You should instead use the Help Topics Manager class, I think? That way you will also test help topics provided by themes and profiles.
d) I'm actually wondering for help topic syntax checks if we should add Coder rules instead of testing this way? Because then it would also cover contrib when they get into writing help topics.
Comment #15
scott_euser commentedThanks for the review and feedback!
I did create one kernel test in there to ensure no core help topics trigger exceptions. The browser test base was to use xpath. I did not think this was a job for regular expressions and I did not see any uses of DOMDocument or Symfony DomCrawler elsewhere in core (I did think about rendering the templates and using those to parse for correct structure). Happy to hear suggestions for alternative approaches.
b) Also you have added some coding standards problems to the file, so you'll need to remove those problems. To see those, click through to the search results, and you'll see where it says there are coding standards problems.Ah sorry! I must have accidentally hit reformat on save and the Drupal 8 coding standards for PHPStorm got applied (which seem to be partly incorrect). Updated patch without any changes to existing code.
I did actually use
HelpTopicDiscovery. I did not use the correspondingHelpTopicPluginManager, though consider this, but then we would have to maintain a hard-coded list of all core modules to activate them all right? Ie, thisUses ModuleHandler which gets active modules only. This is why I followed the other PHPUnit tests from help topics and passed an array of files directly to the constructor of
HelpTopicDiscovery. I used the coreextension.list.moduleservice instead to load the complete list of both inactive and active modules. Happy to use a different approach but I think I'll need to be pointed in the right direction then.d) I'm actually wondering for help topic syntax checks if we should add Coder rules instead of testing this way? Because then it would also cover contrib when they get into writing help topics.By syntax do you mean the h1-h6 hierarchy? How would I go about adding this to coder?
Comment #16
jhodgdonAh, that makes sense about the Discovery process. Very good point. But you'll need to also check the theme directories and the core directory to be complete, just like the getDiscovery() code you quoted:
We may not have any topics in there currently, but we plan to shortly.
Regarding Coder, the project is https://www.drupal.org/project/coder/ ... I do not know if it currently checks Twig files, but it seems like it would be possible. If so, we would create a child issues of this one, put it in that project, and work on the rule there. We'd probably need to create a Twig help topic file that violated each one of our tests, and one that would pass our tests, and then use that in some tests in that project.
Once that rule got into the Coder code, we would then patch in this issue so that Core would run that test by default. The list of coder checks is inside the core/drupalci.yml file.
Comment #17
scott_euser commentedThanks for the quick feedback!
I have done the following:
Comment #18
jhodgdonI think we should check the other syntax things in the Coder issue also. Updating issue summary, and I'll also update the Coder child issue.
Comment #19
jhodgdonI am going to add the Coder issue as a separate item on #3027054: Help Topics module roadmap: the path to beta and stable so we can move forward on this and not delay it on Coder stuff.
What else needs to be done here on this issue?
Comment #20
jhodgdonI took a look at the current patch, and I'm not sure exactly what it is supposed to be testing for really -- I guess just that the Discovery class can read all of the topic files and get their meta-data out? I guess that is good, and I don't see any glaring problems in the patch so far.
But what we really need, in order to satisfy this issue, is:
- Tests for the cache meta data for the admin/help page (with Help Topics enabled)
- Tests for the cache meta data for an individual help topic page
- Unit tests on the various classes that are part of the help topics module, such as HelpTopicDiscovery, the plugin manager, etc. [Some of them may already have unit tests.]
So, setting this to needs work because we need more tests added.
Comment #21
jhodgdonSo, I took a look at what tests we have vs. need:
a) The only Unit test we have is in core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php
It seems to cover the same exception that this test covers, so we probably don't need that exception test. And it is a unit test for the Discovery process. So, that is taken care of.
b) Other classes that we don't have unit tests for:
- HelpTopicPluginManager [not sure if it needs a unit test? probably? do other plugin managers have unit tests?
- HelpTopicTwig
- HelpTopicTwigLoader
c) Functional tests we have:
- HelpTopicTranslationTest: Tests translation of a help topic title, and of something in the body of a help topic
- HelpTopicTest:
-- verifies that the Topics section is on admin/help, that various links/titles are there and as expected, and that they are in the right order
-- verifies that topics go away when you uninstall a module/theme
-- tests the plugin manager alter hook [should that be on the unit test for the manager?]
-- tests correct access to view topics
-- tests breadcrumbs [so I think we don't need a unit test for the Breadcrumb class?]
d) Functional tests missing: cache tags on admin/help and on an individual topic page
e) Topic tests we have in this patch: Discovery process can load all Twig templates (only meta-data is loaded)
f) Topic tests we don't have in this patch:
- Syntax checks that we should probably do in Coder on that other issue
- Can we actually use Twig to render/display the topic... Maybe use the Twig loader to parse the topic? There could be problems with URL variables if routes are not defined (if the module isn't enabled)?
Comment #22
jhodgdonAdding previous comment to issue summary, more or less, and adding issue summary template.
Comment #23
jhodgdonIn investigating #3066512: Add checks for syntax and display of help topic Twig template files I found out that Coder doesn't sniff Twig files. So, that issue is now back in Core and I'm updating issue summary.
Comment #24
jhodgdonAdding some information about caching to the issue summary item (d).
Comment #25
jhodgdonI think that the test in the latest patch here would be better off as part of #3066512: Add checks for syntax and display of help topic Twig template files, so I'm hiding its file here, uploading there, and will ask for scott_euser to get credit on that other issue.
Comment #26
jhodgdonThis is the one remaining issue in our Roadmap to Beta that doesn't have a patch yet (since the other patch, which was all about help topic syntax, got moved to #3066512: Add checks for syntax and display of help topic Twig template files). So... I'm going to work on this today. Also minor update to issue summary.
Comment #27
jhodgdonHere is a patch that adds unit tests for the HelpTopicTwig and HelpTopicTwigLoader classes. They both pass locally. No interdiff -- this is a totally new patch.
That is all I have time to do for the next few days, so unassigning in case someone else wants to write more tests. See issue summary for list of what needs to be done.
Comment #28
jhodgdonAdding more notes about what has been tested to the issue summary... I think we have adequate test coverage for the help topic plugin manager (see new notes in issue summary). So all that remains to be done (I think?) is to add tests for the cache tags/contexts.
So, here it is! I think this issue is done... assuming the bot agrees.
Comment #29
andypostI find it ready too
Comment #30
jhodgdonNot quite I guess -- 2 coding standards messages. Here's an interdiff and new patch.
Comment #31
andypostSure, missed to check styles last time
Comment #32
larowlanshould we use
\Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait::assertCacheTagshere and that way we can assert the tags in bulk?is there a reason we used a static here?
Comment #33
andypostAddressed review
1) the test fails locally but let's see what bot will tell
2) no reason for static
Comment #35
andypostThis trait is for exact comparison, so reverted becases test needs to ensure just partial
Comment #37
andypostProper patch, previous one mixed with related
Comment #38
jhodgdonOK. The only difference between the patch in #37 and in #30 is changing directories to be not static (as suggested in #32 review). The other thing suggested there was to use the trait for cache tags, but as noted in #35, we couldn't do that because it does exact comparison, which is not what we are doing here. So, I think this should be back to RTBC.
Comment #39
larowlanCommitted b04bd60 and pushed to 8.8.x. Thanks!
Comment #41
krzysztof domańskiThe new test fails at PHP 7 (
8.8.x-dev test with PHP 7 & MySQL 5.5).https://www.drupal.org/pift-ci-job/1422958
https://www.drupal.org/pift-ci-job/1423765
Comment #42
krzysztof domańskiPHP 7.1.0 Released
PHP RFC: Support Class Constant Visibility
Comment #43
krzysztof domańskiI created follow-up #3085512: Remove Class Constant Visibility from HelpTopicTwigTest which breaks tests at PHP 7.