/web/core/modules/content_translation/tests/src/Kernel/ContentTranslationEntityBundleInfoTest.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 151 lines · 78 code · 20 blank · 53 comment · 0 complexity · fdc19ea02663944cf8fe115f6e2ff5a8 MD5 · raw file

  1. <?php
  2. namespace Drupal\Tests\content_translation\Kernel;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Drupal\entity_test\Entity\EntityTestMul;
  5. use Drupal\language\Entity\ConfigurableLanguage;
  6. use Drupal\node\Entity\Node;
  7. use Drupal\node\Entity\NodeType;
  8. /**
  9. * Tests the Content Translation bundle info logic.
  10. *
  11. * @group content_translation
  12. */
  13. class ContentTranslationEntityBundleInfoTest extends KernelTestBase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected static $modules = [
  18. 'system',
  19. 'node',
  20. 'user',
  21. 'language',
  22. 'content_translation_test',
  23. 'content_translation',
  24. 'entity_test',
  25. ];
  26. /**
  27. * The content translation manager.
  28. *
  29. * @var \Drupal\content_translation\ContentTranslationManagerInterface
  30. */
  31. protected $contentTranslationManager;
  32. /**
  33. * The bundle info service.
  34. *
  35. * @var \Drupal\Core\Entity\EntityTypeBundleInfo
  36. */
  37. protected $bundleInfo;
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->contentTranslationManager = $this->container->get('content_translation.manager');
  44. $this->bundleInfo = $this->container->get('entity_type.bundle.info');
  45. $this->installEntitySchema('entity_test_mul');
  46. ConfigurableLanguage::createFromLangcode('it')->save();
  47. }
  48. /**
  49. * Tests that modules can know whether bundles are translatable.
  50. */
  51. public function testHookInvocationOrder() {
  52. $this->contentTranslationManager->setEnabled('entity_test_mul', 'entity_test_mul', TRUE);
  53. $this->bundleInfo->clearCachedBundles();
  54. $this->bundleInfo->getAllBundleInfo();
  55. // Verify that the test module comes first in the module list, which would
  56. // normally make its hook implementation to be invoked first.
  57. /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
  58. $module_handler = $this->container->get('module_handler');
  59. $module_list = $module_handler->getModuleList();
  60. $expected_modules = [
  61. 'content_translation_test',
  62. 'content_translation',
  63. ];
  64. $actual_modules = array_keys(array_intersect_key($module_list, array_flip($expected_modules)));
  65. $this->assertEquals($expected_modules, $actual_modules);
  66. // Check that the "content_translation_test" hook implementation has access
  67. // to the "translatable" bundle info property.
  68. /** @var \Drupal\Core\State\StateInterface $state */
  69. $state = $this->container->get('state');
  70. $this->assertTrue($state->get('content_translation_test.translatable'));
  71. }
  72. /**
  73. * Tests that field synchronization is skipped for disabled bundles.
  74. */
  75. public function testFieldSynchronizationWithDisabledBundle() {
  76. $entity = EntityTestMul::create();
  77. $entity->save();
  78. /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
  79. $translation = $entity->addTranslation('it');
  80. $translation->save();
  81. $this->assertTrue($entity->isTranslatable());
  82. }
  83. /**
  84. * Tests that bundle translation settings are propagated on creation.
  85. *
  86. * @throws \Drupal\Core\Entity\EntityStorageException
  87. */
  88. public function testBundleClearOnLanguageContentSettingInsert() {
  89. $node = $this->getBundledNode();
  90. $this->assertFalse($node->isTranslatable());
  91. $this->contentTranslationManager->setEnabled('node', 'bundle_test', TRUE);
  92. $this->assertTrue($node->isTranslatable(), "Bundle info was not cleared on language_content_settings creation.");
  93. }
  94. /**
  95. * Tests that bundle translation setting changes are propagated.
  96. *
  97. * @throws \Drupal\Core\Entity\EntityStorageException
  98. * @throws \Exception
  99. */
  100. public function testBundleClearOnLanguageContentSettingUpdate() {
  101. $node = $this->getBundledNode();
  102. $this->assertFalse($node->isTranslatable());
  103. $this->container->get('entity_type.manager')->getStorage('language_content_settings')->create([
  104. 'target_entity_type_id' => 'node',
  105. 'target_bundle' => 'bundle_test',
  106. ])->save();
  107. $this->assertFalse($node->isTranslatable());
  108. $this->contentTranslationManager->setEnabled('node', 'bundle_test', TRUE);
  109. $this->assertTrue($node->isTranslatable(), "Bundle info was not cleared on language_content_settings update.");
  110. }
  111. /**
  112. * Gets a new bundled node for testing.
  113. *
  114. * @return \Drupal\node\Entity\Node
  115. * The new node.
  116. *
  117. * @throws \Drupal\Core\Entity\EntityStorageException
  118. */
  119. protected function getBundledNode() {
  120. $this->installEntitySchema('node');
  121. $bundle = NodeType::create([
  122. 'type' => 'bundle_test',
  123. 'label' => 'Bundle Test',
  124. ]);
  125. $bundle->save();
  126. $node = Node::create([
  127. 'type' => 'bundle_test',
  128. ]);
  129. return $node;
  130. }
  131. }