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',
52 * Tests the generic UI of a area handler.
54 public function testUI() {
55 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
56 $this->drupalLogin($admin_user);
58 $types = ['header', 'footer', 'empty'];
60 foreach ($types as $type) {
61 $edit_path = 'admin/structure/views/nojs/handler/test_example_area/default/' . $type . '/test_example';
63 // First setup an empty label.
64 $this->drupalPostForm($edit_path, [], t('Apply'));
65 $this->assertText('Test Example area');
67 // Then setup a no empty label.
68 $labels[$type] = $this->randomMachineName();
69 $this->drupalPostForm($edit_path, ['options[admin_label]' => $labels[$type]], t('Apply'));
70 // Make sure that the new label appears on the site.
71 $this->assertText($labels[$type]);
73 // Test that the settings (empty/admin_label) are accessible.
74 $this->drupalGet($edit_path);
75 $this->assertField('options[admin_label]');
76 if ($type !== 'empty') {
77 $this->assertField('options[empty]');
83 * Tests the rendering of an area.
85 public function testRenderArea() {
86 $view = Views::getView('test_example_area');
87 $view->initHandlers();
89 // Insert a random string with XSS injection in the test area plugin.
90 // Ensure that the string is rendered for the header, footer, and empty
91 // text with the markup properly escaped.
92 $header_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
93 $footer_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
94 $empty_string = '<script type="text/javascript">alert("boo");</script><p>' . $this->randomMachineName() . '</p>';
96 $view->header['test_example']->options['string'] = $header_string;
97 $view->header['test_example']->options['empty'] = TRUE;
99 $view->footer['test_example']->options['string'] = $footer_string;
100 $view->footer['test_example']->options['empty'] = TRUE;
102 $view->empty['test_example']->options['string'] = $empty_string;
104 // Check whether the strings exist in the output and are sanitized.
105 $output = $view->preview();
106 $output = $this->container->get('renderer')->renderRoot($output);
107 $this->assertTrue(strpos($output, Xss::filterAdmin($header_string)) !== FALSE, 'Views header exists in the output and is sanitized');
108 $this->assertTrue(strpos($output, Xss::filterAdmin($footer_string)) !== FALSE, 'Views footer exists in the output and is sanitized');
109 $this->assertTrue(strpos($output, Xss::filterAdmin($empty_string)) !== FALSE, 'Views empty exists in the output and is sanitized');
110 $this->assertTrue(strpos($output, '<script') === FALSE, 'Script tags were escaped');
114 * Tests the access for an area.
116 public function testAreaAccess() {
117 // Test with access denied for the area handler.
118 $view = Views::getView('test_example_area_access');
119 $view->initDisplay();
120 $view->initHandlers();
121 $handlers = $view->display_handler->getHandlers('empty');
122 $this->assertEqual(0, count($handlers));
124 $output = $view->preview();
125 $output = \Drupal::service('renderer')->renderRoot($output);
126 // The area output should not be present since access was denied.
127 $this->assertFalse(strpos($output, 'a custom string') !== FALSE);
130 // Test with access granted for the area handler.
131 $view = Views::getView('test_example_area_access');
132 $view->initDisplay();
133 $view->display_handler->overrideOption('empty', [
135 'field' => 'test_example',
136 'id' => 'test_example',
138 'plugin_id' => 'test_example',
139 'string' => 'a custom string',
140 'custom_access' => TRUE,
143 $view->initHandlers();
144 $handlers = $view->display_handler->getHandlers('empty');
146 $output = $view->preview();
147 $output = \Drupal::service('renderer')->renderRoot($output);
148 $this->assertTrue(strpos($output, 'a custom string') !== FALSE);
149 $this->assertEqual(1, count($handlers));
153 * Tests global tokens.
155 public function testRenderAreaToken() {
156 $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
157 $this->drupalLogin($admin_user);
159 $view = Views::getView('test_example_area');
160 $view->initHandlers();
162 $this->drupalGet('admin/structure/views/nojs/handler/test_example_area/default/empty/test_example');
164 // Test that the list is token present.
165 $element = $this->xpath('//ul[@class="global-tokens"]');
166 $this->assertTrue($element, 'Token list found on the options form.');
168 $empty_handler = &$view->empty['test_example'];
170 // Test the list of available tokens.
171 $available = $empty_handler->getAvailableGlobalTokens();
172 foreach (['site', 'view'] as $type) {
173 $this->assertTrue(!empty($available[$type]) && is_array($available[$type]));
174 // Test that each item exists in the list.
175 foreach ($available[$type] as $token => $info) {
176 $this->assertText("[$type:$token]");
180 // Test the rendered output of a token.
181 $empty_handler->options['string'] = '[site:name]';
183 // Test we have the site:name token in the output.
184 $output = $view->preview();
185 $output = $this->container->get('renderer')->renderRoot($output);
186 $expected = \Drupal::token()->replace('[site:name]');
187 $this->assertTrue(strpos($output, $expected) !== FALSE);
191 * Tests overriding the view title using the area title handler.
193 public function testTitleArea() {
194 $view = Views::getView('frontpage');
195 $view->initDisplay('page_1');
197 // Add the title area handler to the empty area.
198 $view->displayHandlers->get('page_1')->overrideOption('empty', [
205 'title' => 'Overridden title',
206 'plugin_id' => 'title',
210 $view->storage->enable()->save();
212 $this->drupalGet('node');
213 $this->assertText('Overridden title', 'Overridden title found.');