View source
<?php
namespace Drupal\Tests\views\Functional\Handler;
use Drupal\Component\Utility\Xss;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Views;
class AreaTest extends ViewTestBase {
public static $testViews = [
'test_example_area',
'test_example_area_access',
];
protected static $modules = [
'node',
'views_ui',
];
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->enableViewsTestModule();
}
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;
}
public function testUI() {
$admin_user = $this
->drupalCreateUser([
'administer views',
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$types = [
'header',
'footer',
'empty',
];
$labels = [];
foreach ($types as $type) {
$edit_path = 'admin/structure/views/nojs/handler/test_example_area/default/' . $type . '/test_example';
$this
->drupalGet($edit_path);
$this
->submitForm([], 'Apply');
$this
->assertSession()
->pageTextContains('Test Example area');
$labels[$type] = $this
->randomMachineName();
$this
->drupalGet($edit_path);
$this
->submitForm([
'options[admin_label]' => $labels[$type],
], 'Apply');
$this
->assertSession()
->pageTextContains($labels[$type]);
$this
->drupalGet($edit_path);
$this
->assertSession()
->fieldExists('options[admin_label]');
if ($type !== 'empty') {
$this
->assertSession()
->fieldExists('options[empty]');
}
}
}
public function testRenderArea() {
$view = Views::getView('test_example_area');
$view
->initHandlers();
$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;
$output = $view
->preview();
$output = $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');
}
public function testAreaAccess() {
$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 = \Drupal::service('renderer')
->renderRoot($output);
$this
->assertStringNotContainsString('a custom string', $output);
$view
->destroy();
$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 = \Drupal::service('renderer')
->renderRoot($output);
$this
->assertStringContainsString('a custom string', $output);
$this
->assertCount(1, $handlers);
}
public function testRenderAreaToken() {
$admin_user = $this
->drupalCreateUser([
'administer views',
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$view = Views::getView('test_example_area');
$view
->initHandlers();
$this
->drupalGet('admin/structure/views/nojs/handler/test_example_area/default/empty/test_example');
$element = $this
->xpath('//ul[@class="global-tokens"]');
$this
->assertNotEmpty($element, 'Token list found on the options form.');
$empty_handler =& $view->empty['test_example'];
$available = $empty_handler
->getAvailableGlobalTokens();
foreach ([
'site',
'view',
] as $type) {
$this
->assertNotEmpty($available[$type]);
$this
->assertIsArray($available[$type]);
foreach ($available[$type] as $token => $info) {
$this
->assertSession()
->pageTextContains("[{$type}:{$token}]");
}
}
$empty_handler->options['string'] = '[site:name]';
$output = $view
->preview();
$output = $this->container
->get('renderer')
->renderRoot($output);
$expected = \Drupal::token()
->replace('[site:name]');
$this
->assertStringContainsString($expected, $output);
}
public function testTitleArea() {
$view = Views::getView('frontpage');
$view
->initDisplay('page_1');
$view->displayHandlers
->get('page_1')
->overrideOption('empty', [
'title' => [
'id' => 'title',
'table' => 'views',
'field' => 'title',
'admin_label' => '',
'empty' => '0',
'title' => 'Overridden title',
'plugin_id' => 'title',
],
]);
$view->storage
->enable()
->save();
$this
->drupalGet('node');
$this
->assertSession()
->pageTextContains('Overridden title');
}
}