View source
<?php
namespace Drupal\Tests\better_exposed_filters\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\better_exposed_filters\Traits\BetterExposedFiltersTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\views\Views;
class BetterExposedFiltersTest extends WebDriverTestBase {
use BetterExposedFiltersTrait;
use ContentTypeCreationTrait;
use NodeCreationTrait;
public static $modules = [
'block',
'node',
'views',
'taxonomy',
'better_exposed_filters',
'bef_test',
];
protected function setUp() {
parent::setUp();
\Drupal::configFactory()
->getEditable('views.view.bef_test')
->set('display.default.display_options.use_ajax', TRUE)
->save();
$this
->createNode([
'title' => 'Page One',
'field_bef_boolean' => '',
'field_bef_email' => 'bef-test@drupal.org',
'field_bef_integer' => '1',
'field_bef_letters' => 'Aardvark',
'field_bef_location' => '10',
'type' => 'bef_test',
]);
$this
->createNode([
'title' => 'Page Two',
'field_bef_boolean' => '',
'field_bef_email' => 'bef-test@drupal.org',
'field_bef_integer' => '2',
'field_bef_letters' => 'Bumble & the Bee',
'field_bef_location' => '15',
'type' => 'bef_test',
]);
}
public function testAutoSubmit() {
$view = Views::getView('bef_test');
$display =& $view->storage
->getDisplay('default');
$this
->setBetterExposedOptions($view, [
'general' => [
'autosubmit' => TRUE,
'autosubmit_exclude_textfield' => TRUE,
],
]);
$this
->drupalGet('bef-test');
$session = $this
->getSession();
$page = $session
->getPage();
$html = $page
->getHtml();
$this
->assertContains('Page One', $html);
$this
->assertContains('Page Two', $html);
$field_bef_integer = $page
->findField('field_bef_integer_value');
$field_bef_integer
->setValue('1');
$field_bef_integer
->blur();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $page
->getHtml();
$this
->assertContains('Page One', $html);
$this
->assertNotContains('Page Two', $html);
$field_bef_email = $page
->find('css', '.form-item-field-bef-email-value input');
$field_bef_email
->setValue('qwerty@test.com');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertContains('Page One', $html);
$this
->assertNotContains('Page Two', $html);
$this
->submitForm([], 'Apply');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $page
->getHtml();
$this
->assertNotContains('Page One', $html);
$this
->assertNotContains('Page Two', $html);
}
public function testAutoSubmitWithExposedFormBlock() {
$view = Views::getView('bef_test');
$display =& $view->storage
->getDisplay('default');
$block = $this
->drupalPlaceBlock('views_exposed_filter_block:bef_test-page_2');
$this
->setBetterExposedOptions($view, [
'general' => [
'autosubmit' => TRUE,
'autosubmit_exclude_textfield' => TRUE,
],
]);
$this
->drupalGet('bef-test-with-block');
$session = $this
->getSession();
$page = $session
->getPage();
$html = $page
->getHtml();
$this
->assertContains('Page One', $html);
$this
->assertContains('Page Two', $html);
$field_bef_integer = $page
->findField('field_bef_integer_value');
$field_bef_integer
->setValue('1');
$field_bef_integer
->blur();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $page
->getHtml();
$this
->assertContains('Page One', $html);
$this
->assertNotContains('Page Two', $html);
$field_bef_email = $page
->find('css', '.form-item-field-bef-email-value input');
$field_bef_email
->setValue('qwerty@test.com');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertContains('Page One', $html);
$this
->assertNotContains('Page Two', $html);
$this
->submitForm([], 'Apply');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $page
->getHtml();
$this
->assertNotContains('Page One', $html);
$this
->assertNotContains('Page Two', $html);
}
public function testSecondaryOptions() {
$view = Views::getView('bef_test');
$display =& $view->storage
->getDisplay('default');
$this
->setBetterExposedOptions($view, [
'general' => [
'allow_secondary' => TRUE,
'secondary_label' => 'Secondary Options TEST',
],
'sort' => [
'plugin_id' => 'default',
'advanced' => [
'is_secondary' => TRUE,
],
],
'pager' => [
'plugin_id' => 'default',
'advanced' => [
'is_secondary' => TRUE,
],
],
'filter' => [
'field_bef_boolean_value' => [
'plugin_id' => 'default',
'advanced' => [
'is_secondary' => TRUE,
],
],
],
]);
$this
->drupalGet('bef-test');
$session = $this
->getSession();
$page = $session
->getPage();
$secondary_options = $page
->find('css', '.bef--secondary');
$this
->assertFalse($secondary_options
->hasAttribute('open'));
$field_bef_boolean = $page
->findField('field_bef_boolean_value');
$this
->assertFalse($field_bef_boolean
->isVisible());
$secondary_options
->click();
$this
->submitForm([
'field_bef_boolean_value' => 1,
], 'Apply');
$session = $this
->getSession();
$page = $session
->getPage();
$secondary_options = $page
->find('css', '.bef--secondary');
$this
->assertTrue($secondary_options
->hasAttribute('open'));
$field_bef_boolean = $page
->findField('field_bef_boolean_value');
$this
->assertTrue($field_bef_boolean
->isVisible());
}
}