annotate core/modules/search/src/Tests/SearchAdvancedSearchFormTest.php @ 0:4c8ae668cc8c

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