View source
<?php
namespace Drupal\Tests\views\FunctionalJavascript\Plugin\views\Handler;
use Drupal\field\Entity\FieldConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\NodeType;
class FilterTest extends WebDriverTestBase {
protected static $modules = [
'node',
'views',
'views_ui',
'user',
];
protected $account;
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
\Drupal::configFactory()
->getEditable('views.settings')
->set('ui.always_live_preview', FALSE)
->save();
$this->account = $this
->drupalCreateUser([
'administer views',
]);
$this
->drupalLogin($this->account);
NodeType::create([
'type' => 'page',
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'field_name' => 'body',
'bundle' => 'page',
])
->save();
}
public function testAddingFilter() {
$web_assert = $this
->assertSession();
$url = '/admin/structure/views/view/content';
$this
->drupalGet($url);
$page = $this
->getSession()
->getPage();
$page
->clickLink('views-add-filter');
$web_assert
->waitForField('override[controls][group]');
$page
->fillField('override[controls][group]', 'content');
$only_content_rows = $this
->waitForOnlyContentRows();
$this
->assertTrue($only_content_rows);
$page
->fillField('override[controls][options_search]', 'body (body)');
$filtering_done = $this
->waitForVisibleElementCount(1, 'tr.filterable-option');
$this
->assertTrue($filtering_done);
$page
->checkField('name[node__body.body_value]');
$page
->find('css', '.ui-dialog .ui-dialog-buttonpane')
->pressButton('Add and configure filter criteria');
$web_assert
->waitForField('options[expose_button][checkbox][checkbox]');
$page
->findField('options[expose_button][checkbox][checkbox]')
->click();
$web_assert
->waitForField('options[expose][label]');
$page
->find('css', '.ui-dialog .ui-dialog-buttonpane')
->pressButton('Apply');
$web_assert
->waitForText('Content: body (exposed)');
$web_assert
->responseContains('Content: body (exposed)');
}
protected function filterVisibleElements($elements) {
$elements = array_filter($elements, function ($element) {
return $element
->isVisible();
});
return $elements;
}
protected function waitForVisibleElementCount($count, $locator, $timeout = 10000) {
$page = $this
->getSession()
->getPage();
return $page
->waitFor($timeout / 1000, function () use ($count, $page, $locator) {
$elements = $page
->findAll('css', $locator);
$visible_elements = $this
->filterVisibleElements($elements);
if (count($visible_elements) === $count) {
return TRUE;
}
return FALSE;
});
}
protected function waitForOnlyContentRows($timeout = 10000) {
$page = $this
->getSession()
->getPage();
return $page
->waitFor($timeout / 1000, function () use ($page) {
$handler_rows = $page
->findAll('css', 'tr.filterable-option');
$handler_rows = $this
->filterVisibleElements($handler_rows);
foreach ($handler_rows as $handler_row) {
if (strpos($handler_row
->getAttribute('class'), 'content') === FALSE) {
return FALSE;
}
}
return TRUE;
});
}
}