3 namespace Drupal\search\Tests;
6 * Indexes content and tests the advanced search form.
10 class SearchAdvancedSearchFormTest extends SearchTestBase {
13 * A node to use for testing.
15 * @var \Drupal\node\NodeInterface
19 protected function 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);
25 // Create initial node.
26 $this->node = $this->drupalCreateNode();
28 // First update the index. This does the initial processing.
29 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
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();
38 * Tests advanced search by node type.
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.");
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.');
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.');
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.');
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.');
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.');
68 * Tests that after submitting the advanced search form, the form is refilled.
70 public function testFormRefill() {
74 'phrase' => 'pets are nice',
75 'negative' => 'fish snake',
76 'type[page]' => 'page',
78 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
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');
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");
92 $elements = $this->xpath('//input[@name=:name]', [':name' => $key]);
93 $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), "Field $key is checked");
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");