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 = 'stark';
protected $adminUser;
protected function setUp() : void {
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
->assertSession()
->linkByHrefExists('/admin/structure/block/add/search_form_block/stark', 0, 'Did not find the search block in block candidate list.');
$block = $this
->drupalPlaceBlock('search_form_block');
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($block
->label());
$pattern = "//input[@type='submit' and @name='']";
$elements = $this
->xpath($pattern);
$this
->assertEmpty($elements, 'The search input field does not have empty name attribute.');
$terms = [
'keys' => 'test',
];
$this
->drupalGet('');
$this
->submitForm($terms, 'Search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Your search yielded no results');
$this
->drupalGet('foo');
$this
->assertSession()
->statusCodeEquals(404);
$this
->submitForm($terms, 'Search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Your search yielded no results');
$visibility = $block
->getVisibility();
$visibility['request_path']['pages'] = 'search';
$block
->setVisibilityConfig('request_path', $visibility['request_path']);
$this
->drupalGet('');
$this
->submitForm($terms, 'Search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Your search yielded no results');
$search_page_repository = \Drupal::service('search.search_page_repository');
$entity_id = $search_page_repository
->getDefaultSearchPage();
$this
->assertEquals($this
->getUrl(), Url::fromRoute('search.view_' . $entity_id, [], [
'query' => [
'keys' => $terms['keys'],
],
'absolute' => TRUE,
])
->toString(), 'Submitted to correct URL.');
$terms = [
'keys' => '',
];
$this
->drupalGet('');
$this
->submitForm($terms, 'Search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->statusMessageContains('Please enter some keywords', 'error');
$this
->assertEquals($this
->getUrl(), Url::fromRoute('search.view_' . $entity_id, [], [
'query' => [
'keys' => '',
],
'absolute' => TRUE,
])
->toString(), 'Redirected to correct URL.');
$this
->drupalGet('node');
$this
->submitForm([
'keys' => $this
->randomMachineName(1),
], 'Search');
$this
->assertSession()
->statusMessageContains('You must include at least one keyword to match in the content', 'warning');
$this
->assertSession()
->statusMessageNotContains('Please enter some keywords');
$this
->submitForm([
'keys' => $this
->randomMachineName(),
], 'Search', 'search-block-form');
$this
->assertSession()
->statusMessageNotContains('You must include at least one keyword to match in the content');
$this
->drupalGet('node');
$this
->submitForm([
'keys' => $this
->randomMachineName(1),
], 'Search');
$this
->submitForm([
'keys' => $this
->randomMachineName(),
], 'Search', 'search-form');
$this
->assertSession()
->statusMessageNotContains('You must include at least one keyword to match in the content');
$this
->drupalGet('admin/structure/block/manage/' . $block
->id());
$this
->submitForm([
'settings[page_id]' => 'user_search',
], 'Save block');
$name = $this->adminUser
->getAccountName();
$this
->drupalGet('node');
$this
->submitForm([
'keys' => $name,
], 'Search');
$this
->assertSession()
->linkExists($name);
}
}