SearchExcerptTest.php 11.3 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\search\Tests\SearchExcerptTest.
6 7 8 9
 */

namespace Drupal\search\Tests;

10
use Drupal\simpletest\WebTestBase;
11 12 13

/**
 * Tests the search_excerpt() function.
14 15
 *
 * @group search
16
 */
17 18 19 20 21 22 23
class SearchExcerptTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = array('search', 'search_langcode_test');
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  /**
   * 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.
   */
  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';
    // Note: The search_excerpt() function adds some extra spaces -- not
    // important for HTML formatting. Remove these for comparison.
    $expected = 'The quick brown fox &amp; jumps over the lazy dog';
41
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('nothing', $text));
42
    $this->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Entire string, stripped of HTML tags, is returned when keyword is not found in short string');
43

44
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('fox', $text));
45 46 47
    $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';
48
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('The', $text));
49 50 51
    $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>';
52
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('dog', $text));
53
    $this->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Keyword is highlighted at end of short string');
54

55
    $longtext = str_repeat(str_replace('brown', 'silver', $text) . ' ', 10) . $text . str_repeat(' ' . str_replace('brown', 'pink', $text), 10);
56
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('brown', $longtext));
57 58 59
    $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');

60
    $longtext = str_repeat($text . ' ', 10);
61
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('nothing', $longtext));
62
    $expected = 'The quick brown fox &amp; jumps over the lazy dog';
63 64 65
    $this->assertTrue(strpos($result, $expected) === 0, 'When keyword is not found in long string, return value starts as expected');

    $entities = str_repeat('k&eacute;sz&iacute;t&eacute;se ', 20);
66
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('nothing', $entities));
67 68 69 70 71
    $this->assertFalse(strpos($result, '&'), 'Entities are not present in excerpt');
    $this->assertTrue(strpos($result, 'í') > 0, '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;
72
    $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> ";
73
    $result = $this->doSearchExcerpt('HTMLTest', $text);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    $this->assertFalse(empty($result),  'Rendered Multi-byte HTML encodings are not corrupted in search excerpts');
  }

  /**
   * Tests search_excerpt() with search keywords matching simplified words.
   *
   * Excerpting should handle keywords that are matched only after going through
   * search_simplify(). This test passes keywords that match simplified words
   * and compares them with strings that contain the original unsimplified word.
   */
  function testSearchExcerptSimplified() {
    $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.';

    // 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.
92
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('123456.7890', $text));
93 94
    $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with exact match');

95
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('1234567890', $text));
96 97
    $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with simplified match');

98
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('Number 1234567890', $text));
99 100
    $this->assertTrue(strpos($result, '<strong>Number</strong>: <strong>123456.7890</strong>') !== FALSE, 'Punctuated and numeric keyword is highlighted with simplified match');

101
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('"Number 1234567890"', $text));
102 103
    $this->assertTrue(strpos($result, '<strong>Number: 123456.7890</strong>') !== FALSE, 'Phrase with punctuated and numeric keyword is highlighted with simplified match');

104
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('"Hyphenated onetwo"', $text));
105 106
    $this->assertTrue(strpos($result, '<strong>Hyphenated: one-two</strong>') !== FALSE, 'Phrase with punctuated and hyphenated keyword is highlighted with simplified match');

107
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('"abc def"', $text));
108 109 110
    $this->assertTrue(strpos($result, '<strong>abc,def</strong>') !== FALSE, 'Phrase with keyword simplified into two separate words is highlighted with simplified match');

    // Test phrases with characters which are being truncated.
111
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('"ipsum _"', $text));
112
    $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part containing "_" is ignored.');
113

114
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('"ipsum 0000"', $text));
115
    $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part "0000" is ignored.');
116 117 118

    // Test combination of the valid keyword and keyword containing only
    // characters which are being truncated during simplification.
119
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('ipsum _', $text));
120 121
    $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "_" is ignored.');

122
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('ipsum 0000', $text));
123
    $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "0000" is ignored.');
124 125 126 127 128 129

    // 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";
130
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('finds', $text, 'ex'));
131
    $this->assertTrue(strpos($result, '<strong>finding</strong>') !== FALSE, 'Search excerpt works with preprocess hook, search for finds');
132
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('find', $text, 'ex'));
133 134 135 136
    $this->assertTrue(strpos($result, '<strong>finding</strong>') !== FALSE, '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";
137
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('finds', $text, 'ex'));
138 139 140
    $this->assertTrue(strpos($result, '<strong>finding</strong>') !== FALSE, 'Search excerpt works with preprocess hook, text at start');

    $text = "at the end finding";
141
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('finds', $text, 'ex'));
142 143 144 145 146
    $this->assertTrue(strpos($result, '<strong>finding</strong>') !== FALSE, '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";
147
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('Dependency', $text, 'ex'));
148 149
    $this->assertTrue(strpos($result, '<strong>DIC</strong>') !== FALSE, 'Search excerpt works with preprocess hook, acronym first word');

150
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('Injection', $text, 'ex'));
151 152
    $this->assertTrue(strpos($result, '<strong>DIC</strong>') !== FALSE, 'Search excerpt works with preprocess hook, acronym second word');

153
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('Container', $text, 'ex'));
154 155 156 157 158
    $this->assertTrue(strpos($result, '<strong>DIC</strong>') !== FALSE, '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";
159
    $result = preg_replace('| +|', ' ', $this->doSearchExcerpt('html', $text, 'ex'));
160
    $this->assertTrue(strpos($result, '<strong>hypertext markup language</strong>') !== FALSE, 'Search excerpt works with preprocess hook, acronym many to one');
161
  }
162

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  /**
   * Calls search_excerpt() and renders output.
   *
   * @param string $keys
   *   A string containing a search query.
   * @param string $render_array
   *   The text to extract fragments from.
   * @param string|null $langcode
   *   Language code for the language of $text, if known.
   *
   * @return string
   *   A string containing HTML for the excerpt.
   */
  protected function doSearchExcerpt($keys, $render_array, $langcode = NULL) {
    $render_array = search_excerpt($keys, $render_array, $langcode);
    return \Drupal::service('renderer')->renderPlain($render_array);
179
  }
180

181
}