3 namespace Drupal\Tests\views\Functional;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Tests\Traits\Core\CronRunTrait;
9 * Tests search integration filters.
13 class SearchIntegrationTest extends ViewTestBase {
22 public static $modules = ['node', 'search'];
25 * Views used by this test.
29 public static $testViews = ['test_search'];
32 * Tests search integration.
34 public function testSearchIntegration() {
35 // Create a content type.
36 $type = $this->drupalCreateContentType();
38 // Add three nodes, one containing the word "pizza", one containing
39 // "sandwich", and one containing "cola is good with pizza". Make the
40 // second node link to the first.
41 $node['title'] = 'pizza';
42 $node['body'] = [['value' => 'pizza']];
43 $node['type'] = $type->id();
44 $this->drupalCreateNode($node);
46 $this->drupalGet('node/1');
47 $node_url = $this->getUrl();
49 $node['title'] = 'sandwich';
50 $node['body'] = [['value' => 'sandwich with a <a href="' . $node_url . '">link to first node</a>']];
51 $this->drupalCreateNode($node);
53 $node['title'] = 'cola';
54 $node['body'] = [['value' => 'cola is good with pizza']];
55 $node['type'] = $type->id();
56 $this->drupalCreateNode($node);
58 // Run cron so that the search index tables are updated.
61 // Test the various views filters by visiting their pages.
62 // These are in the test view 'test_search', and they just display the
63 // titles of the nodes in the result, as links.
65 // Page with a keyword filter of 'pizza'.
66 $this->drupalGet('test-filter');
67 $this->assertLink('pizza');
68 $this->assertNoLink('sandwich');
69 $this->assertLink('cola');
71 // Page with a keyword argument, various argument values.
72 // Verify that the correct nodes are shown, and only once.
73 $this->drupalGet('test-arg/pizza');
74 $this->assertOneLink('pizza');
75 $this->assertNoLink('sandwich');
76 $this->assertOneLink('cola');
78 $this->drupalGet('test-arg/sandwich');
79 $this->assertNoLink('pizza');
80 $this->assertOneLink('sandwich');
81 $this->assertNoLink('cola');
83 $this->drupalGet('test-arg/pizza OR sandwich');
84 $this->assertOneLink('pizza');
85 $this->assertOneLink('sandwich');
86 $this->assertOneLink('cola');
88 $this->drupalGet('test-arg/pizza sandwich OR cola');
89 $this->assertNoLink('pizza');
90 $this->assertNoLink('sandwich');
91 $this->assertOneLink('cola');
93 $this->drupalGet('test-arg/cola pizza');
94 $this->assertNoLink('pizza');
95 $this->assertNoLink('sandwich');
96 $this->assertOneLink('cola');
98 $this->drupalGet('test-arg/"cola is good"');
99 $this->assertNoLink('pizza');
100 $this->assertNoLink('sandwich');
101 $this->assertOneLink('cola');
105 'title' => "Drupal's search rocks.",
106 'type' => $type->id(),
108 $this->drupalCreateNode($node);
109 $node['title'] = "Drupal's search rocks <em>really</em> rocks!";
110 $this->drupalCreateNode($node);
112 $this->drupalGet('test-arg/rocks');
113 $xpath = '//div[@class="views-row"]//a';
114 /** @var \Behat\Mink\Element\NodeElement[] $results */
115 $results = $this->xpath($xpath);
116 $this->assertEqual($results[0]->getText(), "Drupal's search rocks <em>really</em> rocks!");
117 $this->assertEqual($results[1]->getText(), "Drupal's search rocks.");
118 $this->assertEscaped("Drupal's search rocks <em>really</em> rocks!");
120 // Test sorting with another set of titles.
122 'title' => "Testing one two two two",
123 'type' => $type->id(),
125 $this->drupalCreateNode($node);
126 $node['title'] = "Testing one one one";
127 $this->drupalCreateNode($node);
129 $this->drupalGet('test-arg/one');
130 $xpath = '//div[@class="views-row"]//a';
131 /** @var \SimpleXMLElement[] $results */
132 $results = $this->xpath($xpath);
133 $this->assertEqual($results[0]->getText(), "Testing one one one");
134 $this->assertEqual($results[1]->getText(), "Testing one two two two");
138 * Asserts that exactly one link exists with the given text.
140 * @param string $label
141 * Link label to assert.
144 * TRUE if the assertion succeeded, FALSE otherwise.
146 protected function assertOneLink($label) {
147 $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]);
148 $message = new FormattableMarkup('Link with label %label found once.', ['%label' => $label]);
149 return $this->assert(isset($links[0]) && !isset($links[1]), $message);