3 namespace Drupal\Tests\search\Functional;
4 use Drupal\Component\Utility\Unicode;
7 * Tests that the search_simply() function works as intended.
11 class SearchSimplifyTest extends SearchTestBase {
13 * Tests that all Unicode characters simplify correctly.
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);
26 foreach ($basestrings as $key => $string) {
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");
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().
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;
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.");
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.
58 for ($i = 0; $i < 32; $i++) {
61 $this->assertIdentical(' ', search_simplify($string), 'Search simplify works for ASCII control characters.');
65 * Tests that search_simplify() does the right thing with punctuation.
67 public function testSearchSimplifyPunctuation() {
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'],
75 foreach ($cases as $case) {
76 $out = trim(search_simplify($case[0]));
77 $this->assertEqual($out, $case[1], $case[2]);