From 27700fa12901df10ce5b6563d9d22a7091529b08 Mon Sep 17 00:00:00 2001 From: effulgentsia Date: Fri, 26 Jan 2018 22:07:07 -0800 Subject: [PATCH] Issue #2939795 by plach, Wim Leers: Multilingual logic is not applied when a new revision translation is being added --- .../Core/Entity/ContentEntityStorageBase.php | 20 ++++++++++++++++++- ...ntityDecoupledTranslationRevisionsTest.php | 14 ++++++++++--- .../Entity/EntityRevisionTranslationTest.php | 6 +++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index b82af9bd31a..538dffd55c4 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -152,6 +152,23 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con /** * Checks whether any entity revision is translated. * + * @param \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\TranslatableInterface $entity + * The entity object to be checked. + * + * @return bool + * TRUE if the entity has at least one translation in any revision, FALSE + * otherwise. + * + * @see \Drupal\Core\TypedData\TranslatableInterface::getTranslationLanguages() + * @see \Drupal\Core\Entity\ContentEntityStorageBase::isAnyStoredRevisionTranslated() + */ + protected function isAnyRevisionTranslated(TranslatableInterface $entity) { + return $entity->getTranslationLanguages(FALSE) || $this->isAnyStoredRevisionTranslated($entity); + } + + /** + * Checks whether any stored entity revision is translated. + * * A revisionable entity can have translations in a pending revision, hence * the default revision may appear as not translated. This determines whether * the entity has any translation in the storage and thus should be considered @@ -165,8 +182,9 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con * otherwise. * * @see \Drupal\Core\TypedData\TranslatableInterface::getTranslationLanguages() + * @see \Drupal\Core\Entity\ContentEntityStorageBase::isAnyRevisionTranslated() */ - protected function isAnyRevisionTranslated(TranslatableInterface $entity) { + protected function isAnyStoredRevisionTranslated(TranslatableInterface $entity) { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ if ($entity->isNew()) { return FALSE; diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php index ab01d915292..d5cc5330e65 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php @@ -359,12 +359,20 @@ class EntityDecoupledTranslationRevisionsTest extends EntityKernelTestBase { $previous_label = NULL; if (!$entity->isNewTranslation()) { $previous_label = $this->generateNewEntityLabel($entity, $previous_revision_id); + $latest_affected_revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId()); + } + else { + // Normally it would make sense to load the default revision in this + // case, however that would mean simulating here the logic that we need + // to test, thus "masking" possible flaws. To avoid that, we simply + // pretend we are starting from an earlier non translated revision. + // This ensures that the we can check that the merging logic is applied + // also when adding a new translation. + $latest_affected_revision_id = 1; } $previous_revision_id = (int) $entity->getLoadedRevisionId(); - $latest_affected_revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id(), $entity->language()->getId()); /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_affected_revision */ - $latest_affected_revision = isset($latest_affected_revision_id) ? - $this->storage->loadRevision($latest_affected_revision_id) : $this->storage->load($entity->id()); + $latest_affected_revision = $this->storage->loadRevision($latest_affected_revision_id); $translation = $latest_affected_revision->hasTranslation($active_langcode) ? $latest_affected_revision->getTranslation($active_langcode) : $latest_affected_revision->addTranslation($active_langcode); $entity = $this->storage->createRevision($translation, $default_revision); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php index d90756d14f5..addbad2e0f4 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php @@ -192,12 +192,12 @@ class EntityRevisionTranslationTest extends EntityKernelTestBase { /** * Tests that revision translations are correctly detected. * - * @covers \Drupal\Core\Entity\ContentEntityStorageBase::isAnyRevisionTranslated + * @covers \Drupal\Core\Entity\ContentEntityStorageBase::isAnyStoredRevisionTranslated */ - public function testIsAnyRevisionTranslated() { + public function testIsAnyStoredRevisionTranslated() { /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */ $storage = $this->entityManager->getStorage('entity_test_mul'); - $method = new \ReflectionMethod(get_class($storage), 'isAnyRevisionTranslated'); + $method = new \ReflectionMethod(get_class($storage), 'isAnyStoredRevisionTranslated'); $method->setAccessible(TRUE); // Check that a non-revisionable new entity is handled correctly.