View source
<?php
namespace Drupal\Tests\help_topics\Functional;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\help_topics\Plugin\Search\HelpSearch;
class HelpTopicSearchTest extends HelpTopicTranslatedTestBase {
use CronRunTrait;
protected static $modules = [
'search',
'locale',
'language',
];
protected $defaultTheme = 'classy';
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->createUser([
'access administration pages',
'administer site configuration',
'view the administration theme',
'administer permissions',
'administer languages',
'administer search',
'access test help',
'search content',
]));
$this
->drupalPostForm('admin/config/regional/language/add', [
'predefined_langcode' => 'en',
], 'Add language');
$this
->drupalPostForm('admin/config/regional/language', [
'site_default_language' => 'en',
], 'Save configuration');
$this
->rebuildContainer();
$this
->drupalPostForm('search/help', [
'keys' => 'notawordenglish',
], 'Search');
$this
->assertSearchResultsCount(0);
$num_runs = 100;
$plugin = HelpSearch::create($this->container, [], 'help_search', []);
do {
$this
->cronRun();
$remaining = $plugin
->indexStatus()['remaining'];
} while (--$num_runs && $remaining);
$this
->assertNotEmpty($num_runs);
$this
->assertEmpty($remaining);
$this
->drupalGet('admin/config/search/pages');
$this
->assertSession()
->pageTextContains('100% of the site has been indexed');
}
public function testHelpSearch() {
$german = \Drupal::languageManager()
->getLanguage('de');
$session = $this
->assertSession();
$this
->drupalPostForm('search/help', [
'keys' => 'notawordenglish',
], 'Search');
$this
->assertSearchResultsCount(1);
$session
->linkExists('Foo in English title wcsrefsdf');
$this
->drupalPostForm('search/help', [
'keys' => 'notawordgerman',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(1);
$session
->linkExists('Foomm Foreign heading');
$this
->drupalPostForm('search/help', [
'keys' => 'notawordgerman',
], 'Search');
$this
->assertSearchResultsCount(0);
$session
->pageTextContains('no results');
$this
->drupalPostForm('search/help', [
'keys' => 'notawordenglish',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(0);
$session
->pageTextContains('no results');
$this
->drupalPostForm('search/help', [
'keys' => 'sqruct',
], 'Search');
$this
->assertSearchResultsCount(1);
$session
->linkExists('Foo in English title wcsrefsdf');
$this
->drupalPostForm('search/help', [
'keys' => 'asdrsad',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(1);
$session
->linkExists('Foomm Foreign heading');
$this
->drupalPostForm('search/help', [
'keys' => 'nonworditem',
], 'Search');
$this
->assertSearchResultsCount(1);
$session
->linkExists('ABC Help Test module');
$this
->clickLink('ABC Help Test module');
$session
->pageTextContains('This is a test');
$this
->drupalPostForm('search/help', [
'keys' => 'nonwordgerman',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(1);
$session
->linkExists('ABC-Hilfetestmodul');
$this
->clickLink('ABC-Hilfetestmodul');
$session
->pageTextContains('Übersetzung testen.');
$this
->drupalGet('admin/help');
$session
->pageTextContains('Search help');
$this
->drupalPostForm(NULL, [
'keys' => 'nonworditem',
], 'Search');
$this
->assertSearchResultsCount(1);
$session
->linkExists('ABC Help Test module');
$this
->drupalPostForm('admin/help', [
'keys' => 'nonwordgerman',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(1);
$session
->linkExists('ABC-Hilfetestmodul');
$this
->drupalPostForm('search/help', [
'keys' => 'wcsrefsdf',
], 'Search');
$this
->assertSearchResultsCount(1);
$session
->linkExists('Foo in English title wcsrefsdf');
$this
->drupalPostForm('admin/help', [
'keys' => 'sdeeeee',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(1);
$session
->linkExists('Barmm Foreign sdeeeee');
\Drupal::state()
->set('help_topics_test:translated_title', 'Updated translated title');
$this
->cronRun();
$this
->drupalPostForm('admin/help', [
'keys' => 'sdeeeee',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(1);
$session
->linkExists('Updated translated title');
$this
->drupalPostForm('admin/help', [
'keys' => 'translated title',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(0);
$this
->rebuildAll();
$this
->cronRun();
$this
->drupalPostForm('admin/help', [
'keys' => 'sdeeeee',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(0);
$this
->drupalPostForm('admin/help', [
'keys' => 'translated title',
], 'Search', [
'language' => $german,
]);
$this
->assertSearchResultsCount(1);
$session
->linkExists('Updated translated title');
$session
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.help_search');
$session
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:help_search');
$session
->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.permissions');
$session
->responseHeaderContains('X-Drupal-Cache-Contexts', 'languages:language_interface');
$this
->drupalLogin($this
->createUser([
'access administration pages',
'administer site configuration',
'view the administration theme',
'administer permissions',
'administer languages',
'administer search',
'search content',
]));
$this
->drupalGet('admin/help');
$session
->pageTextContains('Search help');
$this
->drupalPostForm('search/help', [
'keys' => 'nonworditem',
], 'Search');
$this
->assertSearchResultsCount(1);
$session
->linkExists('ABC Help Test module');
$this
->drupalPostForm('search/help', [
'keys' => 'notawordenglish',
], 'Search');
$this
->assertSearchResultsCount(0);
$session
->pageTextContains('no results');
\Drupal::service('module_installer')
->uninstall([
'help_topics_test',
]);
$this
->drupalPostForm('search/help', [
'keys' => 'nonworditem',
], 'Search');
$this
->assertSearchResultsCount(0);
}
public function testUninstall() {
$this
->drupalLogin($this->rootUser);
$edit = [];
$edit['uninstall[help_topics]'] = TRUE;
$this
->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
$this
->drupalPostForm(NULL, NULL, t('Uninstall'));
$this
->assertText(t('The selected modules have been uninstalled.'), 'Modules status has been updated.');
$this
->drupalGet('admin/help');
$this
->assertSession()
->statusCodeEquals(200);
}
protected function assertSearchResultsCount($count) {
$this
->assertSession()
->elementsCount('css', '.help_search-results > li', $count);
}
}