You are here

public function ExposedFormUITest::testExposedAdminUi in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views_ui/tests/src/Functional/ExposedFormUITest.php \Drupal\Tests\views_ui\Functional\ExposedFormUITest::testExposedAdminUi()
  2. 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 67

Class

ExposedFormUITest
Tests exposed forms UI functionality.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testExposedAdminUi() {
  $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
    ->assertFieldById('edit-options-operator-in', 'in', 'Operator In exists');
  $this
    ->assertFieldById('edit-options-operator-not-in', 'not in', 'Operator Not In exists');
  $this
    ->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
  $this
    ->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');

  // Click the Expose filter button.
  $this
    ->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', $edit, t('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
    ->assertFieldById('edit-options-operator-in', 'in', 'Operator In exists');
  $this
    ->assertFieldById('edit-options-operator-not-in', 'not in', 'Operator Not In exists');
  $this
    ->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists');
  $this
    ->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists');

  // Check the validations of the filter handler.
  $edit = [];
  $edit['options[expose][identifier]'] = '';
  $this
    ->drupalPostForm(NULL, $edit, t('Apply'));
  $this
    ->assertText(t('The identifier is required if the filter is exposed.'));
  $edit = [];
  $edit['options[expose][identifier]'] = 'value';
  $this
    ->drupalPostForm(NULL, $edit, t('Apply'));
  $this
    ->assertText(t('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
    ->assertNoFieldById('edit-options-expose-label', '', 'Make sure no label field is shown');

  // Un-expose the filter.
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type');
  $this
    ->drupalPostForm(NULL, [], t('Hide filter'));

  // After Un-exposing the filter, Operator and Value should be shown again.
  $this
    ->assertFieldById('edit-options-operator-in', 'in', 'Operator In exists after hide filter');
  $this
    ->assertFieldById('edit-options-operator-not-in', 'not in', 'Operator Not In exists after hide filter');
  $this
    ->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists after hide filter');
  $this
    ->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists after hide filter');

  // Click the Expose sort button.
  $edit = [];
  $this
    ->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created', $edit, t('Expose sort'));

  // Check the label of the expose button.
  $this
    ->helperButtonHasLabel('edit-options-expose-button-button', 'Hide sort');
  $this
    ->assertFieldById('edit-options-expose-label', 'Authored on', 'Make sure a label field is shown');

  // 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
    ->drupalPostForm(NULL, [
    'name[node_field_data.created]' => 1,
  ], t('Add and configure @handler', [
    '@handler' => t('sort criteria'),
  ]));
  $this
    ->assertFieldByXPath('//input[@name="options[order]" and @checked="checked"]', 'ASC', 'The default order is set.');

  // Change the order and expose the sort.
  $this
    ->drupalPostForm(NULL, [
    'options[order]' => 'DESC',
  ], t('Apply'));
  $this
    ->drupalPostForm("admin/structure/views/nojs/handler/{$view_id}/default/sort/created", [], t('Expose sort'));
  $this
    ->assertFieldByXPath('//input[@name="options[order]" and @checked="checked"]', 'DESC');
  $this
    ->assertFieldByName('options[expose][label]', 'Authored on', 'The default label is set.');

  // Change the label and save the view.
  $edit = [
    'options[expose][label]' => $this
      ->randomString(),
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Apply'));
  $this
    ->drupalPostForm(NULL, [], t('Save'));

  // Check that the values were saved.
  $display = View::load($view_id)
    ->getDisplay('default');
  $this
    ->assertTrue($display['display_options']['sorts']['created']['exposed']);
  $this
    ->assertEqual($display['display_options']['sorts']['created']['expose'], [
    'label' => $edit['options[expose][label]'],
  ]);
  $this
    ->assertEqual($display['display_options']['sorts']['created']['order'], 'DESC');
}