Chris@0: drupalCreateUser(['administer blocks', 'search content']); Chris@0: $this->drupalLogin($admin_user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that the search form block can be placed and works. Chris@0: */ Chris@0: public function testSearchFormBlock() { Chris@0: Chris@0: // Test availability of the search block in the admin "Place blocks" list. Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->clickLinkPartialName('Place block'); Chris@0: $this->assertLinkByHref('/admin/structure/block/add/search_form_block/classy', 0, Chris@0: 'Did not find the search block in block candidate list.'); Chris@0: Chris@0: $block = $this->drupalPlaceBlock('search_form_block'); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($block->label(), 'Block title was found.'); Chris@0: Chris@0: // Check that name attribute is not empty. Chris@0: $pattern = "//input[@type='submit' and @name='']"; Chris@0: $elements = $this->xpath($pattern); Chris@0: $this->assertTrue(empty($elements), 'The search input field does not have empty name attribute.'); Chris@0: Chris@0: // Test a normal search via the block form, from the front page. Chris@0: $terms = ['keys' => 'test']; Chris@0: $this->submitGetForm('', $terms, t('Search')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('Your search yielded no results'); Chris@0: Chris@0: // Test a search from the block on a 404 page. Chris@0: $this->drupalGet('foo'); Chris@0: $this->assertResponse(404); Chris@0: $this->submitGetForm(NULL, $terms, t('Search')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('Your search yielded no results'); Chris@0: Chris@0: $visibility = $block->getVisibility(); Chris@0: $visibility['request_path']['pages'] = 'search'; Chris@0: $block->setVisibilityConfig('request_path', $visibility['request_path']); Chris@0: Chris@0: $this->submitGetForm('', $terms, t('Search')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('Your search yielded no results'); Chris@0: Chris@0: // Confirm that the form submits to the default search page. Chris@0: /** @var $search_page_repository \Drupal\search\SearchPageRepositoryInterface */ Chris@0: $search_page_repository = \Drupal::service('search.search_page_repository'); Chris@0: $entity_id = $search_page_repository->getDefaultSearchPage(); Chris@0: $this->assertEqual( Chris@0: $this->getUrl(), Chris@0: \Drupal::url('search.view_' . $entity_id, [], ['query' => ['keys' => $terms['keys']], 'absolute' => TRUE]), Chris@0: 'Submitted to correct URL.' Chris@0: ); Chris@0: Chris@0: // Test an empty search via the block form, from the front page. Chris@0: $terms = ['keys' => '']; Chris@0: $this->submitGetForm('', $terms, t('Search')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('Please enter some keywords'); Chris@0: Chris@0: // Confirm that the user is redirected to the search page, when form is Chris@0: // submitted empty. Chris@0: $this->assertEqual( Chris@0: $this->getUrl(), Chris@0: \Drupal::url('search.view_' . $entity_id, [], ['query' => ['keys' => ''], 'absolute' => TRUE]), Chris@0: 'Redirected to correct URL.' Chris@0: ); Chris@0: Chris@0: // Test that after entering a too-short keyword in the form, you can then Chris@0: // search again with a longer keyword. First test using the block form. Chris@0: $this->submitGetForm('node', ['keys' => $this->randomMachineName(1)], t('Search')); Chris@0: $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: $this->assertNoText(t('Please enter some keywords'), 'With short word entered, no keywords message is not displayed'); Chris@0: $this->submitGetForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), 'search-block-form'); Chris@0: $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: Chris@0: // Same test again, using the search page form for the second search this Chris@0: // time. Chris@0: $this->submitGetForm('node', ['keys' => $this->randomMachineName(1)], t('Search')); Chris@0: $this->drupalPostForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), [], [], 'search-form'); Chris@0: $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: Chris@0: } Chris@0: Chris@0: }