ViewExecutableTest.php 17.2 KB
Newer Older
1
2
3
<?php

/**
4
 * @file
5
 * Contains \Drupal\views\Tests\ViewExecutableTest.
6
7
8
9
 */

namespace Drupal\views\Tests;

10
use Drupal\views\Views;
11
use Drupal\views\ViewExecutable;
12
use Drupal\views\ViewExecutableFactory;
13
use Drupal\views\DisplayPluginCollection;
14
15
use Drupal\views\Plugin\views\display\DefaultDisplay;
use Drupal\views\Plugin\views\display\Page;
16
use Drupal\views\Plugin\views\style\DefaultStyle;
17
18
use Drupal\views\Plugin\views\style\Grid;
use Drupal\views\Plugin\views\row\Fields;
19
use Drupal\views\Plugin\views\query\Sql;
20
21
use Drupal\views\Plugin\views\pager\PagerPluginBase;
use Drupal\views\Plugin\views\query\QueryPluginBase;
22
23
use Drupal\views_test_data\Plugin\views\display\DisplayTest;
use Symfony\Component\HttpFoundation\Response;
24

25
26
27
/**
 * Tests the ViewExecutable class.
 *
28
 * @group views
29
 * @see \Drupal\views\ViewExecutable
30
 */
31
class ViewExecutableTest extends ViewUnitTestBase {
32

33
  public static $modules = array('system', 'node', 'comment', 'user', 'filter', 'field', 'text', 'entity_reference');
34

35
36
37
38
39
40
41
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_destroy', 'test_executable_displays');

42
43
44
45
46
47
48
49
50
51
52
  /**
   * Properties that should be stored in the configuration.
   *
   * @var array
   */
  protected $configProperties = array(
    'disabled',
    'name',
    'description',
    'tag',
    'base_table',
53
    'label',
54
55
56
57
58
59
60
61
62
63
    'core',
    'display',
  );

  /**
   * Properties that should be stored in the executable.
   *
   * @var array
   */
  protected $executableProperties = array(
64
65
66
67
68
69
70
71
72
73
74
75
    'storage',
    'built',
    'executed',
    'args',
    'build_info',
    'result',
    'attachment_before',
    'attachment_after',
    'exposed_data',
    'exposed_raw_input',
    'old_view',
    'parent_views',
76
77
  );

78
  protected function setUpFixtures() {
79
80
81
82
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installEntitySchema('comment');
    $this->installSchema('comment', array('comment_entity_statistics'));
83
    $this->installConfig(array('field'));
84

85
86
87
88
    entity_create('node_type', array(
      'type' => 'page',
      'name' => 'Page',
    ))->save();
89
    $this->container->get('comment.manager')->addDefaultField('node', 'page');
90
    parent::setUpFixtures();
91

92
    $this->installConfig(array('filter'));
93
94
  }

95
96
97
98
99
100
101
102
103
104
  /**
   * Tests the views.exectuable container service.
   */
  public function testFactoryService() {
    $factory = $this->container->get('views.executable');
    $this->assertTrue($factory instanceof ViewExecutableFactory, 'A ViewExecutableFactory instance was returned from the container.');
    $view = entity_load('view', 'test_executable_displays');
    $this->assertTrue($factory->get($view) instanceof ViewExecutable, 'A ViewExecutable instance was returned from the factory.');
  }

105
106
107
108
  /**
   * Tests the initDisplay() and initHandlers() methods.
   */
  public function testInitMethods() {
109
    $view = Views::getView('test_destroy');
110
111
112
    $view->initDisplay();

    $this->assertTrue($view->display_handler instanceof DefaultDisplay, 'Make sure a reference to the current display handler is set.');
113
    $this->assertTrue($view->displayHandlers->get('default') instanceof DefaultDisplay, 'Make sure a display handler is created for each display.');
114
115
116
117
118

    $view->destroy();
    $view->initHandlers();

    // Check for all handler types.
119
    $handler_types = array_keys(ViewExecutable::getHandlerTypes());
120
121
122
123
124
125
126
127
128
129
    foreach ($handler_types as $type) {
      // The views_test integration doesn't have relationships.
      if ($type == 'relationship') {
        continue;
      }
      $this->assertTrue(count($view->$type), format_string('Make sure a %type instance got instantiated.', array('%type' => $type)));
    }

    // initHandlers() should create display handlers automatically as well.
    $this->assertTrue($view->display_handler instanceof DefaultDisplay, 'Make sure a reference to the current display handler is set.');
130
    $this->assertTrue($view->displayHandlers->get('default') instanceof DefaultDisplay, 'Make sure a display handler is created for each display.');
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

    $view_hash = spl_object_hash($view);
    $display_hash = spl_object_hash($view->display_handler);

    // Test the initStyle() method.
    $view->initStyle();
    $this->assertTrue($view->style_plugin instanceof DefaultStyle, 'Make sure a reference to the style plugin is set.');
    // Test the plugin has been inited and view have references to the view and
    // display handler.
    $this->assertEqual(spl_object_hash($view->style_plugin->view), $view_hash);
    $this->assertEqual(spl_object_hash($view->style_plugin->displayHandler), $display_hash);

    // Test the initQuery method().
    $view->initQuery();
    $this->assertTrue($view->query instanceof Sql, 'Make sure a reference to the query is set');
    $this->assertEqual(spl_object_hash($view->query->view), $view_hash);
    $this->assertEqual(spl_object_hash($view->query->displayHandler), $display_hash);
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

    $view->destroy();

    // Test the plugin  get methods.
    $display_plugin = $view->getDisplay();
    $this->assertTrue($display_plugin instanceof DefaultDisplay, 'An instance of DefaultDisplay was returned.');
    $this->assertTrue($view->display_handler instanceof DefaultDisplay, 'The display_handler property has been set.');
    $this->assertIdentical($display_plugin, $view->getDisplay(), 'The same display plugin instance was returned.');

    $style_plugin = $view->getStyle();
    $this->assertTrue($style_plugin instanceof DefaultStyle, 'An instance of DefaultStyle was returned.');
    $this->assertTrue($view->style_plugin instanceof DefaultStyle, 'The style_plugin property has been set.');
    $this->assertIdentical($style_plugin, $view->getStyle(), 'The same style plugin instance was returned.');

    $pager_plugin = $view->getPager();
    $this->assertTrue($pager_plugin instanceof PagerPluginBase, 'An instance of PagerPluginBase was returned.');
    $this->assertTrue($view->pager instanceof PagerPluginBase, 'The pager property has been set.');
    $this->assertIdentical($pager_plugin, $view->getPager(), 'The same pager plugin instance was returned.');

    $query_plugin = $view->getQuery();
    $this->assertTrue($query_plugin instanceof QueryPluginBase, 'An instance of QueryPluginBase was returned.');
    $this->assertTrue($view->query instanceof QueryPluginBase, 'The query property has been set.');
    $this->assertIdentical($query_plugin, $view->getQuery(), 'The same query plugin instance was returned.');
171
172
  }

173
174
175
176
  /**
   * Tests the generation of the executable object.
   */
  public function testConstructing() {
177
    Views::getView('test_destroy');
178
179
180
181
182
183
  }

  /**
   * Tests the accessing of values on the object.
   */
  public function testProperties() {
184
    $view = Views::getView('test_destroy');
185
186
187
    foreach ($this->executableProperties as $property) {
      $this->assertTrue(isset($view->{$property}));
    }
188
189
190

    // Per default exposed input should fall back to an empty array.
    $this->assertEqual($view->getExposedInput(), []);
191
  }
192
193
194
195
196

  /**
   * Tests the display related methods and properties.
   */
  public function testDisplays() {
197
    $view = Views::getView('test_executable_displays');
198
199
200

    // Tests Drupal\views\ViewExecutable::initDisplay().
    $view->initDisplay();
201
    $this->assertTrue($view->displayHandlers instanceof DisplayPluginCollection, 'The displayHandlers property has the right class.');
202
    // Tests the classes of the instances.
203
204
205
    $this->assertTrue($view->displayHandlers->get('default') instanceof DefaultDisplay);
    $this->assertTrue($view->displayHandlers->get('page_1') instanceof Page);
    $this->assertTrue($view->displayHandlers->get('page_2') instanceof Page);
206
207
208

    // After initializing the default display is the current used display.
    $this->assertEqual($view->current_display, 'default');
209
    $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('default')));
210
211

    // All handlers should have a reference to the default display.
212
213
    $this->assertEqual(spl_object_hash($view->displayHandlers->get('page_1')->default_display), spl_object_hash($view->displayHandlers->get('default')));
    $this->assertEqual(spl_object_hash($view->displayHandlers->get('page_2')->default_display), spl_object_hash($view->displayHandlers->get('default')));
214
215
216
217

    // Tests Drupal\views\ViewExecutable::setDisplay().
    $view->setDisplay();
    $this->assertEqual($view->current_display, 'default', 'If setDisplay is called with no parameter the default display should be used.');
218
    $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('default')));
219
220

    // Set two different valid displays.
221
222
    $view->setDisplay('page_1');
    $this->assertEqual($view->current_display, 'page_1', 'If setDisplay is called with a valid display id the appropriate display should be used.');
223
    $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('page_1')));
224
225
226

    $view->setDisplay('page_2');
    $this->assertEqual($view->current_display, 'page_2', 'If setDisplay is called with a valid display id the appropriate display should be used.');
227
    $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('page_2')));
228

229
230
231
232
    // Destroy the view, so we can start again and test an invalid display.
    $view->destroy();

    $count_before = count($this->assertions);
233
    $view->setDisplay('invalid');
234
235
236
    $count_after = count($this->assertions);
    $this->assertTrue($count_after - $count_before, 'Error is triggered while calling the wrong display.');

237
    $this->assertEqual($view->current_display, 'default', 'If setDisplay is called with an invalid display id the default display should be used.');
238
    $this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('default')));
239
240
241
242
243
244
245
246
247
248
249
250
251

    // Test the style and row plugins are replaced correctly when setting the
    // display.
    $view->setDisplay('page_1');
    $view->initStyle();
    $this->assertTrue($view->style_plugin instanceof DefaultStyle);
    $this->assertTrue($view->rowPlugin instanceof Fields);

    $view->setDisplay('page_2');
    $view->initStyle();
    $this->assertTrue($view->style_plugin instanceof Grid);
    // @todo Change this rowPlugin type too.
    $this->assertTrue($view->rowPlugin instanceof Fields);
252
253

    // Test the newDisplay() method.
254
    $view = $this->container->get('entity.manager')->getStorage('view')->create(array('id' => 'test_executable_displays'));
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    $executable = $view->getExecutable();

    $executable->newDisplay('page');
    $executable->newDisplay('page');
    $executable->newDisplay('display_test');

    $this->assertTrue($executable->displayHandlers->get('default') instanceof DefaultDisplay);
    $this->assertFalse(isset($executable->displayHandlers->get('default')->default_display));
    $this->assertTrue($executable->displayHandlers->get('page_1') instanceof Page);
    $this->assertTrue($executable->displayHandlers->get('page_1')->default_display instanceof DefaultDisplay);
    $this->assertTrue($executable->displayHandlers->get('page_2') instanceof Page);
    $this->assertTrue($executable->displayHandlers->get('page_2')->default_display instanceof DefaultDisplay);
    $this->assertTrue($executable->displayHandlers->get('display_test_1') instanceof DisplayTest);
    $this->assertTrue($executable->displayHandlers->get('display_test_1')->default_display instanceof DefaultDisplay);
269
  }
270
271

  /**
272
   * Tests the setting/getting of properties.
273
   */
274
  public function testPropertyMethods() {
275
    $view = Views::getView('test_executable_displays');
276

277
278
279
280
    // Test the setAjaxEnabled() method.
    $this->assertFalse($view->ajaxEnabled());
    $view->setAjaxEnabled(TRUE);
    $this->assertTrue($view->ajaxEnabled());
281
282
283
284
285
286

    $view->setDisplay();
    // There should be no pager set initially.
    $this->assertNull($view->usePager());

    // Add a pager, initialize, and test.
287
    $view->displayHandlers->get('default')->overrideOption('pager', array(
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
      'type' => 'full',
      'options' => array('items_per_page' => 10),
    ));
    $view->initPager();
    $this->assertTrue($view->usePager());

    // Test setting and getting the offset.
    $rand = rand();
    $view->setOffset($rand);
    $this->assertEqual($view->getOffset(), $rand);

    // Test the getBaseTable() method.
    $expected = array(
      'views_test_data' => TRUE,
      '#global' => TRUE,
    );
    $this->assertIdentical($view->getBaseTables(), $expected);

    // Test response methods.
    $this->assertTrue($view->getResponse() instanceof Response, 'New response object returned.');
    $new_response = new Response();
    $view->setResponse($new_response);
    $this->assertIdentical(spl_object_hash($view->getResponse()), spl_object_hash($new_response), 'New response object correctly set.');

    // Test the getPath() method.
313
    $path = $this->randomMachineName();
314
    $view->displayHandlers->get('page_1')->overrideOption('path', $path);
315
316
317
    $view->setDisplay('page_1');
    $this->assertEqual($view->getPath(), $path);
    // Test the override_path property override.
318
    $override_path = $this->randomMachineName();
319
320
321
322
    $view->override_path = $override_path;
    $this->assertEqual($view->getPath(), $override_path);

    // Test the getUrl method().
323
    $url = 'foo';
324
325
    $this->assertEqual($view->getUrl(NULL, $url), $url);
    // Test with arguments.
326
327
    $arg1 = 'bar';
    $arg2 = 12345;
328
329
    $this->assertEqual($view->getUrl(array($arg1, $arg2), $url), "$url/$arg1/$arg2");
    // Test the override_url property override.
330
    $override_url = 'baz';
331
332
333
334
335
336
337
338
339
340
341
342
343
    $view->override_url = $override_url;
    $this->assertEqual($view->getUrl(NULL, $url), $override_url);

    // Test the title methods.
    $title = $this->randomString();
    $view->setTitle($title);
    $this->assertEqual($view->getTitle(), $title);
  }

  /**
   * Tests the deconstructor to be sure that necessary objects are removed.
   */
  public function testDestroy() {
344
    $view = Views::getView('test_destroy');
345
346
347
348
349
350
351

    $view->preview();
    $view->destroy();

    $this->assertViewDestroy($view);
  }

352
353
354
355
356
357
  /**
   * Asserts that expected view properties have been unset by destroy().
   *
   * @param \Drupal\views\ViewExecutable $view
   */
  protected function assertViewDestroy($view) {
358
359
    $reflection = new \ReflectionClass($view);
    $defaults = $reflection->getDefaultProperties();
360
    // The storage and user should remain.
361
    unset($defaults['storage'], $defaults['user'], $defaults['request']);
362
363
364

    foreach ($defaults as $property => $default) {
      $this->assertIdentical($this->getProtectedProperty($view, $property), $default);
365
    }
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  }

  /**
   * Returns a protected property from a class instance.
   *
   * @param object $instance
   *   The class instance to return the property from.
   * @param string $property
   *   The name of the property to return.
   *
   * @return mixed
   *   The instance property value.
   */
  protected function getProtectedProperty($instance, $property) {
    $reflection = new \ReflectionProperty($instance, $property);
    $reflection->setAccessible(TRUE);
    return $reflection->getValue($instance);
383
384
385
  }

  /**
386
   * Tests ViewExecutable::getHandlerTypes().
387
   */
388
389
  public function testGetHandlerTypes() {
    $types = ViewExecutable::getHandlerTypes();
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    foreach (array('field', 'filter', 'argument', 'sort', 'header', 'footer', 'empty') as $type) {
      $this->assertTrue(isset($types[$type]));
      // @todo The key on the display should be footers, headers and empties
      //   or something similar instead of the singular, but so long check for
      //   this special case.
      if (isset($types[$type]['type']) && $types[$type]['type'] == 'area') {
        $this->assertEqual($types[$type]['plural'], $type);
      }
      else {
        $this->assertEqual($types[$type]['plural'], $type . 's');
      }
    }
  }

404
405
406
407
408
409
410
411
412
413
414
415
416
  /**
   * Tests ViewExecutable::getHandlers().
   */
  public function testGetHandlers() {
    $view = Views::getView('test_executable_displays');
    $view->setDisplay('page_1');

    $view->getHandlers('field', 'page_2');

    // getHandlers() shouldn't change the active display.
    $this->assertEqual('page_1', $view->current_display, "The display shouldn't change after getHandlers()");
  }

417
418
419
420
  /**
   * Tests the validation of display handlers.
   */
  public function testValidate() {
421
    $view = Views::getView('test_executable_displays');
422
    $view->setDisplay('page_1');
423

424
    $validate = $view->validate();
425

426
    // Validating a view shouldn't change the active display.
427
    $this->assertEqual('page_1', $view->current_display, "The display should be constant while validating");
428

429
430
431
432
433
    $count = 0;
    foreach ($view->displayHandlers as $id => $display) {
      $match = function($value) use ($display) {
        return strpos($value, $display->display['display_title']) !== false;
      };
434
      $this->assertTrue(array_filter($validate[$id], $match), format_string('Error message found for @id display', array('@id' => $id)));
435
436
437
438
439
440
      $count++;
    }

    $this->assertEqual(count($view->displayHandlers), $count, 'Error messages from all handlers merged.');

    // Test that a deleted display is not included.
441
442
    $display = &$view->storage->getDisplay('default');
    $display['deleted'] = TRUE;
443
444
    $validate_deleted = $view->validate();

445
    $this->assertNotIdentical($validate, $validate_deleted, 'Master display has not been validated.');
446
447
  }

448
}