View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class SearchBlockTest extends BrowserTestBase {
protected static $modules = [
'block',
'node',
'search',
'dblog',
'user',
];
protected $defaultTheme = 'classy';
protected $adminUser;
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'search content',
'access user profiles',
'access content',
]);
$this
->drupalLogin($this->adminUser);
}
public function testSearchFormBlock() {
$this
->drupalGet('admin/structure/block');
$this
->getSession()
->getPage()
->findLink('Place block')
->click();
$this
->assertLinkByHref('/admin/structure/block/add/search_form_block/classy', 0, 'Did not find the search block in block candidate list.');
$block = $this
->drupalPlaceBlock('search_form_block');
$this
->drupalGet('');
$this
->assertText($block
->label(), 'Block title was found.');
$pattern = "//input[@type='submit' and @name='']";
$elements = $this
->xpath($pattern);
$this
->assertTrue(empty($elements), 'The search input field does not have empty name attribute.');
$terms = [
'keys' => 'test',
];
$this
->drupalPostForm('', $terms, t('Search'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('Your search yielded no results');
$this
->drupalGet('foo');
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalPostForm(NULL, $terms, t('Search'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('Your search yielded no results');
$visibility = $block
->getVisibility();
$visibility['request_path']['pages'] = 'search';
$block
->setVisibilityConfig('request_path', $visibility['request_path']);
$this
->drupalPostForm('', $terms, t('Search'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('Your search yielded no results');
$search_page_repository = \Drupal::service('search.search_page_repository');
$entity_id = $search_page_repository
->getDefaultSearchPage();
$this
->assertEqual($this
->getUrl(), Url::fromRoute('search.view_' . $entity_id, [], [
'query' => [
'keys' => $terms['keys'],
],
'absolute' => TRUE,
])
->toString(), 'Submitted to correct URL.');
$terms = [
'keys' => '',
];
$this
->drupalPostForm('', $terms, t('Search'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('Please enter some keywords');
$this
->assertEqual($this
->getUrl(), Url::fromRoute('search.view_' . $entity_id, [], [
'query' => [
'keys' => '',
],
'absolute' => TRUE,
])
->toString(), 'Redirected to correct URL.');
$this
->drupalPostForm('node', [
'keys' => $this
->randomMachineName(1),
], t('Search'));
$this
->assertText('You must include at least one keyword to match in the content', 'Keyword message is displayed when searching for short word');
$this
->assertNoText(t('Please enter some keywords'), 'With short word entered, no keywords message is not displayed');
$this
->drupalPostForm(NULL, [
'keys' => $this
->randomMachineName(),
], t('Search'), [], 'search-block-form');
$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');
$this
->drupalPostForm('node', [
'keys' => $this
->randomMachineName(1),
], t('Search'));
$this
->drupalPostForm(NULL, [
'keys' => $this
->randomMachineName(),
], t('Search'), [], 'search-form');
$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');
$this
->drupalPostForm('admin/structure/block/manage/' . $block
->id(), [
'settings[page_id]' => 'user_search',
], 'Save block');
$name = $this->adminUser
->getAccountName();
$email = $this->adminUser
->getEmail();
$this
->drupalPostForm('node', [
'keys' => $name,
], t('Search'));
$this
->assertSession()
->linkExists($name);
}
}