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