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

/web/core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 314 lines | 180 code | 52 blank | 82 comment | 2 complexity | a2f238dfc32f000a70ddc6235ccd818f MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\Core\Entity;
  3. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  4. use Drupal\Core\Cache\Cache;
  5. use Drupal\Core\Cache\CacheBackendInterface;
  6. use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\Core\Entity\EntityTypeBundleInfo;
  9. use Drupal\Core\Entity\EntityTypeInterface;
  10. use Drupal\Core\Entity\EntityTypeManagerInterface;
  11. use Drupal\Core\Extension\ModuleHandlerInterface;
  12. use Drupal\Core\Language\Language;
  13. use Drupal\Core\Language\LanguageManagerInterface;
  14. use Drupal\Core\TypedData\TypedDataManagerInterface;
  15. use Drupal\Tests\UnitTestCase;
  16. use Prophecy\Argument;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. /**
  19. * @coversDefaultClass \Drupal\Core\Entity\EntityTypeBundleInfo
  20. * @group Entity
  21. */
  22. class EntityTypeBundleInfoTest extends UnitTestCase {
  23. /**
  24. * The module handler.
  25. *
  26. * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
  27. */
  28. protected $moduleHandler;
  29. /**
  30. * The cache backend to use.
  31. *
  32. * @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ProphecyInterface
  33. */
  34. protected $cacheBackend;
  35. /**
  36. * The cache tags invalidator.
  37. *
  38. * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\Prophecy\Prophecy\ProphecyInterface
  39. */
  40. protected $cacheTagsInvalidator;
  41. /**
  42. * The typed data manager.
  43. *
  44. * @var \Drupal\Core\TypedData\TypedDataManagerInterface|\Prophecy\Prophecy\ProphecyInterface
  45. */
  46. protected $typedDataManager;
  47. /**
  48. * The entity type manager.
  49. *
  50. * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
  51. */
  52. protected $entityTypeManager;
  53. /**
  54. * The language manager.
  55. *
  56. * @var \Drupal\Core\Language\LanguageManagerInterface
  57. */
  58. protected $languageManager;
  59. /**
  60. * The entity type bundle info under test.
  61. *
  62. * @var \Drupal\Core\Entity\EntityTypeBundleInfo
  63. */
  64. protected $entityTypeBundleInfo;
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function setUp(): void {
  69. parent::setUp();
  70. $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
  71. $this->moduleHandler->getImplementations('entity_type_build')->willReturn([]);
  72. $this->moduleHandler->alter('entity_type', Argument::type('array'))->willReturn(NULL);
  73. $this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
  74. $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
  75. $this->cacheTagsInvalidator = $this->prophesize(CacheTagsInvalidatorInterface::class);
  76. $language = new Language(['id' => 'en']);
  77. $this->languageManager = $this->prophesize(LanguageManagerInterface::class);
  78. $this->languageManager->getCurrentLanguage()->willReturn($language);
  79. $this->languageManager->getLanguages()->willReturn(['en' => (object) ['id' => 'en']]);
  80. $this->typedDataManager = $this->prophesize(TypedDataManagerInterface::class);
  81. $this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
  82. $container = $this->prophesize(ContainerInterface::class);
  83. $container->get('cache_tags.invalidator')->willReturn($this->cacheTagsInvalidator->reveal());
  84. // $container->get('typed_data_manager')->willReturn($this->typedDataManager->reveal());
  85. \Drupal::setContainer($container->reveal());
  86. $this->entityTypeBundleInfo = new EntityTypeBundleInfo($this->entityTypeManager->reveal(), $this->languageManager->reveal(), $this->moduleHandler->reveal(), $this->typedDataManager->reveal(), $this->cacheBackend->reveal());
  87. }
  88. /**
  89. * Sets up the entity type manager to be tested.
  90. *
  91. * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions
  92. * (optional) An array of entity type definitions.
  93. */
  94. protected function setUpEntityTypeDefinitions($definitions = []) {
  95. $class = $this->getMockClass(EntityInterface::class);
  96. foreach ($definitions as $key => $entity_type) {
  97. // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
  98. // by \Drupal\Core\Entity\EntitTypeManager::processDefinition() so it must
  99. // always be mocked.
  100. $entity_type->getLinkTemplates()->willReturn([]);
  101. // Give the entity type a legitimate class to return.
  102. $entity_type->getClass()->willReturn($class);
  103. $definitions[$key] = $entity_type->reveal();
  104. }
  105. $this->entityTypeManager->getDefinition(Argument::cetera())
  106. ->will(function ($args) use ($definitions) {
  107. $entity_type_id = $args[0];
  108. $exception_on_invalid = $args[1];
  109. if (isset($definitions[$entity_type_id])) {
  110. return $definitions[$entity_type_id];
  111. }
  112. elseif (!$exception_on_invalid) {
  113. return NULL;
  114. }
  115. else {
  116. throw new PluginNotFoundException($entity_type_id);
  117. }
  118. });
  119. $this->entityTypeManager->getDefinitions()->willReturn($definitions);
  120. }
  121. /**
  122. * Tests the clearCachedBundles() method.
  123. *
  124. * @covers ::clearCachedBundles
  125. */
  126. public function testClearCachedBundles() {
  127. $this->setUpEntityTypeDefinitions();
  128. $this->typedDataManager->clearCachedDefinitions()->shouldBeCalled();
  129. $this->cacheTagsInvalidator->invalidateTags(['entity_bundles'])->shouldBeCalled();
  130. $this->entityTypeBundleInfo->clearCachedBundles();
  131. }
  132. /**
  133. * Tests the getBundleInfo() method.
  134. *
  135. * @covers ::getBundleInfo
  136. *
  137. * @dataProvider providerTestGetBundleInfo
  138. */
  139. public function testGetBundleInfo($entity_type_id, $expected) {
  140. $this->moduleHandler->invokeAll('entity_bundle_info')->willReturn([]);
  141. $this->moduleHandler->alter('entity_bundle_info', Argument::type('array'))->willReturn(NULL);
  142. $apple = $this->prophesize(EntityTypeInterface::class);
  143. $apple->getLabel()->willReturn('Apple');
  144. $apple->getBundleEntityType()->willReturn(NULL);
  145. $banana = $this->prophesize(EntityTypeInterface::class);
  146. $banana->getLabel()->willReturn('Banana');
  147. $banana->getBundleEntityType()->willReturn(NULL);
  148. $this->setUpEntityTypeDefinitions([
  149. 'apple' => $apple,
  150. 'banana' => $banana,
  151. ]);
  152. $bundle_info = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
  153. $this->assertSame($expected, $bundle_info);
  154. }
  155. /**
  156. * Provides test data for testGetBundleInfo().
  157. *
  158. * @return array
  159. * Test data.
  160. */
  161. public function providerTestGetBundleInfo() {
  162. return [
  163. ['apple', [
  164. 'apple' => ['label' => 'Apple'],
  165. ],
  166. ],
  167. ['banana', [
  168. 'banana' => ['label' => 'Banana'],
  169. ],
  170. ],
  171. ['pear', []],
  172. ];
  173. }
  174. /**
  175. * Tests the getAllBundleInfo() method.
  176. *
  177. * @covers ::getAllBundleInfo
  178. */
  179. public function testGetAllBundleInfo() {
  180. $this->moduleHandler->invokeAll('entity_bundle_info')->willReturn([]);
  181. $this->moduleHandler->alter('entity_bundle_info', Argument::type('array'))->willReturn(NULL);
  182. $apple = $this->prophesize(EntityTypeInterface::class);
  183. $apple->getLabel()->willReturn('Apple');
  184. $apple->getBundleEntityType()->willReturn(NULL);
  185. $banana = $this->prophesize(EntityTypeInterface::class);
  186. $banana->getLabel()->willReturn('Banana');
  187. $banana->getBundleEntityType()->willReturn(NULL);
  188. $this->setUpEntityTypeDefinitions([
  189. 'apple' => $apple,
  190. 'banana' => $banana,
  191. ]);
  192. $this->cacheBackend->get('entity_bundle_info:en')->willReturn(FALSE);
  193. $this->cacheBackend->set('entity_bundle_info:en', Argument::any(), Cache::PERMANENT, ['entity_types', 'entity_bundles'])
  194. ->will(function () {
  195. $this->get('entity_bundle_info:en')
  196. ->willReturn((object) ['data' => 'cached data'])
  197. ->shouldBeCalled();
  198. })
  199. ->shouldBeCalled();
  200. $this->cacheTagsInvalidator->invalidateTags(['entity_bundles'])->shouldBeCalled();
  201. $this->typedDataManager->clearCachedDefinitions()->shouldBeCalled();
  202. $expected = [
  203. 'apple' => [
  204. 'apple' => [
  205. 'label' => 'Apple',
  206. ],
  207. ],
  208. 'banana' => [
  209. 'banana' => [
  210. 'label' => 'Banana',
  211. ],
  212. ],
  213. ];
  214. $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
  215. $this->assertSame($expected, $bundle_info);
  216. $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
  217. $this->assertSame($expected, $bundle_info);
  218. $this->entityTypeBundleInfo->clearCachedBundles();
  219. $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
  220. $this->assertSame('cached data', $bundle_info);
  221. }
  222. /**
  223. * @covers ::getAllBundleInfo
  224. */
  225. public function testGetAllBundleInfoWithEntityBundleInfo() {
  226. // Ensure that EntityTypeBundleInfo::getAllBundleInfo() does not add
  227. // additional bundles if hook_entity_bundle_info() defines some and the
  228. // entity_type does not define a bundle entity type.
  229. $this->moduleHandler->invokeAll('entity_bundle_info')->willReturn([
  230. 'banana' => [
  231. 'fig' => [
  232. 'label' => 'Fig banana',
  233. ],
  234. ],
  235. ]);
  236. $this->moduleHandler->alter('entity_bundle_info', Argument::type('array'))->willReturn(NULL);
  237. $apple = $this->prophesize(EntityTypeInterface::class);
  238. $apple->getLabel()->willReturn('Apple');
  239. $apple->getBundleEntityType()->willReturn(NULL);
  240. $banana = $this->prophesize(EntityTypeInterface::class);
  241. $banana->getLabel()->willReturn('Banana');
  242. $banana->getBundleEntityType()->willReturn(NULL);
  243. $this->setUpEntityTypeDefinitions([
  244. 'apple' => $apple,
  245. 'banana' => $banana,
  246. ]);
  247. $expected = [
  248. 'banana' => [
  249. 'fig' => [
  250. 'label' => 'Fig banana',
  251. ],
  252. ],
  253. 'apple' => [
  254. 'apple' => [
  255. 'label' => 'Apple',
  256. ],
  257. ],
  258. ];
  259. $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo();
  260. $this->assertSame($expected, $bundle_info);
  261. }
  262. }