SearchAdvancedSearchFormTest.php 2.76 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\search\Tests\SearchAdvancedSearchFormTest.
 */

namespace Drupal\search\Tests;

10 11
/**
 * Indexes content and tests the advanced search form.
12 13
 *
 * @group search
14
 */
15
class SearchAdvancedSearchFormTest extends SearchTestBase {
16 17 18 19 20 21

  /**
   * A node to use for testing.
   *
   * @var \Drupal\node\NodeInterface
   */
22 23
  protected $node;

24
  protected function setUp() {
25 26 27 28 29 30 31 32 33
    parent::setUp();
    // Create and login user.
    $test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes'));
    $this->drupalLogin($test_user);

    // Create initial node.
    $this->node = $this->drupalCreateNode();

    // First update the index. This does the initial processing.
34
    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
35 36 37 38 39 40 41 42 43 44 45 46

    // Then, run the shutdown function. Testing is a unique case where indexing
    // and searching has to happen in the same request, so running the shutdown
    // function manually is needed to finish the indexing process.
    search_update_totals();
  }

  /**
   * Test using the search form with GET and POST queries.
   * Test using the advanced search form to limit search to nodes of type "Basic page".
   */
  function testNodeType() {
47
    $this->assertTrue($this->node->getType() == 'page', 'Node type is Basic page.');
48 49 50

    // Assert that the dummy title doesn't equal the real title.
    $dummy_title = 'Lorem ipsum';
51
    $this->assertNotEqual($dummy_title, $this->node->label(), "Dummy title doesn't equal node title.");
52 53

    // Search for the dummy title with a GET query.
54
    $this->drupalGet('search/node', array('query' => array('keys' => $dummy_title)));
55
    $this->assertNoText($this->node->label(), 'Basic page node is not found with dummy title.');
56 57

    // Search for the title of the node with a GET query.
58
    $this->drupalGet('search/node', array('query' => array('keys' => $this->node->label())));
59
    $this->assertText($this->node->label(), 'Basic page node is found with GET query.');
60 61

    // Search for the title of the node with a POST query.
62
    $edit = array('or' => $this->node->label());
63
    $this->drupalPostForm('search/node', $edit, t('Advanced search'));
64
    $this->assertText($this->node->label(), 'Basic page node is found with POST query.');
65 66

    // Advanced search type option.
67
    $this->drupalPostForm('search/node', array_merge($edit, array('type[page]' => 'page')), t('Advanced search'));
68
    $this->assertText($this->node->label(), 'Basic page node is found with POST query and type:page.');
69

70
    $this->drupalPostForm('search/node', array_merge($edit, array('type[article]' => 'article')), t('Advanced search'));
71
    $this->assertText('bike shed', 'Article node is not found with POST query and type:article.');
72 73
  }
}