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

/core/tests/Drupal/Tests/Core/Field/FieldDefinitionListenerTest.php

https://gitlab.com/geeta7/drupal
PHP | 275 lines | 168 code | 42 blank | 65 comment | 2 complexity | 529ceb9a14049b27822e88d3ecd6d5f4 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Field\FieldDefinitionListenerTest.
  5. */
  6. namespace Drupal\Tests\Core\Field;
  7. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  8. use Drupal\Core\Cache\CacheBackendInterface;
  9. use Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface;
  10. use Drupal\Core\Entity\EntityFieldManagerInterface;
  11. use Drupal\Core\Entity\EntityInterface;
  12. use Drupal\Core\Entity\EntityTypeInterface;
  13. use Drupal\Core\Entity\EntityTypeManagerInterface;
  14. use Drupal\Core\Field\FieldDefinitionInterface;
  15. use Drupal\Core\Field\FieldDefinitionListener;
  16. use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
  17. use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
  18. use Drupal\Tests\UnitTestCase;
  19. use Prophecy\Argument;
  20. /**
  21. * @coversDefaultClass \Drupal\Core\Field\FieldDefinitionListener
  22. * @group Field
  23. */
  24. class FieldDefinitionListenerTest extends UnitTestCase {
  25. /**
  26. * The key-value factory.
  27. *
  28. * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
  29. */
  30. protected $keyValueFactory;
  31. /**
  32. * The entity type manager.
  33. *
  34. * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
  35. */
  36. protected $entityTypeManager;
  37. /**
  38. * The entity field manager.
  39. *
  40. * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\Prophecy\Prophecy\ProphecyInterface
  41. */
  42. protected $entityFieldManager;
  43. /**
  44. * The cache backend.
  45. *
  46. * @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ProphecyInterface
  47. */
  48. protected $cacheBackend;
  49. /**
  50. * The field definition listener under test.
  51. *
  52. * @var \Drupal\Core\Field\FieldDefinitionListener
  53. */
  54. protected $fieldDefinitionListener;
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function setUp() {
  59. parent::setUp();
  60. $this->keyValueFactory = $this->prophesize(KeyValueFactoryInterface::class);
  61. $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
  62. $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
  63. $this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
  64. $this->fieldDefinitionListener = new FieldDefinitionListener($this->entityTypeManager->reveal(), $this->entityFieldManager->reveal(), $this->keyValueFactory->reveal(), $this->cacheBackend->reveal());
  65. }
  66. /**
  67. * Sets up the entity manager to be tested.
  68. *
  69. * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions
  70. * (optional) An array of entity type definitions.
  71. */
  72. protected function setUpEntityManager($definitions = array()) {
  73. $class = $this->getMockClass(EntityInterface::class);
  74. foreach ($definitions as $key => $entity_type) {
  75. // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
  76. // by \Drupal\Core\Entity\EntityManager::processDefinition() so it must
  77. // always be mocked.
  78. $entity_type->getLinkTemplates()->willReturn([]);
  79. // Give the entity type a legitimate class to return.
  80. $entity_type->getClass()->willReturn($class);
  81. $definitions[$key] = $entity_type->reveal();
  82. }
  83. $this->entityTypeManager->getDefinition(Argument::cetera())
  84. ->will(function ($args) use ($definitions) {
  85. $entity_type_id = $args[0];
  86. $exception_on_invalid = $args[1];
  87. if (isset($definitions[$entity_type_id])) {
  88. return $definitions[$entity_type_id];
  89. }
  90. elseif (!$exception_on_invalid) {
  91. return NULL;
  92. }
  93. else throw new PluginNotFoundException($entity_type_id);
  94. });
  95. $this->entityTypeManager->getDefinitions()->willReturn($definitions);
  96. }
  97. /**
  98. * @covers ::onFieldDefinitionCreate
  99. */
  100. public function testOnFieldDefinitionCreateNewField() {
  101. $field_definition = $this->prophesize(FieldDefinitionInterface::class);
  102. $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type');
  103. $field_definition->getTargetBundle()->willReturn('test_bundle');
  104. $field_definition->getName()->willReturn('test_field');
  105. $field_definition->getType()->willReturn('test_type');
  106. $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class);
  107. $storage->onFieldDefinitionCreate($field_definition->reveal())->shouldBeCalledTimes(1);
  108. $this->entityTypeManager->getStorage('test_entity_type')->willReturn($storage->reveal());
  109. $entity = $this->prophesize(EntityTypeInterface::class);
  110. $this->setUpEntityManager(['test_entity_type' => $entity]);
  111. // Set up the stored bundle field map.
  112. $key_value_store = $this->prophesize(KeyValueStoreInterface::class);
  113. $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal());
  114. $key_value_store->get('test_entity_type')->willReturn([]);
  115. $key_value_store->set('test_entity_type', [
  116. 'test_field' => [
  117. 'type' => 'test_type',
  118. 'bundles' => ['test_bundle' => 'test_bundle'],
  119. ],
  120. ])->shouldBeCalled();
  121. $this->fieldDefinitionListener->onFieldDefinitionCreate($field_definition->reveal());
  122. }
  123. /**
  124. * @covers ::onFieldDefinitionCreate
  125. */
  126. public function testOnFieldDefinitionCreateExistingField() {
  127. $field_definition = $this->prophesize(FieldDefinitionInterface::class);
  128. $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type');
  129. $field_definition->getTargetBundle()->willReturn('test_bundle');
  130. $field_definition->getName()->willReturn('test_field');
  131. $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class);
  132. $storage->onFieldDefinitionCreate($field_definition->reveal())->shouldBeCalledTimes(1);
  133. $this->entityTypeManager->getStorage('test_entity_type')->willReturn($storage->reveal());
  134. $entity = $this->prophesize(EntityTypeInterface::class);
  135. $this->setUpEntityManager(['test_entity_type' => $entity]);
  136. // Set up the stored bundle field map.
  137. $key_value_store = $this->prophesize(KeyValueStoreInterface::class);
  138. $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal());
  139. $key_value_store->get('test_entity_type')->willReturn([
  140. 'test_field' => [
  141. 'type' => 'test_type',
  142. 'bundles' => ['existing_bundle' => 'existing_bundle'],
  143. ],
  144. ]);
  145. $key_value_store->set('test_entity_type', [
  146. 'test_field' => [
  147. 'type' => 'test_type',
  148. 'bundles' => ['existing_bundle' => 'existing_bundle', 'test_bundle' => 'test_bundle'],
  149. ],
  150. ])
  151. ->shouldBeCalled();
  152. $this->fieldDefinitionListener->onFieldDefinitionCreate($field_definition->reveal());
  153. }
  154. /**
  155. * @covers ::onFieldDefinitionUpdate
  156. */
  157. public function testOnFieldDefinitionUpdate() {
  158. $field_definition = $this->prophesize(FieldDefinitionInterface::class);
  159. $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type');
  160. $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class);
  161. $storage->onFieldDefinitionUpdate($field_definition->reveal(), $field_definition->reveal())->shouldBeCalledTimes(1);
  162. $this->entityTypeManager->getStorage('test_entity_type')->willReturn($storage->reveal());
  163. $entity = $this->prophesize(EntityTypeInterface::class);
  164. $this->setUpEntityManager(['test_entity_type' => $entity]);
  165. $this->fieldDefinitionListener->onFieldDefinitionUpdate($field_definition->reveal(), $field_definition->reveal());
  166. }
  167. /**
  168. * @covers ::onFieldDefinitionDelete
  169. */
  170. public function testOnFieldDefinitionDeleteMultipleBundles() {
  171. $field_definition = $this->prophesize(FieldDefinitionInterface::class);
  172. $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type');
  173. $field_definition->getTargetBundle()->willReturn('test_bundle');
  174. $field_definition->getName()->willReturn('test_field');
  175. $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class);
  176. $storage->onFieldDefinitionDelete($field_definition->reveal())->shouldBeCalledTimes(1);
  177. $this->entityTypeManager->getStorage('test_entity_type')->willReturn($storage->reveal());
  178. $entity = $this->prophesize(EntityTypeInterface::class);
  179. $this->setUpEntityManager(['test_entity_type' => $entity]);
  180. // Set up the stored bundle field map.
  181. $key_value_store = $this->prophesize(KeyValueStoreInterface::class);
  182. $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal());
  183. $key_value_store->get('test_entity_type')->willReturn([
  184. 'test_field' => [
  185. 'type' => 'test_type',
  186. 'bundles' => ['test_bundle' => 'test_bundle'],
  187. ],
  188. 'second_field' => [
  189. 'type' => 'test_type',
  190. 'bundles' => ['test_bundle' => 'test_bundle'],
  191. ],
  192. ]);
  193. $key_value_store->set('test_entity_type', [
  194. 'second_field' => [
  195. 'type' => 'test_type',
  196. 'bundles' => ['test_bundle' => 'test_bundle'],
  197. ],
  198. ])
  199. ->shouldBeCalled();
  200. $this->fieldDefinitionListener->onFieldDefinitionDelete($field_definition->reveal());
  201. }
  202. /**
  203. * @covers ::onFieldDefinitionDelete
  204. */
  205. public function testOnFieldDefinitionDeleteSingleBundles() {
  206. $field_definition = $this->prophesize(FieldDefinitionInterface::class);
  207. $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type');
  208. $field_definition->getTargetBundle()->willReturn('test_bundle');
  209. $field_definition->getName()->willReturn('test_field');
  210. $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class);
  211. $storage->onFieldDefinitionDelete($field_definition->reveal())->shouldBeCalledTimes(1);
  212. $this->entityTypeManager->getStorage('test_entity_type')->willReturn($storage->reveal());
  213. $entity = $this->prophesize(EntityTypeInterface::class);
  214. $this->setUpEntityManager(['test_entity_type' => $entity]);
  215. // Set up the stored bundle field map.
  216. $key_value_store = $this->prophesize(KeyValueStoreInterface::class);
  217. $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal());
  218. $key_value_store->get('test_entity_type')->willReturn([
  219. 'test_field' => [
  220. 'type' => 'test_type',
  221. 'bundles' => ['test_bundle' => 'test_bundle', 'second_bundle' => 'second_bundle'],
  222. ],
  223. ]);
  224. $key_value_store->set('test_entity_type', [
  225. 'test_field' => [
  226. 'type' => 'test_type',
  227. 'bundles' => ['second_bundle' => 'second_bundle'],
  228. ],
  229. ])
  230. ->shouldBeCalled();
  231. $this->fieldDefinitionListener->onFieldDefinitionDelete($field_definition->reveal());
  232. }
  233. }