diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php index 9ab40eac05691fb70202c4bbfb4f26377690d5a2..8c6d3325089840426b950cfebeaef7f20fe03ad8 100644 --- a/core/modules/content_moderation/src/EntityOperations.php +++ b/core/modules/content_moderation/src/EntityOperations.php @@ -184,11 +184,16 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) { // Sync translations. if ($entity->getEntityType()->hasKey('langcode')) { $entity_langcode = $entity->language()->getId(); - if (!$content_moderation_state->hasTranslation($entity_langcode)) { - $content_moderation_state->addTranslation($entity_langcode); + if ($entity->isDefaultTranslation()) { + $content_moderation_state->langcode = $entity_langcode; } - if ($content_moderation_state->language()->getId() !== $entity_langcode) { - $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode); + else { + if (!$content_moderation_state->hasTranslation($entity_langcode)) { + $content_moderation_state->addTranslation($entity_langcode); + } + if ($content_moderation_state->language()->getId() !== $entity_langcode) { + $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode); + } } } diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php index 76af9ecc07390e5a4e015d651851c97301c6ad6b..4b7d75be4dba4ab2db6258bccb6912c1650fee8d 100644 --- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php +++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php @@ -416,25 +416,83 @@ public function testModerationWithFieldConfigOverride() { /** * Tests that entities with special languages can be moderated. + * + * @dataProvider moderationWithSpecialLanguagesTestCases */ - public function testModerationWithSpecialLanguages() { + public function testModerationWithSpecialLanguages($original_language, $updated_language) { $workflow = $this->createEditorialWorkflow(); $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev'); $workflow->save(); // Create a test entity. $entity = EntityTestRev::create([ - 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + 'langcode' => $original_language, ]); $entity->save(); $this->assertEquals('draft', $entity->moderation_state->value); $entity->moderation_state->value = 'published'; + $entity->langcode = $updated_language; $entity->save(); $this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value); } + /** + * Test cases for ::testModerationWithSpecialLanguages(). + */ + public function moderationWithSpecialLanguagesTestCases() { + return [ + 'Not specified to not specified' => [ + LanguageInterface::LANGCODE_NOT_SPECIFIED, + LanguageInterface::LANGCODE_NOT_SPECIFIED, + ], + 'English to not specified' => [ + 'en', + LanguageInterface::LANGCODE_NOT_SPECIFIED, + ], + 'Not specified to english' => [ + LanguageInterface::LANGCODE_NOT_SPECIFIED, + 'en', + ], + ]; + } + + /** + * Test changing the language of content without adding a translation. + */ + public function testChangingContentLangcode() { + ConfigurableLanguage::createFromLangcode('fr')->save(); + NodeType::create([ + 'type' => 'test_type', + ])->save(); + $workflow = $this->createEditorialWorkflow(); + $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'test_type'); + $workflow->save(); + + $entity = Node::create([ + 'title' => 'Test node', + 'langcode' => 'en', + 'type' => 'test_type', + ]); + $entity->save(); + + $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity); + $this->assertCount(1, $entity->getTranslationLanguages()); + $this->assertCount(1, $content_moderation_state->getTranslationLanguages()); + $this->assertEquals('en', $entity->langcode->value); + $this->assertEquals('en', $content_moderation_state->langcode->value); + + $entity->langcode = 'fr'; + $entity->save(); + + $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity); + $this->assertCount(1, $entity->getTranslationLanguages()); + $this->assertCount(1, $content_moderation_state->getTranslationLanguages()); + $this->assertEquals('fr', $entity->langcode->value); + $this->assertEquals('fr', $content_moderation_state->langcode->value); + } + /** * Tests that a non-translatable entity type with a langcode can be moderated. */