function ExposedFormTest::testExposedBlock

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()
  2. 8.9.x core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormTest::testExposedBlock()
  3. 10 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 252

Class

ExposedFormTest
Tests exposed forms functionality.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testExposedBlock($display) : void {
    $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 content 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.
    $this->assertSession()
        ->elementsCount('xpath', '//div[@id="' . Html::getUniqueId('block-' . $block->id()) . '"]/form/@id', 1);
    // 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.
    $xpath = '//form[@id="' . $this->getExpectedExposedFormId($view) . '"]';
    $this->assertSession()
        ->elementsCount('xpath', $xpath, 1);
    $element = $this->assertSession()
        ->elementExists('xpath', $xpath);
    // 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) {
        $element->find('css', 'select')
            ->selectOption($argument);
        $element->findButton('Apply')
            ->click();
        $this->assertCacheContext('url');
        $this->assertTrue($this->assertSession()
            ->optionExists('Content: Type', $argument)
            ->isSelected());
        $this->assertNodesExist($bundles);
    }
    $element->findButton('Reset')
        ->click();
    $this->assertNodesExist($arguments['All']);
}

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