fbbc96582b72efa6fa4d0758f3b210c70d26784b
[yaffs-website] / web / core / modules / views / tests / src / Functional / SearchIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Tests\Traits\Core\CronRunTrait;
7
8 /**
9  * Tests search integration filters.
10  *
11  * @group views
12  */
13 class SearchIntegrationTest extends ViewTestBase {
14
15   use CronRunTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['node', 'search'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_search'];
30
31   /**
32    * Tests search integration.
33    */
34   public function testSearchIntegration() {
35     // Create a content type.
36     $type = $this->drupalCreateContentType();
37
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);
45
46     $this->drupalGet('node/1');
47     $node_url = $this->getUrl();
48
49     $node['title'] = 'sandwich';
50     $node['body'] = [['value' => 'sandwich with a <a href="' . $node_url . '">link to first node</a>']];
51     $this->drupalCreateNode($node);
52
53     $node['title'] = 'cola';
54     $node['body'] = [['value' => 'cola is good with pizza']];
55     $node['type'] = $type->id();
56     $this->drupalCreateNode($node);
57
58     // Run cron so that the search index tables are updated.
59     $this->cronRun();
60
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.
64
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');
70
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');
77
78     $this->drupalGet('test-arg/sandwich');
79     $this->assertNoLink('pizza');
80     $this->assertOneLink('sandwich');
81     $this->assertNoLink('cola');
82
83     $this->drupalGet('test-arg/pizza OR sandwich');
84     $this->assertOneLink('pizza');
85     $this->assertOneLink('sandwich');
86     $this->assertOneLink('cola');
87
88     $this->drupalGet('test-arg/pizza sandwich OR cola');
89     $this->assertNoLink('pizza');
90     $this->assertNoLink('sandwich');
91     $this->assertOneLink('cola');
92
93     $this->drupalGet('test-arg/cola pizza');
94     $this->assertNoLink('pizza');
95     $this->assertNoLink('sandwich');
96     $this->assertOneLink('cola');
97
98     $this->drupalGet('test-arg/"cola is good"');
99     $this->assertNoLink('pizza');
100     $this->assertNoLink('sandwich');
101     $this->assertOneLink('cola');
102
103     // Test sorting.
104     $node = [
105       'title' => "Drupal's search rocks.",
106       'type' => $type->id(),
107     ];
108     $this->drupalCreateNode($node);
109     $node['title'] = "Drupal's search rocks <em>really</em> rocks!";
110     $this->drupalCreateNode($node);
111     $this->cronRun();
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!");
119
120     // Test sorting with another set of titles.
121     $node = [
122       'title' => "Testing one two two two",
123       'type' => $type->id(),
124     ];
125     $this->drupalCreateNode($node);
126     $node['title'] = "Testing one one one";
127     $this->drupalCreateNode($node);
128     $this->cronRun();
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");
135   }
136
137   /**
138    * Asserts that exactly one link exists with the given text.
139    *
140    * @param string $label
141    *   Link label to assert.
142    *
143    * @return bool
144    *   TRUE if the assertion succeeded, FALSE otherwise.
145    */
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);
150   }
151
152 }