function HelpTopicSearchTest::testHelpSearch

Same name in other branches
  1. 9 core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php \Drupal\Tests\help_topics\Functional\HelpTopicSearchTest::testHelpSearch()
  2. 8.9.x core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php \Drupal\Tests\help_topics\Functional\HelpTopicSearchTest::testHelpSearch()
  3. 10 core/modules/help/tests/src/Functional/HelpTopicSearchTest.php \Drupal\Tests\help\Functional\HelpTopicSearchTest::testHelpSearch()

Tests help topic search.

File

core/modules/help/tests/src/Functional/HelpTopicSearchTest.php, line 99

Class

HelpTopicSearchTest
Verifies help topic search.

Namespace

Drupal\Tests\help\Functional

Code

public function testHelpSearch() : void {
    $german = \Drupal::languageManager()->getLanguage('de');
    $session = $this->assertSession();
    // Verify that when we search in English for a word that is only in
    // English text, we find the topic. Note that these "words" are provided
    // by the topics that come from
    // \Drupal\help_topics_test\Plugin\HelpSection\TestHelpSection.
    $this->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'not-a-word-english',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Foo in English title wcsrefsdf');
    // Same for German.
    $this->drupalGet('search/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'not-a-word-german',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Foomm Foreign heading');
    // Verify when we search in English for a word that only exists in German,
    // we get no results.
    $this->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'not-a-word-german',
    ], 'Search');
    $this->assertSearchResultsCount(0);
    $session->pageTextContains('no results');
    // Same for German.
    $this->drupalGet('search/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'not-a-word-english',
    ], 'Search');
    $this->assertSearchResultsCount(0);
    $session->pageTextContains('no results');
    // Verify when we search in English for a word that exists in one topic
    // in English and a different topic in German, we only get the one English
    // topic.
    $this->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'sqruct',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Foo in English title wcsrefsdf');
    // Same for German.
    $this->drupalGet('search/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'asdrsad',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Foomm Foreign heading');
    // All of the above tests used the TestHelpSection plugin. Also verify
    // that we can search for translated regular help topics, in both English
    // and German.
    $this->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'non-word-item',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('ABC Help Test module');
    // Click the link and verify we ended up on the topic page.
    $this->clickLink('ABC Help Test module');
    $session->pageTextContains('This is a test');
    $this->drupalGet('search/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'non-word-german',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('ABC-Hilfetestmodul');
    $this->clickLink('ABC-Hilfetestmodul');
    $session->pageTextContains('Übersetzung testen.');
    // Verify that we can search from the admin/help page.
    $this->drupalGet('admin/help');
    $session->pageTextContains('Search help');
    $this->submitForm([
        'keys' => 'non-word-item',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('ABC Help Test module');
    // Same for German.
    $this->drupalGet('admin/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'non-word-german',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('ABC-Hilfetestmodul');
    // Verify we can search for title text (other searches used text
    // that was part of the body).
    $this->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'wcsrefsdf',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Foo in English title wcsrefsdf');
    $this->drupalGet('admin/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'sdeeeee',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Barmm Foreign sdeeeee');
    // Just changing the title and running cron is not enough to reindex so
    // 'sdeeeee' still hits a match. The content is updated because the help
    // topic is rendered each time.
    \Drupal::state()->set('help_topics_test:translated_title', 'Updated translated title');
    $this->cronRun();
    $this->drupalGet('admin/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'sdeeeee',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Updated translated title');
    // Searching for the updated test shouldn't produce a match.
    $this->drupalGet('admin/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'translated title',
    ], 'Search');
    $this->assertSearchResultsCount(0);
    // Clear the caches and re-run cron - this should re-index the help.
    $this->rebuildAll();
    $this->cronRun();
    $this->drupalGet('admin/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'sdeeeee',
    ], 'Search');
    $this->assertSearchResultsCount(0);
    $this->drupalGet('admin/help', [
        'language' => $german,
    ]);
    $this->submitForm([
        'keys' => 'translated title',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('Updated translated title');
    // Verify the cache tags and contexts.
    $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');
    // Log in as a user that does not have permission to see TestHelpSection
    // items, and verify they can still search for help topics but not see these
    // items.
    $this->drupalLogin($this->createUser([
        'access help 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->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'non-word-item',
    ], 'Search');
    $this->assertSearchResultsCount(1);
    $session->linkExists('ABC Help Test module');
    $this->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'not-a-word-english',
    ], 'Search');
    $this->assertSearchResultsCount(0);
    $session->pageTextContains('no results');
    // Uninstall the test module and verify its topics are immediately not
    // searchable.
    \Drupal::service('module_installer')->uninstall([
        'help_topics_test',
    ]);
    $this->drupalGet('search/help');
    $this->submitForm([
        'keys' => 'non-word-item',
    ], 'Search');
    $this->assertSearchResultsCount(0);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.