class AreaTest

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Kernel/Handler/AreaTest.php \Drupal\Tests\views\Kernel\Handler\AreaTest

Tests the plugin base of the area handler.

@group views

Hierarchy

  • class \Drupal\Tests\views\Kernel\Handler\AreaTest

Expanded class hierarchy of AreaTest

See also

\Drupal\views\Plugin\views\area\AreaPluginBase

\Drupal\views_test\Plugin\views\area\TestExample

File

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

Namespace

Drupal\Tests\views\Kernel\Handler
View source
class AreaTest extends ViewsKernelTestBase {
  
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = [
    'test_example_area',
    'test_example_area_access',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE) : void {
    parent::setUp();
    $this->installEntitySchema('node');
  }
  
  /**
   * {@inheritdoc}
   */
  protected function viewsData() {
    $data = parent::viewsData();
    $data['views']['test_example'] = [
      'title' => 'Test Example area',
      'help' => 'A area handler which just exists for tests.',
      'area' => [
        'id' => 'test_example',
      ],
    ];
    return $data;
  }
  
  /**
   * Tests the rendering of an area.
   */
  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');
  }
  
  /**
   * Tests the access for an area.
   */
  public function testAreaAccess() : void {
    // Test with access denied for the area handler.
    $view = Views::getView('test_example_area_access');
    $view->initDisplay();
    $view->initHandlers();
    $handlers = $view->display_handler
      ->getHandlers('empty');
    $this->assertCount(0, $handlers);
    $output = $view->preview();
    $output = (string) \Drupal::service('renderer')->renderRoot($output);
    // The area output should not be present since access was denied.
    $this->assertStringNotContainsString('a custom string', $output);
    $view->destroy();
    // Test with access granted for the area handler.
    $view = Views::getView('test_example_area_access');
    $view->initDisplay();
    $view->display_handler
      ->overrideOption('empty', [
      'test_example' => [
        'field' => 'test_example',
        'id' => 'test_example',
        'table' => 'views',
        'plugin_id' => 'test_example',
        'string' => 'a custom string',
        'custom_access' => TRUE,
      ],
    ]);
    $view->initHandlers();
    $handlers = $view->display_handler
      ->getHandlers('empty');
    $output = $view->preview();
    $output = (string) \Drupal::service('renderer')->renderRoot($output);
    $this->assertStringContainsString('a custom string', $output);
    $this->assertCount(1, $handlers);
  }

}

Members

Title Sort descending Modifiers Object type Summary
AreaTest::$modules protected static property Modules to install.
AreaTest::$testViews public static property Views used by this test.
AreaTest::setUp protected function
AreaTest::testAreaAccess public function Tests the access for an area.
AreaTest::testRenderArea public function Tests the rendering of an area.
AreaTest::viewsData protected function Returns the views data definition.

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