View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Views;
use Drupal\views_test_data\Plugin\views\filter\FilterTest as FilterPlugin;
class FilterTest extends ViewTestBase {
public static $testViews = [
'test_filter',
'test_filter_in_operator_ui',
];
protected static $modules = [
'views_ui',
'node',
];
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->enableViewsTestModule();
$this->adminUser = $this
->drupalCreateUser([
'administer views',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
}
protected function viewsData() {
$data = parent::viewsData();
$data['views_test_data']['name']['filter']['id'] = 'test_filter';
return $data;
}
public function testFilterQuery() {
$plugin = $this->container
->get('plugin.manager.views.filter')
->createInstance('test_filter');
$this
->assertInstanceOf(FilterPlugin::class, $plugin);
$view = Views::getView('test_filter');
$view
->initDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'test_filter' => [
'id' => 'test_filter',
'table' => 'views_test_data',
'field' => 'name',
'operator' => '=',
'value' => 'John',
'group' => 0,
],
]);
$this
->executeView($view);
$this
->assertTrue(!empty($view->query->where));
$where = $view->query->where;
$this
->assertSame('views_test_data.name', $where[0]['conditions'][0]['field'], 'Where condition field matches');
$this
->assertSame('John', $where[0]['conditions'][0]['value'], 'Where condition value matches');
$this
->assertSame('=', $where[0]['conditions'][0]['operator'], 'Where condition operator matches');
$this
->executeView($view);
$this
->assertSame('=', $view->filter['test_filter']->operator);
$this
->assertSame('John', $view->filter['test_filter']->value);
$this
->assertCount(1, $view->result, new FormattableMarkup('Results were returned. @count results.', [
'@count' => count($view->result),
]));
$view
->destroy();
$view
->initDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'test_filter' => [
'id' => 'test_filter',
'table' => 'views_test_data',
'field' => 'name',
'operator' => '<>',
'value' => 'John',
'group' => 0,
],
]);
$this
->executeView($view);
$this
->assertSame('<>', $view->filter['test_filter']->operator);
$this
->assertSame('John', $view->filter['test_filter']->value);
$this
->assertCount(4, $view->result, new FormattableMarkup('Results were returned. @count results.', [
'@count' => count($view->result),
]));
$view
->destroy();
$view
->initDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'test_filter' => [
'id' => 'test_filter',
'table' => 'views_test_data',
'field' => 'name',
'operator' => '<>',
'value' => 'John',
'group' => 0,
'test_enable' => FALSE,
],
]);
$this
->executeView($view);
$this
->assertCount(5, $view->result, new FormattableMarkup('All @count results returned', [
'@count' => count($view->displayHandlers),
]));
}
public function testInOperatorSelectAllOptions() {
$row['row[type]'] = 'fields';
$this
->drupalGet('admin/structure/views/nojs/display/test_filter_in_operator_ui/default/row');
$this
->submitForm($row, 'Apply');
$field['name[node_field_data.nid]'] = TRUE;
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_filter_in_operator_ui/default/field');
$this
->submitForm($field, 'Add and configure fields');
$this
->drupalGet('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/field/nid');
$this
->submitForm([], 'Apply');
$edit['options[value][all]'] = TRUE;
$edit['options[value][article]'] = TRUE;
$edit['options[value][page]'] = TRUE;
$this
->drupalGet('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/filter/type');
$this
->submitForm($edit, 'Apply');
$this
->drupalGet('admin/structure/views/view/test_filter_in_operator_ui/edit/default');
$this
->submitForm([], 'Save');
$this
->submitForm([], 'Update preview');
$this
->assertSession()
->pageTextNotContains('An illegal choice has been detected.');
}
public function testLimitExposedOperators() {
$this
->drupalGet('test_filter_in_operator_ui');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->optionExists('edit-nid-op', '<');
$this
->assertSession()
->optionExists('edit-nid-op', '<=');
$this
->assertSession()
->optionExists('edit-nid-op', '=');
$this
->assertSession()
->optionNotExists('edit-nid-op', '>');
$this
->assertSession()
->optionNotExists('edit-nid-op', '>=');
$this
->assertSession()
->fieldExists('edit-nid-value');
$this
->assertSession()
->fieldNotExists('edit-nid-min');
$this
->assertSession()
->fieldNotExists('edit-nid-max');
$edit = [];
$edit['options[operator]'] = '>';
$edit['options[expose][operator_list][]'] = [
'>',
'>=',
'between',
];
$this
->drupalGet('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/filter/nid');
$this
->submitForm($edit, 'Apply');
$this
->drupalGet('admin/structure/views/view/test_filter_in_operator_ui/edit/default');
$this
->submitForm([], 'Save');
$this
->drupalGet('test_filter_in_operator_ui');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->optionNotExists('edit-nid-op', '<');
$this
->assertSession()
->optionNotExists('edit-nid-op', '<=');
$this
->assertSession()
->optionNotExists('edit-nid-op', '=');
$this
->assertSession()
->optionExists('edit-nid-op', '>');
$this
->assertSession()
->optionExists('edit-nid-op', '>=');
$this
->assertSession()
->fieldExists('edit-nid-value');
$this
->assertSession()
->fieldExists('edit-nid-min');
$this
->assertSession()
->fieldExists('edit-nid-max');
$edit = [];
$edit['options[operator]'] = '=';
$edit['options[expose][operator_list][]'] = [
'<',
'>',
];
$this
->drupalGet('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/filter/nid');
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('You selected the "Is equal to" operator as the default value but is not included in the list of limited operators.');
}
}