d631373d23ef00921e9823ad6e7d147c2a477e2e
[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   /**
53    * Tests the generic UI of a area handler.
54    */
55   public function testUI() {
56     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
57     $this->drupalLogin($admin_user);
58
59     $types = ['header', 'footer', 'empty'];
60     $labels = [];
61     foreach ($types as $type) {
62       $edit_path = 'admin/structure/views/nojs/handler/test_example_area/default/' . $type . '/test_example';
63
64       // First setup an empty label.
65       $this->drupalPostForm($edit_path, [], t('Apply'));
66       $this->assertText('Test Example area');
67
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]);
73
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]');
79       }
80     }
81   }
82
83   /**
84    * Tests the rendering of an area.
85    */
86   public function testRenderArea() {
87     $view = Views::getView('test_example_area');
88     $view->initHandlers();
89
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>';
96
97     $view->header['test_example']->options['string'] = $header_string;
98     $view->header['test_example']->options['empty'] = TRUE;
99
100     $view->footer['test_example']->options['string'] = $footer_string;
101     $view->footer['test_example']->options['empty'] = TRUE;
102
103     $view->empty['test_example']->options['string'] = $empty_string;
104
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');
112   }
113
114   /**
115    * Tests the access for an area.
116    */
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));
124
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);
129     $view->destroy();
130
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', [
135       'test_example' => [
136         'field' => 'test_example',
137         'id' => 'test_example',
138         'table' => 'views',
139         'plugin_id' => 'test_example',
140         'string' => 'a custom string',
141         'custom_access' => TRUE,
142       ],
143     ]);
144     $view->initHandlers();
145     $handlers = $view->display_handler->getHandlers('empty');
146
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));
151   }
152
153   /**
154    * Tests global tokens.
155    */
156   public function testRenderAreaToken() {
157     $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']);
158     $this->drupalLogin($admin_user);
159
160     $view = Views::getView('test_example_area');
161     $view->initHandlers();
162
163     $this->drupalGet('admin/structure/views/nojs/handler/test_example_area/default/empty/test_example');
164
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.');
168
169     $empty_handler = &$view->empty['test_example'];
170
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]");
178       }
179     }
180
181     // Test the rendered output of a token.
182     $empty_handler->options['string'] = '[site:name]';
183
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);
189   }
190
191   /**
192    * Tests overriding the view title using the area title handler.
193    */
194   public function testTitleArea() {
195     $view = Views::getView('frontpage');
196     $view->initDisplay('page_1');
197
198     // Add the title area handler to the empty area.
199     $view->displayHandlers->get('page_1')->overrideOption('empty', [
200       'title' => [
201         'id' => 'title',
202         'table' => 'views',
203         'field' => 'title',
204         'admin_label' => '',
205         'empty' => '0',
206         'title' => 'Overridden title',
207         'plugin_id' => 'title',
208       ],
209     ]);
210
211     $view->storage->enable()->save();
212
213     $this->drupalGet('node');
214     $this->assertText('Overridden title', 'Overridden title found.');
215   }
216
217 }