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