Same name and namespace in other branches
  1. 8.9.x core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormTest::testExposedIdentifier()
  2. 9 core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormTest::testExposedIdentifier()

Tests the exposed form with a non-standard identifier.

File

core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php, line 110

Class

ExposedFormTest
Tests exposed forms functionality.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testExposedIdentifier() {

  // Alter the identifier of the filter to a random string.
  $view = Views::getView('test_exposed_form_buttons');
  $view
    ->setDisplay();
  $identifier = 'new_identifier';
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'type' => [
      'exposed' => TRUE,
      'field' => 'type',
      'id' => 'type',
      'table' => 'node_field_data',
      'plugin_id' => 'in_operator',
      'entity_type' => 'node',
      'entity_field' => 'type',
      'expose' => [
        'identifier' => $identifier,
        'label' => 'Content: Type',
        'operator_id' => 'type_op',
        'reduce' => FALSE,
        'description' => 'Exposed overridden description',
      ],
    ],
  ]);
  $view
    ->save();
  $this
    ->drupalGet('test_exposed_form_buttons', [
    'query' => [
      $identifier => 'article',
    ],
  ]);
  $this
    ->assertSession()
    ->fieldValueEquals(Html::getId('edit-' . $identifier), 'article');

  // Alter the identifier of the filter to a random string containing
  // restricted characters.
  $view = Views::getView('test_exposed_form_buttons');
  $view
    ->setDisplay();
  $identifier = 'bad identifier';
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'type' => [
      'exposed' => TRUE,
      'field' => 'type',
      'id' => 'type',
      'table' => 'node_field_data',
      'plugin_id' => 'in_operator',
      'entity_type' => 'node',
      'entity_field' => 'type',
      'expose' => [
        'identifier' => $identifier,
        'label' => 'Content: Type',
        'operator_id' => 'type_op',
        'reduce' => FALSE,
        'description' => 'Exposed overridden description',
      ],
    ],
  ]);
  $this
    ->executeView($view);
  $errors = $view
    ->validate();
  $expected = [
    'default' => [
      'This identifier has illegal characters.',
    ],
    'page_1' => [
      'This identifier has illegal characters.',
    ],
  ];
  $this
    ->assertEquals($expected, $errors);
  foreach (FilterPluginBase::RESTRICTED_IDENTIFIERS as $restricted_identifier) {
    $view = Views::getView('test_exposed_form_buttons');
    $view
      ->setDisplay();
    $view->displayHandlers
      ->get('default')
      ->overrideOption('filters', [
      'type' => [
        'exposed' => TRUE,
        'field' => 'type',
        'id' => 'type',
        'table' => 'node_field_data',
        'plugin_id' => 'in_operator',
        'entity_type' => 'node',
        'entity_field' => 'type',
        'expose' => [
          'identifier' => $restricted_identifier,
          'label' => 'Content: Type',
          'operator_id' => 'type_op',
          'reduce' => FALSE,
          'description' => 'Exposed overridden description',
        ],
      ],
    ]);
    $this
      ->executeView($view);
    $errors = $view
      ->validate();
    $expected = [
      'default' => [
        'This identifier is not allowed.',
      ],
      'page_1' => [
        'This identifier is not allowed.',
      ],
    ];
    $this
      ->assertEquals($expected, $errors);
  }
}