EntityApiTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\Core\Entity\EntityStorageException;
  4. use Drupal\entity_test\Entity\EntityTest;
  5. use Drupal\user\UserInterface;
  6. /**
  7. * Tests basic CRUD functionality.
  8. *
  9. * @group Entity
  10. */
  11. class EntityApiTest extends EntityKernelTestBase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. protected function setUp() {
  16. parent::setUp();
  17. foreach (entity_test_entity_types() as $entity_type_id) {
  18. // The entity_test schema is installed by the parent.
  19. if ($entity_type_id != 'entity_test') {
  20. $this->installEntitySchema($entity_type_id);
  21. }
  22. }
  23. }
  24. /**
  25. * Tests basic CRUD functionality of the Entity API.
  26. */
  27. public function testCRUD() {
  28. // All entity variations have to have the same results.
  29. foreach (entity_test_entity_types() as $entity_type) {
  30. $this->assertCRUD($entity_type, $this->createUser());
  31. }
  32. }
  33. /**
  34. * Executes a test set for a defined entity type and user.
  35. *
  36. * @param string $entity_type
  37. * The entity type to run the tests with.
  38. * @param \Drupal\user\UserInterface $user1
  39. * The user to run the tests with.
  40. */
  41. protected function assertCRUD($entity_type, UserInterface $user1) {
  42. // Create some test entities.
  43. $entity = $this->container->get('entity_type.manager')
  44. ->getStorage($entity_type)
  45. ->create(['name' => 'test', 'user_id' => $user1->id()]);
  46. $entity->save();
  47. $entity = $this->container->get('entity_type.manager')
  48. ->getStorage($entity_type)
  49. ->create(['name' => 'test2', 'user_id' => $user1->id()]);
  50. $entity->save();
  51. $entity = $this->container->get('entity_type.manager')
  52. ->getStorage($entity_type)
  53. ->create(['name' => 'test', 'user_id' => NULL]);
  54. $entity->save();
  55. /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
  56. $storage = $this->container->get('entity_type.manager')
  57. ->getStorage($entity_type);
  58. $entities = array_values($storage->loadByProperties(['name' => 'test']));
  59. $this->assertEqual($entities[0]->name->value, 'test', format_string('%entity_type: Created and loaded entity', ['%entity_type' => $entity_type]));
  60. $this->assertEqual($entities[1]->name->value, 'test', format_string('%entity_type: Created and loaded entity', ['%entity_type' => $entity_type]));
  61. // Test loading a single entity.
  62. $loaded_entity = $storage->load($entity->id());
  63. $this->assertEqual($loaded_entity->id(), $entity->id(), format_string('%entity_type: Loaded a single entity by id.', ['%entity_type' => $entity_type]));
  64. // Test deleting an entity.
  65. $entities = array_values($storage->loadByProperties(['name' => 'test2']));
  66. $entities[0]->delete();
  67. $entities = array_values($storage->loadByProperties(['name' => 'test2']));
  68. $this->assertEqual($entities, [], format_string('%entity_type: Entity deleted.', ['%entity_type' => $entity_type]));
  69. // Test updating an entity.
  70. $entities = array_values($storage->loadByProperties(['name' => 'test']));
  71. $entities[0]->name->value = 'test3';
  72. $entities[0]->save();
  73. $entity = $storage->load($entities[0]->id());
  74. $this->assertEqual($entity->name->value, 'test3', format_string('%entity_type: Entity updated.', ['%entity_type' => $entity_type]));
  75. // Try deleting multiple test entities by deleting all.
  76. $ids = array_keys($storage->loadMultiple());
  77. entity_delete_multiple($entity_type, $ids);
  78. $all = $storage->loadMultiple();
  79. $this->assertTrue(empty($all), format_string('%entity_type: Deleted all entities.', ['%entity_type' => $entity_type]));
  80. // Verify that all data got deleted.
  81. $definition = \Drupal::entityManager()->getDefinition($entity_type);
  82. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField(), 'Base table was emptied');
  83. if ($data_table = $definition->getDataTable()) {
  84. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $data_table . '}')->fetchField(), 'Data table was emptied');
  85. }
  86. if ($revision_table = $definition->getRevisionTable()) {
  87. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_table . '}')->fetchField(), 'Data table was emptied');
  88. }
  89. if ($revision_data_table = $definition->getRevisionDataTable()) {
  90. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_data_table . '}')->fetchField(), 'Data table was emptied');
  91. }
  92. // Test deleting a list of entities not indexed by entity id.
  93. $entities = [];
  94. $entity = entity_create($entity_type, ['name' => 'test', 'user_id' => $user1->id()]);
  95. $entity->save();
  96. $entities['test'] = $entity;
  97. $entity = entity_create($entity_type, ['name' => 'test2', 'user_id' => $user1->id()]);
  98. $entity->save();
  99. $entities['test2'] = $entity;
  100. $controller = \Drupal::entityManager()->getStorage($entity_type);
  101. $controller->delete($entities);
  102. // Verify that entities got deleted.
  103. $all = $storage->loadMultiple();
  104. $this->assertTrue(empty($all), format_string('%entity_type: Deleted all entities.', ['%entity_type' => $entity_type]));
  105. // Verify that all data got deleted from the tables.
  106. $definition = \Drupal::entityManager()->getDefinition($entity_type);
  107. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField(), 'Base table was emptied');
  108. if ($data_table = $definition->getDataTable()) {
  109. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $data_table . '}')->fetchField(), 'Data table was emptied');
  110. }
  111. if ($revision_table = $definition->getRevisionTable()) {
  112. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_table . '}')->fetchField(), 'Data table was emptied');
  113. }
  114. if ($revision_data_table = $definition->getRevisionDataTable()) {
  115. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {' . $revision_data_table . '}')->fetchField(), 'Data table was emptied');
  116. }
  117. }
  118. /**
  119. * Tests that exceptions are thrown when saving or deleting an entity.
  120. */
  121. public function testEntityStorageExceptionHandling() {
  122. $entity = EntityTest::create(['name' => 'test']);
  123. try {
  124. $GLOBALS['entity_test_throw_exception'] = TRUE;
  125. $entity->save();
  126. $this->fail('Entity presave EntityStorageException thrown but not caught.');
  127. }
  128. catch (EntityStorageException $e) {
  129. $this->assertEqual($e->getcode(), 1, 'Entity presave EntityStorageException caught.');
  130. }
  131. $entity = EntityTest::create(['name' => 'test2']);
  132. try {
  133. unset($GLOBALS['entity_test_throw_exception']);
  134. $entity->save();
  135. $this->pass('Exception presave not thrown and not caught.');
  136. }
  137. catch (EntityStorageException $e) {
  138. $this->assertNotEqual($e->getCode(), 1, 'Entity presave EntityStorageException caught.');
  139. }
  140. $entity = EntityTest::create(['name' => 'test3']);
  141. $entity->save();
  142. try {
  143. $GLOBALS['entity_test_throw_exception'] = TRUE;
  144. $entity->delete();
  145. $this->fail('Entity predelete EntityStorageException not thrown.');
  146. }
  147. catch (EntityStorageException $e) {
  148. $this->assertEqual($e->getCode(), 2, 'Entity predelete EntityStorageException caught.');
  149. }
  150. unset($GLOBALS['entity_test_throw_exception']);
  151. $entity = EntityTest::create(['name' => 'test4']);
  152. $entity->save();
  153. try {
  154. $entity->delete();
  155. $this->pass('Entity predelete EntityStorageException not thrown and not caught.');
  156. }
  157. catch (EntityStorageException $e) {
  158. $this->assertNotEqual($e->getCode(), 2, 'Entity predelete EntityStorageException thrown.');
  159. }
  160. }
  161. /**
  162. * Tests that resaving a revision with a different revision ID throws an exception.
  163. */
  164. public function testUpdateWithRevisionId() {
  165. $storage = \Drupal::entityTypeManager()->getStorage('entity_test_mulrev');
  166. // Create a new entity.
  167. /** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity */
  168. $entity = $storage->create(['name' => 'revision_test']);
  169. $entity->save();
  170. $this->setExpectedException(EntityStorageException::class, "Update existing 'entity_test_mulrev' entity revision while changing the revision ID is not supported.");
  171. $entity->revision_id = 60;
  172. $entity->save();
  173. }
  174. /**
  175. * Tests that resaving an entity with a different entity ID throws an exception.
  176. */
  177. public function testUpdateWithId() {
  178. $storage = \Drupal::entityTypeManager()->getStorage('entity_test_mulrev');
  179. // Create a new entity.
  180. /** @var \Drupal\entity_test\Entity\EntityTestMulRev $entity */
  181. $entity = $storage->create(['name' => 'revision_test']);
  182. $entity->save();
  183. $this->setExpectedException(EntityStorageException::class, "Update existing 'entity_test_mulrev' entity while changing the ID is not supported.");
  184. $entity->id = 60;
  185. $entity->save();
  186. }
  187. }