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