annotate core/modules/search/tests/src/Functional/SearchBlockTest.php @ 19:fa3358dc1485 tip

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