annotate core/modules/search/tests/src/Functional/SearchBlockTest.php @ 5:12f9dff5fda9 tip

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