Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\search\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Tests that numbers can be searched with more complex matching.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @group search
|
Chris@0
|
11 */
|
Chris@0
|
12 class SearchNumberMatchingTest extends SearchTestBase {
|
Chris@0
|
13 /**
|
Chris@0
|
14 * A user with permission to administer nodes.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @var \Drupal\user\UserInterface
|
Chris@0
|
17 */
|
Chris@0
|
18 protected $testUser;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * An array of strings containing numbers to use for testing.
|
Chris@0
|
22 *
|
Chris@0
|
23 * Define a group of numbers that should all match each other --
|
Chris@0
|
24 * numbers with internal punctuation should match each other, as well
|
Chris@0
|
25 * as numbers with and without leading zeros and leading/trailing
|
Chris@0
|
26 * . and -.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var string[]
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $numbers = [
|
Chris@0
|
31 '123456789',
|
Chris@0
|
32 '12/34/56789',
|
Chris@0
|
33 '12.3456789',
|
Chris@0
|
34 '12-34-56789',
|
Chris@0
|
35 '123,456,789',
|
Chris@0
|
36 '-123456789',
|
Chris@0
|
37 '0123456789',
|
Chris@0
|
38 ];
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * An array of nodes created for testing purposes.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var \Drupal\node\NodeInterface[]
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $nodes;
|
Chris@0
|
46
|
Chris@0
|
47 protected function setUp() {
|
Chris@0
|
48 parent::setUp();
|
Chris@0
|
49
|
Chris@0
|
50 $this->testUser = $this->drupalCreateUser(['search content', 'access content', 'administer nodes', 'access site reports']);
|
Chris@0
|
51 $this->drupalLogin($this->testUser);
|
Chris@0
|
52
|
Chris@0
|
53 foreach ($this->numbers as $num) {
|
Chris@0
|
54 $info = [
|
Chris@0
|
55 'body' => [['value' => $num]],
|
Chris@0
|
56 'type' => 'page',
|
Chris@0
|
57 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
Chris@0
|
58 ];
|
Chris@0
|
59 $this->nodes[] = $this->drupalCreateNode($info);
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 // Run cron to ensure the content is indexed.
|
Chris@0
|
63 $this->cronRun();
|
Chris@0
|
64 $this->drupalGet('admin/reports/dblog');
|
Chris@0
|
65 $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Tests that all the numbers can be searched.
|
Chris@0
|
70 */
|
Chris@0
|
71 public function testNumberSearching() {
|
Chris@0
|
72 for ($i = 0; $i < count($this->numbers); $i++) {
|
Chris@0
|
73 $node = $this->nodes[$i];
|
Chris@0
|
74
|
Chris@0
|
75 // Verify that the node title does not appear on the search page
|
Chris@0
|
76 // with a dummy search.
|
Chris@0
|
77 $this->drupalPostForm('search/node',
|
Chris@0
|
78 ['keys' => 'foo'],
|
Chris@0
|
79 t('Search'));
|
Chris@0
|
80 $this->assertNoText($node->label(), format_string('%number: node title not shown in dummy search', ['%number' => $i]));
|
Chris@0
|
81
|
Chris@0
|
82 // Now verify that we can find node i by searching for any of the
|
Chris@0
|
83 // numbers.
|
Chris@0
|
84 for ($j = 0; $j < count($this->numbers); $j++) {
|
Chris@0
|
85 $number = $this->numbers[$j];
|
Chris@0
|
86 // If the number is negative, remove the - sign, because - indicates
|
Chris@0
|
87 // "not keyword" when searching.
|
Chris@0
|
88 $number = ltrim($number, '-');
|
Chris@0
|
89
|
Chris@0
|
90 $this->drupalPostForm('search/node',
|
Chris@0
|
91 ['keys' => $number],
|
Chris@0
|
92 t('Search'));
|
Chris@0
|
93 $this->assertText($node->label(), format_string('%i: node title shown (search found the node) in search for number %number', ['%i' => $i, '%number' => $number]));
|
Chris@0
|
94 }
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 }
|