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