PageRenderTime 34ms CodeModel.GetById 28ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/search/tests/src/Kernel/SearchTokenizerTest.php

http://github.com/drupal/drupal
PHP | 155 lines | 95 code | 20 blank | 40 comment | 4 complexity | bdb22f9f59c80917e04d54c91823e07c MD5 | raw file
  1<?php
  2
  3namespace Drupal\Tests\search\Kernel;
  4
  5use Drupal\KernelTests\KernelTestBase;
  6
  7/**
  8 * Tests that CJK tokenizer works as intended.
  9 *
 10 * @group search
 11 */
 12class SearchTokenizerTest extends KernelTestBase {
 13
 14  /**
 15   * {@inheritdoc}
 16   */
 17  protected static $modules = ['search'];
 18
 19  /**
 20   * Verifies that strings of CJK characters are tokenized.
 21   *
 22   * The search_simplify() function does special things with numbers, symbols,
 23   * and punctuation. So we only test that CJK characters that are not in these
 24   * character classes are tokenized properly. See PREG_CLASS_CKJ for more
 25   * information.
 26   */
 27  public function testTokenizer() {
 28    // Set the minimum word size to 1 (to split all CJK characters) and make
 29    // sure CJK tokenizing is turned on.
 30    $this->config('search.settings')
 31      ->set('index.minimum_word_size', 1)
 32      ->set('index.overlap_cjk', TRUE)
 33      ->save();
 34
 35    // Create a string of CJK characters from various character ranges in
 36    // the Unicode tables.
 37
 38    // Beginnings of the character ranges.
 39    $starts = [
 40      'CJK unified' => 0x4e00,
 41      'CJK Ext A' => 0x3400,
 42      'CJK Compat' => 0xf900,
 43      'Hangul Jamo' => 0x1100,
 44      'Hangul Ext A' => 0xa960,
 45      'Hangul Ext B' => 0xd7b0,
 46      'Hangul Compat' => 0x3131,
 47      'Half non-punct 1' => 0xff21,
 48      'Half non-punct 2' => 0xff41,
 49      'Half non-punct 3' => 0xff66,
 50      'Hangul Syllables' => 0xac00,
 51      'Hiragana' => 0x3040,
 52      'Katakana' => 0x30a1,
 53      'Katakana Ext' => 0x31f0,
 54      'CJK Reserve 1' => 0x20000,
 55      'CJK Reserve 2' => 0x30000,
 56      'Bomofo' => 0x3100,
 57      'Bomofo Ext' => 0x31a0,
 58      'Lisu' => 0xa4d0,
 59      'Yi' => 0xa000,
 60    ];
 61
 62    // Ends of the character ranges.
 63    $ends = [
 64      'CJK unified' => 0x9fcf,
 65      'CJK Ext A' => 0x4dbf,
 66      'CJK Compat' => 0xfaff,
 67      'Hangul Jamo' => 0x11ff,
 68      'Hangul Ext A' => 0xa97f,
 69      'Hangul Ext B' => 0xd7ff,
 70      'Hangul Compat' => 0x318e,
 71      'Half non-punct 1' => 0xff3a,
 72      'Half non-punct 2' => 0xff5a,
 73      'Half non-punct 3' => 0xffdc,
 74      'Hangul Syllables' => 0xd7af,
 75      'Hiragana' => 0x309f,
 76      'Katakana' => 0x30ff,
 77      'Katakana Ext' => 0x31ff,
 78      'CJK Reserve 1' => 0x2fffd,
 79      'CJK Reserve 2' => 0x3fffd,
 80      'Bomofo' => 0x312f,
 81      'Bomofo Ext' => 0x31b7,
 82      'Lisu' => 0xa4fd,
 83      'Yi' => 0xa48f,
 84    ];
 85
 86    // Generate characters consisting of starts, midpoints, and ends.
 87    $chars = [];
 88    $charcodes = [];
 89    foreach ($starts as $key => $value) {
 90      $charcodes[] = $starts[$key];
 91      $chars[] = $this->code2utf($starts[$key]);
 92      $mid = round(0.5 * ($starts[$key] + $ends[$key]));
 93      $charcodes[] = $mid;
 94      $chars[] = $this->code2utf($mid);
 95      $charcodes[] = $ends[$key];
 96      $chars[] = $this->code2utf($ends[$key]);
 97    }
 98
 99    // Merge into a string and tokenize.
100    $string = implode('', $chars);
101    $out = trim(search_simplify($string));
102    $expected = mb_strtolower(implode(' ', $chars));
103
104    // Verify that the output matches what we expect.
105    $this->assertEqual($out, $expected, 'CJK tokenizer worked on all supplied CJK characters');
106  }
107
108  /**
109   * Verifies that strings of non-CJK characters are not tokenized.
110   *
111   * This is just a sanity check - it verifies that strings of letters are
112   * not tokenized.
113   */
114  public function testNoTokenizer() {
115    // Set the minimum word size to 1 (to split all CJK characters) and make
116    // sure CJK tokenizing is turned on.
117    $this->config('search.settings')
118      ->set('index.minimum_word_size', 1)
119      ->set('index.overlap_cjk', TRUE)
120      ->save();
121
122    $letters = 'abcdefghijklmnopqrstuvwxyz';
123    $out = trim(search_simplify($letters));
124
125    $this->assertEqual($letters, $out, 'Letters are not CJK tokenized');
126  }
127
128  /**
129   * Like PHP chr() function, but for unicode characters.
130   *
131   * Function chr() only works for ASCII characters up to character 255. This
132   * function converts a number to the corresponding unicode character. Adapted
133   * from functions supplied in comments on several functions on php.net.
134   */
135  public function code2utf($num) {
136    if ($num < 128) {
137      return chr($num);
138    }
139
140    if ($num < 2048) {
141      return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
142    }
143
144    if ($num < 65536) {
145      return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
146    }
147
148    if ($num < 2097152) {
149      return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
150    }
151
152    return '';
153  }
154
155}