Commit a0c92ff1 authored by plach's avatar plach

Issue #2946402 by Sam152, awm, jibran, lamp5, mattshoaf, amateescu: Content...

Issue #2946402 by Sam152, awm, jibran, lamp5, mattshoaf, amateescu: Content moderation incorrectly always assumes a language is being added when the default language of an entity is being changed

(cherry picked from commit dd571111)
parent 8f2862aa
...@@ -184,11 +184,16 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) { ...@@ -184,11 +184,16 @@ protected function updateOrCreateFromEntity(EntityInterface $entity) {
// Sync translations. // Sync translations.
if ($entity->getEntityType()->hasKey('langcode')) { if ($entity->getEntityType()->hasKey('langcode')) {
$entity_langcode = $entity->language()->getId(); $entity_langcode = $entity->language()->getId();
if (!$content_moderation_state->hasTranslation($entity_langcode)) { if ($entity->isDefaultTranslation()) {
$content_moderation_state->addTranslation($entity_langcode); $content_moderation_state->langcode = $entity_langcode;
} }
if ($content_moderation_state->language()->getId() !== $entity_langcode) { else {
$content_moderation_state = $content_moderation_state->getTranslation($entity_langcode); 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);
}
} }
} }
......
...@@ -416,25 +416,83 @@ public function testModerationWithFieldConfigOverride() { ...@@ -416,25 +416,83 @@ public function testModerationWithFieldConfigOverride() {
/** /**
* Tests that entities with special languages can be moderated. * 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 = $this->createEditorialWorkflow();
$workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev'); $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
$workflow->save(); $workflow->save();
// Create a test entity. // Create a test entity.
$entity = EntityTestRev::create([ $entity = EntityTestRev::create([
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'langcode' => $original_language,
]); ]);
$entity->save(); $entity->save();
$this->assertEquals('draft', $entity->moderation_state->value); $this->assertEquals('draft', $entity->moderation_state->value);
$entity->moderation_state->value = 'published'; $entity->moderation_state->value = 'published';
$entity->langcode = $updated_language;
$entity->save(); $entity->save();
$this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value); $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. * Tests that a non-translatable entity type with a langcode can be moderated.
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment