function AreaTest::testRenderArea

Same name in other branches
  1. 9 core/modules/views/tests/src/Functional/Handler/AreaTest.php \Drupal\Tests\views\Functional\Handler\AreaTest::testRenderArea()
  2. 8.9.x core/modules/views/tests/src/Functional/Handler/AreaTest.php \Drupal\Tests\views\Functional\Handler\AreaTest::testRenderArea()
  3. 10 core/modules/views/tests/src/Kernel/Handler/AreaTest.php \Drupal\Tests\views\Kernel\Handler\AreaTest::testRenderArea()

Tests the rendering of an area.

File

core/modules/views/tests/src/Kernel/Handler/AreaTest.php, line 59

Class

AreaTest
Tests the plugin base of the area handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRenderArea() : void {
    $view = Views::getView('test_example_area');
    $view->initHandlers();
    // Insert a random string with XSS injection in the test area plugin.
    // Ensure that the string is rendered for the header, footer, and empty
    // text with the markup properly escaped.
    $header_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
    $footer_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
    $empty_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
    $view->header['test_example']->options['string'] = $header_string;
    $view->header['test_example']->options['empty'] = TRUE;
    $view->footer['test_example']->options['string'] = $footer_string;
    $view->footer['test_example']->options['empty'] = TRUE;
    $view->empty['test_example']->options['string'] = $empty_string;
    // Check whether the strings exist in the output and are sanitized.
    $output = $view->preview();
    $output = (string) $this->container
        ->get('renderer')
        ->renderRoot($output);
    $this->assertStringContainsString(Xss::filterAdmin($header_string), $output, 'Views header exists in the output and is sanitized');
    $this->assertStringContainsString(Xss::filterAdmin($footer_string), $output, 'Views footer exists in the output and is sanitized');
    $this->assertStringContainsString(Xss::filterAdmin($empty_string), $output, 'Views empty exists in the output and is sanitized');
    $this->assertStringNotContainsString('<script', $output, 'Script tags were escaped');
}

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