You are here

public function HelpTopicSearchTest::testHelpSearch in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php \Drupal\Tests\help_topics\Functional\HelpTopicSearchTest::testHelpSearch()

Tests help topic search.

File

core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php, line 84

Class

HelpTopicSearchTest
Verifies help topic search.

Namespace

Drupal\Tests\help_topics\Functional

Code

public function testHelpSearch() {
  $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
    ->drupalPostForm('search/help', [
    'keys' => 'notawordenglish',
  ], 'Search');
  $this
    ->assertSearchResultsCount(1);
  $session
    ->linkExists('Foo in English title wcsrefsdf');

  // Same for German.
  $this
    ->drupalPostForm('search/help', [
    'keys' => 'notawordgerman',
  ], 'Search', [
    'language' => $german,
  ]);
  $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
    ->drupalPostForm('search/help', [
    'keys' => 'notawordgerman',
  ], 'Search');
  $this
    ->assertSearchResultsCount(0);
  $session
    ->pageTextContains('no results');

  // Same for German.
  $this
    ->drupalPostForm('search/help', [
    'keys' => 'notawordenglish',
  ], 'Search', [
    'language' => $german,
  ]);
  $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
    ->drupalPostForm('search/help', [
    'keys' => 'sqruct',
  ], 'Search');
  $this
    ->assertSearchResultsCount(1);
  $session
    ->linkExists('Foo in English title wcsrefsdf');

  // Same for German.
  $this
    ->drupalPostForm('search/help', [
    'keys' => 'asdrsad',
  ], 'Search', [
    'language' => $german,
  ]);
  $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
    ->drupalPostForm('search/help', [
    'keys' => 'nonworditem',
  ], '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
    ->drupalPostForm('search/help', [
    'keys' => 'nonwordgerman',
  ], 'Search', [
    'language' => $german,
  ]);
  $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
    ->drupalPostForm(NULL, [
    'keys' => 'nonworditem',
  ], 'Search');
  $this
    ->assertSearchResultsCount(1);
  $session
    ->linkExists('ABC Help Test module');

  // Same for German.
  $this
    ->drupalPostForm('admin/help', [
    'keys' => 'nonwordgerman',
  ], 'Search', [
    'language' => $german,
  ]);
  $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
    ->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');

  // 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
    ->drupalPostForm('admin/help', [
    'keys' => 'sdeeeee',
  ], 'Search', [
    'language' => $german,
  ]);
  $this
    ->assertSearchResultsCount(1);
  $session
    ->linkExists('Updated translated title');

  // Searching for the updated test shouldn't produce a match.
  $this
    ->drupalPostForm('admin/help', [
    'keys' => 'translated title',
  ], 'Search', [
    'language' => $german,
  ]);
  $this
    ->assertSearchResultsCount(0);

  // Clear the caches and re-run cron - this should re-index the help.
  $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');

  // 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 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');

  // Uninstall the test module and verify its topics are immediately not
  // searchable.
  \Drupal::service('module_installer')
    ->uninstall([
    'help_topics_test',
  ]);
  $this
    ->drupalPostForm('search/help', [
    'keys' => 'nonworditem',
  ], 'Search');
  $this
    ->assertSearchResultsCount(0);
}