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