View source
<?php
namespace Drupal\Tests\help\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
class HelpTest extends BrowserTestBase {
public static $modules = [
'help_test',
'help_page_test',
];
protected $defaultTheme = 'stark';
protected $profile = 'standard';
protected $adminUser;
protected $anyUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'view the administration theme',
'administer permissions',
]);
$this->anyUser = $this
->drupalCreateUser([]);
}
public function testHelp() {
$this
->drupalLogin($this->rootUser);
$this
->verifyHelp();
$this
->drupalLogin($this->anyUser);
$this
->verifyHelp(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/help');
$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',
]));
$this
->assertRaw('<h2>' . t('Module overviews') . '</h2>');
$this
->assertRaw('<p>' . t('Module overviews are provided by modules. Overviews available for your installed modules:'), '</p>');
$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.'));
foreach ($this
->getModuleList() as $module => $name) {
$this
->assertSession()
->linkExists($name, 0, new FormattableMarkup('Link properly added to @name (admin/help/@module)', [
'@module' => $module,
'@name' => $name,
]));
}
$this
->clickLink(\Drupal::moduleHandler()
->getName('help_test'));
$this
->assertRaw(t('No help is available for module %module.', [
'%module' => \Drupal::moduleHandler()
->getName('help_test'),
]));
$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
->assertSession()
->linkExists($name);
$new_pos = strpos($page_text, $name, $start);
$this
->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
$pos = $new_pos;
}
}
protected function verifyHelp($response = 200) {
$this
->drupalGet('admin/index');
$this
->assertSession()
->statusCodeEquals($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.');
}
foreach ($this
->getModuleList() as $module => $name) {
$this
->drupalGet('admin/help/' . $module);
$this
->assertSession()
->statusCodeEquals($response);
if ($response == 200) {
$this
->assertTitle("{$name} | Drupal");
$this
->assertEquals($name, $this
->cssSelect('h1.page-title')[0]
->getText(), "{$module} heading was displayed");
$info = \Drupal::service('extension.list.module')
->getExtensionInfo($module);
$admin_tasks = system_get_module_admin_tasks($module, $info);
if (!empty($admin_tasks)) {
$this
->assertText(t('@module administration pages', [
'@module' => $name,
]));
}
foreach ($admin_tasks as $task) {
$this
->assertSession()
->linkExists($task['title']);
$this
->assertNoEscaped('&');
$this
->assertNoEscaped('<');
$this
->assertNoEscaped('<');
}
$this
->assertNoEscaped('&');
$this
->assertNoEscaped('<');
$this
->assertNoEscaped('<');
}
}
}
protected function getModuleList() {
$modules = [];
$module_data = $this->container
->get('extension.list.module')
->getList();
foreach (\Drupal::moduleHandler()
->getImplementations('help') as $module) {
$modules[$module] = $module_data[$module]->info['name'];
}
return $modules;
}
}