You are here

public function SearchExcerptTest::testSearchExcerptSimplified in Drupal 9

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

Tests search_excerpt() with search keywords matching simplified words.

Excerpting should handle keywords that are matched only after going through text analysis. This test passes keywords that match simplified words and compares them with strings that contain the original unsimplified word.

File

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

Class

SearchExcerptTest
Tests the search_excerpt() function.

Namespace

Drupal\Tests\search\Kernel

Code

public function testSearchExcerptSimplified() {
  $start_time = microtime(TRUE);

  // cSpell:disable
  $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.';

  // cSpell:enable
  // Make some text with some keywords that will get simplified.
  $text = $lorem1 . ' Number: 123456.7890 Hyphenated: one-two abc,def ' . $lorem2;

  // Note: The search_excerpt() function adds some extra spaces -- not
  // important for HTML formatting. Remove these for comparison.
  $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');

  // Test phrases with characters which are being truncated.
  $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.');

  // Test combination of the valid keyword and keyword containing only
  // characters which are being truncated during simplification.
  $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.');

  // Test using the hook_search_preprocess() from the test module.
  // The hook replaces "finding" or "finds" with "find".
  // So, if we search for "find" or "finds" or "finding", we should
  // highlight "finding".
  $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');

  // Just to be sure, test with the replacement at the beginning and end.
  $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');

  // Testing with a one-to-many replacement: the test module replaces DIC
  // with Dependency Injection Container.
  $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');

  // Testing with a many-to-one replacement: the test module replaces
  // hypertext markup language with HTML.
  $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');

  // Test with accents and caps in a longer piece of text with the target
  // near the end.
  $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');

  // Test with an acronym provided by the hook, with the target text in the
  // middle of a long string.
  $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');

  // Test a long string with a lot of whitespace in it.
  $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');
}