3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Component\Utility\Html;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
8 use Drupal\Tests\views\Functional\ViewTestBase;
9 use Drupal\views\ViewExecutable;
10 use Drupal\views\Views;
11 use Drupal\views\Entity\View;
14 * Tests exposed forms functionality.
18 class ExposedFormTest extends ViewTestBase {
20 use AssertPageCacheContextsAndTagsTrait;
23 * Views used by this test.
27 public static $testViews = ['test_exposed_form_buttons', 'test_exposed_block', 'test_exposed_form_sort_items_per_page'];
34 public static $modules = ['node', 'views_ui', 'block', 'entity_test'];
36 protected function setUp($import_test_views = TRUE) {
37 parent::setUp($import_test_views);
39 $this->enableViewsTestModule();
41 $this->drupalCreateContentType(['type' => 'article']);
43 // Create some random nodes.
44 for ($i = 0; $i < 5; $i++) {
45 $this->drupalCreateNode(['type' => 'article']);
50 * Tests the submit button.
52 public function testSubmitButton() {
53 // Test the submit button value defaults to 'Apply'.
54 $this->drupalGet('test_exposed_form_buttons');
55 $this->assertResponse(200);
56 $this->helperButtonHasLabel('edit-submit-test-exposed-form-buttons', t('Apply'));
58 // Rename the label of the submit button.
59 $view = Views::getView('test_exposed_form_buttons');
62 $exposed_form = $view->display_handler->getOption('exposed_form');
63 $exposed_form['options']['submit_button'] = $expected_label = $this->randomMachineName();
64 $view->display_handler->setOption('exposed_form', $exposed_form);
67 // Make sure the submit button label changed.
68 $this->drupalGet('test_exposed_form_buttons');
69 $this->helperButtonHasLabel('edit-submit-test-exposed-form-buttons', $expected_label);
71 // Make sure an empty label uses the default 'Apply' button value too.
72 $view = Views::getView('test_exposed_form_buttons');
75 $exposed_form = $view->display_handler->getOption('exposed_form');
76 $exposed_form['options']['submit_button'] = '';
77 $view->display_handler->setOption('exposed_form', $exposed_form);
80 // Make sure the submit button label shows 'Apply'.
81 $this->drupalGet('test_exposed_form_buttons');
82 $this->helperButtonHasLabel('edit-submit-test-exposed-form-buttons', t('Apply'));
86 * Tests the exposed form with a non-standard identifier.
88 public function testExposedIdentifier() {
89 // Alter the identifier of the filter to a random string.
90 $view = Views::getView('test_exposed_form_buttons');
92 $identifier = 'new_identifier';
93 $view->displayHandlers->get('default')->overrideOption('filters', [
98 'table' => 'node_field_data',
99 'plugin_id' => 'in_operator',
100 'entity_type' => 'node',
101 'entity_field' => 'type',
103 'identifier' => $identifier,
104 'label' => 'Content: Type',
105 'operator_id' => 'type_op',
107 'description' => 'Exposed overridden description',
112 $this->drupalGet('test_exposed_form_buttons', ['query' => [$identifier => 'article']]);
113 $this->assertFieldById(Html::getId('edit-' . $identifier), 'article', "Article type filter set with new identifier.");
115 // Alter the identifier of the filter to a random string containing
116 // restricted characters.
117 $view = Views::getView('test_exposed_form_buttons');
119 $identifier = 'bad identifier';
120 $view->displayHandlers->get('default')->overrideOption('filters', [
125 'table' => 'node_field_data',
126 'plugin_id' => 'in_operator',
127 'entity_type' => 'node',
128 'entity_field' => 'type',
130 'identifier' => $identifier,
131 'label' => 'Content: Type',
132 'operator_id' => 'type_op',
134 'description' => 'Exposed overridden description',
138 $this->executeView($view);
140 $errors = $view->validate();
142 'default' => ['This identifier has illegal characters.'],
143 'page_1' => ['This identifier has illegal characters.'],
145 $this->assertEqual($errors, $expected);
149 * Tests whether the reset button works on an exposed form.
151 public function testResetButton() {
152 // Test the button is hidden when there is no exposed input.
153 $this->drupalGet('test_exposed_form_buttons');
154 $this->assertNoField('edit-reset');
156 $this->drupalGet('test_exposed_form_buttons', ['query' => ['type' => 'article']]);
157 // Test that the type has been set.
158 $this->assertFieldById('edit-type', 'article', 'Article type filter set.');
160 // Test the reset works.
161 $this->drupalGet('test_exposed_form_buttons', ['query' => ['op' => 'Reset']]);
162 $this->assertResponse(200);
163 // Test the type has been reset.
164 $this->assertFieldById('edit-type', 'All', 'Article type filter has been reset.');
166 // Test the button is hidden after reset.
167 $this->assertNoField('edit-reset');
169 // Test the reset works with type set.
170 $this->drupalGet('test_exposed_form_buttons', ['query' => ['type' => 'article', 'op' => 'Reset']]);
171 $this->assertResponse(200);
172 $this->assertFieldById('edit-type', 'All', 'Article type filter has been reset.');
174 // Test the button is hidden after reset.
175 $this->assertNoField('edit-reset');
177 // Rename the label of the reset button.
178 $view = Views::getView('test_exposed_form_buttons');
181 $exposed_form = $view->display_handler->getOption('exposed_form');
182 $exposed_form['options']['reset_button_label'] = $expected_label = $this->randomMachineName();
183 $exposed_form['options']['reset_button'] = TRUE;
184 $view->display_handler->setOption('exposed_form', $exposed_form);
187 // Look whether the reset button label changed.
188 $this->drupalGet('test_exposed_form_buttons', ['query' => ['type' => 'article']]);
189 $this->assertResponse(200);
191 $this->helperButtonHasLabel('edit-reset', $expected_label);
195 * Tests the exposed block functionality.
197 public function testExposedBlock() {
198 $this->drupalCreateContentType(['type' => 'page']);
199 $view = Views::getView('test_exposed_block');
200 $view->setDisplay('page_1');
201 $block = $this->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-page_1');
202 $this->drupalGet('test_exposed_block');
204 // Test there is an exposed form in a block.
205 $xpath = $this->buildXPathQuery('//div[@id=:id]/form/@id', [':id' => Html::getUniqueId('block-' . $block->id())]);
206 $result = $this->xpath($xpath);
207 $this->assertEquals(1, count($result));
209 // Test there is not an exposed form in the view page content area.
210 $xpath = $this->buildXPathQuery('//div[@class="view-content"]/form/@id', [':id' => Html::getUniqueId('block-' . $block->id())]);
211 $this->assertNoFieldByXpath($xpath, $this->getExpectedExposedFormId($view), 'No exposed form found in views content region.');
213 // Test there is only one views exposed form on the page.
214 $elements = $this->xpath('//form[@id=:id]', [':id' => $this->getExpectedExposedFormId($view)]);
215 $this->assertEqual(count($elements), 1, 'One exposed form block found.');
217 // Test that the correct option is selected after form submission.
218 $this->assertCacheContext('url');
219 $this->assertOptionSelected('edit-type', 'All');
220 foreach (['All', 'article', 'page'] as $argument) {
221 $this->drupalGet('test_exposed_block', ['query' => ['type' => $argument]]);
222 $this->assertCacheContext('url');
223 $this->assertOptionSelected('edit-type', $argument);
228 * Test the input required exposed form type.
230 public function testInputRequired() {
231 $view = View::load('test_exposed_form_buttons');
232 $display = &$view->getDisplay('default');
233 $display['display_options']['exposed_form']['type'] = 'input_required';
236 $this->drupalGet('test_exposed_form_buttons');
237 $this->assertResponse(200);
238 $this->helperButtonHasLabel('edit-submit-test-exposed-form-buttons', t('Apply'));
240 // Ensure that no results are displayed.
241 $rows = $this->xpath("//div[contains(@class, 'views-row')]");
242 $this->assertEqual(count($rows), 0, 'No rows are displayed by default when no input is provided.');
244 $this->drupalGet('test_exposed_form_buttons', ['query' => ['type' => 'article']]);
246 // Ensure that results are displayed.
247 $rows = $this->xpath("//div[contains(@class, 'views-row')]");
248 $this->assertEqual(count($rows), 5, 'All rows are displayed by default when input is provided.');
252 * Test the "on demand text" for the input required exposed form type.
254 public function testTextInputRequired() {
255 $view = Views::getView('test_exposed_form_buttons');
256 $display = &$view->storage->getDisplay('default');
257 $display['display_options']['exposed_form']['type'] = 'input_required';
258 // Set up the "on demand text".
259 // @see https://www.drupal.org/node/535868
260 $on_demand_text = 'Select any filter and click Apply to see results.';
261 $display['display_options']['exposed_form']['options']['text_input_required'] = $on_demand_text;
262 $display['display_options']['exposed_form']['options']['text_input_required_format'] = filter_default_format();
265 // Ensure that the "on demand text" is displayed when no exposed filters are
267 $this->drupalGet('test_exposed_form_buttons');
268 $this->assertText('Select any filter and click Apply to see results.');
270 // Ensure that the "on demand text" is not displayed when an exposed filter
272 $this->drupalGet('test_exposed_form_buttons', ['query' => ['type' => 'article']]);
273 $this->assertNoText($on_demand_text);
277 * Tests exposed forms with exposed sort and items per page.
279 public function testExposedSortAndItemsPerPage() {
280 for ($i = 0; $i < 50; $i++) {
281 $entity = EntityTest::create([]);
285 'languages:language_interface',
286 'entity_test_view_grants',
289 'languages:language_content',
292 $this->drupalGet('test_exposed_form_sort_items_per_page');
293 $this->assertCacheContexts($contexts);
294 $this->assertIds(range(1, 10, 1));
296 $this->drupalGet('test_exposed_form_sort_items_per_page', ['query' => ['sort_order' => 'DESC']]);
297 $this->assertCacheContexts($contexts);
298 $this->assertIds(range(50, 41, 1));
300 $this->drupalGet('test_exposed_form_sort_items_per_page', ['query' => ['sort_order' => 'DESC', 'items_per_page' => 25]]);
301 $this->assertCacheContexts($contexts);
302 $this->assertIds(range(50, 26, 1));
304 $this->drupalGet('test_exposed_form_sort_items_per_page', ['query' => ['sort_order' => 'DESC', 'items_per_page' => 25, 'offset' => 10]]);
305 $this->assertCacheContexts($contexts);
306 $this->assertIds(range(40, 16, 1));
310 * Checks whether the specified ids are the ones displayed in the view output.
316 * TRUE if ids match, FALSE otherwise.
318 protected function assertIds(array $ids) {
319 $elements = $this->cssSelect('div.view-test-exposed-form-sort-items-per-page div.views-row span.field-content');
321 foreach ($elements as $element) {
322 $actual_ids[] = (int) $element->getText();
325 return $this->assertIdentical($ids, $actual_ids);
329 * Returns a views exposed form ID.
331 * @param \Drupal\views\ViewExecutable $view
332 * The view to create an ID for.
337 protected function getExpectedExposedFormId(ViewExecutable $view) {
338 return Html::cleanCssIdentifier('views-exposed-form-' . $view->storage->id() . '-' . $view->current_display);
342 * Tests a view which is rendered after a form with a validation error.
344 public function testFormErrorWithExposedForm() {
345 $this->drupalGet('views_test_data_error_form_page');
346 $this->assertResponse(200);
347 $form = $this->cssSelect('form.views-exposed-form');
348 $this->assertTrue($form, 'The exposed form element was found.');
349 $this->assertRaw(t('Apply'), 'Ensure the exposed form is rendered before submitting the normal form.');
350 $this->assertRaw('<div class="views-row">', 'Views result shown.');
352 $this->drupalPostForm(NULL, [], t('Submit'));
353 $this->assertResponse(200);
354 $form = $this->cssSelect('form.views-exposed-form');
355 $this->assertTrue($form, 'The exposed form element was found.');
356 $this->assertRaw(t('Apply'), 'Ensure the exposed form is rendered after submitting the normal form.');
357 $this->assertRaw('<div class="views-row">', 'Views result shown.');