3 namespace Drupal\Tests\search\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests that the search_simply() function works as intended.
12 class SearchSimplifyTest extends BrowserTestBase {
17 protected static $modules = ['search'];
20 * Tests that all Unicode characters simplify correctly.
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);
33 foreach ($basestrings as $key => $string) {
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");
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().
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;
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.");
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.
65 for ($i = 0; $i < 32; $i++) {
68 $this->assertIdentical(' ', search_simplify($string), 'Search simplify works for ASCII control characters.');
72 * Tests that search_simplify() does the right thing with punctuation.
74 public function testSearchSimplifyPunctuation() {
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'],
82 foreach ($cases as $case) {
83 $out = trim(search_simplify($case[0]));
84 $this->assertEqual($out, $case[1], $case[2]);