1827319bbd05d48a6181cd788ed8d143d09d33d4
[yaffs-website] / web / core / modules / views / tests / src / Functional / Handler / AreaTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Handler;
4
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Views;
8
9 /**
10  * Tests the plugin base of the area handler.
11  *
12  * @group views
13  * @see \Drupal\views\Plugin\views\area\AreaPluginBase
14  * @see \Drupal\views_test\Plugin\views\area\TestExample
15  */
16 class AreaTest extends ViewTestBase {
17
18   /**
19    * Views used by this test.
20    *
21    * @var array
22    */
23   public static $testViews = ['test_example_area', 'test_example_area_access'];
24
25   /**
26    * Modules to enable.
27    *
28    * @var array
29    */
30   public static $modules = ['node', 'views_ui'];
31
32   protected function setUp($import_test_views = TRUE) {
33     parent::setUp($import_test_views);
34
35     $this->enableViewsTestModule();
36   }
37
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.',
43       'area' => [
44         'id' => 'test_example',
45       ],
46     ];
47
48     return $data;
49   }
50
51   /**
52    * Tests the generic UI of a area handler.
53    */
54   public function testUI() {
55     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
56     $this->drupalLogin($admin_user);
57
58     $types = ['header', 'footer', 'empty'];
59     $labels = [];
60     foreach ($types as $type) {
61       $edit_path = 'admin/structure/views/nojs/handler/test_example_area/default/' . $type . '/test_example';
62
63       // First setup an empty label.
64       $this->drupalPostForm($edit_path, [], t('Apply'));
65       $this->assertText('Test Example area');
66
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]);
72
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]');
78       }
79     }
80   }
81
82   /**
83    * Tests the rendering of an area.
84    */
85   public function testRenderArea() {
86     $view = Views::getView('test_example_area');
87     $view->initHandlers();
88
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>';
95
96     $view->header['test_example']->options['string'] = $header_string;
97     $view->header['test_example']->options['empty'] = TRUE;
98
99     $view->footer['test_example']->options['string'] = $footer_string;
100     $view->footer['test_example']->options['empty'] = TRUE;
101
102     $view->empty['test_example']->options['string'] = $empty_string;
103
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');
111   }
112
113   /**
114    * Tests the access for an area.
115    */
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));
123
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);
128     $view->destroy();
129
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', [
134       'test_example' => [
135         'field' => 'test_example',
136         'id' => 'test_example',
137         'table' => 'views',
138         'plugin_id' => 'test_example',
139         'string' => 'a custom string',
140         'custom_access' => TRUE,
141       ],
142     ]);
143     $view->initHandlers();
144     $handlers = $view->display_handler->getHandlers('empty');
145
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));
150   }
151
152   /**
153    * Tests global tokens.
154    */
155   public function testRenderAreaToken() {
156     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
157     $this->drupalLogin($admin_user);
158
159     $view = Views::getView('test_example_area');
160     $view->initHandlers();
161
162     $this->drupalGet('admin/structure/views/nojs/handler/test_example_area/default/empty/test_example');
163
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.');
167
168     $empty_handler = &$view->empty['test_example'];
169
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]");
177       }
178     }
179
180     // Test the rendered output of a token.
181     $empty_handler->options['string'] = '[site:name]';
182
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);
188   }
189
190   /**
191    * Tests overriding the view title using the area title handler.
192    */
193   public function testTitleArea() {
194     $view = Views::getView('frontpage');
195     $view->initDisplay('page_1');
196
197     // Add the title area handler to the empty area.
198     $view->displayHandlers->get('page_1')->overrideOption('empty', [
199       'title' => [
200         'id' => 'title',
201         'table' => 'views',
202         'field' => 'title',
203         'admin_label' => '',
204         'empty' => '0',
205         'title' => 'Overridden title',
206         'plugin_id' => 'title',
207       ],
208     ]);
209
210     $view->storage->enable()->save();
211
212     $this->drupalGet('node');
213     $this->assertText('Overridden title', 'Overridden title found.');
214   }
215
216 }