You are here

public function ExposedFormTest::testExposedBlock in Drupal 8

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

Tests the exposed block functionality.

File

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

Class

ExposedFormTest
Tests exposed forms functionality.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testExposedBlock() {
  $this
    ->drupalCreateContentType([
    'type' => 'page',
  ]);
  $view = Views::getView('test_exposed_block');
  $view
    ->setDisplay('page_1');
  $block = $this
    ->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-page_1');

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

  // Test that the block label is found.
  $this
    ->drupalGet('test_exposed_block');
  $this
    ->assertText($view
    ->getTitle(), 'Block title found.');

  // 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
    ->assertRaw('<strong>Custom</strong> titlealert("hacked!");', 'Custom block title found.');

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

  // Test that the label is removed.
  $this
    ->drupalGet('test_exposed_block');
  $this
    ->assertNoRaw('<strong>Custom</strong> titlealert("hacked!");', 'Custom title was not displayed.');
  $this
    ->assertNoText($view
    ->getTitle(), 'Block title was not displayed.');

  // Test there is an exposed form in a block.
  $xpath = $this
    ->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
    ->buildXPathQuery('//div[@class="view-content"]/form/@id', [
    ':id' => Html::getUniqueId('block-' . $block
      ->id()),
  ]);
  $this
    ->assertNoFieldByXpath($xpath, $this
    ->getExpectedExposedFormId($view), 'No exposed form found in views content region.');

  // 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
    ->assertOptionSelected('Content: Type', 'All');
  foreach ([
    'All',
    'article',
    'page',
  ] as $argument) {
    $this
      ->drupalGet('test_exposed_block', [
      'query' => [
        'type' => $argument,
      ],
    ]);
    $this
      ->assertCacheContext('url');
    $this
      ->assertOptionSelected('Content: Type', $argument);
  }
}