diff -u b/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php b/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php --- b/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php +++ b/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php @@ -5,7 +5,6 @@ use Drupal\Component\Discovery\DiscoveryException; use Drupal\Tests\BrowserTestBase; use Drupal\help_topics\HelpTopicDiscovery; -use org\bovigo\vfs\vfsStream; /** * Verifies that all core Help topics can be rendered and comply with standards. @@ -44,6 +43,7 @@ $directories = $module_directories + $theme_directories + $this->listDirectories('profile'); $directories['core'] = 'core/help_topics'; + $directories['bad_help_topics'] = \Drupal::service('extension.list.module')->getPath('help_topics_test') . '/bad_help_topics/syntax/'; // Verify that a few key modules, themes, and profiles are listed, so that // we can be certain our directory list is complete and we will be testing @@ -60,9 +60,16 @@ $this->assertTrue(count($definitions) > 1, 'At least 1 topic was found'); $top_level = $this->findTopLevel($definitions); - // Test each topic for compliance with standards. + // Test each topic for compliance with standards, or for failing in the + // right way. foreach (array_keys($definitions) as $id) { - $this->verifyTopic($id, $definitions, $top_level); + $is_bad_topic = (strpos($id, 'bad_help_topics.') === 0); + if ($is_bad_topic) { + $this->verifyBadTopic($id, $definitions, $top_level); + } + else { + $this->verifyTopic($id, $definitions, $top_level); + } } } @@ -113,8 +120,10 @@ * Array of all topic definitions, keyed by ID. * @param array $top_level * Array of top-level topic definitions, keyed by ID. + * @param int $response + * Expected response from visiting the page for the topic. */ - protected function verifyTopic($id, $definitions, $top_level) { + protected function verifyTopic($id, $definitions, $top_level, $response = 200) { $definition = $definitions[$id]; // Visit the URL for the topic. @@ -122,8 +131,10 @@ // Verify the title and response. $session = $this->assertSession(); - $session->statusCodeEquals(200); - $session->titleEquals($definition['label'] . ' | Drupal'); + $session->statusCodeEquals($response); + if ($response == 200) { + $session->titleEquals($definition['label'] . ' | Drupal'); + } // Verify that all the related topics exist. Also check to see if any of // them are top-level (we will need that in the next section). @@ -151,6 +162,7 @@ $body = preg_replace('|\|sU', '', $body); $body = preg_replace('|\{\{.*\}\}|sU', '', $body); $body = preg_replace('|\{\% set.*\%\}|sU', '', $body); + $body = trim($body); $this->assertNotEmpty($body, 'Topic ' . $id . ' Twig file contains some text outside of meta tags'); // Verify that if we remove all the translated text, whitespace, and @@ -185,6 +197,62 @@ } } } + + /** + * Verifies that a bad topic fails in the expected way. + * + * @param string $id + * ID of the topic to verify. It should start with "bad_help_topics.". + * @param array $definitions + * Array of all topic definitions, keyed by ID. + * @param array $top_level + * Array of top-level topic definitions, keyed by ID. + */ + protected function verifyBadTopic($id, $definitions, $top_level) { + $bad_topic_type = substr($id, 16); + // Topics should fail verifyTopic() in specific ways. + try { + $this->verifyTopic($id, $definitions, $top_level, 404); + } + catch (\PHPUnit_Framework_Error_Warning $e) { + // The bad HTML topic fails PHP DOM parsing, which throws a PHP warning. + $this->assertTrue($bad_topic_type == 'bad_html'); + $this->assertTrue(strpos($e->getMessage(), 'Unexpected end tag') !== FALSE); + } + catch (\PHPUnit_Framework_ExpectationFailedException $e) { + // The other bad topics fail assertions. + $message = $e->getMessage(); + switch ($bad_topic_type) { + case 'related': + $this->assertTrue(strpos($message, 'only related to topics that exist') !== FALSE); + break; + + case 'top_level': + $this->assertTrue(strpos($message, 'is either top-level or related to at least one other top-level topic') !== FALSE); + break; + + case 'empty': + $this->assertTrue(strpos($message, 'contains some text outside of meta tags') !== FALSE); + break; + + case 'translated': + $this->assertTrue(strpos($message, 'Twig file has all of its text translated') !== FALSE); + break; + + case 'h1': + $this->assertTrue(strpos($message, 'has no H1 tag') !== FALSE); + break; + + case 'hierarchy': + $this->assertTrue(strpos($message, 'has the correct H2-H6 heading hierarchy') !== FALSE); + break; + + default: + // This was an unexpected error. + throw $e; + } + } + } /** * Lists the extension help topic directories of a certain type. only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/bad_help_topics/syntax/bad_help_topics.bad_html.html.twig @@ -0,0 +1,3 @@ + + +

{% trans %}Body goes here{% endtrans %} only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/bad_help_topics/syntax/bad_help_topics.empty.html.twig @@ -0,0 +1,2 @@ + + only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/bad_help_topics/syntax/bad_help_topics.h1.html.twig @@ -0,0 +1,3 @@ + + +

{% trans %}Body goes here{% endtrans %}

only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/bad_help_topics/syntax/bad_help_topics.hierarchy.html.twig @@ -0,0 +1,3 @@ + + +

{% trans %}Body goes here{% endtrans %}

only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/bad_help_topics/syntax/bad_help_topics.related.html.twig @@ -0,0 +1,4 @@ + + + +

{% trans %}Body goes here{% trans %}

only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/bad_help_topics/syntax/bad_help_topics.top_level.html.twig @@ -0,0 +1,2 @@ + +

{% trans %}Body goes here{% endtrans %}

only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/bad_help_topics/syntax/bad_help_topics.translated.html.twig @@ -0,0 +1,3 @@ + + +

Body goes here

only in patch2: unchanged: --- a/core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php +++ b/core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php @@ -34,7 +34,7 @@ public function testDiscoveryExceptionProviderMismatch() { $discovery = new HelpTopicDiscovery(['foo' => vfsStream::url('root/modules/foo/help_topics')]); $this->expectException(DiscoveryException::class); - $this->expectExceptionMessage("vfs://root/modules/foo/help_topics/test.topic.html.twig should begin with 'foo.'"); + $this->expectExceptionMessage("vfs://root/modules/foo/help_topics/test.topic.html.twig file name should begin with 'foo'"); $discovery->getDefinitions(); }