3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Views;
7 use Drupal\views_test_data\Plugin\views\filter\FilterTest as FilterPlugin;
10 * Tests general filter plugin functionality.
13 * @see \Drupal\views\Plugin\views\filter\FilterPluginBase
15 class FilterTest extends ViewTestBase {
18 * Views used by this test.
22 public static $testViews = ['test_filter', 'test_filter_in_operator_ui'];
29 public static $modules = ['views_ui', 'node'];
31 protected function setUp($import_test_views = TRUE) {
32 parent::setUp($import_test_views);
34 $this->enableViewsTestModule();
36 $this->adminUser = $this->drupalCreateUser(['administer views']);
37 $this->drupalLogin($this->adminUser);
38 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
39 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
45 protected function viewsData() {
46 $data = parent::viewsData();
47 $data['views_test_data']['name']['filter']['id'] = 'test_filter';
53 * Test query of the row plugin.
55 public function testFilterQuery() {
56 // Check that we can find the test filter plugin.
57 $plugin = $this->container->get('plugin.manager.views.filter')->createInstance('test_filter');
58 $this->assertTrue($plugin instanceof FilterPlugin, 'Test filter plugin found.');
60 $view = Views::getView('test_filter');
63 // Change the filtering.
64 $view->displayHandlers->get('default')->overrideOption('filters', [
66 'id' => 'test_filter',
67 'table' => 'views_test_data',
75 $this->executeView($view);
77 // Make sure the query have where data.
78 $this->assertTrue(!empty($view->query->where));
80 // Check the data added.
81 $where = $view->query->where;
82 $this->assertIdentical($where[0]['conditions'][0]['field'], 'views_test_data.name', 'Where condition field matches');
83 $this->assertIdentical($where[0]['conditions'][0]['value'], 'John', 'Where condition value matches');
84 $this->assertIdentical($where[0]['conditions'][0]['operator'], '=', 'Where condition operator matches');
86 $this->executeView($view);
88 // Check that our operator and value match on the filter.
89 $this->assertIdentical($view->filter['test_filter']->operator, '=');
90 $this->assertIdentical($view->filter['test_filter']->value, 'John');
92 // Check that we have a single element, as a result of applying the '= John'
94 $this->assertEqual(count($view->result), 1, format_string('Results were returned. @count results.', ['@count' => count($view->result)]));
100 // Change the filtering.
101 $view->displayHandlers->get('default')->overrideOption('filters', [
103 'id' => 'test_filter',
104 'table' => 'views_test_data',
112 $this->executeView($view);
114 // Check that our operator and value match on the filter.
115 $this->assertIdentical($view->filter['test_filter']->operator, '<>');
116 $this->assertIdentical($view->filter['test_filter']->value, 'John');
118 // Check if we have the other elements in the dataset, as a result of
119 // applying the '<> John' filter.
120 $this->assertEqual(count($view->result), 4, format_string('Results were returned. @count results.', ['@count' => count($view->result)]));
123 $view->initDisplay();
125 // Set the test_enable option to FALSE. The 'where' clause should not be
126 // added to the query.
127 $view->displayHandlers->get('default')->overrideOption('filters', [
129 'id' => 'test_filter',
130 'table' => 'views_test_data',
135 // Disable this option, so nothing should be added to the query.
136 'test_enable' => FALSE,
140 // Execute the view again.
141 $this->executeView($view);
143 // Check if we have all 5 results.
144 $this->assertEqual(count($view->result), 5, format_string('All @count results returned', ['@count' => count($view->displayHandlers)]));
148 * Test no error message is displayed when all options are selected in an
151 public function testInOperatorSelectAllOptions() {
152 $view = Views::getView('test_filter_in_operator_ui');
153 $row['row[type]'] = 'fields';
154 $this->drupalPostForm('admin/structure/views/nojs/display/test_filter_in_operator_ui/default/row', $row, t('Apply'));
155 $field['name[node_field_data.nid]'] = TRUE;
156 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_filter_in_operator_ui/default/field', $field, t('Add and configure fields'));
157 $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/field/nid', [], t('Apply'));
158 $edit['options[value][all]'] = TRUE;
159 $edit['options[value][article]'] = TRUE;
160 $edit['options[value][page]'] = TRUE;
161 $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_in_operator_ui/default/filter/type', $edit, t('Apply'));
162 $this->drupalPostForm('admin/structure/views/view/test_filter_in_operator_ui/edit/default', [], t('Save'));
163 $this->drupalPostForm(NULL, [], t('Update preview'));
164 $this->assertNoText('An illegal choice has been detected.');