View source
<?php
namespace Drupal\Tests\search_api_page\Functional;
class BlockTest extends FunctionalTestBase {
public function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this
->setupSearchApi();
$this
->createSearchPage('search', 'Search', 'search', $this->index
->id());
$this
->createSearchPage('other_search', 'Search (Other)', 'other_search', $this->index
->id());
$this
->placeSearchBlock('search', 1);
$this
->placeSearchBlock('other_search', 2);
$this
->drupalGet('<front>');
}
private function createSearchPage($id, $label, $path, $indexId) {
$this
->drupalGet('admin/config/search/search-api-pages');
$this
->assertSession()
->statusCodeEquals(200);
$step1 = [
'label' => $label,
'id' => $id,
'index' => $indexId,
];
$this
->drupalPostForm('admin/config/search/search-api-pages/add', $step1, 'Next');
$step2 = [
'path' => $path,
];
$this
->drupalPostForm(NULL, $step2, 'Save');
}
private function placeSearchBlock($pageId, $weight) {
$this
->drupalPlaceBlock('search_api_page_form_block', [
'search_api_page' => $pageId,
'weight' => $weight,
]);
}
public function testSearchForm() {
$form = $this
->getSession()
->getPage()
->find('css', '.search-form form');
$form
->fillField('Search', 'Owls');
$form
->submit();
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertPath('search/Owls');
$this
->assertSession()
->pageTextContains('9 results found');
}
private function assertPath($expectedPath) {
$url = $this
->buildUrl($expectedPath, [
'absolute' => TRUE,
]);
$this
->assertEquals($url, $this
->getSession()
->getCurrentUrl());
}
public function testSearchFormsSearchOnCorrectPage() {
$forms = $this
->getSession()
->getPage()
->findAll('css', '.search-form form');
$form = end($forms);
$form
->fillField('Search', 'Owls');
$form
->submit();
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertPath('other_search/Owls');
$this
->assertSession()
->pageTextContains('9 results found');
}
public function testDefaultValue() {
$form = $this
->getSession()
->getPage()
->find('css', '.search-form form');
$form
->fillField('Search', 'Owls');
$form
->submit();
$this
->assertSession()
->fieldValueEquals("edit-keys", 'Owls');
$this
->assertSession()
->fieldValueEquals('edit-keys--2', '');
}
}