Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / search / src / Tests / SearchAdvancedSearchFormTest.php
1 <?php
2
3 namespace Drupal\search\Tests;
4
5 /**
6  * Indexes content and tests the advanced search form.
7  *
8  * @group search
9  */
10 class SearchAdvancedSearchFormTest extends SearchTestBase {
11
12   /**
13    * A node to use for testing.
14    *
15    * @var \Drupal\node\NodeInterface
16    */
17   protected $node;
18
19   protected function setUp() {
20     parent::setUp();
21     // Create and log in user.
22     $test_user = $this->drupalCreateUser(['access content', 'search content', 'use advanced search', 'administer nodes']);
23     $this->drupalLogin($test_user);
24
25     // Create initial node.
26     $this->node = $this->drupalCreateNode();
27
28     // First update the index. This does the initial processing.
29     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
30
31     // Then, run the shutdown function. Testing is a unique case where indexing
32     // and searching has to happen in the same request, so running the shutdown
33     // function manually is needed to finish the indexing process.
34     search_update_totals();
35   }
36
37   /**
38    * Tests advanced search by node type.
39    */
40   public function testNodeType() {
41     // Verify some properties of the node that was created.
42     $this->assertTrue($this->node->getType() == 'page', 'Node type is Basic page.');
43     $dummy_title = 'Lorem ipsum';
44     $this->assertNotEqual($dummy_title, $this->node->label(), "Dummy title doesn't equal node title.");
45
46     // Search for the dummy title with a GET query.
47     $this->drupalGet('search/node', ['query' => ['keys' => $dummy_title]]);
48     $this->assertNoText($this->node->label(), 'Basic page node is not found with dummy title.');
49
50     // Search for the title of the node with a GET query.
51     $this->drupalGet('search/node', ['query' => ['keys' => $this->node->label()]]);
52     $this->assertText($this->node->label(), 'Basic page node is found with GET query.');
53
54     // Search for the title of the node with a POST query.
55     $edit = ['or' => $this->node->label()];
56     $this->drupalPostForm('search/node', $edit, t('Advanced search'));
57     $this->assertText($this->node->label(), 'Basic page node is found with POST query.');
58
59     // Search by node type.
60     $this->drupalPostForm('search/node', array_merge($edit, ['type[page]' => 'page']), t('Advanced search'));
61     $this->assertText($this->node->label(), 'Basic page node is found with POST query and type:page.');
62
63     $this->drupalPostForm('search/node', array_merge($edit, ['type[article]' => 'article']), t('Advanced search'));
64     $this->assertText('search yielded no results', 'Article node is not found with POST query and type:article.');
65   }
66
67   /**
68    * Tests that after submitting the advanced search form, the form is refilled.
69    */
70   public function testFormRefill() {
71     $edit = [
72       'keys' => 'cat',
73       'or' => 'dog gerbil',
74       'phrase' => 'pets are nice',
75       'negative' => 'fish snake',
76       'type[page]' => 'page',
77     ];
78     $this->drupalPostForm('search/node', $edit, t('Advanced search'));
79
80     // Test that the encoded query appears in the page title. Only test the
81     // part not including the quote, because assertText() cannot seem to find
82     // the quote marks successfully.
83     $this->assertText('Search for cat dog OR gerbil -fish -snake');
84
85     // Verify that all of the form fields are filled out.
86     foreach ($edit as $key => $value) {
87       if ($key != 'type[page]') {
88         $elements = $this->xpath('//input[@name=:name]', [':name' => $key]);
89         $this->assertTrue(isset($elements[0]) && $elements[0]['value'] == $value, "Field $key is set to $value");
90       }
91       else {
92         $elements = $this->xpath('//input[@name=:name]', [':name' => $key]);
93         $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), "Field $key is checked");
94       }
95     }
96
97     // Now test by submitting the or/not part of the query in the main
98     // search box, and verify that the advanced form is not filled out.
99     // (It shouldn't be filled out unless you submit values in those fields.)
100     $edit2 = ['keys' => 'cat dog OR gerbil -fish -snake'];
101     $this->drupalPostForm('search/node', $edit2, t('Advanced search'));
102     $this->assertText('Search for cat dog OR gerbil -fish -snake');
103     foreach ($edit as $key => $value) {
104       if ($key != 'type[page]') {
105         $elements = $this->xpath('//input[@name=:name]', [':name' => $key]);
106         $this->assertFalse(isset($elements[0]) && $elements[0]['value'] == $value, "Field $key is not set to $value");
107       }
108     }
109   }
110
111 }