Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\views\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\Tests\CommentTestTrait;
|
Chris@0
|
6 use Drupal\Component\Utility\Xss;
|
Chris@0
|
7 use Drupal\node\Entity\NodeType;
|
Chris@0
|
8 use Drupal\views\Entity\View;
|
Chris@0
|
9 use Drupal\views\Views;
|
Chris@0
|
10 use Drupal\views\ViewExecutable;
|
Chris@0
|
11 use Drupal\views\ViewExecutableFactory;
|
Chris@0
|
12 use Drupal\views\DisplayPluginCollection;
|
Chris@0
|
13 use Drupal\views\Plugin\views\display\DefaultDisplay;
|
Chris@0
|
14 use Drupal\views\Plugin\views\display\Page;
|
Chris@0
|
15 use Drupal\views\Plugin\views\style\DefaultStyle;
|
Chris@0
|
16 use Drupal\views\Plugin\views\style\Grid;
|
Chris@0
|
17 use Drupal\views\Plugin\views\row\Fields;
|
Chris@0
|
18 use Drupal\views\Plugin\views\query\Sql;
|
Chris@0
|
19 use Drupal\views\Plugin\views\pager\PagerPluginBase;
|
Chris@0
|
20 use Drupal\views\Plugin\views\query\QueryPluginBase;
|
Chris@0
|
21 use Drupal\views_test_data\Plugin\views\display\DisplayTest;
|
Chris@0
|
22 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Tests the ViewExecutable class.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @group views
|
Chris@0
|
28 * @see \Drupal\views\ViewExecutable
|
Chris@0
|
29 */
|
Chris@0
|
30 class ViewExecutableTest extends ViewsKernelTestBase {
|
Chris@0
|
31
|
Chris@0
|
32 use CommentTestTrait;
|
Chris@0
|
33
|
Chris@0
|
34 public static $modules = ['system', 'node', 'comment', 'user', 'filter', 'field', 'text'];
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Views used by this test.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var array
|
Chris@0
|
40 */
|
Chris@16
|
41 public static $testViews = ['test_destroy', 'test_executable_displays', 'test_argument_dependency'];
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Properties that should be stored in the configuration.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @var array
|
Chris@0
|
47 */
|
Chris@0
|
48 protected $configProperties = [
|
Chris@0
|
49 'disabled',
|
Chris@0
|
50 'name',
|
Chris@0
|
51 'description',
|
Chris@0
|
52 'tag',
|
Chris@0
|
53 'base_table',
|
Chris@0
|
54 'label',
|
Chris@0
|
55 'core',
|
Chris@0
|
56 'display',
|
Chris@0
|
57 ];
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Properties that should be stored in the executable.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @var array
|
Chris@0
|
63 */
|
Chris@0
|
64 protected $executableProperties = [
|
Chris@0
|
65 'storage',
|
Chris@0
|
66 'built',
|
Chris@0
|
67 'executed',
|
Chris@0
|
68 'args',
|
Chris@0
|
69 'build_info',
|
Chris@0
|
70 'result',
|
Chris@0
|
71 'attachment_before',
|
Chris@0
|
72 'attachment_after',
|
Chris@0
|
73 'exposed_data',
|
Chris@0
|
74 'exposed_raw_input',
|
Chris@0
|
75 'old_view',
|
Chris@0
|
76 'parent_views',
|
Chris@0
|
77 ];
|
Chris@0
|
78
|
Chris@0
|
79 protected function setUpFixtures() {
|
Chris@0
|
80 $this->installEntitySchema('user');
|
Chris@0
|
81 $this->installEntitySchema('node');
|
Chris@0
|
82 $this->installEntitySchema('comment');
|
Chris@0
|
83 $this->installSchema('comment', ['comment_entity_statistics']);
|
Chris@0
|
84 $this->installConfig(['system', 'field', 'node', 'comment']);
|
Chris@0
|
85
|
Chris@0
|
86 NodeType::create([
|
Chris@0
|
87 'type' => 'page',
|
Chris@0
|
88 'name' => 'Page',
|
Chris@0
|
89 ])->save();
|
Chris@0
|
90 $this->addDefaultCommentField('node', 'page');
|
Chris@0
|
91 parent::setUpFixtures();
|
Chris@0
|
92
|
Chris@0
|
93 $this->installConfig(['filter']);
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * Tests the views.executable container service.
|
Chris@0
|
98 */
|
Chris@0
|
99 public function testFactoryService() {
|
Chris@0
|
100 $factory = $this->container->get('views.executable');
|
Chris@0
|
101 $this->assertTrue($factory instanceof ViewExecutableFactory, 'A ViewExecutableFactory instance was returned from the container.');
|
Chris@0
|
102 $view = View::load('test_executable_displays');
|
Chris@0
|
103 $this->assertTrue($factory->get($view) instanceof ViewExecutable, 'A ViewExecutable instance was returned from the factory.');
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * Tests the initDisplay() and initHandlers() methods.
|
Chris@0
|
108 */
|
Chris@0
|
109 public function testInitMethods() {
|
Chris@0
|
110 $view = Views::getView('test_destroy');
|
Chris@0
|
111 $view->initDisplay();
|
Chris@0
|
112
|
Chris@0
|
113 $this->assertTrue($view->display_handler instanceof DefaultDisplay, 'Make sure a reference to the current display handler is set.');
|
Chris@0
|
114 $this->assertTrue($view->displayHandlers->get('default') instanceof DefaultDisplay, 'Make sure a display handler is created for each display.');
|
Chris@0
|
115
|
Chris@0
|
116 $view->destroy();
|
Chris@0
|
117 $view->initHandlers();
|
Chris@0
|
118
|
Chris@0
|
119 // Check for all handler types.
|
Chris@0
|
120 $handler_types = array_keys(ViewExecutable::getHandlerTypes());
|
Chris@0
|
121 foreach ($handler_types as $type) {
|
Chris@0
|
122 // The views_test integration doesn't have relationships.
|
Chris@0
|
123 if ($type == 'relationship') {
|
Chris@0
|
124 continue;
|
Chris@0
|
125 }
|
Chris@0
|
126 $this->assertTrue(count($view->$type), format_string('Make sure a %type instance got instantiated.', ['%type' => $type]));
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 // initHandlers() should create display handlers automatically as well.
|
Chris@0
|
130 $this->assertTrue($view->display_handler instanceof DefaultDisplay, 'Make sure a reference to the current display handler is set.');
|
Chris@0
|
131 $this->assertTrue($view->displayHandlers->get('default') instanceof DefaultDisplay, 'Make sure a display handler is created for each display.');
|
Chris@0
|
132
|
Chris@0
|
133 $view_hash = spl_object_hash($view);
|
Chris@0
|
134 $display_hash = spl_object_hash($view->display_handler);
|
Chris@0
|
135
|
Chris@0
|
136 // Test the initStyle() method.
|
Chris@0
|
137 $view->initStyle();
|
Chris@0
|
138 $this->assertTrue($view->style_plugin instanceof DefaultStyle, 'Make sure a reference to the style plugin is set.');
|
Chris@17
|
139 // Test the plugin has been invited and view have references to the view and
|
Chris@0
|
140 // display handler.
|
Chris@0
|
141 $this->assertEqual(spl_object_hash($view->style_plugin->view), $view_hash);
|
Chris@0
|
142 $this->assertEqual(spl_object_hash($view->style_plugin->displayHandler), $display_hash);
|
Chris@0
|
143
|
Chris@0
|
144 // Test the initQuery method().
|
Chris@0
|
145 $view->initQuery();
|
Chris@0
|
146 $this->assertTrue($view->query instanceof Sql, 'Make sure a reference to the query is set');
|
Chris@0
|
147 $this->assertEqual(spl_object_hash($view->query->view), $view_hash);
|
Chris@0
|
148 $this->assertEqual(spl_object_hash($view->query->displayHandler), $display_hash);
|
Chris@0
|
149
|
Chris@0
|
150 $view->destroy();
|
Chris@0
|
151
|
Chris@0
|
152 // Test the plugin get methods.
|
Chris@0
|
153 $display_plugin = $view->getDisplay();
|
Chris@0
|
154 $this->assertTrue($display_plugin instanceof DefaultDisplay, 'An instance of DefaultDisplay was returned.');
|
Chris@0
|
155 $this->assertTrue($view->display_handler instanceof DefaultDisplay, 'The display_handler property has been set.');
|
Chris@0
|
156 $this->assertIdentical($display_plugin, $view->getDisplay(), 'The same display plugin instance was returned.');
|
Chris@0
|
157
|
Chris@0
|
158 $style_plugin = $view->getStyle();
|
Chris@0
|
159 $this->assertTrue($style_plugin instanceof DefaultStyle, 'An instance of DefaultStyle was returned.');
|
Chris@0
|
160 $this->assertTrue($view->style_plugin instanceof DefaultStyle, 'The style_plugin property has been set.');
|
Chris@0
|
161 $this->assertIdentical($style_plugin, $view->getStyle(), 'The same style plugin instance was returned.');
|
Chris@0
|
162
|
Chris@0
|
163 $pager_plugin = $view->getPager();
|
Chris@0
|
164 $this->assertTrue($pager_plugin instanceof PagerPluginBase, 'An instance of PagerPluginBase was returned.');
|
Chris@0
|
165 $this->assertTrue($view->pager instanceof PagerPluginBase, 'The pager property has been set.');
|
Chris@0
|
166 $this->assertIdentical($pager_plugin, $view->getPager(), 'The same pager plugin instance was returned.');
|
Chris@0
|
167
|
Chris@0
|
168 $query_plugin = $view->getQuery();
|
Chris@0
|
169 $this->assertTrue($query_plugin instanceof QueryPluginBase, 'An instance of QueryPluginBase was returned.');
|
Chris@0
|
170 $this->assertTrue($view->query instanceof QueryPluginBase, 'The query property has been set.');
|
Chris@0
|
171 $this->assertIdentical($query_plugin, $view->getQuery(), 'The same query plugin instance was returned.');
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * Tests the generation of the executable object.
|
Chris@0
|
176 */
|
Chris@0
|
177 public function testConstructing() {
|
Chris@0
|
178 Views::getView('test_destroy');
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * Tests the accessing of values on the object.
|
Chris@0
|
183 */
|
Chris@0
|
184 public function testProperties() {
|
Chris@0
|
185 $view = Views::getView('test_destroy');
|
Chris@0
|
186 foreach ($this->executableProperties as $property) {
|
Chris@0
|
187 $this->assertTrue(isset($view->{$property}));
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 // Per default exposed input should fall back to an empty array.
|
Chris@0
|
191 $this->assertEqual($view->getExposedInput(), []);
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 public function testSetDisplayWithInvalidDisplay() {
|
Chris@0
|
195 $view = Views::getView('test_executable_displays');
|
Chris@0
|
196 $view->initDisplay();
|
Chris@0
|
197
|
Chris@0
|
198 // Error is triggered while calling the wrong display.
|
Chris@0
|
199 try {
|
Chris@0
|
200 $view->setDisplay('invalid');
|
Chris@0
|
201 $this->fail('Expected error, when setDisplay() called with invalid display ID');
|
Chris@0
|
202 }
|
Chris@0
|
203 catch (\PHPUnit_Framework_Error_Warning $e) {
|
Chris@0
|
204 $this->assertEquals('setDisplay() called with invalid display ID "invalid".', $e->getMessage());
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 $this->assertEqual($view->current_display, 'default', 'If setDisplay is called with an invalid display id the default display should be used.');
|
Chris@0
|
208 $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('default')));
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * Tests the display related methods and properties.
|
Chris@0
|
213 */
|
Chris@0
|
214 public function testDisplays() {
|
Chris@0
|
215 $view = Views::getView('test_executable_displays');
|
Chris@0
|
216
|
Chris@0
|
217 // Tests Drupal\views\ViewExecutable::initDisplay().
|
Chris@0
|
218 $view->initDisplay();
|
Chris@0
|
219 $this->assertTrue($view->displayHandlers instanceof DisplayPluginCollection, 'The displayHandlers property has the right class.');
|
Chris@0
|
220 // Tests the classes of the instances.
|
Chris@0
|
221 $this->assertTrue($view->displayHandlers->get('default') instanceof DefaultDisplay);
|
Chris@0
|
222 $this->assertTrue($view->displayHandlers->get('page_1') instanceof Page);
|
Chris@0
|
223 $this->assertTrue($view->displayHandlers->get('page_2') instanceof Page);
|
Chris@0
|
224
|
Chris@0
|
225 // After initializing the default display is the current used display.
|
Chris@0
|
226 $this->assertEqual($view->current_display, 'default');
|
Chris@0
|
227 $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('default')));
|
Chris@0
|
228
|
Chris@0
|
229 // All handlers should have a reference to the default display.
|
Chris@0
|
230 $this->assertEqual(spl_object_hash($view->displayHandlers->get('page_1')->default_display), spl_object_hash($view->displayHandlers->get('default')));
|
Chris@0
|
231 $this->assertEqual(spl_object_hash($view->displayHandlers->get('page_2')->default_display), spl_object_hash($view->displayHandlers->get('default')));
|
Chris@0
|
232
|
Chris@0
|
233 // Tests Drupal\views\ViewExecutable::setDisplay().
|
Chris@0
|
234 $view->setDisplay();
|
Chris@0
|
235 $this->assertEqual($view->current_display, 'default', 'If setDisplay is called with no parameter the default display should be used.');
|
Chris@0
|
236 $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('default')));
|
Chris@0
|
237
|
Chris@0
|
238 // Set two different valid displays.
|
Chris@0
|
239 $view->setDisplay('page_1');
|
Chris@0
|
240 $this->assertEqual($view->current_display, 'page_1', 'If setDisplay is called with a valid display id the appropriate display should be used.');
|
Chris@0
|
241 $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('page_1')));
|
Chris@0
|
242
|
Chris@0
|
243 $view->setDisplay('page_2');
|
Chris@0
|
244 $this->assertEqual($view->current_display, 'page_2', 'If setDisplay is called with a valid display id the appropriate display should be used.');
|
Chris@0
|
245 $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('page_2')));
|
Chris@0
|
246
|
Chris@0
|
247 // Destroy the view, so we can start again and test an invalid display.
|
Chris@0
|
248 $view->destroy();
|
Chris@0
|
249
|
Chris@0
|
250 // Test the style and row plugins are replaced correctly when setting the
|
Chris@0
|
251 // display.
|
Chris@0
|
252 $view->setDisplay('page_1');
|
Chris@0
|
253 $view->initStyle();
|
Chris@0
|
254 $this->assertTrue($view->style_plugin instanceof DefaultStyle);
|
Chris@0
|
255 $this->assertTrue($view->rowPlugin instanceof Fields);
|
Chris@0
|
256
|
Chris@0
|
257 $view->setDisplay('page_2');
|
Chris@0
|
258 $view->initStyle();
|
Chris@0
|
259 $this->assertTrue($view->style_plugin instanceof Grid);
|
Chris@0
|
260 // @todo Change this rowPlugin type too.
|
Chris@0
|
261 $this->assertTrue($view->rowPlugin instanceof Fields);
|
Chris@0
|
262
|
Chris@0
|
263 // Test the newDisplay() method.
|
Chris@0
|
264 $view = $this->container->get('entity.manager')->getStorage('view')->create(['id' => 'test_executable_displays']);
|
Chris@0
|
265 $executable = $view->getExecutable();
|
Chris@0
|
266
|
Chris@0
|
267 $executable->newDisplay('page');
|
Chris@0
|
268 $executable->newDisplay('page');
|
Chris@0
|
269 $executable->newDisplay('display_test');
|
Chris@0
|
270
|
Chris@0
|
271 $this->assertTrue($executable->displayHandlers->get('default') instanceof DefaultDisplay);
|
Chris@0
|
272 $this->assertFalse(isset($executable->displayHandlers->get('default')->default_display));
|
Chris@0
|
273 $this->assertTrue($executable->displayHandlers->get('page_1') instanceof Page);
|
Chris@0
|
274 $this->assertTrue($executable->displayHandlers->get('page_1')->default_display instanceof DefaultDisplay);
|
Chris@0
|
275 $this->assertTrue($executable->displayHandlers->get('page_2') instanceof Page);
|
Chris@0
|
276 $this->assertTrue($executable->displayHandlers->get('page_2')->default_display instanceof DefaultDisplay);
|
Chris@0
|
277 $this->assertTrue($executable->displayHandlers->get('display_test_1') instanceof DisplayTest);
|
Chris@0
|
278 $this->assertTrue($executable->displayHandlers->get('display_test_1')->default_display instanceof DefaultDisplay);
|
Chris@0
|
279 }
|
Chris@0
|
280
|
Chris@0
|
281 /**
|
Chris@0
|
282 * Tests the setting/getting of properties.
|
Chris@0
|
283 */
|
Chris@0
|
284 public function testPropertyMethods() {
|
Chris@0
|
285 $view = Views::getView('test_executable_displays');
|
Chris@0
|
286
|
Chris@0
|
287 // Test the setAjaxEnabled() method.
|
Chris@0
|
288 $this->assertFalse($view->ajaxEnabled());
|
Chris@0
|
289 $view->setAjaxEnabled(TRUE);
|
Chris@0
|
290 $this->assertTrue($view->ajaxEnabled());
|
Chris@0
|
291
|
Chris@0
|
292 $view->setDisplay();
|
Chris@0
|
293 // There should be no pager set initially.
|
Chris@0
|
294 $this->assertNull($view->usePager());
|
Chris@0
|
295
|
Chris@0
|
296 // Add a pager, initialize, and test.
|
Chris@0
|
297 $view->displayHandlers->get('default')->overrideOption('pager', [
|
Chris@0
|
298 'type' => 'full',
|
Chris@0
|
299 'options' => ['items_per_page' => 10],
|
Chris@0
|
300 ]);
|
Chris@0
|
301 $view->initPager();
|
Chris@0
|
302 $this->assertTrue($view->usePager());
|
Chris@0
|
303
|
Chris@0
|
304 // Test setting and getting the offset.
|
Chris@0
|
305 $rand = rand();
|
Chris@0
|
306 $view->setOffset($rand);
|
Chris@0
|
307 $this->assertEqual($view->getOffset(), $rand);
|
Chris@0
|
308
|
Chris@0
|
309 // Test the getBaseTable() method.
|
Chris@0
|
310 $expected = [
|
Chris@0
|
311 'views_test_data' => TRUE,
|
Chris@0
|
312 '#global' => TRUE,
|
Chris@0
|
313 ];
|
Chris@0
|
314 $this->assertIdentical($view->getBaseTables(), $expected);
|
Chris@0
|
315
|
Chris@0
|
316 // Test response methods.
|
Chris@0
|
317 $this->assertTrue($view->getResponse() instanceof Response, 'New response object returned.');
|
Chris@0
|
318 $new_response = new Response();
|
Chris@0
|
319 $view->setResponse($new_response);
|
Chris@0
|
320 $this->assertIdentical(spl_object_hash($view->getResponse()), spl_object_hash($new_response), 'New response object correctly set.');
|
Chris@0
|
321
|
Chris@0
|
322 // Test the getPath() method.
|
Chris@0
|
323 $path = $this->randomMachineName();
|
Chris@0
|
324 $view->displayHandlers->get('page_1')->overrideOption('path', $path);
|
Chris@0
|
325 $view->setDisplay('page_1');
|
Chris@0
|
326 $this->assertEqual($view->getPath(), $path);
|
Chris@0
|
327 // Test the override_path property override.
|
Chris@0
|
328 $override_path = $this->randomMachineName();
|
Chris@0
|
329 $view->override_path = $override_path;
|
Chris@0
|
330 $this->assertEqual($view->getPath(), $override_path);
|
Chris@0
|
331
|
Chris@0
|
332 // Test the title methods.
|
Chris@0
|
333 $title = $this->randomString();
|
Chris@0
|
334 $view->setTitle($title);
|
Chris@0
|
335 $this->assertEqual($view->getTitle(), Xss::filterAdmin($title));
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 /**
|
Chris@0
|
339 * Tests the deconstructor to be sure that necessary objects are removed.
|
Chris@0
|
340 */
|
Chris@0
|
341 public function testDestroy() {
|
Chris@0
|
342 $view = Views::getView('test_destroy');
|
Chris@0
|
343
|
Chris@0
|
344 $view->preview();
|
Chris@0
|
345 $view->destroy();
|
Chris@0
|
346
|
Chris@0
|
347 $this->assertViewDestroy($view);
|
Chris@0
|
348 }
|
Chris@0
|
349
|
Chris@0
|
350 /**
|
Chris@0
|
351 * Asserts that expected view properties have been unset by destroy().
|
Chris@0
|
352 *
|
Chris@0
|
353 * @param \Drupal\views\ViewExecutable $view
|
Chris@0
|
354 */
|
Chris@0
|
355 protected function assertViewDestroy($view) {
|
Chris@0
|
356 $reflection = new \ReflectionClass($view);
|
Chris@0
|
357 $defaults = $reflection->getDefaultProperties();
|
Chris@0
|
358 // The storage and user should remain.
|
Chris@0
|
359 unset(
|
Chris@0
|
360 $defaults['storage'],
|
Chris@0
|
361 $defaults['user'],
|
Chris@0
|
362 $defaults['request'],
|
Chris@0
|
363 $defaults['routeProvider'],
|
Chris@0
|
364 $defaults['viewsData']
|
Chris@0
|
365 );
|
Chris@0
|
366
|
Chris@0
|
367 foreach ($defaults as $property => $default) {
|
Chris@0
|
368 $this->assertIdentical($this->getProtectedProperty($view, $property), $default);
|
Chris@0
|
369 }
|
Chris@0
|
370 }
|
Chris@0
|
371
|
Chris@0
|
372 /**
|
Chris@0
|
373 * Returns a protected property from a class instance.
|
Chris@0
|
374 *
|
Chris@0
|
375 * @param object $instance
|
Chris@0
|
376 * The class instance to return the property from.
|
Chris@0
|
377 * @param string $property
|
Chris@0
|
378 * The name of the property to return.
|
Chris@0
|
379 *
|
Chris@0
|
380 * @return mixed
|
Chris@0
|
381 * The instance property value.
|
Chris@0
|
382 */
|
Chris@0
|
383 protected function getProtectedProperty($instance, $property) {
|
Chris@0
|
384 $reflection = new \ReflectionProperty($instance, $property);
|
Chris@0
|
385 $reflection->setAccessible(TRUE);
|
Chris@0
|
386 return $reflection->getValue($instance);
|
Chris@0
|
387 }
|
Chris@0
|
388
|
Chris@0
|
389 /**
|
Chris@0
|
390 * Tests ViewExecutable::getHandlerTypes().
|
Chris@0
|
391 */
|
Chris@0
|
392 public function testGetHandlerTypes() {
|
Chris@0
|
393 $types = ViewExecutable::getHandlerTypes();
|
Chris@0
|
394 foreach (['field', 'filter', 'argument', 'sort', 'header', 'footer', 'empty'] as $type) {
|
Chris@0
|
395 $this->assertTrue(isset($types[$type]));
|
Chris@0
|
396 // @todo The key on the display should be footers, headers and empties
|
Chris@0
|
397 // or something similar instead of the singular, but so long check for
|
Chris@0
|
398 // this special case.
|
Chris@0
|
399 if (isset($types[$type]['type']) && $types[$type]['type'] == 'area') {
|
Chris@0
|
400 $this->assertEqual($types[$type]['plural'], $type);
|
Chris@0
|
401 }
|
Chris@0
|
402 else {
|
Chris@0
|
403 $this->assertEqual($types[$type]['plural'], $type . 's');
|
Chris@0
|
404 }
|
Chris@0
|
405 }
|
Chris@0
|
406 }
|
Chris@0
|
407
|
Chris@0
|
408 /**
|
Chris@0
|
409 * Tests ViewExecutable::getHandlers().
|
Chris@0
|
410 */
|
Chris@0
|
411 public function testGetHandlers() {
|
Chris@0
|
412 $view = Views::getView('test_executable_displays');
|
Chris@0
|
413 $view->setDisplay('page_1');
|
Chris@0
|
414
|
Chris@0
|
415 $view->getHandlers('field', 'page_2');
|
Chris@0
|
416
|
Chris@0
|
417 // getHandlers() shouldn't change the active display.
|
Chris@0
|
418 $this->assertEqual('page_1', $view->current_display, "The display shouldn't change after getHandlers()");
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 /**
|
Chris@0
|
422 * Tests the validation of display handlers.
|
Chris@0
|
423 */
|
Chris@0
|
424 public function testValidate() {
|
Chris@0
|
425 $view = Views::getView('test_executable_displays');
|
Chris@0
|
426 $view->setDisplay('page_1');
|
Chris@0
|
427
|
Chris@0
|
428 $validate = $view->validate();
|
Chris@0
|
429
|
Chris@0
|
430 // Validating a view shouldn't change the active display.
|
Chris@0
|
431 $this->assertEqual('page_1', $view->current_display, "The display should be constant while validating");
|
Chris@0
|
432
|
Chris@0
|
433 $count = 0;
|
Chris@0
|
434 foreach ($view->displayHandlers as $id => $display) {
|
Chris@0
|
435 $match = function ($value) use ($display) {
|
Chris@0
|
436 return strpos($value, $display->display['display_title']) !== FALSE;
|
Chris@0
|
437 };
|
Chris@0
|
438 $this->assertTrue(array_filter($validate[$id], $match), format_string('Error message found for @id display', ['@id' => $id]));
|
Chris@0
|
439 $count++;
|
Chris@0
|
440 }
|
Chris@0
|
441
|
Chris@0
|
442 $this->assertEqual(count($view->displayHandlers), $count, 'Error messages from all handlers merged.');
|
Chris@0
|
443
|
Chris@0
|
444 // Test that a deleted display is not included.
|
Chris@0
|
445 $display = &$view->storage->getDisplay('default');
|
Chris@0
|
446 $display['deleted'] = TRUE;
|
Chris@0
|
447 $validate_deleted = $view->validate();
|
Chris@0
|
448
|
Chris@0
|
449 $this->assertNotIdentical($validate, $validate_deleted, 'Master display has not been validated.');
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@0
|
452 /**
|
Chris@0
|
453 * Tests that nested loops of the display handlers won't break validation.
|
Chris@0
|
454 */
|
Chris@0
|
455 public function testValidateNestedLoops() {
|
Chris@0
|
456 $view = View::create(['id' => 'test_validate_nested_loops']);
|
Chris@0
|
457 $executable = $view->getExecutable();
|
Chris@0
|
458
|
Chris@0
|
459 $executable->newDisplay('display_test');
|
Chris@0
|
460 $executable->newDisplay('display_test');
|
Chris@0
|
461 $errors = $executable->validate();
|
Chris@0
|
462 $total_error_count = array_reduce($errors, function ($carry, $item) {
|
Chris@0
|
463 $carry += count($item);
|
Chris@0
|
464
|
Chris@0
|
465 return $carry;
|
Chris@0
|
466 });
|
Chris@0
|
467 // Assert that there were 9 total errors across 3 displays.
|
Chris@0
|
468 $this->assertIdentical(9, $total_error_count);
|
Chris@0
|
469 $this->assertIdentical(3, count($errors));
|
Chris@0
|
470 }
|
Chris@0
|
471
|
Chris@0
|
472 /**
|
Chris@0
|
473 * Tests serialization of the ViewExecutable object.
|
Chris@0
|
474 */
|
Chris@0
|
475 public function testSerialization() {
|
Chris@0
|
476 $view = Views::getView('test_executable_displays');
|
Chris@0
|
477 $view->setDisplay('page_1');
|
Chris@0
|
478 $view->setArguments(['test']);
|
Chris@0
|
479 $view->setCurrentPage(2);
|
Chris@0
|
480
|
Chris@0
|
481 $serialized = serialize($view);
|
Chris@0
|
482
|
Chris@0
|
483 // Test the view storage object is not present in the actual serialized
|
Chris@0
|
484 // string.
|
Chris@0
|
485 $this->assertIdentical(strpos($serialized, '"Drupal\views\Entity\View"'), FALSE, 'The Drupal\views\Entity\View class was not found in the serialized string.');
|
Chris@0
|
486
|
Chris@0
|
487 /** @var \Drupal\views\ViewExecutable $unserialized */
|
Chris@0
|
488 $unserialized = unserialize($serialized);
|
Chris@0
|
489
|
Chris@0
|
490 $this->assertTrue($unserialized instanceof ViewExecutable);
|
Chris@0
|
491 $this->assertIdentical($view->storage->id(), $unserialized->storage->id(), 'The expected storage entity was loaded on the unserialized view.');
|
Chris@0
|
492 $this->assertIdentical($unserialized->current_display, 'page_1', 'The expected display was set on the unserialized view.');
|
Chris@0
|
493 $this->assertIdentical($unserialized->args, ['test'], 'The expected argument was set on the unserialized view.');
|
Chris@0
|
494 $this->assertIdentical($unserialized->getCurrentPage(), 2, 'The expected current page was set on the unserialized view.');
|
Chris@0
|
495
|
Chris@0
|
496 // Get the definition of node's nid field, for example. Only get it not from
|
Chris@0
|
497 // the field manager directly, but from the item data definition. It should
|
Chris@0
|
498 // be the same base field definition object (the field and item definitions
|
Chris@0
|
499 // refer to each other).
|
Chris@0
|
500 // See https://bugs.php.net/bug.php?id=66052
|
Chris@0
|
501 $field_manager = $this->container->get('entity_field.manager');
|
Chris@0
|
502 $nid_definition_before = $field_manager->getBaseFieldDefinitions('node')['nid']
|
Chris@0
|
503 ->getItemDefinition()
|
Chris@0
|
504 ->getFieldDefinition();
|
Chris@0
|
505
|
Chris@0
|
506 // Load and execute a view.
|
Chris@0
|
507 $view_entity = View::load('content');
|
Chris@0
|
508 $view_executable = $view_entity->getExecutable();
|
Chris@0
|
509 $view_executable->execute('page_1');
|
Chris@0
|
510
|
Chris@0
|
511 // Reset the static cache. Don't use clearCachedFieldDefinitions() since
|
Chris@0
|
512 // that clears the persistent cache and we need to get the serialized cache
|
Chris@0
|
513 // data.
|
Chris@0
|
514 $field_manager->useCaches(FALSE);
|
Chris@0
|
515 $field_manager->useCaches(TRUE);
|
Chris@0
|
516
|
Chris@0
|
517 // Serialize the ViewExecutable as part of other data.
|
Chris@0
|
518 unserialize(serialize(['SOMETHING UNEXPECTED', $view_executable]));
|
Chris@0
|
519
|
Chris@0
|
520 // Make sure the serialisation of the ViewExecutable didn't influence the
|
Chris@0
|
521 // field definitions.
|
Chris@0
|
522 $nid_definition_after = $field_manager->getBaseFieldDefinitions('node')['nid']
|
Chris@0
|
523 ->getItemDefinition()
|
Chris@0
|
524 ->getFieldDefinition();
|
Chris@0
|
525 $this->assertEquals($nid_definition_before->getPropertyDefinitions(), $nid_definition_after->getPropertyDefinitions());
|
Chris@0
|
526 }
|
Chris@0
|
527
|
Chris@16
|
528 /**
|
Chris@16
|
529 * Tests if argument overrides by validators are propagated to tokens.
|
Chris@16
|
530 */
|
Chris@16
|
531 public function testArgumentValidatorValueOverride() {
|
Chris@16
|
532 $view = Views::getView('test_argument_dependency');
|
Chris@16
|
533 $view->setDisplay('page_1');
|
Chris@16
|
534 $view->setArguments(['1', 'this value should be replaced']);
|
Chris@16
|
535 $view->execute();
|
Chris@16
|
536 $expected = [
|
Chris@16
|
537 '{{ arguments.uid }}' => '1',
|
Chris@16
|
538 '{{ raw_arguments.uid }}' => '1',
|
Chris@16
|
539 ];
|
Chris@16
|
540 $this->assertEquals($expected, $view->build_info['substitutions']);
|
Chris@16
|
541 }
|
Chris@16
|
542
|
Chris@0
|
543 }
|