bd3bbb43d6a1cbfd4fdc9e23a72a11232a83fee5
[yaffs-website] / web / core / modules / search / tests / src / Functional / SearchSimplifyTest.php
1 <?php
2
3 namespace Drupal\Tests\search\Functional;
4 use Drupal\Component\Utility\Unicode;
5
6 /**
7  * Tests that the search_simply() function works as intended.
8  *
9  * @group search
10  */
11 class SearchSimplifyTest extends SearchTestBase {
12   /**
13    * Tests that all Unicode characters simplify correctly.
14    */
15   public function testSearchSimplifyUnicode() {
16     // This test uses a file that was constructed so that the even lines are
17     // boundary characters, and the odd lines are valid word characters. (It
18     // was generated as a sequence of all the Unicode characters, and then the
19     // boundary characters (punctuation, spaces, etc.) were split off into
20     // their own lines).  So the even-numbered lines should simplify to nothing,
21     // and the odd-numbered lines we need to split into shorter chunks and
22     // verify that simplification doesn't lose any characters.
23     $input = file_get_contents(\Drupal::root() . '/core/modules/search/tests/UnicodeTest.txt');
24     $basestrings = explode(chr(10), $input);
25     $strings = [];
26     foreach ($basestrings as $key => $string) {
27       if ($key % 2) {
28         // Even line - should simplify down to a space.
29         $simplified = search_simplify($string);
30         $this->assertIdentical($simplified, ' ', "Line $key is excluded from the index");
31       }
32       else {
33         // Odd line, should be word characters.
34         // Split this into 30-character chunks, so we don't run into limits
35         // of truncation in search_simplify().
36         $start = 0;
37         while ($start < Unicode::strlen($string)) {
38           $newstr = Unicode::substr($string, $start, 30);
39           // Special case: leading zeros are removed from numeric strings,
40           // and there's one string in this file that is numbers starting with
41           // zero, so prepend a 1 on that string.
42           if (preg_match('/^[0-9]+$/', $newstr)) {
43             $newstr = '1' . $newstr;
44           }
45           $strings[] = $newstr;
46           $start += 30;
47         }
48       }
49     }
50     foreach ($strings as $key => $string) {
51       $simplified = search_simplify($string);
52       $this->assertTrue(Unicode::strlen($simplified) >= Unicode::strlen($string), "Nothing is removed from string $key.");
53     }
54
55     // Test the low-numbered ASCII control characters separately. They are not
56     // in the text file because they are problematic for diff, especially \0.
57     $string = '';
58     for ($i = 0; $i < 32; $i++) {
59       $string .= chr($i);
60     }
61     $this->assertIdentical(' ', search_simplify($string), 'Search simplify works for ASCII control characters.');
62   }
63
64   /**
65    * Tests that search_simplify() does the right thing with punctuation.
66    */
67   public function testSearchSimplifyPunctuation() {
68     $cases = [
69       ['20.03/94-28,876', '20039428876', 'Punctuation removed from numbers'],
70       ['great...drupal--module', 'great drupal module', 'Multiple dot and dashes are word boundaries'],
71       ['very_great-drupal.module', 'verygreatdrupalmodule', 'Single dot, dash, underscore are removed'],
72       ['regular,punctuation;word', 'regular punctuation word', 'Punctuation is a word boundary'],
73     ];
74
75     foreach ($cases as $case) {
76       $out = trim(search_simplify($case[0]));
77       $this->assertEqual($out, $case[1], $case[2]);
78     }
79   }
80
81 }