3 namespace Drupal\Tests\help\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Verify help display and user access to help based on permissions.
12 class HelpTest extends BrowserTestBase {
17 * The help_test module implements hook_help() but does not provide a module
18 * overview page. The help_page_test module has a page section plugin that
23 public static $modules = ['help_test', 'help_page_test'];
26 * Use the Standard profile to test help implementations of many core modules.
30 protected $profile = 'standard';
33 * The admin user that will be created.
38 * The anonymous user that will be created.
42 protected function setUp() {
46 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view the administration theme', 'administer permissions']);
47 $this->anyUser = $this->drupalCreateUser([]);
51 * Logs in users, tests help pages.
53 public function testHelp() {
54 // Log in the root user to ensure as many admin links appear as possible on
55 // the module overview pages.
56 $this->drupalLogin($this->rootUser);
59 // Log in the regular user.
60 $this->drupalLogin($this->anyUser);
61 $this->verifyHelp(403);
63 // Verify that introductory help text exists, goes for 100% module coverage.
64 $this->drupalLogin($this->adminUser);
65 $this->drupalGet('admin/help');
66 $this->assertRaw(t('For more information, refer to the help listed on this page or to the <a href=":docs">online documentation</a> and <a href=":support">support</a> pages at <a href=":drupal">drupal.org</a>.', [':docs' => 'https://www.drupal.org/documentation', ':support' => 'https://www.drupal.org/support', ':drupal' => 'https://www.drupal.org']));
68 // Verify that hook_help() section title and description appear.
69 $this->assertRaw('<h2>' . t('Module overviews') . '</h2>');
70 $this->assertRaw('<p>' . t('Module overviews are provided by modules. Overviews available for your installed modules:'), '</p>');
72 // Verify that an empty section is handled correctly.
73 $this->assertRaw('<h2>' . t('Empty section') . '</h2>');
74 $this->assertRaw('<p>' . t('This description should appear.'), '</p>');
75 $this->assertText(t('There is currently nothing in this section.'));
77 // Make sure links are properly added for modules implementing hook_help().
78 foreach ($this->getModuleList() as $module => $name) {
79 $this->assertLink($name, 0, format_string('Link properly added to @name (admin/help/@module)', ['@module' => $module, '@name' => $name]));
82 // Ensure that module which does not provide an module overview page is
84 $this->clickLink(\Drupal::moduleHandler()->getName('help_test'));
85 $this->assertRaw(t('No help is available for module %module.', ['%module' => \Drupal::moduleHandler()->getName('help_test')]));
87 // Verify that the order of topics is alphabetical by displayed module
88 // name, by checking the order of some modules, including some that would
89 // have a different order if it was done by machine name instead.
90 $this->drupalGet('admin/help');
91 $page_text = $this->getTextContent();
92 $start = strpos($page_text, 'Module overviews');
94 $list = ['Block', 'Color', 'Custom Block', 'History', 'Text Editor'];
95 foreach ($list as $name) {
96 $this->assertLink($name);
97 $new_pos = strpos($page_text, $name, $start);
98 $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
104 * Verifies the logged in user has access to the various help pages.
106 * @param int $response
107 * (optional) An HTTP response code. Defaults to 200.
109 protected function verifyHelp($response = 200) {
110 $this->drupalGet('admin/index');
111 $this->assertResponse($response);
112 if ($response == 200) {
113 $this->assertText('This page shows you all available administration tasks for each module.');
116 $this->assertNoText('This page shows you all available administration tasks for each module.');
119 foreach ($this->getModuleList() as $module => $name) {
120 // View module help page.
121 $this->drupalGet('admin/help/' . $module);
122 $this->assertResponse($response);
123 if ($response == 200) {
124 $this->assertTitle($name . ' | Drupal', format_string('%module title was displayed', ['%module' => $module]));
125 $this->assertEquals($name, $this->cssSelect('h1.page-title')[0]->getText(), "$module heading was displayed");
126 $admin_tasks = system_get_module_admin_tasks($module, system_get_info('module', $module));
127 if (!empty($admin_tasks)) {
128 $this->assertText(t('@module administration pages', ['@module' => $name]));
130 foreach ($admin_tasks as $task) {
131 $this->assertLink($task['title']);
132 // Ensure there are no double escaped '&' or '<' characters.
133 $this->assertNoEscaped('&', 'The help text does not have double escaped &.');
134 $this->assertNoEscaped('<', 'The help text does not have double escaped <.');
135 // Ensure there are no escaped '<' characters.
136 $this->assertNoEscaped('<', 'The help text does not have single escaped <.');
138 // Ensure there are no double escaped '&' or '<' characters.
139 $this->assertNoEscaped('&');
140 $this->assertNoEscaped('<');
141 // Ensure there are no escaped '<' characters.
142 $this->assertNoEscaped('<');
148 * Gets the list of enabled modules that implement hook_help().
151 * A list of enabled modules.
153 protected function getModuleList() {
155 $module_data = system_rebuild_module_data();
156 foreach (\Drupal::moduleHandler()->getImplementations('help') as $module) {
157 $modules[$module] = $module_data[$module]->info['name'];