function ExposedFormUITest::testExposedAdminUi

Same name in other branches
  1. 9 core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php \Drupal\Tests\views_ui\Functional\ExposedFormUITest::testExposedAdminUi()
  2. 8.9.x core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php \Drupal\Tests\views_ui\Functional\ExposedFormUITest::testExposedAdminUi()
  3. 10 core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php \Drupal\Tests\views_ui\Functional\ExposedFormUITest::testExposedAdminUi()

Tests the admin interface of exposed filter and sort items.

File

core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php, line 72

Class

ExposedFormUITest
Tests exposed forms UI functionality.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testExposedAdminUi() : void {
    $edit = [];
    $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
    // Be sure that the button is called exposed.
    $this->helperButtonHasLabel('edit-options-expose-button-button', 'Expose filter');
    // The first time the filter UI is displayed, the operator and the
    // value forms should be shown.
    $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');
    // Click the Expose filter button.
    $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
    $this->submitForm($edit, 'Expose filter');
    // Check the label of the expose button.
    $this->helperButtonHasLabel('edit-options-expose-button-button', 'Hide filter');
    // After exposing the filter, Operator and Value should be still here.
    $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');
    // Check the validations of the filter handler.
    $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.');
    // Now check the sort criteria.
    $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');
    // Un-expose the filter.
    $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
    $this->submitForm([], 'Hide filter');
    // After Un-exposing the filter, Operator and Value should be shown again.
    $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');
    // Click the Expose sort button.
    $edit = [];
    $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created');
    $this->submitForm($edit, 'Expose sort');
    // Check the label of the expose button.
    $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');
    // Test adding a new exposed sort criteria.
    $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');
    // Change the order and expose the sort.
    $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');
    // Change the label and try with an empty identifier.
    $edit = [
        'options[expose][label]' => $this->randomString(),
        'options[expose][field_identifier]' => '',
    ];
    $this->submitForm($edit, 'Apply');
    $this->assertSession()
        ->pageTextContains('Sort field identifier field is required.');
    // Try with an invalid identifiers.
    $edit['options[expose][field_identifier]'] = 'abc&! ###08.';
    $this->submitForm($edit, 'Apply');
    $this->assertSession()
        ->pageTextContains('This identifier has illegal characters.');
    $edit['options[expose][field_identifier]'] = '^abcde';
    $this->submitForm($edit, 'Apply');
    $this->assertSession()
        ->pageTextContains('This identifier has illegal characters.');
    // Use a valid identifier.
    $edit['options[expose][field_identifier]'] = $this->randomMachineName() . '_-~.';
    $this->submitForm($edit, 'Apply');
    $this->submitForm([], 'Save');
    // Check that the values were saved.
    $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']);
    // Test the identifier uniqueness.
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.