ViewStorageTest.php 13.7 KB
Newer Older
1 2 3
<?php

/**
tim.plunkett's avatar
tim.plunkett committed
4
 * @file
5
 * Definition of Drupal\views\Tests\ViewStorageTest.
6 7 8 9 10
 */

namespace Drupal\views\Tests;

use Drupal\views\ViewStorageController;
11
use Drupal\views\Plugin\Core\Entity\View;
12
use Drupal\views\Plugin\views\display\Page;
13 14
use Drupal\views\Plugin\views\display\DefaultDisplay;
use Drupal\views\Plugin\views\display\Feed;
15

16
/**
17
 * Tests the functionality of View and ViewStorageController.
18
 *
19
 * @see Drupal\views\Plugin\Core\Entity\View
20
 * @see Drupal\views\ViewStorageController
21
 */
22
class ViewStorageTest extends ViewUnitTestBase {
23

24 25 26 27 28 29
  /**
   * Properties that should be stored in the configuration.
   *
   * @var array
   */
  protected $config_properties = array(
30
    'status',
31
    'module',
32
    'id',
33 34 35
    'description',
    'tag',
    'base_table',
36
    'label',
37 38 39 40
    'core',
    'display',
  );

41
  /**
42
   * The configuration entity information from entity_get_info().
43 44 45 46 47 48
   *
   * @var array
   */
  protected $info;

  /**
49
   * The configuration entity storage controller.
50 51 52 53 54
   *
   * @var Drupal\views\ViewStorageController
   */
  protected $controller;

55
  /**
56
   * Views used by this test.
57 58 59
   *
   * @var array
   */
60
  public static $testViews = array('test_view_storage');
61 62 63

  public static function getInfo() {
    return array(
64 65
      'name' => 'View storage tests',
      'description' => 'Tests the CRUD functionality for a view.',
66 67 68 69 70 71 72
      'group' => 'Views',
    );
  }

  /**
   * Tests CRUD operations.
   */
73 74
  function testConfigurationEntityCRUD() {
    // Get the configuration entity information and controller.
75
    $this->info = entity_get_info('view');
76
    $this->controller = $this->container->get('plugin.manager.entity')->getStorageController('view');
77

78
    // Confirm that an info array has been returned.
79
    $this->assertTrue(!empty($this->info) && is_array($this->info), 'The View info array is loaded.');
80

81
    // Confirm we have the correct controller class.
82 83 84 85 86 87
    $this->assertTrue($this->controller instanceof ViewStorageController, 'The correct controller is loaded.');

    // CRUD tests.
    $this->loadTests();
    $this->createTests();
    $this->displayTests();
88 89 90

    // Helper method tests
    $this->displayMethodTests();
91
  }
92

93
  /**
94
   * Tests loading configuration entities.
95 96
   */
  protected function loadTests() {
97 98
    $view = entity_load('view', 'test_view_storage');
    $data = config('views.view.test_view_storage')->get();
99

100 101
    // Confirm that an actual view object is loaded and that it returns all of
    // expected properties.
102
    $this->assertTrue($view instanceof View, 'Single View instance loaded.');
103
    foreach ($this->config_properties as $property) {
104
      $this->assertTrue($view->get($property) !== NULL, format_string('Property: @property loaded onto View.', array('@property' => $property)));
105 106 107
    }

    // Check the displays have been loaded correctly from config display data.
108
    $expected_displays = array('default', 'page_1', 'block_1');
109
    $this->assertEqual(array_keys($view->get('display')), $expected_displays, 'The correct display names are present.');
110

111 112
    // Check each ViewDisplay object and confirm that it has the correct key and
    // property values.
113
    foreach ($view->get('display') as $key => $display) {
114
      $this->assertEqual($key, $display['id'], 'The display has the correct ID assigned.');
115 116 117 118 119

      // Get original display data and confirm that the display options array
      // exists.
      $original_options = $data['display'][$key];
      foreach ($original_options as $orig_key => $value) {
120
        $this->assertIdentical($display[$orig_key], $value, format_string('@key is identical to saved data', array('@key' => $key)));
121
      }
122 123
    }

124
    // Make sure that loaded default views get a UUID.
125
    $view = views_get_view('test_view_storage');
126
    $this->assertTrue($view->storage->uuid());
127
  }
128

129
  /**
130
   * Tests creating configuration entities.
131 132
   */
  protected function createTests() {
133
    // Create a new View instance with empty values.
134
    $created = $this->controller->create(array());
135

136
    $this->assertTrue($created instanceof View, 'Created object is a View.');
137 138
    // Check that the View contains all of the properties.
    foreach ($this->config_properties as $property) {
139
      $this->assertTrue(property_exists($created, $property), format_string('Property: @property created on View.', array('@property' => $property)));
140 141 142
    }

    // Create a new View instance with config values.
143
    $values = config('views.view.test_view_storage')->get();
144
    $created = $this->controller->create($values);
145

146
    $this->assertTrue($created instanceof View, 'Created object is a View.');
147
    // Check that the View contains all of the properties.
damiankloip's avatar
damiankloip committed
148
    $properties = $this->config_properties;
149
    // Remove display from list.
damiankloip's avatar
damiankloip committed
150 151 152 153
    array_pop($properties);

    // Test all properties except displays.
    foreach ($properties as $property) {
154
      $this->assertTrue($created->get($property) !== NULL, format_string('Property: @property created on View.', array('@property' => $property)));
155
      $this->assertIdentical($values[$property], $created->get($property), format_string('Property value: @property matches configuration value.', array('@property' => $property)));
156
    }
damiankloip's avatar
damiankloip committed
157

158
    // Check the UUID of the loaded View.
159
    $created->set('id', 'test_view_storage_new');
160
    $created->save();
161
    $created_loaded = entity_load('view', 'test_view_storage_new');
162
    $this->assertIdentical($created->uuid(), $created_loaded->uuid(), 'The created UUID has been saved correctly.');
163 164 165
  }

  /**
166
   * Tests adding, saving, and loading displays on configuration entities.
167 168
   */
  protected function displayTests() {
169
    // Check whether a display can be added and saved to a View.
170
    $view = entity_load('view', 'test_view_storage_new');
171

172 173
    $new_id = $view->newDisplay('page', 'Test', 'test');
    $display = $view->get('display');
174

xjm's avatar
xjm committed
175
    // Ensure the right display_plugin is created/instantiated.
176 177 178 179
    $this->assertEqual($display[$new_id]['display_plugin'], 'page', 'New page display "test" uses the right display plugin.');

    $executable = $view->get('executable');
    $executable->initDisplay();
180
    $this->assertTrue($executable->displayHandlers->get($new_id) instanceof Page, 'New page display "test" uses the right display plugin.');
181

182
    $view->set('id', 'test_view_storage_new_new2');
183
    $view->save();
184
    $values = config('views.view.test_view_storage_new_new2')->get();
185 186

    $this->assertTrue(isset($values['display']['test']) && is_array($values['display']['test']), 'New display was saved.');
187
  }
damiankloip's avatar
damiankloip committed
188

189 190 191 192
  /**
   * Tests the display related functions like getDisplaysList().
   */
  protected function displayMethodTests() {
193 194
    // Enable the system module so l() can work using url_alias table.
    $this->enableModules(array('system'));
195
    $this->installSchema('system', 'url_alias');
196

197
    $config['display'] = array(
198
      'page_1' => array(
199 200 201
        'display_options' => array('path' => 'test'),
        'display_plugin' => 'page',
        'id' => 'page_2',
202
        'display_title' => 'Page 1',
203 204
        'position' => 1
      ),
205
      'feed_1' => array(
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        'display_options' => array('path' => 'test.xml'),
        'display_plugin' => 'feed',
        'id' => 'feed',
        'display_title' => 'Feed',
        'position' => 2
      ),
      'page_2' => array(
        'display_options' => array('path' => 'test/%/extra'),
        'display_plugin' => 'page',
        'id' => 'page_2',
        'display_title' => 'Page 2',
        'position' => 3
      )
    );
    $view = $this->controller->create($config);

    // Paths with a "%" shouldn't not be linked
    $expected_paths = array();
    $expected_paths[] = l('/test', 'test');
    $expected_paths[] = l('/test.xml', 'test.xml');
    $expected_paths[] = '/test/%/extra';

    $this->assertEqual($view->getPaths(), $expected_paths, 'Make sure the paths in the ui are generated as expected.');

230
    // Tests Drupal\views\Plugin\Core\Entity\View::addDisplay()
231 232 233 234
    $view = $this->controller->create(array());
    $random_title = $this->randomName();

    $id = $view->addDisplay('page', $random_title);
235
    $this->assertEqual($id, 'page_1', format_string('Make sure the first display (%id_new) has the expected ID (%id)', array('%id_new' => $id, '%id' => 'page_1')));
236 237
    $display = $view->get('display');
    $this->assertEqual($display[$id]['display_title'], $random_title);
238 239 240

    $random_title = $this->randomName();
    $id = $view->addDisplay('page', $random_title);
241
    $display = $view->get('display');
242
    $this->assertEqual($id, 'page_2', format_string('Make sure the second display (%id_new) has the expected ID (%id)', array('%id_new' => $id, '%id' => 'page_2')));
243
    $this->assertEqual($display[$id]['display_title'], $random_title);
244 245

    $id = $view->addDisplay('page');
246 247
    $display = $view->get('display');
    $this->assertEqual($display[$id]['display_title'], 'Page 3');
248

249
    // Tests Drupal\views\Plugin\Core\Entity\View::generateDisplayId().
250 251
    // @todo Sadly this method is not public so it cannot be tested.
    // $view = $this->controller->create(array());
252 253
    // $this->assertEqual($view->generateDisplayId('default'), 'default', 'The plugin ID for default is always default.');
    // $this->assertEqual($view->generateDisplayId('feed'), 'feed_1', 'The generated ID for the first instance of a plugin type should have an suffix of _1.');
254
    // $view->addDisplay('feed', 'feed title');
255
    // $this->assertEqual($view->generateDisplayId('feed'), 'feed_2', 'The generated ID for the first instance of a plugin type should have an suffix of _2.');
256

257
    // Tests Drupal\views\Plugin\Core\Entity\View::newDisplay().
258 259 260 261
    $view = $this->controller->create(array());
    $view->newDisplay('default');

    $display = $view->newDisplay('page');
262
    $this->assertEqual($display, 'page_1');
263
    $display = $view->newDisplay('page');
264
    $this->assertEqual($display, 'page_2');
265
    $display = $view->newDisplay('feed');
266 267 268 269 270
    $this->assertEqual($display, 'feed_1');

    $executable = $view->get('executable');
    $executable->initDisplay();

271 272 273 274 275 276
    $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('feed_1') instanceof Feed);
    $this->assertTrue($executable->displayHandlers->get('feed_1')->default_display instanceof DefaultDisplay);
277 278 279 280

    // Tests item related methods().
    $view = $this->controller->create(array('base_table' => 'views_test_data'));
    $view->addDisplay('default');
281
    $view = $view->get('executable');
282 283 284 285 286 287 288 289 290 291 292

    $display_id = 'default';
    $expected_items = array();
    // Tests addItem with getItem.
    // Therefore add one item without any optioins and one item with some
    // options.
    $id1 = $view->addItem($display_id, 'field', 'views_test_data', 'id');
    $item1 = $view->getItem($display_id, 'field', 'id');
    $expected_items[$id1] = $expected_item = array(
      'id' => 'id',
      'table' => 'views_test_data',
293 294
      'field' => 'id',
      'plugin_id' => 'numeric',
295
    );
296
    $this->assertEqual($item1, $expected_item);
297 298 299 300 301 302 303 304 305 306 307

    $options = array(
      'alter' => array(
        'text' => $this->randomName()
      )
    );
    $id2 = $view->addItem($display_id, 'field', 'views_test_data', 'name', $options);
    $item2 = $view->getItem($display_id, 'field', 'name');
    $expected_items[$id2] = $expected_item = array(
      'id' => 'name',
      'table' => 'views_test_data',
308 309
      'field' => 'name',
      'plugin_id' => 'standard',
310
    ) + $options;
311
    $this->assertEqual($item2, $expected_item);
312 313

    // Tests the expected fields from the previous additions.
314
    $this->assertEqual($view->getItems('field', $display_id), $expected_items);
315 316 317 318 319 320 321 322 323 324

    // Alter an existing item via setItem and check the result via getItem
    // and getItems.
    $item = array(
      'alter' => array(
        'text' => $this->randomName(),
      )
    ) + $item1;
    $expected_items[$id1] = $item;
    $view->setItem($display_id, 'field', $id1, $item);
325 326
    $this->assertEqual($view->getItem($display_id, 'field', 'id'), $item);
    $this->assertEqual($view->getItems('field', $display_id), $expected_items);
327 328 329 330 331

    // Test removeItem method.
    unset($expected_items[$id2]);
    $view->removeItem($display_id, 'field', $id2);
    $this->assertEqual($view->getItems('field', $display_id), $expected_items);
332 333
  }

334 335 336 337
  /**
   * Tests the createDuplicate() View method.
   */
  public function testCreateDuplicate() {
338
    $view = views_get_view('test_view_storage');
339
    $copy = $view->storage->createDuplicate();
340

341
    $this->assertTrue($copy instanceof View, 'The copied object is a View.');
342 343 344 345

    // Check that the original view and the copy have different UUIDs.
    $this->assertNotIdentical($view->storage->uuid(), $copy->uuid(), 'The copied view has a new UUID.');

346
    // Check the 'name' (ID) is using the View objects default value (NULL) as it
347
    // gets unset.
348
    $this->assertIdentical($copy->id(), NULL, 'The ID has been reset.');
349 350 351 352 353 354 355 356

    // Check the other properties.
    // @todo Create a reusable property on the base test class for these?
    $config_properties = array(
      'disabled',
      'description',
      'tag',
      'base_table',
357
      'label',
358 359 360 361
      'core',
    );

    foreach ($config_properties as $property) {
362
      $this->assertIdentical($view->storage->get($property), $copy->get($property), format_string('@property property is identical.', array('@property' => $property)));
363 364 365
    }

    // Check the displays are the same.
366
    $copy_display = $copy->get('display');
367
    foreach ($view->storage->get('display') as $id => $display) {
368
      // assertIdentical will not work here.
369
      $this->assertEqual($display, $copy_display[$id], format_string('The @display display has been copied correctly.', array('@display' => $id)));
370 371 372
    }
  }

373
}