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