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.

Comments

jhodgdon created an issue. See original summary.

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.

jhodgdon’s picture

On #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 }?

andypost’s picture

Assigned: Unassigned » andypost

Already faced with "label required" in #3055317-6: Convert history, statistics, tracker module hook_help() to topic(s) trying to add stub test

jhodgdon’s picture

Yes, 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!

andypost’s picture

Translation also needs some testing plan

jhodgdon’s picture

Yes, translation already has another issue.

jhodgdon’s picture

Issue summary: View changes

The 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.

jhodgdon’s picture

Issue summary: View changes

We 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).

scott_euser’s picture

Issue summary: View changes

Updating 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()

scott_euser’s picture

Issue summary: View changes

Hmm, switching back as maybe we are intending to actually test each discovered core help_topic?

scott_euser’s picture

Here 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'.

scott_euser’s picture

Status: Active » Needs review
StatusFileSize
new8.27 KB
new9.03 KB

Test now covers ensuring all core help topics have a valid heading 1 - 6 structure.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks 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.

scott_euser’s picture

Status: Needs work » Needs review
StatusFileSize
new1.43 KB
new7.77 KB

Thanks for the review and feedback!

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.

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.

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.

I did actually use HelpTopicDiscovery. I did not use the corresponding HelpTopicPluginManager, though consider this, but then we would have to maintain a hard-coded list of all core modules to activate them all right? Ie, this

  /**
   * {@inheritdoc}
   */
  protected function getDiscovery() {
    if (!isset($this->discovery)) {
      // We want to find help topic plugins in core, modules and themes in
      // a sub-directory called help_topics.
      $directories = array_merge(
        ['core'],
        $this->moduleHandler->getModuleDirectories(),
        $this->themeHandler->getThemeDirectories()
      );

      $directories = array_map(function ($dir) {
        return [$dir . '/help_topics'];
      }, $directories);

      $this->discovery = new HelpTopicDiscovery($directories);
    }
    return $this->discovery;
  }

Uses 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 core extension.list.module service 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?

jhodgdon’s picture

Status: Needs review » Active

Ah, 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 want to find help topic plugins in core, modules and themes in
      // a sub-directory called help_topics.
      $directories = array_merge(
        ['core'],
        $this->moduleHandler->getModuleDirectories(),
        $this->themeHandler->getThemeDirectories()
      );

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.

scott_euser’s picture

Thanks for the quick feedback!

I have done the following:

  1. created an issue in coder https://www.drupal.org/project/coder/issues/3066512
  2. created a follow-up issue for this issue to add that to Drupal CI yml https://www.drupal.org/project/drupal/issues/3066510
  3. updated the patch to include only the kernel test which also now covers the theme list + core help topics. The theme handler does actually return all themes not yet installed (ie, the behaviour of appearance/themes) so it did not have quite the same issue
  4. updated the issue summary
jhodgdon’s picture

Issue summary: View changes

I 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.

jhodgdon’s picture

Issue summary: View changes

I 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?

jhodgdon’s picture

Status: Needs review » Needs work

I 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.

jhodgdon’s picture

So, 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)?

jhodgdon’s picture

Issue summary: View changes

Adding previous comment to issue summary, more or less, and adding issue summary template.

jhodgdon’s picture

Issue summary: View changes

In 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.

jhodgdon’s picture

Issue summary: View changes

Adding some information about caching to the issue summary item (d).

jhodgdon’s picture

I 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.

jhodgdon’s picture

Assigned: andypost » jhodgdon
Issue summary: View changes
Status: Needs work » Active

This 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.

jhodgdon’s picture

Assigned: jhodgdon » Unassigned
Status: Active » Needs review
StatusFileSize
new6.87 KB

Here 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.

jhodgdon’s picture

Issue summary: View changes
StatusFileSize
new3.88 KB
new10.74 KB

Adding 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.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

I find it ready too

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.3 KB
new10.75 KB

Not quite I guess -- 2 coding standards messages. Here's an interdiff and new patch.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Sure, missed to check styles last time

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
  1. +++ b/core/modules/help_topics/tests/src/Functional/HelpTopicTest.php
    @@ -160,9 +161,13 @@ protected function verifyHelp($response = 200) {
    +        foreach ($info['tags'] as $tag) {
    +          $session->responseHeaderContains('X-Drupal-Cache-Tags', $tag);
    +        }
    

    should we use \Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait::assertCacheTags here and that way we can assert the tags in bulk?

  2. +++ b/core/modules/help_topics/tests/src/Unit/HelpTopicTwigLoaderTest.php
    @@ -0,0 +1,115 @@
    +  protected static $directories;
    ...
    +    self::$directories = [
    

    is there a reason we used a static here?

andypost’s picture

StatusFileSize
new3.07 KB
new11.19 KB

Addressed review
1) the test fails locally but let's see what bot will tell
2) no reason for static

Status: Needs review » Needs work

The last submitted patch, 33: 3037228-33.patch, failed testing. View results

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1.15 KB
new29.45 KB
+++ b/core/modules/help_topics/tests/src/Functional/HelpTopicTest.php
@@ -166,9 +168,7 @@ protected function verifyHelp($response = 200) {
-        foreach ($info['tags'] as $tag) {
-          $session->responseHeaderContains('X-Drupal-Cache-Tags', $tag);
...
+        $this->assertCacheTags($info['tags']);

This trait is for exact comparison, so reverted becases test needs to ensure just partial

Status: Needs review » Needs work

The last submitted patch, 35: 3037228-35.patch, failed testing. View results

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new10.85 KB

Proper patch, previous one mixed with related

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

OK. 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.

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed b04bd60 and pushed to 8.8.x. Thanks!

  • larowlan committed b04bd60 on 8.8.x
    Issue #3037228 by scott_euser, jhodgdon, andypost: Add more test...
krzysztof domański’s picture

The new test fails at PHP 7 (8.8.x-dev test with PHP 7 & MySQL 5.5).

Unknown
fail: [run-tests.sh check] Line 0 of :
FATAL Drupal\Tests\help_topics\Unit\HelpTopicTwigTest: test runner returned a non-zero error code (255).

https://www.drupal.org/pift-ci-job/1422958
https://www.drupal.org/pift-ci-job/1423765

krzysztof domański’s picture

   * @var array
   */
  protected const PLUGIN_INFORMATION = [

PHP 7.1.0 Released
PHP RFC: Support Class Constant Visibility

Status: Fixed » Closed (fixed)

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