View source
<?php
namespace Drupal\Tests\search\Kernel;
use Drupal\KernelTests\KernelTestBase;
class SearchExcerptTest extends KernelTestBase {
protected static $modules = [
'search',
'search_langcode_test',
];
public function testSearchExcerpt() {
$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');
$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');
}
public function testSearchExcerptSimplified() {
$start_time = microtime(TRUE);
$lorem1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae arcu at leo cursus laoreet. Curabitur dui tortor, adipiscing malesuada tempor in, bibendum ac diam. Cras non tellus a libero pellentesque condimentum. What is a Drupalism? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero.';
$lorem2 = 'Ut fermentum est vitae metus convallis scelerisque. Phasellus pellentesque rhoncus tellus, eu dignissim purus posuere id. Quisque eu fringilla ligula. Morbi ullamcorper, lorem et mattis egestas, tortor neque pretium velit, eget eleifend odio turpis eu purus. Donec vitae metus quis leo pretium tincidunt a pulvinar sem. Morbi adipiscing laoreet mauris vel placerat. Nullam elementum, nisl sit amet scelerisque malesuada, dolor nunc hendrerit quam, eu ultrices erat est in orci.';
$text = $lorem1 . ' Number: 123456.7890 Hyphenated: one-two abc,def ' . $lorem2;
$result = $this
->doSearchExcerpt('123456.7890', $text);
$this
->assertStringContainsString('Number: <strong>123456.7890</strong>', $result, 'Numeric keyword is highlighted with exact match');
$result = $this
->doSearchExcerpt('1234567890', $text);
$this
->assertStringContainsString('Number: <strong>123456.7890</strong>', $result, 'Numeric keyword is highlighted with simplified match');
$result = $this
->doSearchExcerpt('Number 1234567890', $text);
$this
->assertStringContainsString('<strong>Number</strong>: <strong>123456.7890</strong>', $result, 'Punctuated and numeric keyword is highlighted with simplified match');
$result = $this
->doSearchExcerpt('"Number 1234567890"', $text);
$this
->assertStringContainsString('<strong>Number: 123456.7890</strong>', $result, 'Phrase with punctuated and numeric keyword is highlighted with simplified match');
$result = $this
->doSearchExcerpt('"Hyphenated onetwo"', $text);
$this
->assertStringContainsString('<strong>Hyphenated: one-two</strong>', $result, 'Phrase with punctuated and hyphenated keyword is highlighted with simplified match');
$result = $this
->doSearchExcerpt('"abc def"', $text);
$this
->assertStringContainsString('<strong>abc,def</strong>', $result, 'Phrase with keyword simplified into two separate words is highlighted with simplified match');
$result = $this
->doSearchExcerpt('"ipsum _"', $text);
$this
->assertStringContainsString('<strong>ipsum</strong>', $result, 'Only valid part of the phrase is highlighted and invalid part containing "_" is ignored.');
$result = $this
->doSearchExcerpt('"ipsum 0000"', $text);
$this
->assertStringContainsString('<strong>ipsum</strong>', $result, 'Only valid part of the phrase is highlighted and invalid part "0000" is ignored.');
$result = $this
->doSearchExcerpt('ipsum _', $text);
$this
->assertStringContainsString('<strong>ipsum</strong>', $result, 'Only valid keyword is highlighted and invalid keyword "_" is ignored.');
$result = $this
->doSearchExcerpt('ipsum 0000', $text);
$this
->assertStringContainsString('<strong>ipsum</strong>', $result, 'Only valid keyword is highlighted and invalid keyword "0000" is ignored.');
$text = "this tests finding a string";
$result = $this
->doSearchExcerpt('finds', $text, 'ex');
$this
->assertStringContainsString('<strong>finding</strong>', $result, 'Search excerpt works with preprocess hook, search for finds');
$result = $this
->doSearchExcerpt('find', $text, 'ex');
$this
->assertStringContainsString('<strong>finding</strong>', $result, 'Search excerpt works with preprocess hook, search for find');
$text = "finding at the beginning";
$result = $this
->doSearchExcerpt('finds', $text, 'ex');
$this
->assertStringContainsString('<strong>finding</strong>', $result, 'Search excerpt works with preprocess hook, text at start');
$text = "at the end finding";
$result = $this
->doSearchExcerpt('finds', $text, 'ex');
$this
->assertStringContainsString('<strong>finding</strong>', $result, 'Search excerpt works with preprocess hook, text at end');
$text = "something about the DIC is happening";
$result = $this
->doSearchExcerpt('Dependency', $text, 'ex');
$this
->assertStringContainsString('<strong>DIC</strong>', $result, 'Search excerpt works with preprocess hook, acronym first word');
$result = $this
->doSearchExcerpt('Injection', $text, 'ex');
$this
->assertStringContainsString('<strong>DIC</strong>', $result, 'Search excerpt works with preprocess hook, acronym second word');
$result = $this
->doSearchExcerpt('Container', $text, 'ex');
$this
->assertStringContainsString('<strong>DIC</strong>', $result, 'Search excerpt works with preprocess hook, acronym third word');
$text = "we always use hypertext markup language to describe things";
$result = $this
->doSearchExcerpt('html', $text, 'ex');
$this
->assertStringContainsString('<strong>hypertext markup language</strong>', $result, 'Search excerpt works with preprocess hook, acronym many to one');
$text = str_repeat($lorem2, 20) . ' ' . $lorem1;
$result = $this
->doSearchExcerpt('Lìbêró', $text);
$this
->assertStringContainsString('<strong>libero</strong>', $result, 'Search excerpt works with caps and accents in longer text');
$text = str_repeat($lorem2, 10) . ' DIC ' . str_repeat($lorem2, 10);
$result = $this
->doSearchExcerpt('Dependency', $text, 'ex');
$this
->assertStringContainsString('<strong>DIC</strong>', $result, 'Search excerpt works with acronym in longer text');
$lorem3 = str_replace(' ', str_repeat(" \n", 20), $lorem2);
$text = str_repeat($lorem3, 20) . ' ' . $lorem1;
$result = $this
->doSearchExcerpt('Lìbêró', $text);
$this
->assertStringContainsString('<strong>libero</strong>', $result, 'Search excerpt works with caps and accents in longer text with whitespace');
}
protected function doSearchExcerpt($keys, $render_array, $langcode = NULL) {
$render_array = search_excerpt($keys, $render_array, $langcode);
$text = \Drupal::service('renderer')
->renderPlain($render_array);
return preg_replace('| +|', ' ', $text);
}
}