annotate core/modules/search/tests/src/Functional/SearchSimplifyTest.php @ 0:c75dbcec494b

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