Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / FilterTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Tests\views\Functional\ViewTestBase;
6 use Drupal\views\Views;
7 use Drupal\views_test_data\Plugin\views\filter\FilterTest as FilterPlugin;
8
9 /**
10  * Tests general filter plugin functionality.
11  *
12  * @group views
13  * @see \Drupal\views\Plugin\views\filter\FilterPluginBase
14  */
15 class FilterTest extends ViewTestBase {
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['test_filter', 'test_filter_in_operator_ui'];
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['views_ui', 'node'];
30
31   protected function setUp($import_test_views = TRUE) {
32     parent::setUp($import_test_views);
33
34     $this->enableViewsTestModule();
35
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']);
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function viewsData() {
46     $data = parent::viewsData();
47     $data['views_test_data']['name']['filter']['id'] = 'test_filter';
48
49     return $data;
50   }
51
52   /**
53    * Test query of the row plugin.
54    */
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.');
59
60     $view = Views::getView('test_filter');
61     $view->initDisplay();
62
63     // Change the filtering.
64     $view->displayHandlers->get('default')->overrideOption('filters', [
65       'test_filter' => [
66         'id' => 'test_filter',
67         'table' => 'views_test_data',
68         'field' => 'name',
69         'operator' => '=',
70         'value' => 'John',
71         'group' => 0,
72       ],
73     ]);
74
75     $this->executeView($view);
76
77     // Make sure the query have where data.
78     $this->assertTrue(!empty($view->query->where));
79
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');
85
86     $this->executeView($view);
87
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');
91
92     // Check that we have a single element, as a result of applying the '= John'
93     // filter.
94     $this->assertEqual(count($view->result), 1, format_string('Results were returned. @count results.', ['@count' => count($view->result)]));
95
96     $view->destroy();
97
98     $view->initDisplay();
99
100     // Change the filtering.
101     $view->displayHandlers->get('default')->overrideOption('filters', [
102       'test_filter' => [
103         'id' => 'test_filter',
104         'table' => 'views_test_data',
105         'field' => 'name',
106         'operator' => '<>',
107         'value' => 'John',
108         'group' => 0,
109       ],
110     ]);
111
112     $this->executeView($view);
113
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');
117
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)]));
121
122     $view->destroy();
123     $view->initDisplay();
124
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', [
128       'test_filter' => [
129         'id' => 'test_filter',
130         'table' => 'views_test_data',
131         'field' => 'name',
132         'operator' => '<>',
133         'value' => 'John',
134         'group' => 0,
135         // Disable this option, so nothing should be added to the query.
136         'test_enable' => FALSE,
137       ],
138     ]);
139
140     // Execute the view again.
141     $this->executeView($view);
142
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)]));
145   }
146
147   /**
148    * Test no error message is displayed when all options are selected in an
149    * exposed filter.
150    */
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.');
165   }
166
167 }