View source
<?php
namespace Drupal\Tests\views_ui\Functional;
use Drupal\views\Entity\View;
class ExposedFormUITest extends UITestBase {
public static $testViews = [
'test_exposed_admin_ui',
];
protected static $modules = [
'node',
'views_ui',
'block',
'taxonomy',
'field_ui',
'datetime',
];
protected $defaultTheme = 'stark';
protected $groupFormUiErrors = [];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
]);
for ($i = 0; $i < 5; $i++) {
$this
->drupalCreateNode();
}
$this->groupFormUiErrors['missing_value'] = 'A value is required if the label for this item is defined.';
$this->groupFormUiErrors['missing_title'] = 'A label is required if the value for this item is defined.';
$this->groupFormUiErrors['missing_title_empty_operator'] = 'A label is required for the specified operator.';
}
public function testExposedAdminUi() {
$edit = [];
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->helperButtonHasLabel('edit-options-expose-button-button', 'Expose filter');
$this
->assertSession()
->fieldValueEquals('edit-options-operator-in', 'in');
$this
->assertSession()
->fieldValueEquals('edit-options-operator-not-in', 'in');
$this
->assertSession()
->checkboxNotChecked('edit-options-value-page');
$this
->assertSession()
->checkboxNotChecked('edit-options-value-article');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm($edit, 'Expose filter');
$this
->helperButtonHasLabel('edit-options-expose-button-button', 'Hide filter');
$this
->assertSession()
->fieldValueEquals('edit-options-operator-in', 'in');
$this
->assertSession()
->fieldValueEquals('edit-options-operator-not-in', 'in');
$this
->assertSession()
->checkboxNotChecked('edit-options-value-page');
$this
->assertSession()
->checkboxNotChecked('edit-options-value-article');
$edit = [];
$edit['options[expose][identifier]'] = '';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('The identifier is required if the filter is exposed.');
$edit = [];
$edit['options[expose][identifier]'] = 'value';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('This identifier is not allowed.');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created');
$this
->helperButtonHasLabel('edit-options-expose-button-button', 'Expose sort');
$this
->assertSession()
->fieldNotExists('edit-options-expose-label');
$this
->assertSession()
->fieldNotExists('Sort field identifier');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm([], 'Hide filter');
$this
->assertSession()
->fieldValueEquals('edit-options-operator-in', 'in');
$this
->assertSession()
->fieldValueEquals('edit-options-operator-not-in', 'in');
$this
->assertSession()
->checkboxNotChecked('edit-options-value-page');
$this
->assertSession()
->checkboxNotChecked('edit-options-value-article');
$edit = [];
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created');
$this
->submitForm($edit, 'Expose sort');
$this
->helperButtonHasLabel('edit-options-expose-button-button', 'Hide sort');
$this
->assertSession()
->fieldValueEquals('edit-options-expose-label', 'Authored on');
$this
->assertSession()
->fieldValueEquals('Sort field identifier', 'created');
$view_id = $this
->randomView()['id'];
$this
->drupalGet("admin/structure/views/nojs/add-handler/{$view_id}/default/sort");
$this
->submitForm([
'name[node_field_data.created]' => 1,
], 'Add and configure sort criteria');
$this
->assertSession()
->fieldValueEquals('options[order]', 'ASC');
$this
->submitForm([
'options[order]' => 'DESC',
], 'Apply');
$this
->drupalGet("admin/structure/views/nojs/handler/{$view_id}/default/sort/created");
$this
->submitForm([], 'Expose sort');
$this
->assertSession()
->fieldValueEquals('options[order]', 'DESC');
$this
->assertSession()
->fieldValueEquals('options[expose][label]', 'Authored on');
$this
->assertSession()
->fieldValueEquals('Sort field identifier', 'created');
$edit = [
'options[expose][label]' => $this
->randomString(),
'options[expose][field_identifier]' => '',
];
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('Sort field identifier field is required.');
$edit['options[expose][field_identifier]'] = 'abc&! ###08.';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('This identifier has illegal characters.');
$edit['options[expose][field_identifier]'] = $this
->randomMachineName() . '_-~.';
$this
->submitForm($edit, 'Apply');
$this
->submitForm([], 'Save');
$display = View::load($view_id)
->getDisplay('default');
$this
->assertTrue($display['display_options']['sorts']['created']['exposed']);
$this
->assertSame([
'label' => $edit['options[expose][label]'],
'field_identifier' => $edit['options[expose][field_identifier]'],
], $display['display_options']['sorts']['created']['expose']);
$this
->assertSame('DESC', $display['display_options']['sorts']['created']['order']);
$this
->drupalGet("admin/structure/views/nojs/handler/{$view_id}/default/sort/created_1");
$this
->submitForm([], 'Expose sort');
$this
->submitForm([
'options[expose][field_identifier]' => $edit['options[expose][field_identifier]'],
], 'Apply');
$this
->assertSession()
->pageTextContains('This identifier is already used by Content: Authored on sort handler.');
}
public function testGroupedFilterAdminUi() {
$edit = [];
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm($edit, 'Expose filter');
$this
->helperButtonHasLabel('edit-options-group-button-button', 'Grouped filters');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm([], 'Grouped filters');
$this
->assertSession()
->fieldNotExists('edit-options-operator-in');
$this
->assertSession()
->fieldNotExists('edit-options-operator-not-in');
$this
->assertSession()
->fieldNotExists('edit-options-value-page');
$this
->assertSession()
->fieldNotExists('edit-options-value-article');
$this
->helperButtonHasLabel('edit-options-group-info-add-group', 'Add another item');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$edit = [];
$edit["options[group_info][group_items][1][title]"] = 'Is Article';
$edit["options[group_info][group_items][1][value][article]"] = 'article';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->addressEquals('admin/structure/views/view/test_exposed_admin_ui/edit/default');
$this
->assertNoGroupedFilterErrors();
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$edit = [];
$edit["options[group_info][group_items][1][title]"] = 'Is Article';
$edit["options[group_info][group_items][1][value][article]"] = 'article';
$edit["options[group_info][group_items][2][title]"] = 'Is Page';
$edit["options[group_info][group_items][2][value][page]"] = 'page';
$edit["options[group_info][group_items][3][title]"] = 'Is Page and Article';
$edit["options[group_info][group_items][3][value][article]"] = 'article';
$edit["options[group_info][group_items][3][value][page]"] = 'page';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->addressEquals('admin/structure/views/view/test_exposed_admin_ui/edit/default');
$this
->assertNoGroupedFilterErrors();
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/body_value');
$edit = [];
$edit["options[group_info][group_items][1][title]"] = 'No body';
$edit["options[group_info][group_items][1][operator]"] = 'empty';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->addressEquals('admin/structure/views/view/test_exposed_admin_ui/edit/default');
$this
->assertNoGroupedFilterErrors();
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_exposed_admin_ui/default/filter');
$this
->submitForm([
'name[node_field_data.nid]' => TRUE,
], 'Add and configure filter criteria');
$this
->submitForm([], 'Expose filter');
$this
->submitForm([], 'Grouped filters');
$edit = [];
$edit['options[group_info][group_items][1][title]'] = 'Testing zero';
$edit['options[group_info][group_items][1][operator]'] = '>';
$edit['options[group_info][group_items][1][value][value]'] = '0';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->addressEquals('admin/structure/views/view/test_exposed_admin_ui/edit/default');
$this
->assertNoGroupedFilterErrors();
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/nid');
$edit['options[group_info][group_items][1][title]'] = 'ID between test';
$edit['options[group_info][group_items][1][operator]'] = 'between';
$edit['options[group_info][group_items][1][value][min]'] = '0';
$edit['options[group_info][group_items][1][value][max]'] = '10';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->addressEquals('admin/structure/views/view/test_exposed_admin_ui/edit/default');
$this
->assertNoGroupedFilterErrors();
}
public function testGroupedFilterAdminUiErrors() {
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/body_value');
$edit = [];
$edit["options[group_info][group_items][1][title]"] = '';
$edit["options[group_info][group_items][1][operator]"] = 'empty';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains($this->groupFormUiErrors['missing_title_empty_operator']);
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm([], 'Expose filter');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm([], 'Grouped filters');
$edit = [];
$edit["options[group_info][group_items][1][title]"] = 'Is Article';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains($this->groupFormUiErrors['missing_value']);
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$edit = [];
$edit["options[group_info][group_items][1][title]"] = '';
$edit["options[group_info][group_items][1][value][article]"] = 'article';
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains($this->groupFormUiErrors['missing_title']);
}
protected function assertNoGroupedFilterErrors($message = '', $group = 'Other') {
foreach ($this->groupFormUiErrors as $error) {
if (empty($message)) {
return $this
->assertSession()
->responseNotContains($error);
}
}
return TRUE;
}
public function testExposedGroupedFilter() {
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm([], 'Expose filter');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
$this
->submitForm([], 'Grouped filters');
$edit = [
'options[group_button][radios][radios]' => 1,
'options[group_info][group_items][1][title]' => '1st',
'options[group_info][group_items][1][value][all]' => 'all',
'options[group_info][group_items][2][title]' => '2nd',
'options[group_info][group_items][2][value][article]' => 'article',
'options[group_info][group_items][3][title]' => '3rd',
'options[group_info][group_items][3][value][page]' => 'page',
];
$this
->submitForm($edit, 'Apply');
$this
->submitForm([], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/structure/views/nojs/add-handler/test_exposed_admin_ui/default/filter');
$this
->submitForm([
'name[node_field_data.status]' => 1,
], 'Add and configure filter criteria');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status');
$this
->submitForm([], 'Expose filter');
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status');
$this
->submitForm([], 'Grouped filters');
$edit = [
'options[group_button][radios][radios]' => 1,
'options[group_info][group_items][1][title]' => 'Any',
'options[group_info][group_items][1][value]' => 'All',
'options[group_info][group_items][2][title]' => 'Published',
'options[group_info][group_items][2][value]' => 1,
'options[group_info][group_items][3][title]' => 'Unpublished',
'options[group_info][group_items][3][value]' => 0,
];
$this
->submitForm($edit, 'Apply');
$this
->submitForm([], 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/status');
$this
->assertSession()
->checkboxChecked('edit-options-group-info-group-items-1-value-all');
$this
->assertSession()
->checkboxChecked('edit-options-group-info-group-items-2-value-1');
$this
->assertSession()
->checkboxChecked('edit-options-group-info-group-items-3-value-0');
}
}