public function SearchExcerptTest::testSearchExcerpt in Drupal 10
Same name and namespace in other branches
- 8 core/modules/search/tests/src/Kernel/SearchExcerptTest.php \Drupal\Tests\search\Kernel\SearchExcerptTest::testSearchExcerpt()
- 9 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\KernelCode
public function testSearchExcerpt() {
// Make some text with entities and tags.
$text = 'The <strong>quick</strong> <a href="#">brown</a> fox & jumps <h2>over</h2> the lazy dog';
$expected = 'The quick brown fox & jumps over the lazy dog';
$result = $this
->doSearchExcerpt('nothing', $text);
$this
->assertEquals($expected, preg_replace('| +|', ' ', $result), 'Entire string, stripped of HTML tags, is returned when keyword is not found in short string');
$result = $this
->doSearchExcerpt('fox', $text);
$this
->assertEquals('The quick brown <strong>fox</strong> & jumps over the lazy dog', $result, 'Found keyword is highlighted');
$expected = '<strong>The</strong> quick brown fox & jumps over <strong>the</strong> lazy dog';
$result = $this
->doSearchExcerpt('The', $text);
$this
->assertEquals($expected, preg_replace('| +|', ' ', $result), 'Keyword is highlighted at beginning of short string');
$expected = 'The quick brown fox & jumps over the lazy <strong>dog</strong>';
$result = $this
->doSearchExcerpt('dog', $text);
$this
->assertEquals($expected, preg_replace('| +|', ' ', $result), '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 & jumps over the lazy dog The quick <strong>brown</strong> fox & jumps over the lazy dog The quick …';
$this
->assertEquals($expected, $result, 'Snippet around keyword in long text is correctly capped');
$longtext = str_repeat($text . ' ', 10);
$result = $this
->doSearchExcerpt('nothing', $longtext);
$expected = 'The quick brown fox & 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észíté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+‘ +‘ +‘ +‘ +12345678 +‘ +‘ +‘ ‘
$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
->assertNotEmpty($result, 'Rendered Multi-byte HTML encodings are not corrupted in search excerpts');
}