HelpTest.php 6.07 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\help\Functional;
4

5
use Drupal\Tests\BrowserTestBase;
6

7
/**
8 9 10
 * Verify help display and user access to help based on permissions.
 *
 * @group help
11
 */
12
class HelpTest extends BrowserTestBase {
13 14 15 16

  /**
   * Modules to enable.
   *
17
   * The help_test module implements hook_help() but does not provide a module
18 19
   * overview page. The help_page_test module has a page section plugin that
   * 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 31
  protected $profile = 'standard';

32 33 34
  /**
   * The admin user that will be created.
   */
35
  protected $adminUser;
36 37 38 39

  /**
   * The anonymous user that will be created.
   */
40
  protected $anyUser;
41

42
  protected function setUp() {
43
    parent::setUp();
44 45

    // Create users.
46 47
    $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view the administration theme', 'administer permissions']);
    $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 56
    // the module overview pages.
    $this->drupalLogin($this->rootUser);
57 58
    $this->verifyHelp();

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 69 70 71 72 73 74 75
    // Verify that hook_help() section title and description appear.
    $this->assertRaw('<h2>' . t('Module overviews') . '</h2>');
    $this->assertRaw('<p>' . t('Module overviews are provided by modules. Overviews available for your installed modules:'), '</p>');

    // Verify that an empty section is handled correctly.
    $this->assertRaw('<h2>' . t('Empty section') . '</h2>');
    $this->assertRaw('<p>' . t('This description should appear.'), '</p>');
    $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 83 84

    // Ensure that module which does not provide an module overview page is
    // handled correctly.
    $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 88 89 90 91 92 93 94 95 96 97 98 99 100

    // Verify that the order of topics is alphabetical by displayed module
    // name, by checking the order of some modules, including some that would
    // have a different order if it was done by machine name instead.
    $this->drupalGet('admin/help');
    $page_text = $this->getTextContent();
    $start = strpos($page_text, 'Module overviews');
    $pos = $start;
    $list = ['Block', 'Color', 'Custom Block', 'History', 'Text Editor'];
    foreach ($list as $name) {
      $this->assertLink($name);
      $new_pos = strpos($page_text, $name, $start);
      $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
      $pos = $new_pos;
    }
101 102 103
  }

  /**
104
   * Verifies the logged in user has access to the various help pages.
105
   *
106 107
   * @param int $response
   *   (optional) An HTTP response code. Defaults to 200.
108
   */
109
  protected function verifyHelp($response = 200) {
110 111 112 113 114 115 116 117 118
    $this->drupalGet('admin/index');
    $this->assertResponse($response);
    if ($response == 200) {
      $this->assertText('This page shows you all available administration tasks for each module.');
    }
    else {
      $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 127
        $admin_tasks = system_get_module_admin_tasks($module, system_get_info('module', $module));
        if (!empty($admin_tasks)) {
128
          $this->assertText(t('@module administration pages', ['@module' => $name]));
129 130 131
        }
        foreach ($admin_tasks as $task) {
          $this->assertLink($task['title']);
132 133 134 135 136
          // Ensure there are no double escaped '&' or '<' characters.
          $this->assertNoEscaped('&amp;', 'The help text does not have double escaped &amp;.');
          $this->assertNoEscaped('&lt;', 'The help text does not have double escaped &lt;.');
          // Ensure there are no escaped '<' characters.
          $this->assertNoEscaped('<', 'The help text does not have single escaped &lt;.');
137
        }
138 139 140 141 142
        // Ensure there are no double escaped '&' or '<' characters.
        $this->assertNoEscaped('&amp;');
        $this->assertNoEscaped('&lt;');
        // Ensure there are no escaped '<' characters.
        $this->assertNoEscaped('<');
143
      }
144 145 146 147
    }
  }

  /**
148
   * Gets the list of enabled modules that implement hook_help().
149
   *
150 151
   * @return array
   *   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
}