3 namespace Drupal\views_ui\Tests;
5 use Drupal\views\Entity\View;
8 * Tests exposed forms UI functionality.
12 class ExposedFormUITest extends UITestBase {
15 * Views used by this test.
19 public static $testViews = ['test_exposed_admin_ui'];
24 public static $modules = ['node', 'views_ui', 'block', 'taxonomy', 'field_ui', 'datetime'];
27 * Array of error message strings raised by the grouped form.
31 * @see FilterPluginBase::buildGroupValidate
33 protected $groupFormUiErrors = [];
35 protected function setUp() {
38 $this->drupalCreateContentType(['type' => 'article']);
39 $this->drupalCreateContentType(['type' => 'page']);
41 // Create some random nodes.
42 for ($i = 0; $i < 5; $i++) {
43 $this->drupalCreateNode();
46 // Error strings used in the grouped filter form validation.
47 $this->groupFormUiErrors['missing_value'] = t('A value is required if the label for this item is defined.');
48 $this->groupFormUiErrors['missing_title'] = t('A label is required if the value for this item is defined.');
49 $this->groupFormUiErrors['missing_title_empty_operator'] = t('A label is required for the specified operator.');
53 * Tests the admin interface of exposed filter and sort items.
55 public function testExposedAdminUi() {
58 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
59 // Be sure that the button is called exposed.
60 $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose filter'));
62 // The first time the filter UI is displayed, the operator and the
63 // value forms should be shown.
64 $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
65 $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
66 $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
67 $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');
69 // Click the Expose filter button.
70 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', $edit, t('Expose filter'));
71 // Check the label of the expose button.
72 $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide filter'));
74 // After exposing the filter, Operator and Value should be still here.
75 $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists');
76 $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists');
77 $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
78 $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');
80 // Check the validations of the filter handler.
82 $edit['options[expose][identifier]'] = '';
83 $this->drupalPostForm(NULL, $edit, t('Apply'));
84 $this->assertText(t('The identifier is required if the filter is exposed.'));
87 $edit['options[expose][identifier]'] = 'value';
88 $this->drupalPostForm(NULL, $edit, t('Apply'));
89 $this->assertText(t('This identifier is not allowed.'));
91 // Now check the sort criteria.
92 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created');
93 $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose sort'));
94 $this->assertNoFieldById('edit-options-expose-label', '', 'Make sure no label field is shown');
96 // Un-expose the filter.
97 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
98 $this->drupalPostForm(NULL, [], t('Hide filter'));
100 // After Un-exposing the filter, Operator and Value should be shown again.
101 $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists after hide filter');
102 $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists after hide filter');
103 $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists after hide filter');
104 $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists after hide filter');
106 // Click the Expose sort button.
108 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created', $edit, t('Expose sort'));
109 // Check the label of the expose button.
110 $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide sort'));
111 $this->assertFieldById('edit-options-expose-label', '', 'Make sure a label field is shown');
113 // Test adding a new exposed sort criteria.
114 $view_id = $this->randomView()['id'];
115 $this->drupalGet("admin/structure/views/nojs/add-handler/$view_id/default/sort");
116 $this->drupalPostForm(NULL, ['name[node_field_data.created]' => 1], t('Add and configure @handler', ['@handler' => t('sort criteria')]));
117 $this->assertFieldByXPath('//input[@name="options[order]" and @checked="checked"]', 'ASC', 'The default order is set.');
118 // Change the order and expose the sort.
119 $this->drupalPostForm(NULL, ['options[order]' => 'DESC'], t('Apply'));
120 $this->drupalPostForm("admin/structure/views/nojs/handler/$view_id/default/sort/created", [], t('Expose sort'));
121 $this->assertFieldByXPath('//input[@name="options[order]" and @checked="checked"]', 'DESC');
122 $this->assertFieldByName('options[expose][label]', 'Authored on', 'The default label is set.');
123 // Change the label and save the view.
124 $edit = ['options[expose][label]' => $this->randomString()];
125 $this->drupalPostForm(NULL, $edit, t('Apply'));
126 $this->drupalPostForm(NULL, [], t('Save'));
127 // Check that the values were saved.
128 $display = View::load($view_id)->getDisplay('default');
129 $this->assertTrue($display['display_options']['sorts']['created']['exposed']);
130 $this->assertEqual($display['display_options']['sorts']['created']['expose'], ['label' => $edit['options[expose][label]']]);
131 $this->assertEqual($display['display_options']['sorts']['created']['order'], 'DESC');
135 * Tests the admin interface of exposed grouped filters.
137 public function testGroupedFilterAdminUi() {
140 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
142 // Click the Expose filter button.
143 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', $edit, t('Expose filter'));
144 // Check the label of the grouped filters button.
145 $this->helperButtonHasLabel('edit-options-group-button-button', t('Grouped filters'));
147 // Click the Grouped Filters button.
148 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
149 $this->drupalPostForm(NULL, [], t('Grouped filters'));
151 // After click on 'Grouped Filters', the standard operator and value should
153 $this->assertNoFieldById('edit-options-operator-in', '', 'Operator In not exists');
154 $this->assertNoFieldById('edit-options-operator-not-in', '', 'Operator Not In not exists');
155 $this->assertNoFieldById('edit-options-value-page', '', 'Checkbox for Page not exists');
156 $this->assertNoFieldById('edit-options-value-article', '', 'Checkbox for Article not exists');
158 // Check that after click on 'Grouped Filters', a new button is shown to
159 // add more items to the list.
160 $this->helperButtonHasLabel('edit-options-group-info-add-group', t('Add another item'));
162 // Validate a single entry for a grouped filter.
163 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
165 $edit["options[group_info][group_items][1][title]"] = 'Is Article';
166 $edit["options[group_info][group_items][1][value][article]"] = 'article';
167 $this->drupalPostForm(NULL, $edit, t('Apply'));
168 $this->assertUrl('admin/structure/views/view/test_exposed_admin_ui/edit/default');
169 $this->assertNoGroupedFilterErrors();
171 // Validate multiple entries for grouped filters.
172 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
174 $edit["options[group_info][group_items][1][title]"] = 'Is Article';
175 $edit["options[group_info][group_items][1][value][article]"] = 'article';
176 $edit["options[group_info][group_items][2][title]"] = 'Is Page';
177 $edit["options[group_info][group_items][2][value][page]"] = 'page';
178 $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
179 $edit["options[group_info][group_items][3][value][article]"] = 'article';
180 $edit["options[group_info][group_items][3][value][page]"] = 'page';
181 $this->drupalPostForm(NULL, $edit, t('Apply'));
182 $this->assertUrl('admin/structure/views/view/test_exposed_admin_ui/edit/default', [], 'Correct validation of the node type filter.');
183 $this->assertNoGroupedFilterErrors();
185 // Validate an "is empty" filter -- title without value is valid.
186 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/body_value');
188 $edit["options[group_info][group_items][1][title]"] = 'No body';
189 $edit["options[group_info][group_items][1][operator]"] = 'empty';
190 $this->drupalPostForm(NULL, $edit, t('Apply'));
191 $this->assertUrl('admin/structure/views/view/test_exposed_admin_ui/edit/default', [], 'The "empty" operator validates correctly.');
192 $this->assertNoGroupedFilterErrors();
194 // Ensure the string "0" can be used as a value for numeric filters.
195 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_exposed_admin_ui/default/filter', ['name[node_field_data.nid]' => TRUE], t('Add and configure @handler', ['@handler' => t('filter criteria')]));
196 $this->drupalPostForm(NULL, [], t('Expose filter'));
197 $this->drupalPostForm(NULL, [], t('Grouped filters'));
199 $edit['options[group_info][group_items][1][title]'] = 'Testing zero';
200 $edit['options[group_info][group_items][1][operator]'] = '>';
201 $edit['options[group_info][group_items][1][value][value]'] = '0';
202 $this->drupalPostForm(NULL, $edit, t('Apply'));
203 $this->assertUrl('admin/structure/views/view/test_exposed_admin_ui/edit/default', [], 'A string "0" is a valid value.');
204 $this->assertNoGroupedFilterErrors();
206 // Ensure "between" filters validate correctly.
207 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/nid');
208 $edit['options[group_info][group_items][1][title]'] = 'ID between test';
209 $edit['options[group_info][group_items][1][operator]'] = 'between';
210 $edit['options[group_info][group_items][1][value][min]'] = '0';
211 $edit['options[group_info][group_items][1][value][max]'] = '10';
212 $this->drupalPostForm(NULL, $edit, t('Apply'));
213 $this->assertUrl('admin/structure/views/view/test_exposed_admin_ui/edit/default', [], 'The "between" filter validates correctly.');
214 $this->assertNoGroupedFilterErrors();
217 public function testGroupedFilterAdminUiErrors() {
218 // Select the empty operator without a title specified.
219 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/body_value');
221 $edit["options[group_info][group_items][1][title]"] = '';
222 $edit["options[group_info][group_items][1][operator]"] = 'empty';
223 $this->drupalPostForm(NULL, $edit, t('Apply'));
224 $this->assertText($this->groupFormUiErrors['missing_title_empty_operator']);
226 // Specify a title without a value.
227 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
228 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', [], t('Expose filter'));
229 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', [], t('Grouped filters'));
231 $edit["options[group_info][group_items][1][title]"] = 'Is Article';
232 $this->drupalPostForm(NULL, $edit, t('Apply'));
233 $this->assertText($this->groupFormUiErrors['missing_value']);
235 // Specify a value without a title.
236 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
238 $edit["options[group_info][group_items][1][title]"] = '';
239 $edit["options[group_info][group_items][1][value][article]"] = 'article';
240 $this->drupalPostForm(NULL, $edit, t('Apply'));
241 $this->assertText($this->groupFormUiErrors['missing_title']);
245 * Asserts that there are no Grouped Filters errors.
247 * @param string $message
248 * The assert message.
249 * @param string $group
250 * The assertion group.
253 * Result of the assertion.
255 protected function assertNoGroupedFilterErrors($message = '', $group = 'Other') {
256 foreach ($this->groupFormUiErrors as $error) {
257 $err_message = $message;
258 if (empty($err_message)) {
259 $err_message = "Verify that '$error' is not in the HTML output.";
261 if (empty($message)) {
262 return $this->assertNoRaw($error, $err_message, $group);
269 * Tests the configuration of grouped exposed filters.
271 public function testExposedGroupedFilter() {
272 // Click the Expose filter button.
273 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', [], t('Expose filter'));
274 // Select 'Grouped filters' radio button.
275 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', [], t('Grouped filters'));
278 'options[group_button][radios][radios]' => 1,
279 'options[group_info][group_items][1][title]' => '1st',
280 'options[group_info][group_items][1][value][all]' => 'all',
281 'options[group_info][group_items][2][title]' => '2nd',
282 'options[group_info][group_items][2][value][article]' => 'article',
283 'options[group_info][group_items][3][title]' => '3rd',
284 'options[group_info][group_items][3][value][page]' => 'page',
286 // Apply the filter settings.
287 $this->drupalPostForm(NULL, $edit, t('Apply'));
288 // Check that the view is saved without errors.
289 $this->drupalPostForm(NULL, [], t('Save'));
290 $this->assertResponse(200);
292 // Click the Expose filter button.
293 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_exposed_admin_ui/default/filter', ['name[node_field_data.status]' => 1], t('Add and configure filter criteria'));
294 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status', [], t('Expose filter'));
295 // Select 'Grouped filters' radio button.
296 $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status', [], t('Grouped filters'));
299 'options[group_button][radios][radios]' => 1,
300 'options[group_info][group_items][1][title]' => 'Any',
301 'options[group_info][group_items][1][value]' => 'All',
302 'options[group_info][group_items][2][title]' => 'Published',
303 'options[group_info][group_items][2][value]' => 1,
304 'options[group_info][group_items][3][title]' => 'Unpublished',
305 'options[group_info][group_items][3][value]' => 0,
307 // Apply the filter settings.
308 $this->drupalPostForm(NULL, $edit, t('Apply'));
309 // Check that the view is saved without errors.
310 $this->drupalPostForm(NULL, [], t('Save'));
311 $this->assertResponse(200);
313 $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status');
314 // Assert the same settings defined before still are there.
315 $this->assertFieldChecked('edit-options-group-info-group-items-1-value-all');
316 $this->assertFieldChecked('edit-options-group-info-group-items-2-value-1');
317 $this->assertFieldChecked('edit-options-group-info-group-items-3-value-0');