diff -u b/core/modules/help_topics/tests/src/Kernel/HelpTopicCoreTopicsTest.php b/core/modules/help_topics/tests/src/Kernel/HelpTopicCoreTopicsTest.php --- b/core/modules/help_topics/tests/src/Kernel/HelpTopicCoreTopicsTest.php +++ b/core/modules/help_topics/tests/src/Kernel/HelpTopicCoreTopicsTest.php @@ -3,8 +3,8 @@ namespace Drupal\Tests\help_topics\Kernel; use Drupal\Component\Discovery\DiscoveryException; -use Drupal\help_topics\HelpTopicDiscovery; use Drupal\KernelTests\KernelTestBase; +use Drupal\help_topics\HelpTopicDiscovery; use org\bovigo\vfs\vfsStream; /** @@ -13,27 +13,16 @@ */ class HelpTopicCoreTopicsTest extends KernelTestBase { + use HelpTopicDiscoveryHelperTrait; + /** * Ensure all core help topics do not throw exceptions and follow standards. */ public function testCoreTopics() { - $directories = []; - - // Add directories for all modules, themes, and profiles, even if they are - // not installed, but excluding test ones. - foreach (['module', 'theme', 'profile'] as $type) { - $lister = \Drupal::service('extension.list.' . $type); - foreach ($lister->getAllAvailableInfo() as $name => $info) { - $path = $lister->getPath($name); - // You can tell test modules because they are in package 'Testing', but - // test themes are only known by being found in test directories. So... - // exclude things in test directories. - if (strpos($path, '/tests/' . $type . 's/') === FALSE) { - $directories[$name] = $path . '/help_topics'; - } - } - } + $directories = $this->listDirectories('module') + + $this->listDirectories('theme') + + $this->listDirectories('profile'); // Just to make sure, verify that a few key modules, themes, and profiles // are listed. If these lines fail, it is probably because something is @@ -48,14 +37,9 @@ $directories['core'] = 'core/help_topics'; // Discover the help topics within these directories. - $discovery = new HelpTopicDiscovery($directories); - $definitions = $discovery->getDefinitions(); + $definitions = $this->discoverAllTopics($directories); $this->assertTrue(count($definitions) > 1, 'At least 1 topic was found'); - - // Figure out which topics are top-level. - $top_level = array_filter($definitions, function($definition) { - return isset($definition['top_level']) && $definition['top_level']; - }); + $top_level = $this->findTopLevel($definitions); // Test each topic for compliance with standards. foreach (array_keys($definitions) as $id) { @@ -79,8 +63,7 @@ // Check that just this expected failure topic does throw an exception. $this->expectException(DiscoveryException::class); $this->expectExceptionMessage("vfs://root/modules/test/help_topics/test.topic.html.twig should begin with 'expected_failure.'"); - $fail_discovery = new HelpTopicDiscovery($fail_directories); - $fail_discovery->getDefinitions(); + $this->discoverAllTopics($fail_directories); } /** only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/src/Functional/HelpTopicsRenderTest.php @@ -0,0 +1,71 @@ +drupalLogin($this->rootUser); + + // Enable all modules and themes, so that hopefully all routes will be + // defined. + $module_directories = $this->listDirectories('module'); + $this->container->get('module_installer')->install(array_keys($module_directories)); + $theme_directories = $this->listDirectories('theme'); + $this->container->get('theme_installer')->install(array_keys($theme_directories)); + + $directories = $module_directories + $theme_directories + + $this->listDirectories('profile'); + $directories['core'] = 'core/help_topics'; + + $definitions = $this->discoverAllTopics($directories); + foreach ($definitions as $id => $definition) { + $this->verifyTopicRendering($id, $definition); + } + } + + /** + * Verifies rendering of one help topic. + * + * @param string $id + * ID of the topic to render. + * @param array $definition + * Definition of the topic. + */ + protected function verifyTopicRendering($id, $definition) { + // Visit the URL for the topic. + $this->drupalGet('admin/help/topic/' . $id); + + // Verify the title and response. + $session = $this->assertSession(); + $session->statusCodeEquals(200); + $session->titleEquals($definition['label'] . ' | Drupal'); + } + +} only in patch2: unchanged: --- /dev/null +++ b/core/modules/help_topics/tests/src/Kernel/HelpTopicDiscoveryHelperTrait.php @@ -0,0 +1,72 @@ +getAllAvailableInfo() as $name => $info) { + $path = $lister->getPath($name); + // You can tell test modules because they are in package 'Testing', but + // test themes are only known by being found in test directories. So... + // exclude things in test directories. + if ((strpos($path, '/tests') === FALSE) && + (strpos($path, '/testing') === FALSE)) { + $directories[$name] = $path . '/help_topics'; + } + } + return $directories; + } + + /** + * 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']; + }); + } + +}