View source
<?php
namespace Drupal\Tests\facets\FunctionalJavascript;
use Drupal\views\Entity\View;
class AjaxBehaviorTest extends JsBase {
public function setUp() {
parent::setUp();
$view = View::load('search_api_test_view');
$display = $view
->getDisplay('page_1');
$display['display_options']['use_ajax'] = TRUE;
$view
->save();
}
public function testAjaxLinks() {
$this
->createFacet('owl');
$this
->createFacet('duck', 'keywords');
$this
->drupalGet('search-api-test-fulltext');
$page = $this
->getSession()
->getPage();
$block_owl = $page
->findById('block-owl-block');
$block_owl
->isVisible();
$block_duck = $page
->findById('block-duck-block');
$block_duck
->isVisible();
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertNotEmpty($links);
$this
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Displaying 3 search results');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertEmpty($links);
$this
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertNotEmpty($links);
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'strawberry',
]);
$this
->assertNotEmpty($links);
$this
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'strawberry',
]);
$this
->assertEmpty($links);
}
public function testAjaxDropdown() {
$this
->createFacet('owl');
$this
->createFacet('duck', 'category', 'dropdown', []);
$this
->drupalGet('search-api-test-fulltext');
$page = $this
->getSession()
->getPage();
$block_owl = $page
->findById('block-owl-block');
$block_owl
->isVisible();
$block_duck = $page
->findById('block-duck-block');
$block_duck
->isVisible();
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$dropdown_entry = $this
->xpath('//*[@id="block-duck-block"]/div/select/option[normalize-space(text())=:label]', [
':label' => 'article_category',
]);
$this
->assertNotEmpty($dropdown_entry);
$block_owl
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$dropdown_entry = $this
->xpath('//*[@id="block-duck-block"]/div/select/option[normalize-space(text())=:label]', [
':label' => 'article_category',
]);
$this
->assertEmpty($dropdown_entry);
$block_owl
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$dropdown = $this
->xpath('//*[@id="block-duck-block"]/div/select');
$dropdown[0]
->selectOption('article_category');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Displaying 2 search results');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertNotEmpty($links);
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'item',
]);
$this
->assertEmpty($links);
}
public function testAjaxCheckbox() {
$this
->createFacet('owl');
$this
->createFacet('duck', 'keywords', 'checkbox');
$this
->drupalGet('search-api-test-fulltext');
$page = $this
->getSession()
->getPage();
$block_owl = $page
->findById('block-owl-block');
$block_owl
->isVisible();
$block_duck = $page
->findById('block-duck-block');
$block_duck
->isVisible();
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertNotEmpty($links);
$this
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Displaying 3 search results');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertEmpty($links);
$this
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Displaying 5 search results');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertNotEmpty($links);
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'strawberry',
]);
$this
->assertNotEmpty($links);
$this
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'strawberry',
]);
$this
->assertEmpty($links);
$this
->clickLink('item');
$this
->getSession()
->getPage()
->checkField('strawberry');
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'article',
]);
$this
->assertNotEmpty($links);
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => 'item',
]);
$this
->assertEmpty($links);
}
public function testLinksWithExposedFilter() {
$view = View::load('search_api_test_view');
$display = $view
->getDisplay('page_1');
$display['display_options']['filters']['search_api_fulltext']['expose']['required'] = TRUE;
$view
->save();
$this
->createFacet('owl');
$this
->drupalGet('search-api-test-fulltext');
$page = $this
->getSession()
->getPage();
$block_owl = $page
->findById('block-owl-block');
$block_owl
->isVisible();
$this
->assertSession()
->fieldExists('edit-search-api-fulltext')
->setValue('baz');
$this
->click('.form-submit');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Displaying 3 search results');
$this
->clickLink('item');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Displaying 1 search results');
}
}