comparison core/modules/search/tests/src/Functional/SearchSimplifyTest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\search\Functional; 3 namespace Drupal\Tests\search\Functional;
4 4
5 use Drupal\Component\Utility\Unicode; 5 use Drupal\Tests\BrowserTestBase;
6 6
7 /** 7 /**
8 * Tests that the search_simply() function works as intended. 8 * Tests that the search_simply() function works as intended.
9 * 9 *
10 * @group search 10 * @group search
11 */ 11 */
12 class SearchSimplifyTest extends SearchTestBase { 12 class SearchSimplifyTest extends BrowserTestBase {
13
14 /**
15 * {@inheritdoc}
16 */
17 protected static $modules = ['search'];
18
13 /** 19 /**
14 * Tests that all Unicode characters simplify correctly. 20 * Tests that all Unicode characters simplify correctly.
15 */ 21 */
16 public function testSearchSimplifyUnicode() { 22 public function testSearchSimplifyUnicode() {
17 // This test uses a file that was constructed so that the even lines are 23 // This test uses a file that was constructed so that the even lines are
19 // was generated as a sequence of all the Unicode characters, and then the 25 // was generated as a sequence of all the Unicode characters, and then the
20 // boundary characters (punctuation, spaces, etc.) were split off into 26 // boundary characters (punctuation, spaces, etc.) were split off into
21 // their own lines). So the even-numbered lines should simplify to nothing, 27 // their own lines). So the even-numbered lines should simplify to nothing,
22 // and the odd-numbered lines we need to split into shorter chunks and 28 // and the odd-numbered lines we need to split into shorter chunks and
23 // verify that simplification doesn't lose any characters. 29 // verify that simplification doesn't lose any characters.
24 $input = file_get_contents(\Drupal::root() . '/core/modules/search/tests/UnicodeTest.txt'); 30 $input = file_get_contents($this->root . '/core/modules/search/tests/UnicodeTest.txt');
25 $basestrings = explode(chr(10), $input); 31 $basestrings = explode(chr(10), $input);
26 $strings = []; 32 $strings = [];
27 foreach ($basestrings as $key => $string) { 33 foreach ($basestrings as $key => $string) {
28 if ($key % 2) { 34 if ($key % 2) {
29 // Even line - should simplify down to a space. 35 // Even line - should simplify down to a space.
33 else { 39 else {
34 // Odd line, should be word characters. 40 // Odd line, should be word characters.
35 // Split this into 30-character chunks, so we don't run into limits 41 // Split this into 30-character chunks, so we don't run into limits
36 // of truncation in search_simplify(). 42 // of truncation in search_simplify().
37 $start = 0; 43 $start = 0;
38 while ($start < Unicode::strlen($string)) { 44 while ($start < mb_strlen($string)) {
39 $newstr = Unicode::substr($string, $start, 30); 45 $newstr = mb_substr($string, $start, 30);
40 // Special case: leading zeros are removed from numeric strings, 46 // Special case: leading zeros are removed from numeric strings,
41 // and there's one string in this file that is numbers starting with 47 // and there's one string in this file that is numbers starting with
42 // zero, so prepend a 1 on that string. 48 // zero, so prepend a 1 on that string.
43 if (preg_match('/^[0-9]+$/', $newstr)) { 49 if (preg_match('/^[0-9]+$/', $newstr)) {
44 $newstr = '1' . $newstr; 50 $newstr = '1' . $newstr;
48 } 54 }
49 } 55 }
50 } 56 }
51 foreach ($strings as $key => $string) { 57 foreach ($strings as $key => $string) {
52 $simplified = search_simplify($string); 58 $simplified = search_simplify($string);
53 $this->assertTrue(Unicode::strlen($simplified) >= Unicode::strlen($string), "Nothing is removed from string $key."); 59 $this->assertTrue(mb_strlen($simplified) >= mb_strlen($string), "Nothing is removed from string $key.");
54 } 60 }
55 61
56 // Test the low-numbered ASCII control characters separately. They are not 62 // Test the low-numbered ASCII control characters separately. They are not
57 // in the text file because they are problematic for diff, especially \0. 63 // in the text file because they are problematic for diff, especially \0.
58 $string = ''; 64 $string = '';