You are here

public function AreaTest::testRenderArea in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/AreaTest.php \Drupal\views\Tests\Handler\AreaTest::testRenderArea()

Tests the rendering of an area.

File

core/modules/views/src/Tests/Handler/AreaTest.php, line 90
Contains \Drupal\views\Tests\Handler\AreaTest.

Class

AreaTest
Tests the plugin base of the area handler.

Namespace

Drupal\views\Tests\Handler

Code

public function testRenderArea() {
  $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 = $this->container
    ->get('renderer')
    ->renderRoot($output);
  $this
    ->assertTrue(strpos($output, Xss::filterAdmin($header_string)) !== FALSE, 'Views header exists in the output and is sanitized');
  $this
    ->assertTrue(strpos($output, Xss::filterAdmin($footer_string)) !== FALSE, 'Views footer exists in the output and is sanitized');
  $this
    ->assertTrue(strpos($output, Xss::filterAdmin($empty_string)) !== FALSE, 'Views empty exists in the output and is sanitized');
  $this
    ->assertTrue(strpos($output, '<script') === FALSE, 'Script tags were escaped');
}