View source
<?php
namespace Drupal\Tests\help_topics\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
class HelpTopicTest extends BrowserTestBase {
use AssertBreadcrumbTrait;
protected static $modules = [
'help_topics_test',
'help',
'help_topics',
'block',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $noTestUser;
protected $anyUser;
protected function setUp() : void {
parent::setUp();
\Drupal::service('theme_installer')
->install([
'help_topics_test_theme',
]);
$settings = [
'theme' => 'stark',
'region' => 'help',
];
$this
->placeBlock('help_block', $settings);
$this
->placeBlock('local_tasks_block', $settings);
$this
->placeBlock('local_actions_block', $settings);
$this
->placeBlock('page_title_block', $settings);
$this
->placeBlock('system_breadcrumb_block', $settings);
$this->adminUser = $this
->createUser([
'access administration pages',
'view the administration theme',
'administer permissions',
'administer site configuration',
'access test help',
]);
$this->noTestUser = $this
->createUser([
'access administration pages',
'view the administration theme',
'administer permissions',
'administer site configuration',
]);
$this->anyUser = $this
->createUser([]);
}
public function testHelp() {
$session = $this
->assertSession();
$this
->drupalLogin($this->anyUser);
$this
->verifyHelp(403);
$this
->drupalLogin($this->adminUser);
$this
->verifyHelp();
$this
->verifyBreadCrumb();
$this
->drupalGet('admin/help');
$session
->responseContains('<h2>Topics</h2>');
$session
->pageTextContains('Topics can be provided by modules or themes');
$session
->responseHeaderContains('X-Drupal-Cache-Tags', 'core.extension');
$page_text = $this
->getTextContent();
$start = strpos($page_text, 'Topics can be provided');
$pos = $start;
foreach ($this
->getTopicList() as $info) {
$name = $info['name'];
$session
->linkExists($name);
$new_pos = strpos($page_text, $name, $start);
$this
->assertGreaterThan($pos, $new_pos, "Order of {$name} is not correct on page");
$pos = $new_pos;
}
$session
->linkExists('ABC Help Test module');
\Drupal::state()
->set('help_topics_test.test:top_level', FALSE);
\Drupal::service('plugin.manager.help_topic')
->clearCachedDefinitions();
$this
->drupalGet('admin/help');
$session
->linkNotExists('ABC Help Test module');
\Drupal::state()
->set('help_topics_test.test:top_level', TRUE);
\Drupal::service('plugin.manager.help_topic')
->clearCachedDefinitions();
$this
->drupalGet('admin/help');
$session
->linkExists('ABC Help Test module');
$session
->linkExists('ABC Help Test');
$session
->linkExists('XYZ Help Test theme');
$this->container
->get('module_installer')
->uninstall([
'help_topics_test',
]);
$this
->drupalGet('admin/help');
$session
->linkNotExists('ABC Help Test module');
$session
->linkNotExists('ABC Help Test');
$session
->linkExists('XYZ Help Test theme');
$this->container
->get('theme_installer')
->uninstall([
'help_topics_test_theme',
]);
$this
->drupalGet('admin/help');
$session
->linkNotExists('XYZ Help Test theme');
}
protected function verifyHelp($response = 200) {
foreach ($this
->getTopicList() as $topic => $info) {
$this
->drupalGet('admin/help/topic/' . $topic);
$session = $this
->assertSession();
$session
->statusCodeEquals($response);
if ($response == 200) {
$name = $info['name'];
$session
->titleEquals($name . ' | Drupal');
$session
->responseContains('<h1>' . $name . '</h1>');
foreach ($info['tags'] as $tag) {
$session
->responseHeaderContains('X-Drupal-Cache-Tags', $tag);
}
}
}
}
public function testHelpLinks() {
$session = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$page = 'admin/help/topic/help_topics_test.test';
$links = [
'Linked topic' => 'This topic is not supposed to be top-level',
'Additional topic' => 'This topic should get listed automatically',
'URL test topic' => 'It is used to test URLs',
];
foreach ($links as $link_text => $page_text) {
$this
->drupalGet($page);
$this
->clickLink($link_text);
$session
->pageTextContains($page_text);
}
$this
->drupalGet('admin/help/topic/help_topics_test_theme.test');
$session
->pageTextContains('This is a theme provided topic.');
$this
->assertStringContainsString('This is a theme provided topic.', $session
->elementExists('css', 'article')
->getText());
$this
->clickLink('Additional topic');
$session
->linkExists('XYZ Help Test theme');
$this
->drupalGet('admin/help');
$session
->linkNotExists('Linked topic');
$session
->linkNotExists('Additional topic');
$this
->drupalGet('admin/help/topic/help_topics_test.test_urls');
$links = [
'not a route' => FALSE,
'missing params' => FALSE,
'invalid params' => FALSE,
'valid link' => TRUE,
'Additional topic' => TRUE,
'Missing help topic not_a_topic' => FALSE,
];
foreach ($links as $text => $should_be_link) {
if ($should_be_link) {
$session
->linkExists($text);
}
else {
$session
->pageTextContains($text);
$session
->linkNotExists($text);
}
}
$this
->drupalLogin($this->noTestUser);
$this
->drupalGet('admin/help/topic/help_topics_test.test_urls');
$session
->pageTextContains('valid link');
$session
->linkNotExists('valid link');
}
protected function getTopicList() {
return [
'help_topics_test.test' => [
'name' => 'ABC Help Test module',
'tags' => [
'core.extension',
],
],
'help_topics_derivatives:test_derived_topic' => [
'name' => 'Label for test_derived_topic',
'tags' => [
'foobar',
],
],
'help_topics_test_direct_yml' => [
'name' => 'Test direct yaml topic label',
'tags' => [
'foobar',
],
],
];
}
public function verifyBreadCrumb() {
$trail = [
'' => 'Home',
'admin' => 'Administration',
'admin/help' => 'Help',
];
$this
->assertBreadcrumb('admin/help/topic/help_topics_test.test', $trail);
$this
->assertSession()
->pageTextContains('Also there should be a related topic link below to the Help module topic page and the linked topic.');
$trail = [
'' => 'Home',
'admin' => 'Administration',
'admin/config' => 'Configuration',
'admin/config/system' => 'System',
];
$this
->assertBreadcrumb('admin/config/system/site-information', $trail);
}
}