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