summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrancesco Placella2018-11-15 09:25:23 (GMT)
committerFrancesco Placella2018-11-15 10:49:28 (GMT)
commita0c92ff16177170ac2e57b9f62356f39ed156704 (patch)
treec2f3e0fb5f0ec674ab136b4a0a0da8c1e512d56f
parent8f2862aaa66084d0a43610fb62ea737f02bb2219 (diff)
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 dd571111fc9c2db4667781beca4e29dc1bcd713b)
-rw-r--r--core/modules/content_moderation/src/EntityOperations.php13
-rw-r--r--core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php62
2 files changed, 69 insertions, 6 deletions
diff --git a/core/modules/content_moderation/src/EntityOperations.php b/core/modules/content_moderation/src/EntityOperations.php
index 9ab40ea..8c6d332 100644
--- a/core/modules/content_moderation/src/EntityOperations.php
+++ b/core/modules/content_moderation/src/EntityOperations.php
@@ -184,11 +184,16 @@ class EntityOperations implements ContainerInjectionInterface {
// 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 76af9ec..4b7d75b 100644
--- a/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/ContentModerationStateTest.php
@@ -416,26 +416,84 @@ class ContentModerationStateTest extends KernelTestBase {
/**
* 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.
*/
public function testNonTranslatableEntityTypeModeration() {