3 namespace Drupal\Tests\views\Functional\Handler;
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Views;
10 * Tests the plugin base of the area handler.
13 * @see \Drupal\views\Plugin\views\area\AreaPluginBase
14 * @see \Drupal\views_test\Plugin\views\area\TestExample
16 class AreaTest extends ViewTestBase {
19 * Views used by this test.
23 public static $testViews = ['test_example_area', 'test_example_area_access'];
30 public static $modules = ['node', 'views_ui'];
32 protected function setUp($import_test_views = TRUE) {
33 parent::setUp($import_test_views);
35 $this->enableViewsTestModule();
38 protected function viewsData() {
39 $data = parent::viewsData();
40 $data['views']['test_example'] = [
41 'title' => 'Test Example area',
42 'help' => 'A area handler which just exists for tests.',
44 'id' => 'test_example'
53 * Tests the generic UI of a area handler.
55 public function testUI() {
56 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
57 $this->drupalLogin($admin_user);
59 $types = ['header', 'footer', 'empty'];
61 foreach ($types as $type) {
62 $edit_path = 'admin/structure/views/nojs/handler/test_example_area/default/' . $type . '/test_example';
64 // First setup an empty label.
65 $this->drupalPostForm($edit_path, [], t('Apply'));
66 $this->assertText('Test Example area');
68 // Then setup a no empty label.
69 $labels[$type] = $this->randomMachineName();
70 $this->drupalPostForm($edit_path, ['options[admin_label]' => $labels[$type]], t('Apply'));
71 // Make sure that the new label appears on the site.
72 $this->assertText($labels[$type]);
74 // Test that the settings (empty/admin_label) are accessible.
75 $this->drupalGet($edit_path);
76 $this->assertField('options[admin_label]');
77 if ($type !== 'empty') {
78 $this->assertField('options[empty]');
84 * Tests the rendering of an area.
86 public function testRenderArea() {
87 $view = Views::getView('test_example_area');
88 $view->initHandlers();
90 // Insert a random string with XSS injection in the test area plugin.
91 // Ensure that the string is rendered for the header, footer, and empty
92 // text with the markup properly escaped.
93 $header_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
94 $footer_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
95 $empty_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
97 $view->header['test_example']->options['string'] = $header_string;
98 $view->header['test_example']->options['empty'] = TRUE;
100 $view->footer['test_example']->options['string'] = $footer_string;
101 $view->footer['test_example']->options['empty'] = TRUE;
103 $view->empty['test_example']->options['string'] = $empty_string;
105 // Check whether the strings exist in the output and are sanitized.
106 $output = $view->preview();
107 $output = $this->container->get('renderer')->renderRoot($output);
108 $this->assertTrue(strpos($output, Xss::filterAdmin($header_string)) !== FALSE, 'Views header exists in the output and is sanitized');
109 $this->assertTrue(strpos($output, Xss::filterAdmin($footer_string)) !== FALSE, 'Views footer exists in the output and is sanitized');
110 $this->assertTrue(strpos($output, Xss::filterAdmin($empty_string)) !== FALSE, 'Views empty exists in the output and is sanitized');
111 $this->assertTrue(strpos($output, '<script') === FALSE, 'Script tags were escaped');
115 * Tests the access for an area.
117 public function testAreaAccess() {
118 // Test with access denied for the area handler.
119 $view = Views::getView('test_example_area_access');
120 $view->initDisplay();
121 $view->initHandlers();
122 $handlers = $view->display_handler->getHandlers('empty');
123 $this->assertEqual(0, count($handlers));
125 $output = $view->preview();
126 $output = \Drupal::service('renderer')->renderRoot($output);
127 // The area output should not be present since access was denied.
128 $this->assertFalse(strpos($output, 'a custom string') !== FALSE);
131 // Test with access granted for the area handler.
132 $view = Views::getView('test_example_area_access');
133 $view->initDisplay();
134 $view->display_handler->overrideOption('empty', [
136 'field' => 'test_example',
137 'id' => 'test_example',
139 'plugin_id' => 'test_example',
140 'string' => 'a custom string',
141 'custom_access' => TRUE,
144 $view->initHandlers();
145 $handlers = $view->display_handler->getHandlers('empty');
147 $output = $view->preview();
148 $output = \Drupal::service('renderer')->renderRoot($output);
149 $this->assertTrue(strpos($output, 'a custom string') !== FALSE);
150 $this->assertEqual(1, count($handlers));
154 * Tests global tokens.
156 public function testRenderAreaToken() {
157 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
158 $this->drupalLogin($admin_user);
160 $view = Views::getView('test_example_area');
161 $view->initHandlers();
163 $this->drupalGet('admin/structure/views/nojs/handler/test_example_area/default/empty/test_example');
165 // Test that the list is token present.
166 $element = $this->xpath('//ul[@class="global-tokens"]');
167 $this->assertTrue($element, 'Token list found on the options form.');
169 $empty_handler = &$view->empty['test_example'];
171 // Test the list of available tokens.
172 $available = $empty_handler->getAvailableGlobalTokens();
173 foreach (['site', 'view'] as $type) {
174 $this->assertTrue(!empty($available[$type]) && is_array($available[$type]));
175 // Test that each item exists in the list.
176 foreach ($available[$type] as $token => $info) {
177 $this->assertText("[$type:$token]");
181 // Test the rendered output of a token.
182 $empty_handler->options['string'] = '[site:name]';
184 // Test we have the site:name token in the output.
185 $output = $view->preview();
186 $output = $this->container->get('renderer')->renderRoot($output);
187 $expected = \Drupal::token()->replace('[site:name]');
188 $this->assertTrue(strpos($output, $expected) !== FALSE);
192 * Tests overriding the view title using the area title handler.
194 public function testTitleArea() {
195 $view = Views::getView('frontpage');
196 $view->initDisplay('page_1');
198 // Add the title area handler to the empty area.
199 $view->displayHandlers->get('page_1')->overrideOption('empty', [
206 'title' => 'Overridden title',
207 'plugin_id' => 'title',
211 $view->storage->enable()->save();
213 $this->drupalGet('node');
214 $this->assertText('Overridden title', 'Overridden title found.');