dff11ef173c6366c42130a51ef34c11ff3522cb5
[yaffs-website] / web / core / modules / help / tests / src / Functional / HelpTest.php
1 <?php
2
3 namespace Drupal\Tests\help\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Verify help display and user access to help based on permissions.
9  *
10  * @group help
11  */
12 class HelpTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
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
19    * returns no links.
20    *
21    * @var array.
22    */
23   public static $modules = ['help_test', 'help_page_test'];
24
25   /**
26    * Use the Standard profile to test help implementations of many core modules.
27    *
28    * @var string
29    */
30   protected $profile = 'standard';
31
32   /**
33    * The admin user that will be created.
34    */
35   protected $adminUser;
36
37   /**
38    * The anonymous user that will be created.
39    */
40   protected $anyUser;
41
42   protected function setUp() {
43     parent::setUp();
44
45     // Create users.
46     $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view the administration theme', 'administer permissions']);
47     $this->anyUser = $this->drupalCreateUser([]);
48   }
49
50   /**
51    * Logs in users, tests help pages.
52    */
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);
57     $this->verifyHelp();
58
59     // Log in the regular user.
60     $this->drupalLogin($this->anyUser);
61     $this->verifyHelp(403);
62
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']));
67
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>');
71
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.'));
76
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]));
80     }
81
82     // Ensure that module which does not provide an module overview page is
83     // handled correctly.
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')]));
86
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');
93     $pos = $start;
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');
99       $pos = $new_pos;
100     }
101   }
102
103   /**
104    * Verifies the logged in user has access to the various help pages.
105    *
106    * @param int $response
107    *   (optional) An HTTP response code. Defaults to 200.
108    */
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.');
114     }
115     else {
116       $this->assertNoText('This page shows you all available administration tasks for each module.');
117     }
118
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]));
129         }
130         foreach ($admin_tasks as $task) {
131           $this->assertLink($task['title']);
132           // Ensure there are no double escaped '&' or '<' characters.
133           $this->assertNoEscaped('&amp;', 'The help text does not have double escaped &amp;.');
134           $this->assertNoEscaped('&lt;', 'The help text does not have double escaped &lt;.');
135           // Ensure there are no escaped '<' characters.
136           $this->assertNoEscaped('<', 'The help text does not have single escaped &lt;.');
137         }
138         // Ensure there are no double escaped '&' or '<' characters.
139         $this->assertNoEscaped('&amp;');
140         $this->assertNoEscaped('&lt;');
141         // Ensure there are no escaped '<' characters.
142         $this->assertNoEscaped('<');
143       }
144     }
145   }
146
147   /**
148    * Gets the list of enabled modules that implement hook_help().
149    *
150    * @return array
151    *   A list of enabled modules.
152    */
153   protected function getModuleList() {
154     $modules = [];
155     $module_data = system_rebuild_module_data();
156     foreach (\Drupal::moduleHandler()->getImplementations('help') as $module) {
157       $modules[$module] = $module_data[$module]->info['name'];
158     }
159     return $modules;
160   }
161
162 }