You are here

public function ExposedFormTest::testExposedSortAndItemsPerPage in Drupal 10

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

Tests exposed forms with exposed sort and items per page.

File

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

Class

ExposedFormTest
Tests exposed forms functionality.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testExposedSortAndItemsPerPage() {
  for ($i = 0; $i < 50; $i++) {
    $entity = EntityTest::create([]);
    $entity
      ->save();
  }
  $contexts = [
    'languages:language_interface',
    'entity_test_view_grants',
    'theme',
    'url.query_args',
    'languages:language_content',
  ];
  $this
    ->drupalGet('test_exposed_form_sort_items_per_page');
  $this
    ->assertCacheContexts($contexts);
  $this
    ->assertIds(range(1, 10, 1));
  $this
    ->drupalGet('test_exposed_form_sort_items_per_page', [
    'query' => [
      'sort_order' => 'DESC',
    ],
  ]);
  $this
    ->assertCacheContexts($contexts);
  $this
    ->assertIds(range(50, 41, 1));
  $this
    ->drupalGet('test_exposed_form_sort_items_per_page', [
    'query' => [
      'sort_order' => 'DESC',
      'items_per_page' => 25,
    ],
  ]);
  $this
    ->assertCacheContexts($contexts);
  $this
    ->assertIds(range(50, 26, 1));
  $this
    ->drupalGet('test_exposed_form_sort_items_per_page', [
    'query' => [
      'sort_order' => 'DESC',
      'items_per_page' => 25,
      'offset' => 10,
    ],
  ]);
  $this
    ->assertCacheContexts($contexts);
  $this
    ->assertIds(range(40, 16, 1));
  $view = Views::getView('test_exposed_form_sort_items_per_page');
  $view
    ->setDisplay();
  $sorts = $view->display_handler
    ->getOption('sorts');

  // Change the label to something with special characters.
  $sorts['id']['expose']['label'] = $expected_label = "<script>alert('unsafe&dangerous');</script>";

  // Use a custom sort field identifier.
  $sorts['id']['expose']['field_identifier'] = $field_identifier = $this
    ->randomMachineName() . '-_.~';
  $view->display_handler
    ->setOption('sorts', $sorts);
  $view
    ->save();

  // Test label escaping.
  $this
    ->drupalGet('test_exposed_form_sort_items_per_page');
  $options = $this
    ->assertSession()
    ->selectExists('edit-sort-by')
    ->findAll('css', 'option');
  $this
    ->assertCount(1, $options);

  // Check option existence by option label.
  $this
    ->assertSession()
    ->optionExists('Sort by', $expected_label);

  // Check option existence by option value.
  $this
    ->assertSession()
    ->optionExists('Sort by', $field_identifier);
  $escape_1 = Html::escape($expected_label);
  $escape_2 = Html::escape($escape_1);

  // Make sure we see the single-escaped string in the raw output.
  $this
    ->assertSession()
    ->responseContains($escape_1);

  // But no double-escaped string.
  $this
    ->assertSession()
    ->responseNotContains($escape_2);

  // And not the raw label, either.
  $this
    ->assertSession()
    ->responseNotContains($expected_label);

  // Check that the custom field identifier is used in the URL query string.
  $this
    ->submitForm([
    'sort_order' => 'DESC',
  ], 'Apply');
  $this
    ->assertCacheContexts($contexts);
  $this
    ->assertIds(range(50, 41));
  $url = $this
    ->getSession()
    ->getCurrentUrl();
  $this
    ->assertStringContainsString('sort_by=' . urlencode($field_identifier), $url);
}