3 namespace Drupal\search\Tests;
6 * Tests if the search form block is available.
10 class SearchBlockTest extends SearchTestBase {
17 public static $modules = ['block'];
19 protected function setUp() {
22 // Create and log in user.
23 $admin_user = $this->drupalCreateUser(['administer blocks', 'search content']);
24 $this->drupalLogin($admin_user);
28 * Test that the search form block can be placed and works.
30 public function testSearchFormBlock() {
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.');
38 $block = $this->drupalPlaceBlock('search_form_block');
41 $this->assertText($block->label(), 'Block title was found.');
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.');
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');
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');
61 $visibility = $block->getVisibility();
62 $visibility['request_path']['pages'] = 'search';
63 $block->setVisibilityConfig('request_path', $visibility['request_path']);
65 $this->submitGetForm('', $terms, t('Search'));
66 $this->assertResponse(200);
67 $this->assertText('Your search yielded no results');
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();
75 \Drupal::url('search.view_' . $entity_id, [], ['query' => ['keys' => $terms['keys']], 'absolute' => TRUE]),
76 'Submitted to correct URL.'
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');
85 // Confirm that the user is redirected to the search page, when form is
89 \Drupal::url('search.view_' . $entity_id, [], ['query' => ['keys' => ''], 'absolute' => TRUE]),
90 'Redirected to correct URL.'
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');
101 // Same test again, using the search page form for the second search this
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');