3 namespace Drupal\Tests\search\Functional;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\Traits\Core\CronRunTrait;
10 * Tests that numbers can be searched with more complex matching.
14 class SearchNumberMatchingTest extends BrowserTestBase {
21 protected static $modules = ['dblog', 'node', 'search'];
24 * A user with permission to administer nodes.
26 * @var \Drupal\user\UserInterface
31 * An array of strings containing numbers to use for testing.
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
40 protected $numbers = [
51 * An array of nodes created for testing purposes.
53 * @var \Drupal\node\NodeInterface[]
57 protected function setUp() {
60 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
62 $this->testUser = $this->drupalCreateUser(['search content', 'access content', 'administer nodes', 'access site reports']);
63 $this->drupalLogin($this->testUser);
65 foreach ($this->numbers as $num) {
67 'body' => [['value' => $num]],
69 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
71 $this->nodes[] = $this->drupalCreateNode($info);
74 // Run cron to ensure the content is indexed.
76 $this->drupalGet('admin/reports/dblog');
77 $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
81 * Tests that all the numbers can be searched.
83 public function testNumberSearching() {
84 for ($i = 0; $i < count($this->numbers); $i++) {
85 $node = $this->nodes[$i];
87 // Verify that the node title does not appear on the search page
88 // with a dummy search.
89 $this->drupalPostForm('search/node',
92 $this->assertNoText($node->label(), format_string('%number: node title not shown in dummy search', ['%number' => $i]));
94 // Now verify that we can find node i by searching for any of the
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, '-');
102 $this->drupalPostForm('search/node',
105 $this->assertText($node->label(), format_string('%i: node title shown (search found the node) in search for number %number', ['%i' => $i, '%number' => $number]));