PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views/tests/src/Kernel/ViewStorageTest.php

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