0901a270e5ffa4f4a9a2db9a6e4fe48ba6586c11
[yaffs-website] / web / core / modules / search / tests / src / Functional / SearchNumbersTest.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.
11  *
12  * @group search
13  */
14 class SearchNumbersTest 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 containing a series of "numbers" for testing purposes.
32    *
33    * Create content with various numbers in it.
34    * Note: 50 characters is the current limit of the search index's word
35    * field.
36    *
37    * @var string[]
38    */
39   protected $numbers = [
40     'ISBN' => '978-0446365383',
41     'UPC' => '036000 291452',
42     'EAN bar code' => '5901234123457',
43     'negative' => '-123456.7890',
44     'quoted negative' => '"-123456.7890"',
45     'leading zero' => '0777777777',
46     'tiny' => '111',
47     'small' => '22222222222222',
48     'medium' => '333333333333333333333333333',
49     'large' => '444444444444444444444444444444444444444',
50     'gigantic' => '5555555555555555555555555555555555555555555555555',
51     'over fifty characters' => '666666666666666666666666666666666666666666666666666666666666',
52     'date' => '01/02/2009',
53     'commas' => '987,654,321',
54   ];
55
56   /**
57    * An array of nodes created for testing purposes.
58    *
59    * @var \Drupal\node\NodeInterface[]
60    */
61   protected $nodes;
62
63   protected function setUp() {
64     parent::setUp();
65
66     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
67
68     $this->testUser = $this->drupalCreateUser(['search content', 'access content', 'administer nodes', 'access site reports']);
69     $this->drupalLogin($this->testUser);
70
71     foreach ($this->numbers as $doc => $num) {
72       $info = [
73         'body' => [['value' => $num]],
74         'type' => 'page',
75         'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
76         'title' => $doc . ' number',
77       ];
78       $this->nodes[$doc] = $this->drupalCreateNode($info);
79     }
80
81     // Run cron to ensure the content is indexed.
82     $this->cronRun();
83     $this->drupalGet('admin/reports/dblog');
84     $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
85   }
86
87   /**
88    * Tests that all the numbers can be searched.
89    */
90   public function testNumberSearching() {
91     $types = array_keys($this->numbers);
92
93     foreach ($types as $type) {
94       $number = $this->numbers[$type];
95       // If the number is negative, remove the - sign, because - indicates
96       // "not keyword" when searching.
97       $number = ltrim($number, '-');
98       $node = $this->nodes[$type];
99
100       // Verify that the node title does not appear on the search page
101       // with a dummy search.
102       $this->drupalPostForm('search/node',
103         ['keys' => 'foo'],
104         t('Search'));
105       $this->assertNoText($node->label(), $type . ': node title not shown in dummy search');
106
107       // Verify that the node title does appear as a link on the search page
108       // when searching for the number.
109       $this->drupalPostForm('search/node',
110         ['keys' => $number],
111         t('Search'));
112       $this->assertText($node->label(), format_string('%type: node title shown (search found the node) in search for number %number.', ['%type' => $type, '%number' => $number]));
113     }
114   }
115
116 }