You are here

public function SearchExcerptTest::testSearchExcerpt in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/search/tests/src/Kernel/SearchExcerptTest.php \Drupal\Tests\search\Kernel\SearchExcerptTest::testSearchExcerpt()
  2. 10 core/modules/search/tests/src/Kernel/SearchExcerptTest.php \Drupal\Tests\search\Kernel\SearchExcerptTest::testSearchExcerpt()

Tests search_excerpt() with several simulated search keywords.

Passes keywords and a sample marked up string, "The quick brown fox jumps over the lazy dog", and compares it to the correctly marked up string. The correctly marked up string contains either highlighted keywords or the original marked up string if no keywords matched the string.

File

core/modules/search/tests/src/Kernel/SearchExcerptTest.php, line 30

Class

SearchExcerptTest
Tests the search_excerpt() function.

Namespace

Drupal\Tests\search\Kernel

Code

public function testSearchExcerpt() {

  // Make some text with entities and tags.
  $text = 'The <strong>quick</strong> <a href="#">brown</a> fox &amp; jumps <h2>over</h2> the lazy dog';
  $expected = 'The quick brown fox &amp; jumps over the lazy dog';
  $result = $this
    ->doSearchExcerpt('nothing', $text);
  $this
    ->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Entire string, stripped of HTML tags, is returned when keyword is not found in short string');
  $result = $this
    ->doSearchExcerpt('fox', $text);
  $this
    ->assertEqual($result, 'The quick brown <strong>fox</strong> &amp; jumps over the lazy dog', 'Found keyword is highlighted');
  $expected = '<strong>The</strong> quick brown fox &amp; jumps over <strong>the</strong> lazy dog';
  $result = $this
    ->doSearchExcerpt('The', $text);
  $this
    ->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Keyword is highlighted at beginning of short string');
  $expected = 'The quick brown fox &amp; jumps over the lazy <strong>dog</strong>';
  $result = $this
    ->doSearchExcerpt('dog', $text);
  $this
    ->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Keyword is highlighted at end of short string');
  $longtext = str_repeat(str_replace('brown', 'silver', $text) . ' ', 10) . $text . str_repeat(' ' . str_replace('brown', 'pink', $text), 10);
  $result = $this
    ->doSearchExcerpt('brown', $longtext);
  $expected = '… silver fox &amp; jumps over the lazy dog The quick <strong>brown</strong> fox &amp; jumps over the lazy dog The quick …';
  $this
    ->assertEqual($result, $expected, 'Snippet around keyword in long text is correctly capped');
  $longtext = str_repeat($text . ' ', 10);
  $result = $this
    ->doSearchExcerpt('nothing', $longtext);
  $expected = 'The quick brown fox &amp; jumps over the lazy dog';
  $this
    ->assertStringStartsWith($expected, $result, 'When keyword is not found in long string, return value starts as expected');
  $entities = str_repeat('k&eacute;sz&iacute;t&eacute;se ', 20);
  $result = $this
    ->doSearchExcerpt('nothing', $entities);
  $this
    ->assertStringNotContainsString('&', $result, 'Entities are not present in excerpt');
  $this
    ->assertStringContainsString('í', $result, 'Entities are converted in excerpt');

  // The node body that will produce this rendered $text is:
  // 123456789 HTMLTest +123456789+&lsquo;  +&lsquo;  +&lsquo;  +&lsquo;  +12345678  &nbsp;&nbsp;  +&lsquo;  +&lsquo;  +&lsquo;   &lsquo;
  $text = "<div class=\"field field--name-body field--type-text-with-summary field--label-hidden\"><div class=\"field__items\"><div class=\"field__item even\" property=\"content:encoded\"><p>123456789 HTMLTest +123456789+‘  +‘  +‘  +‘  +12345678      +‘  +‘  +‘   ‘</p>\n</div></div></div> ";
  $result = $this
    ->doSearchExcerpt('HTMLTest', $text);
  $this
    ->assertFalse(empty($result), 'Rendered Multi-byte HTML encodings are not corrupted in search excerpts');
}