21afda7f05f5dd72ec3959db0aca2c8c8504014b
[yaffs-website] / web / core / modules / search / src / Tests / SearchBlockTest.php
1 <?php
2
3 namespace Drupal\search\Tests;
4
5 /**
6  * Tests if the search form block is available.
7  *
8  * @group search
9  */
10 class SearchBlockTest extends SearchTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = ['block'];
18
19   protected function setUp() {
20     parent::setUp();
21
22     // Create and log in user.
23     $admin_user = $this->drupalCreateUser(['administer blocks', 'search content']);
24     $this->drupalLogin($admin_user);
25   }
26
27   /**
28    * Test that the search form block can be placed and works.
29    */
30   public function testSearchFormBlock() {
31
32     // Test availability of the search block in the admin "Place blocks" list.
33     $this->drupalGet('admin/structure/block');
34     $this->clickLinkPartialName('Place block');
35     $this->assertLinkByHref('/admin/structure/block/add/search_form_block/classy', 0,
36       'Did not find the search block in block candidate list.');
37
38     $block = $this->drupalPlaceBlock('search_form_block');
39
40     $this->drupalGet('');
41     $this->assertText($block->label(), 'Block title was found.');
42
43     // Check that name attribute is not empty.
44     $pattern = "//input[@type='submit' and @name='']";
45     $elements = $this->xpath($pattern);
46     $this->assertTrue(empty($elements), 'The search input field does not have empty name attribute.');
47
48     // Test a normal search via the block form, from the front page.
49     $terms = ['keys' => 'test'];
50     $this->submitGetForm('', $terms, t('Search'));
51     $this->assertResponse(200);
52     $this->assertText('Your search yielded no results');
53
54     // Test a search from the block on a 404 page.
55     $this->drupalGet('foo');
56     $this->assertResponse(404);
57     $this->submitGetForm(NULL, $terms, t('Search'));
58     $this->assertResponse(200);
59     $this->assertText('Your search yielded no results');
60
61     $visibility = $block->getVisibility();
62     $visibility['request_path']['pages'] = 'search';
63     $block->setVisibilityConfig('request_path', $visibility['request_path']);
64
65     $this->submitGetForm('', $terms, t('Search'));
66     $this->assertResponse(200);
67     $this->assertText('Your search yielded no results');
68
69     // Confirm that the form submits to the default search page.
70     /** @var $search_page_repository \Drupal\search\SearchPageRepositoryInterface */
71     $search_page_repository = \Drupal::service('search.search_page_repository');
72     $entity_id = $search_page_repository->getDefaultSearchPage();
73     $this->assertEqual(
74       $this->getUrl(),
75       \Drupal::url('search.view_' . $entity_id, [], ['query' => ['keys' => $terms['keys']], 'absolute' => TRUE]),
76       'Submitted to correct URL.'
77     );
78
79     // Test an empty search via the block form, from the front page.
80     $terms = ['keys' => ''];
81     $this->submitGetForm('', $terms, t('Search'));
82     $this->assertResponse(200);
83     $this->assertText('Please enter some keywords');
84
85     // Confirm that the user is redirected to the search page, when form is
86     // submitted empty.
87     $this->assertEqual(
88       $this->getUrl(),
89       \Drupal::url('search.view_' . $entity_id, [], ['query' => ['keys' => ''], 'absolute' => TRUE]),
90       'Redirected to correct URL.'
91     );
92
93     // Test that after entering a too-short keyword in the form, you can then
94     // search again with a longer keyword. First test using the block form.
95     $this->submitGetForm('node', ['keys' => $this->randomMachineName(1)], t('Search'));
96     $this->assertText('You must include at least one keyword to match in the content', 'Keyword message is displayed when searching for short word');
97     $this->assertNoText(t('Please enter some keywords'), 'With short word entered, no keywords message is not displayed');
98     $this->submitGetForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), 'search-block-form');
99     $this->assertNoText('You must include at least one keyword to match in the content', 'Keyword message is not displayed when searching for long word after short word search');
100
101     // Same test again, using the search page form for the second search this
102     // time.
103     $this->submitGetForm('node', ['keys' => $this->randomMachineName(1)], t('Search'));
104     $this->drupalPostForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), [], [], 'search-form');
105     $this->assertNoText('You must include at least one keyword to match in the content', 'Keyword message is not displayed when searching for long word after short word search');
106
107   }
108
109 }