summaryrefslogtreecommitdiff
path: root/modules/search/search.test
diff options
context:
space:
mode:
Diffstat (limited to 'modules/search/search.test')
-rw-r--r--modules/search/search.test48
1 files changed, 48 insertions, 0 deletions
diff --git a/modules/search/search.test b/modules/search/search.test
index 9079326a1..c43f8d7d2 100644
--- a/modules/search/search.test
+++ b/modules/search/search.test
@@ -1138,6 +1138,54 @@ class SearchConfigSettingsForm extends DrupalWebTestCase {
}
/**
+ * Tests the search_excerpt() function.
+ */
+class SearchExcerptTestCase extends DrupalUnitTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Search excerpt extraction',
+ 'description' => 'Tests that the search_excerpt() function works.',
+ 'group' => 'Search',
+ );
+ }
+
+ function setUp() {
+ parent::setUp('search');
+ }
+
+ /**
+ * 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';
+ $result = preg_replace('| +|', ' ', search_excerpt('nothing', $text));
+ $this->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Entire string is returned when keyword is not found in short string');
+
+ $result = preg_replace('| +|', ' ', search_excerpt('fox', $text));
+ $this->assertEqual($result, 'The quick brown <strong>fox</strong> &amp; jumps over the lazy dog ...', 'Found keyword is highlighted');
+
+ $longtext = str_repeat($text . ' ', 10);
+ $result = preg_replace('| +|', ' ', search_excerpt('nothing', $text));
+ $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);
+ $result = preg_replace('| +|', ' ', search_excerpt('nothing', $entities));
+ $this->assertFalse(strpos($result, '&'), 'Entities are not present in excerpt');
+ $this->assertTrue(strpos($result, 'í') > 0, 'Entities are converted in excerpt');
+ }
+}
+
+/**
* Test the CJK tokenizer.
*/
class SearchTokenizerTestCase extends DrupalWebTestCase {