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 @@ -36,9 +36,9 @@ // Enable all modules and themes, so that all routes mentioned in topics // will be defined. $module_directories = $this->listDirectories('module'); - $this->container->get('module_installer')->install(array_keys($module_directories)); + \Drupal::service('module_installer')->install(array_keys($module_directories)); $theme_directories = $this->listDirectories('theme'); - $this->container->get('theme_installer')->install(array_keys($theme_directories)); + \Drupal::service('theme_installer')->install(array_keys($theme_directories)); $directories = $module_directories + $theme_directories + $this->listDirectories('profile'); @@ -57,24 +57,22 @@ // it is probably because something we chose to list here is being removed. // Substitute another item of the same type that still exists, so that this // test can continue. - $this->assertTrue(isset($directories['system']), 'System module is being scanned'); - $this->assertTrue(isset($directories['help']), 'Help module is being scanned'); - $this->assertTrue(isset($directories['seven']), 'Seven theme is being scanned'); - $this->assertTrue(isset($directories['standard']), 'Standard profile is being scanned'); - - $definitions = $this->discoverAllTopics($directories); - $this->assertTrue(count($definitions) > 1, 'At least 1 topic was found'); - $top_level = $this->findTopLevel($definitions); + $this->assertArrayHasKey('system', $directories, 'System module is being scanned'); + $this->assertArrayHasKey('help', $directories, 'Help module is being scanned'); + $this->assertArrayHasKey('seven', $directories, 'Seven theme is being scanned'); + $this->assertArrayHasKey('standard', $directories, 'Standard profile is being scanned'); + + $definitions = (new HelpTopicDiscovery($directories))->getDefinitions(); + $this->assertGreaterThan(0, count($definitions), 'At least 1 topic was found'); // Test each topic for compliance with standards, or for failing in the // right way. foreach (array_keys($definitions) as $id) { - $is_bad_topic = (strpos($id, 'bad_help_topics.') === 0); - if ($is_bad_topic) { - $this->verifyBadTopic($id, $definitions, $top_level); + if (strpos($id, 'bad_help_topics.') === 0) { + $this->verifyBadTopic($id, $definitions); } else { - $this->verifyTopic($id, $definitions, $top_level); + $this->verifyTopic($id, $definitions); } } } @@ -86,12 +84,10 @@ * ID of the topic to verify. * @param array $definitions * 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, $response = 200) { + protected function verifyTopic($id, $definitions, $response = 200) { $definition = $definitions[$id]; // Visit the URL for the topic. @@ -109,17 +105,16 @@ $has_top_level_related = FALSE; if (isset($definition['related'])) { foreach ($definition['related'] as $related_id) { - $this->assertTrue(isset($definitions[$related_id]), 'Topic ' . $id . ' is only related to topics that exist (' . $related_id . ')'); - $has_top_level_related = $has_top_level_related || isset($top_level[$related_id]); + $this->assertArrayHasKey($related_id, $definitions, 'Topic ' . $id . ' is only related to topics that exist (' . $related_id . ')'); + $has_top_level_related = $has_top_level_related || !empty($definitions[$related_id]['top_level']); } } // Verify this is either top-level or related to a top-level topic. - $this->assertTrue(isset($top_level[$id]) || $has_top_level_related, - 'Topic ' . $id . ' is either top-level or related to at least one other top-level topic'); + $this->assertTrue(!empty($definition['top_level']) || $has_top_level_related, 'Topic ' . $id . ' is either top-level or related to at least one other top-level topic'); // Verify that the label is not empty. - $this->assertTrue(isset($definition['label']) && !empty($definition['label']), 'Topic ' . $id . ' has a non-empty label'); + $this->assertNotEmpty($definition['label'], 'Topic ' . $id . ' has a non-empty label'); // Read in the file so we can run some tests on that. $body = file_get_contents($definition[HelpTopicDiscovery::FILE_KEY]); @@ -158,7 +153,7 @@ foreach ($levels as $level) { $num_headings[$level] = $doc->getElementsByTagName('h' . $level)->length; if ($level == 1) { - $this->assertTrue($num_headings[1] == 0, 'Topic ' . $id . ' has no H1 tag'); + $this->assertSame(0, $num_headings[1], 'Topic ' . $id . ' has no H1 tag'); // Set num_headings to 1 for this level, so the rest of the hierarchy // can be tested using simpler code. $num_headings[1] = 1; @@ -180,44 +175,42 @@ * 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) { + protected function verifyBadTopic($id, $definitions) { $bad_topic_type = substr($id, 16); // Topics should fail verifyTopic() in specific ways. try { - $this->verifyTopic($id, $definitions, $top_level, 404); + $this->verifyTopic($id, $definitions, 404); } catch (ExpectationFailedException $e) { $message = $e->getMessage(); switch ($bad_topic_type) { case 'related': - $this->assertTrue(strpos($message, 'only related to topics that exist') !== FALSE); + $this->assertContains('only related to topics that exist', $message); break; case 'bad_html': - $this->assertTrue(strpos($message, 'Unexpected end tag') !== FALSE); + $this->assertContains('Unexpected end tag', $message); break; case 'top_level': - $this->assertTrue(strpos($message, 'is either top-level or related to at least one other top-level topic') !== FALSE); + $this->assertContains('is either top-level or related to at least one other top-level topic', $message); break; case 'empty': - $this->assertTrue(strpos($message, 'contains some text outside of front matter') !== FALSE); + $this->assertContains('contains some text outside of front matter', $message); break; case 'translated': - $this->assertTrue(strpos($message, 'Twig file has all of its text translated') !== FALSE); + $this->assertContains('Twig file has all of its text translated', $message); break; case 'h1': - $this->assertTrue(strpos($message, 'has no H1 tag') !== FALSE); + $this->assertContains('has no H1 tag', $message); break; case 'hierarchy': - $this->assertTrue(strpos($message, 'has the correct H2-H6 heading hierarchy') !== FALSE); + $this->assertContains('has the correct H2-H6 heading hierarchy', $message); break; default: @@ -258,33 +251,2 @@ - /** - * Discovers the help topics in a list of locations. - * - * @param string[] $directories - * An array of directories to find topics in, keyed by extension short name. - * - * @return array - * Array of all topic definitions, keyed by topic ID, from non-test - * modules, themes, and profiles. - */ - protected function discoverAllTopics(array $directories) { - $discovery = new HelpTopicDiscovery($directories); - return $discovery->getDefinitions(); - } - - /** - * Finds the list of top-level topics. - * - * @param array $definitions - * An array of topic definitions, keyed by ID. - * - * @return array - * The items from $definitions that represent top-level topics, still - * keyed by ID. - */ - protected function findTopLevel(array $definitions) { - return array_filter($definitions, function ($definition) { - return isset($definition['top_level']) && $definition['top_level']; - }); - } - }