PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/Entity/Update/UpdateApiEntityDefinitionUpdateTest.php

https://gitlab.com/reasonat/test8
PHP | 209 lines | 108 code | 35 blank | 66 comment | 0 complexity | b7232946833ae6de6897f337b623208d MD5 | raw file
  1. <?php
  2. namespace Drupal\system\Tests\Entity\Update;
  3. use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
  4. use Drupal\entity_test\Entity\EntityTest;
  5. use Drupal\simpletest\WebTestBase;
  6. use Drupal\system\Tests\Update\DbUpdatesTrait;
  7. /**
  8. * Tests performing entity updates through the Update API.
  9. *
  10. * @group Entity
  11. */
  12. class UpdateApiEntityDefinitionUpdateTest extends WebTestBase {
  13. use DbUpdatesTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected static $modules = ['entity_test'];
  18. /**
  19. * The entity manager.
  20. *
  21. * @var \Drupal\Core\Entity\EntityManagerInterface
  22. */
  23. protected $entityManager;
  24. /**
  25. * The entity definition update manager.
  26. *
  27. * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
  28. */
  29. protected $updatesManager;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function setUp() {
  34. parent::setUp();
  35. $this->entityManager = $this->container->get('entity.manager');
  36. $this->updatesManager = $this->container->get('entity.definition_update_manager');
  37. $admin = $this->drupalCreateUser([], FALSE, TRUE);
  38. $this->drupalLogin($admin);
  39. }
  40. /**
  41. * Tests that individual updates applied sequentially work as expected.
  42. */
  43. public function testSingleUpdates() {
  44. // Create a test entity.
  45. $user_ids = [mt_rand(), mt_rand()];
  46. $entity = EntityTest::create(['name' => $this->randomString(), 'user_id' => $user_ids]);
  47. $entity->save();
  48. // Check that only a single value is stored for 'user_id'.
  49. $entity = $this->reloadEntity($entity);
  50. $this->assertEqual(count($entity->user_id), 1);
  51. $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
  52. // Make 'user_id' multiple by applying updates.
  53. $this->enableUpdates('entity_test', 'entity_definition_updates', 8001);
  54. $this->applyUpdates();
  55. // Ensure the 'entity_test__user_id' table got created.
  56. $this->assertTrue(\Drupal::database()->schema()->tableExists('entity_test__user_id'));
  57. // Check that data was correctly migrated.
  58. $entity = $this->reloadEntity($entity);
  59. $this->assertEqual(count($entity->user_id), 1);
  60. $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
  61. // Store multiple data and check it is correctly stored.
  62. $entity->user_id = $user_ids;
  63. $entity->save();
  64. $entity = $this->reloadEntity($entity);
  65. $this->assertEqual(count($entity->user_id), 2);
  66. $this->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
  67. $this->assertEqual($entity->user_id[1]->target_id, $user_ids[1]);
  68. // Make 'user_id' single again by applying updates.
  69. $this->enableUpdates('entity_test', 'entity_definition_updates', 8002);
  70. $this->applyUpdates();
  71. // Check that data was correctly migrated/dropped.
  72. $entity = $this->reloadEntity($entity);
  73. $this->assertEqual(count($entity->user_id), 1);
  74. $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
  75. // Check that only a single value is stored for 'user_id' again.
  76. $entity->user_id = $user_ids;
  77. $entity->save();
  78. $entity = $this->reloadEntity($entity);
  79. $this->assertEqual(count($entity->user_id), 1);
  80. $this->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
  81. }
  82. /**
  83. * Tests that multiple updates applied in bulk work as expected.
  84. */
  85. public function testMultipleUpdates() {
  86. // Create a test entity.
  87. $user_ids = [mt_rand(), mt_rand()];
  88. $entity = EntityTest::create(['name' => $this->randomString(), 'user_id' => $user_ids]);
  89. $entity->save();
  90. // Check that only a single value is stored for 'user_id'.
  91. $entity = $this->reloadEntity($entity);
  92. $this->assertEqual(count($entity->user_id), 1);
  93. $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
  94. // Make 'user_id' multiple and then single again by applying updates.
  95. $this->enableUpdates('entity_test', 'entity_definition_updates', 8002);
  96. $this->applyUpdates();
  97. // Check that data was correctly migrated back and forth.
  98. $entity = $this->reloadEntity($entity);
  99. $this->assertEqual(count($entity->user_id), 1);
  100. $this->assertEqual($entity->user_id->target_id, $user_ids[0]);
  101. // Check that only a single value is stored for 'user_id' again.
  102. $entity->user_id = $user_ids;
  103. $entity->save();
  104. $entity = $this->reloadEntity($entity);
  105. $this->assertEqual(count($entity->user_id), 1);
  106. $this->assertEqual($entity->user_id[0]->target_id, $user_ids[0]);
  107. }
  108. /**
  109. * Tests that entity updates are correctly reported in the status report page.
  110. */
  111. function testStatusReport() {
  112. // Create a test entity.
  113. $entity = EntityTest::create(['name' => $this->randomString(), 'user_id' => mt_rand()]);
  114. $entity->save();
  115. // Check that the status report initially displays no error.
  116. $this->drupalGet('admin/reports/status');
  117. $this->assertNoRaw('Out of date');
  118. $this->assertNoRaw('Mismatched entity and/or field definitions');
  119. // Enable an entity update and check that we have a dedicated status report
  120. // item.
  121. $this->container->get('state')->set('entity_test.remove_name_field', TRUE);
  122. $this->drupalGet('admin/reports/status');
  123. $this->assertNoRaw('Out of date');
  124. $this->assertRaw('Mismatched entity and/or field definitions');
  125. // Enable a db update and check that now the entity update status report
  126. // item is no longer displayed. We assume an update function will fix the
  127. // mismatch.
  128. $this->enableUpdates('entity_test', 'status_report', 8001);
  129. $this->drupalGet('admin/reports/status');
  130. $this->assertRaw('Out of date');
  131. $this->assertRaw('Mismatched entity and/or field definitions');
  132. // Apply db updates and check that entity updates were not applied.
  133. $this->applyUpdates();
  134. $this->drupalGet('admin/reports/status');
  135. $this->assertNoRaw('Out of date');
  136. $this->assertRaw('Mismatched entity and/or field definitions');
  137. // Check that en exception would be triggered when trying to apply them with
  138. // existing data.
  139. $message = 'Entity updates cannot run if entity data exists.';
  140. try {
  141. $this->updatesManager->applyUpdates();
  142. $this->fail($message);
  143. }
  144. catch (FieldStorageDefinitionUpdateForbiddenException $e) {
  145. $this->pass($message);
  146. }
  147. // Check the status report is the same after trying to apply updates.
  148. $this->drupalGet('admin/reports/status');
  149. $this->assertNoRaw('Out of date');
  150. $this->assertRaw('Mismatched entity and/or field definitions');
  151. // Delete entity data, enable a new update, run updates again and check that
  152. // entity updates were not applied even when no data exists.
  153. $entity->delete();
  154. $this->enableUpdates('entity_test', 'status_report', 8002);
  155. $this->applyUpdates();
  156. $this->drupalGet('admin/reports/status');
  157. $this->assertNoRaw('Out of date');
  158. $this->assertRaw('Mismatched entity and/or field definitions');
  159. }
  160. /**
  161. * Reloads the specified entity.
  162. *
  163. * @param \Drupal\entity_test\Entity\EntityTest $entity
  164. * An entity object.
  165. *
  166. * @return \Drupal\entity_test\Entity\EntityTest
  167. * The reloaded entity object.
  168. */
  169. protected function reloadEntity(EntityTest $entity) {
  170. $this->entityManager->useCaches(FALSE);
  171. $this->entityManager->getStorage('entity_test')->resetCache([$entity->id()]);
  172. return EntityTest::load($entity->id());
  173. }
  174. }