annotate core/modules/help/tests/src/Functional/HelpTest.php @ 2:92f882872392

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