Chris@0: drupalCreateUser(['access content', 'search content', 'use advanced search', 'administer nodes']); Chris@0: $this->drupalLogin($test_user); Chris@0: Chris@0: // Create initial node. Chris@0: $this->node = $this->drupalCreateNode(); Chris@0: Chris@0: // First update the index. This does the initial processing. Chris@0: $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex(); Chris@0: Chris@0: // Then, run the shutdown function. Testing is a unique case where indexing Chris@0: // and searching has to happen in the same request, so running the shutdown Chris@0: // function manually is needed to finish the indexing process. Chris@0: search_update_totals(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests advanced search by node type. Chris@0: */ Chris@0: public function testNodeType() { Chris@0: // Verify some properties of the node that was created. Chris@0: $this->assertTrue($this->node->getType() == 'page', 'Node type is Basic page.'); Chris@0: $dummy_title = 'Lorem ipsum'; Chris@0: $this->assertNotEqual($dummy_title, $this->node->label(), "Dummy title doesn't equal node title."); Chris@0: Chris@0: // Search for the dummy title with a GET query. Chris@0: $this->drupalGet('search/node', ['query' => ['keys' => $dummy_title]]); Chris@0: $this->assertNoText($this->node->label(), 'Basic page node is not found with dummy title.'); Chris@0: Chris@0: // Search for the title of the node with a GET query. Chris@0: $this->drupalGet('search/node', ['query' => ['keys' => $this->node->label()]]); Chris@0: $this->assertText($this->node->label(), 'Basic page node is found with GET query.'); Chris@0: Chris@0: // Search for the title of the node with a POST query. Chris@0: $edit = ['or' => $this->node->label()]; Chris@0: $this->drupalPostForm('search/node', $edit, t('Advanced search')); Chris@0: $this->assertText($this->node->label(), 'Basic page node is found with POST query.'); Chris@0: Chris@0: // Search by node type. Chris@0: $this->drupalPostForm('search/node', array_merge($edit, ['type[page]' => 'page']), t('Advanced search')); Chris@0: $this->assertText($this->node->label(), 'Basic page node is found with POST query and type:page.'); Chris@0: Chris@0: $this->drupalPostForm('search/node', array_merge($edit, ['type[article]' => 'article']), t('Advanced search')); Chris@0: $this->assertText('search yielded no results', 'Article node is not found with POST query and type:article.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that after submitting the advanced search form, the form is refilled. Chris@0: */ Chris@0: public function testFormRefill() { Chris@0: $edit = [ Chris@0: 'keys' => 'cat', Chris@0: 'or' => 'dog gerbil', Chris@0: 'phrase' => 'pets are nice', Chris@0: 'negative' => 'fish snake', Chris@0: 'type[page]' => 'page', Chris@0: ]; Chris@0: $this->drupalPostForm('search/node', $edit, t('Advanced search')); Chris@0: Chris@0: // Test that the encoded query appears in the page title. Only test the Chris@0: // part not including the quote, because assertText() cannot seem to find Chris@0: // the quote marks successfully. Chris@0: $this->assertText('Search for cat dog OR gerbil -fish -snake'); Chris@0: Chris@0: // Verify that all of the form fields are filled out. Chris@0: foreach ($edit as $key => $value) { Chris@0: if ($key != 'type[page]') { Chris@0: $elements = $this->xpath('//input[@name=:name]', [':name' => $key]); Chris@0: $this->assertTrue(isset($elements[0]) && $elements[0]['value'] == $value, "Field $key is set to $value"); Chris@0: } Chris@0: else { Chris@0: $elements = $this->xpath('//input[@name=:name]', [':name' => $key]); Chris@0: $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), "Field $key is checked"); Chris@0: } Chris@0: } Chris@0: Chris@0: // Now test by submitting the or/not part of the query in the main Chris@0: // search box, and verify that the advanced form is not filled out. Chris@0: // (It shouldn't be filled out unless you submit values in those fields.) Chris@0: $edit2 = ['keys' => 'cat dog OR gerbil -fish -snake']; Chris@0: $this->drupalPostForm('search/node', $edit2, t('Advanced search')); Chris@0: $this->assertText('Search for cat dog OR gerbil -fish -snake'); Chris@0: foreach ($edit as $key => $value) { Chris@0: if ($key != 'type[page]') { Chris@0: $elements = $this->xpath('//input[@name=:name]', [':name' => $key]); Chris@0: $this->assertFalse(isset($elements[0]) && $elements[0]['value'] == $value, "Field $key is not set to $value"); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }