PageRenderTime 33ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/views/src/Tests/ViewStorageTest.php

https://gitlab.com/geeta7/drupal
PHP | 360 lines | 212 code | 52 blank | 96 comment | 1 complexity | c10320b92cd4b71dd6a91fa5bf009258 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\views\Tests\ViewStorageTest.
  5. */
  6. namespace Drupal\views\Tests;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\views\Entity\View;
  9. use Drupal\views\Plugin\views\display\Page;
  10. use Drupal\views\Views;
  11. /**
  12. * Tests the CRUD functionality for a view.
  13. *
  14. * @group views
  15. * @see \Drupal\views\Entity\View
  16. * @see \Drupal\Core\Config\Entity\ConfigEntityStorage
  17. */
  18. class ViewStorageTest extends ViewKernelTestBase {
  19. /**
  20. * Properties that should be stored in the configuration.
  21. *
  22. * @var array
  23. */
  24. protected $configProperties = array(
  25. 'status',
  26. 'module',
  27. 'id',
  28. 'description',
  29. 'tag',
  30. 'base_table',
  31. 'label',
  32. 'core',
  33. 'display',
  34. );
  35. /**
  36. * The entity type definition.
  37. *
  38. * @var \Drupal\Core\Entity\EntityTypeInterface
  39. */
  40. protected $entityType;
  41. /**
  42. * The configuration entity storage.
  43. *
  44. * @var \Drupal\Core\Config\Entity\ConfigEntityStorage
  45. */
  46. protected $controller;
  47. /**
  48. * Views used by this test.
  49. *
  50. * @var array
  51. */
  52. public static $testViews = array('test_view_storage');
  53. /**
  54. * Tests CRUD operations.
  55. */
  56. function testConfigurationEntityCRUD() {
  57. // Get the configuration entity type and controller.
  58. $this->entityType = \Drupal::entityManager()->getDefinition('view');
  59. $this->controller = $this->container->get('entity.manager')->getStorage('view');
  60. // Confirm that an info array has been returned.
  61. $this->assertTrue($this->entityType instanceof EntityTypeInterface, 'The View info array is loaded.');
  62. // CRUD tests.
  63. $this->loadTests();
  64. $this->createTests();
  65. $this->displayTests();
  66. // Helper method tests
  67. $this->displayMethodTests();
  68. }
  69. /**
  70. * Tests loading configuration entities.
  71. */
  72. protected function loadTests() {
  73. $view = entity_load('view', 'test_view_storage');
  74. $data = $this->config('views.view.test_view_storage')->get();
  75. // Confirm that an actual view object is loaded and that it returns all of
  76. // expected properties.
  77. $this->assertTrue($view instanceof View, 'Single View instance loaded.');
  78. foreach ($this->configProperties as $property) {
  79. $this->assertTrue($view->get($property) !== NULL, format_string('Property: @property loaded onto View.', array('@property' => $property)));
  80. }
  81. // Check the displays have been loaded correctly from config display data.
  82. $expected_displays = array('default', 'block_1', 'page_1');
  83. $this->assertEqual(array_keys($view->get('display')), $expected_displays, 'The correct display names are present.');
  84. // Check each ViewDisplay object and confirm that it has the correct key and
  85. // property values.
  86. foreach ($view->get('display') as $key => $display) {
  87. $this->assertEqual($key, $display['id'], 'The display has the correct ID assigned.');
  88. // Get original display data and confirm that the display options array
  89. // exists.
  90. $original_options = $data['display'][$key];
  91. foreach ($original_options as $orig_key => $value) {
  92. $this->assertIdentical($display[$orig_key], $value, format_string('@key is identical to saved data', array('@key' => $key)));
  93. }
  94. }
  95. // Make sure that loaded default views get a UUID.
  96. $view = Views::getView('test_view_storage');
  97. $this->assertTrue($view->storage->uuid());
  98. }
  99. /**
  100. * Tests creating configuration entities.
  101. */
  102. protected function createTests() {
  103. // Create a new View instance with empty values.
  104. $created = $this->controller->create(array());
  105. $this->assertTrue($created instanceof View, 'Created object is a View.');
  106. // Check that the View contains all of the properties.
  107. foreach ($this->configProperties as $property) {
  108. $this->assertTrue(property_exists($created, $property), format_string('Property: @property created on View.', array('@property' => $property)));
  109. }
  110. // Create a new View instance with config values.
  111. $values = $this->config('views.view.test_view_storage')->get();
  112. $values['id'] = 'test_view_storage_new';
  113. unset($values['uuid']);
  114. $created = $this->controller->create($values);
  115. $this->assertTrue($created instanceof View, 'Created object is a View.');
  116. // Check that the View contains all of the properties.
  117. $properties = $this->configProperties;
  118. // Remove display from list.
  119. array_pop($properties);
  120. // Test all properties except displays.
  121. foreach ($properties as $property) {
  122. $this->assertTrue($created->get($property) !== NULL, format_string('Property: @property created on View.', array('@property' => $property)));
  123. $this->assertIdentical($values[$property], $created->get($property), format_string('Property value: @property matches configuration value.', array('@property' => $property)));
  124. }
  125. // Check the UUID of the loaded View.
  126. $created->save();
  127. $created_loaded = entity_load('view', 'test_view_storage_new');
  128. $this->assertIdentical($created->uuid(), $created_loaded->uuid(), 'The created UUID has been saved correctly.');
  129. }
  130. /**
  131. * Tests adding, saving, and loading displays on configuration entities.
  132. */
  133. protected function displayTests() {
  134. // Check whether a display can be added and saved to a View.
  135. $view = entity_load('view', 'test_view_storage_new');
  136. $new_id = $view->addDisplay('page', 'Test', 'test');
  137. $display = $view->get('display');
  138. // Ensure the right display_plugin is created/instantiated.
  139. $this->assertEqual($display[$new_id]['display_plugin'], 'page', 'New page display "test" uses the right display plugin.');
  140. $executable = $view->getExecutable();
  141. $executable->initDisplay();
  142. $this->assertTrue($executable->displayHandlers->get($new_id) instanceof Page, 'New page display "test" uses the right display plugin.');
  143. // To save this with a new ID, we should use createDuplicate().
  144. $view = $view->createDuplicate();
  145. $view->set('id', 'test_view_storage_new_new2');
  146. $view->save();
  147. $values = $this->config('views.view.test_view_storage_new_new2')->get();
  148. $this->assertTrue(isset($values['display']['test']) && is_array($values['display']['test']), 'New display was saved.');
  149. }
  150. /**
  151. * Tests the display related functions like getDisplaysList().
  152. */
  153. protected function displayMethodTests() {
  154. // Enable the system module so the link generator can work using url_alias
  155. // table.
  156. $this->installSchema('system', 'url_alias');
  157. $config['display'] = array(
  158. 'page_1' => array(
  159. 'display_options' => array('path' => 'test'),
  160. 'display_plugin' => 'page',
  161. 'id' => 'page_2',
  162. 'display_title' => 'Page 1',
  163. 'position' => 1
  164. ),
  165. 'feed_1' => array(
  166. 'display_options' => array('path' => 'test.xml'),
  167. 'display_plugin' => 'feed',
  168. 'id' => 'feed',
  169. 'display_title' => 'Feed',
  170. 'position' => 2
  171. ),
  172. 'page_2' => array(
  173. 'display_options' => array('path' => 'test/%/extra'),
  174. 'display_plugin' => 'page',
  175. 'id' => 'page_2',
  176. 'display_title' => 'Page 2',
  177. 'position' => 3
  178. )
  179. );
  180. $view = $this->controller->create($config);
  181. // Tests Drupal\views\Entity\View::addDisplay()
  182. $view = $this->controller->create(array());
  183. $random_title = $this->randomMachineName();
  184. $id = $view->addDisplay('page', $random_title);
  185. $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')));
  186. $display = $view->get('display');
  187. $this->assertEqual($display[$id]['display_title'], $random_title);
  188. $random_title = $this->randomMachineName();
  189. $id = $view->addDisplay('page', $random_title);
  190. $display = $view->get('display');
  191. $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')));
  192. $this->assertEqual($display[$id]['display_title'], $random_title);
  193. $id = $view->addDisplay('page');
  194. $display = $view->get('display');
  195. $this->assertEqual($display[$id]['display_title'], 'Page 3');
  196. // Ensure the 'default' display always has position zero, regardless of when
  197. // it was set relative to other displays. Even if the 'default' display
  198. // exists, adding it again will overwrite it, which is asserted with the new
  199. // title.
  200. $view->addDisplay('default', $random_title);
  201. $displays = $view->get('display');
  202. $this->assertEqual($displays['default']['display_title'], $random_title, 'Default display is defined with the new title');
  203. $this->assertEqual($displays['default']['position'], 0, 'Default displays are always in position zero');
  204. // Tests Drupal\views\Entity\View::generateDisplayId(). Since
  205. // generateDisplayId() is protected, we have to use reflection to unit-test
  206. // it.
  207. $view = $this->controller->create(array());
  208. $ref_generate_display_id = new \ReflectionMethod($view, 'generateDisplayId');
  209. $ref_generate_display_id->setAccessible(TRUE);
  210. $this->assertEqual(
  211. $ref_generate_display_id->invoke($view, 'default'),
  212. 'default',
  213. 'The plugin ID for default is always default.'
  214. );
  215. $this->assertEqual(
  216. $ref_generate_display_id->invoke($view, 'feed'),
  217. 'feed_1',
  218. 'The generated ID for the first instance of a plugin type should have an suffix of _1.'
  219. );
  220. $view->addDisplay('feed', 'feed title');
  221. $this->assertEqual(
  222. $ref_generate_display_id->invoke($view, 'feed'),
  223. 'feed_2',
  224. 'The generated ID for the first instance of a plugin type should have an suffix of _2.'
  225. );
  226. // Tests item related methods().
  227. $view = $this->controller->create(array('base_table' => 'views_test_data'));
  228. $view->addDisplay('default');
  229. $view = $view->getExecutable();
  230. $display_id = 'default';
  231. $expected_items = array();
  232. // Tests addHandler with getItem.
  233. // Therefore add one item without any options and one item with some
  234. // options.
  235. $id1 = $view->addHandler($display_id, 'field', 'views_test_data', 'id');
  236. $item1 = $view->getHandler($display_id, 'field', 'id');
  237. $expected_items[$id1] = $expected_item = array(
  238. 'id' => 'id',
  239. 'table' => 'views_test_data',
  240. 'field' => 'id',
  241. 'plugin_id' => 'numeric',
  242. );
  243. $this->assertEqual($item1, $expected_item);
  244. $options = array(
  245. 'alter' => array(
  246. 'text' => $this->randomMachineName()
  247. )
  248. );
  249. $id2 = $view->addHandler($display_id, 'field', 'views_test_data', 'name', $options);
  250. $item2 = $view->getHandler($display_id, 'field', 'name');
  251. $expected_items[$id2] = $expected_item = array(
  252. 'id' => 'name',
  253. 'table' => 'views_test_data',
  254. 'field' => 'name',
  255. 'plugin_id' => 'standard',
  256. ) + $options;
  257. $this->assertEqual($item2, $expected_item);
  258. // Tests the expected fields from the previous additions.
  259. $this->assertEqual($view->getHandlers('field', $display_id), $expected_items);
  260. // Alter an existing item via setItem and check the result via getItem
  261. // and getItems.
  262. $item = array(
  263. 'alter' => array(
  264. 'text' => $this->randomMachineName(),
  265. )
  266. ) + $item1;
  267. $expected_items[$id1] = $item;
  268. $view->setHandler($display_id, 'field', $id1, $item);
  269. $this->assertEqual($view->getHandler($display_id, 'field', 'id'), $item);
  270. $this->assertEqual($view->getHandlers('field', $display_id), $expected_items);
  271. // Test removeItem method.
  272. unset($expected_items[$id2]);
  273. $view->removeHandler($display_id, 'field', $id2);
  274. $this->assertEqual($view->getHandlers('field', $display_id), $expected_items);
  275. }
  276. /**
  277. * Tests the createDuplicate() View method.
  278. */
  279. public function testCreateDuplicate() {
  280. $view = Views::getView('test_view_storage');
  281. $copy = $view->storage->createDuplicate();
  282. $this->assertTrue($copy instanceof View, 'The copied object is a View.');
  283. // Check that the original view and the copy have different UUIDs.
  284. $this->assertNotIdentical($view->storage->uuid(), $copy->uuid(), 'The copied view has a new UUID.');
  285. // Check the 'name' (ID) is using the View objects default value (NULL) as it
  286. // gets unset.
  287. $this->assertIdentical($copy->id(), NULL, 'The ID has been reset.');
  288. // Check the other properties.
  289. // @todo Create a reusable property on the base test class for these?
  290. $config_properties = array(
  291. 'disabled',
  292. 'description',
  293. 'tag',
  294. 'base_table',
  295. 'label',
  296. 'core',
  297. );
  298. foreach ($config_properties as $property) {
  299. $this->assertIdentical($view->storage->get($property), $copy->get($property), format_string('@property property is identical.', array('@property' => $property)));
  300. }
  301. // Check the displays are the same.
  302. $copy_display = $copy->get('display');
  303. foreach ($view->storage->get('display') as $id => $display) {
  304. // assertIdentical will not work here.
  305. $this->assertEqual($display, $copy_display[$id], format_string('The @display display has been copied correctly.', array('@display' => $id)));
  306. }
  307. }
  308. }