function ExposedFormTest::testExposedSortAndItemsPerPage

Tests exposed forms with exposed sort and items per page.

File

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

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));
  // Change the label to something with special characters.
  $view = Views::getView('test_exposed_form_sort_items_per_page');
  $view->setDisplay();
  $sorts = $view->display_handler
    ->getOption('sorts');
  $sorts['id']['expose']['label'] = $expected_label = "<script>alert('unsafe&dangerous');</script>";
  $view->display_handler
    ->setOption('sorts', $sorts);
  $view->save();
  $this->drupalGet('test_exposed_form_sort_items_per_page');
  $options = $this->xpath('//select[@id=:id]/option', [
    ':id' => 'edit-sort-by',
  ]);
  $this->assertCount(1, $options);
  $this->assertSession()
    ->optionExists('edit-sort-by', $expected_label);
  $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->assertRaw($escape_1);
  // But no double-escaped string.
  $this->assertNoRaw($escape_2);
  // And not the raw label, either.
  $this->assertNoRaw($expected_label);
}

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