comparison core/modules/help/tests/src/Functional/HelpTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 protected $profile = 'standard';
29
30 /**
31 * The admin user that will be created.
32 */
33 protected $adminUser;
34
35 /**
36 * The anonymous user that will be created.
37 */
38 protected $anyUser;
39
40 protected function setUp() {
41 parent::setUp();
42
43 // Create users.
44 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view the administration theme', 'administer permissions']);
45 $this->anyUser = $this->drupalCreateUser([]);
46 }
47
48 /**
49 * Logs in users, tests help pages.
50 */
51 public function testHelp() {
52 // Log in the root user to ensure as many admin links appear as possible on
53 // the module overview pages.
54 $this->drupalLogin($this->rootUser);
55 $this->verifyHelp();
56
57 // Log in the regular user.
58 $this->drupalLogin($this->anyUser);
59 $this->verifyHelp(403);
60
61 // Verify that introductory help text exists, goes for 100% module coverage.
62 $this->drupalLogin($this->adminUser);
63 $this->drupalGet('admin/help');
64 $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']));
65
66 // Verify that hook_help() section title and description appear.
67 $this->assertRaw('<h2>' . t('Module overviews') . '</h2>');
68 $this->assertRaw('<p>' . t('Module overviews are provided by modules. Overviews available for your installed modules:'), '</p>');
69
70 // Verify that an empty section is handled correctly.
71 $this->assertRaw('<h2>' . t('Empty section') . '</h2>');
72 $this->assertRaw('<p>' . t('This description should appear.'), '</p>');
73 $this->assertText(t('There is currently nothing in this section.'));
74
75 // Make sure links are properly added for modules implementing hook_help().
76 foreach ($this->getModuleList() as $module => $name) {
77 $this->assertLink($name, 0, format_string('Link properly added to @name (admin/help/@module)', ['@module' => $module, '@name' => $name]));
78 }
79
80 // Ensure that module which does not provide an module overview page is
81 // handled correctly.
82 $this->clickLink(\Drupal::moduleHandler()->getName('help_test'));
83 $this->assertRaw(t('No help is available for module %module.', ['%module' => \Drupal::moduleHandler()->getName('help_test')]));
84
85 // Verify that the order of topics is alphabetical by displayed module
86 // name, by checking the order of some modules, including some that would
87 // have a different order if it was done by machine name instead.
88 $this->drupalGet('admin/help');
89 $page_text = $this->getTextContent();
90 $start = strpos($page_text, 'Module overviews');
91 $pos = $start;
92 $list = ['Block', 'Color', 'Custom Block', 'History', 'Text Editor'];
93 foreach ($list as $name) {
94 $this->assertLink($name);
95 $new_pos = strpos($page_text, $name, $start);
96 $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
97 $pos = $new_pos;
98 }
99 }
100
101 /**
102 * Verifies the logged in user has access to the various help pages.
103 *
104 * @param int $response
105 * (optional) An HTTP response code. Defaults to 200.
106 */
107 protected function verifyHelp($response = 200) {
108 $this->drupalGet('admin/index');
109 $this->assertResponse($response);
110 if ($response == 200) {
111 $this->assertText('This page shows you all available administration tasks for each module.');
112 }
113 else {
114 $this->assertNoText('This page shows you all available administration tasks for each module.');
115 }
116
117 foreach ($this->getModuleList() as $module => $name) {
118 // View module help page.
119 $this->drupalGet('admin/help/' . $module);
120 $this->assertResponse($response);
121 if ($response == 200) {
122 $this->assertTitle($name . ' | Drupal', format_string('%module title was displayed', ['%module' => $module]));
123 $this->assertEquals($name, $this->cssSelect('h1.page-title')[0]->getText(), "$module heading was displayed");
124 $admin_tasks = system_get_module_admin_tasks($module, system_get_info('module', $module));
125 if (!empty($admin_tasks)) {
126 $this->assertText(t('@module administration pages', ['@module' => $name]));
127 }
128 foreach ($admin_tasks as $task) {
129 $this->assertLink($task['title']);
130 // Ensure there are no double escaped '&' or '<' characters.
131 $this->assertNoEscaped('&amp;', 'The help text does not have double escaped &amp;.');
132 $this->assertNoEscaped('&lt;', 'The help text does not have double escaped &lt;.');
133 // Ensure there are no escaped '<' characters.
134 $this->assertNoEscaped('<', 'The help text does not have single escaped &lt;.');
135 }
136 // Ensure there are no double escaped '&' or '<' characters.
137 $this->assertNoEscaped('&amp;');
138 $this->assertNoEscaped('&lt;');
139 // Ensure there are no escaped '<' characters.
140 $this->assertNoEscaped('<');
141 }
142 }
143 }
144
145 /**
146 * Gets the list of enabled modules that implement hook_help().
147 *
148 * @return array
149 * A list of enabled modules.
150 */
151 protected function getModuleList() {
152 $modules = [];
153 $module_data = system_rebuild_module_data();
154 foreach (\Drupal::moduleHandler()->getImplementations('help') as $module) {
155 $modules[$module] = $module_data[$module]->info['name'];
156 }
157 return $modules;
158 }
159
160 }