You are here

public function ExposedFormTest::testExposedBlock 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::testExposedBlock()
  2. 9 core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormTest::testExposedBlock()

Tests the exposed block functionality.

@dataProvider providerTestExposedBlock

File

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

Class

ExposedFormTest
Tests exposed forms functionality.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testExposedBlock($display) {
  $view = Views::getView('test_exposed_block');
  $view
    ->setDisplay($display);
  $block = $this
    ->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-' . $display);

  // Set label to display on the exposed filter form block.
  $block
    ->getPlugin()
    ->setConfigurationValue('label_display', TRUE);
  $block
    ->save();

  // Assert that the only two occurrences of `$view->getTitle()` are the title
  // and h2 tags.
  $this
    ->drupalGet('test_exposed_block');
  $this
    ->assertSession()
    ->elementContains('css', 'title', $view
    ->getTitle());
  $this
    ->assertSession()
    ->elementExists('xpath', '//h2[text()="' . $view
    ->getTitle() . '"]');
  $this
    ->assertSession()
    ->pageTextMatchesCount(2, '/' . $view
    ->getTitle() . '/');

  // Set a custom label on the exposed filter form block.
  $block
    ->getPlugin()
    ->setConfigurationValue('views_label', '<strong>Custom</strong> title<script>alert("hacked!");</script>');
  $block
    ->save();

  // Test that the custom block label is found.
  $this
    ->drupalGet('test_exposed_block');
  $this
    ->assertSession()
    ->responseContains('<strong>Custom</strong> titlealert("hacked!");');

  // Set label to hidden on the exposed filter form block.
  $block
    ->getPlugin()
    ->setConfigurationValue('label_display', FALSE);
  $block
    ->save();

  // Test that the label is removed.
  // Assert that the only occurrence of `$view->getTitle()` is the title tag
  // now that label has been removed.
  $this
    ->drupalGet('test_exposed_block');
  $this
    ->assertSession()
    ->responseNotContains('<strong>Custom</strong> titlealert("hacked!");');
  $this
    ->assertSession()
    ->elementContains('css', 'title', $view
    ->getTitle());
  $this
    ->assertSession()
    ->pageTextMatchesCount(1, '/' . $view
    ->getTitle() . '/');

  // Test there is an exposed form in a block.
  $xpath = $this
    ->assertSession()
    ->buildXPathQuery('//div[@id=:id]/form/@id', [
    ':id' => Html::getUniqueId('block-' . $block
      ->id()),
  ]);
  $result = $this
    ->xpath($xpath);
  $this
    ->assertCount(1, $result);

  // Test there is not an exposed form in the view page content area.
  $xpath = $this
    ->assertSession()
    ->buildXPathQuery('//div[@class="view-content"]/form/@id', [
    ':id' => Html::getUniqueId('block-' . $block
      ->id()),
  ]);
  $this
    ->assertSession()
    ->elementNotExists('xpath', $xpath);

  // Test there is only one views exposed form on the page.
  $elements = $this
    ->xpath('//form[@id=:id]', [
    ':id' => $this
      ->getExpectedExposedFormId($view),
  ]);
  $this
    ->assertCount(1, $elements, 'One exposed form block found.');

  // Test that the correct option is selected after form submission.
  $this
    ->assertCacheContext('url');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('Content: Type', 'All')
    ->isSelected());
  $arguments = [
    'All' => [
      'article',
      'page',
    ],
    'article' => [
      'article',
    ],
    'page' => [
      'page',
    ],
  ];
  foreach ($arguments as $argument => $bundles) {
    $elements[0]
      ->find('css', 'select')
      ->selectOption($argument);
    $elements[0]
      ->findButton('Apply')
      ->click();
    $this
      ->assertCacheContext('url');
    $this
      ->assertTrue($this
      ->assertSession()
      ->optionExists('Content: Type', $argument)
      ->isSelected());
    $this
      ->assertNodesExist($bundles);
  }
  $elements[0]
    ->findButton('Reset')
    ->click();
  $this
    ->assertNodesExist($arguments['All']);
}