View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\Entity\User;
class EntityDecoupledTranslationRevisionsTest extends EntityKernelTestBase {
protected static $modules = [
'system',
'entity_test',
'language',
];
protected $bundleInfo;
protected $storage;
protected $translations;
protected $previousRevisionId = [];
protected $previousUntranslatableFieldValue;
protected $stepIndex;
protected $stepInfo;
protected function setUp() : void {
parent::setUp();
$entity_type_id = 'entity_test_mulrev';
$this
->installEntitySchema($entity_type_id);
$this->storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type_id);
$this
->installConfig([
'language',
]);
$langcodes = [
'it',
'fr',
];
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$values = [
'name' => $this
->randomString(),
'status' => 1,
];
User::create($values)
->save();
$this->state
->set('entity_test.translation', TRUE);
$this->bundleInfo = \Drupal::service('entity_type.bundle.info');
$this->bundleInfo
->clearCachedBundles();
}
public function dataTestDecoupledPendingRevisions() {
$sets = [];
$sets['Intermixed languages - No initial default translation'][] = [
[
'en',
TRUE,
],
[
'en',
FALSE,
],
[
'it',
FALSE,
],
[
'en',
FALSE,
],
[
'it',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
TRUE,
],
];
$sets['Intermixed languages - With initial default translation'][] = [
[
'en',
TRUE,
],
[
'it',
TRUE,
],
[
'en',
FALSE,
],
[
'it',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
TRUE,
],
];
$sets['Alternate languages - No initial default translation'][] = [
[
'en',
TRUE,
],
[
'en',
FALSE,
],
[
'en',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
FALSE,
],
[
'it',
FALSE,
],
[
'it',
TRUE,
],
];
$sets['Alternate languages - With initial default translation'][] = [
[
'en',
TRUE,
],
[
'it',
TRUE,
],
[
'en',
TRUE,
],
[
'en',
FALSE,
],
[
'en',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
TRUE,
],
[
'it',
FALSE,
],
[
'it',
FALSE,
],
[
'it',
TRUE,
],
];
$sets['Multiple languages - No initial default translation'][] = [
[
'en',
TRUE,
],
[
'it',
FALSE,
],
[
'fr',
FALSE,
],
[
'en',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
TRUE,
],
[
'fr',
FALSE,
],
[
'en',
FALSE,
],
[
'it',
FALSE,
],
[
'en',
TRUE,
],
[
'fr',
TRUE,
],
[
'it',
TRUE,
],
[
'fr',
TRUE,
],
];
$sets['Multiple languages - With initial default translation'][] = [
[
'en',
TRUE,
],
[
'it',
TRUE,
],
[
'fr',
TRUE,
],
[
'en',
FALSE,
],
[
'it',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
TRUE,
],
[
'fr',
FALSE,
],
[
'en',
FALSE,
],
[
'it',
FALSE,
],
[
'en',
TRUE,
],
[
'fr',
TRUE,
],
[
'it',
TRUE,
],
[
'fr',
TRUE,
],
];
return $sets;
}
public function testDecoupledPendingRevisions($sequence) {
$revision_id = $this
->doTestEditSequence($sequence);
$this
->assertCount($revision_id, $sequence);
}
public function dataTestUntranslatableFields() {
$sets = [];
$sets['Default behavior - Untranslatable fields affect all revisions'] = [
[
[
'en',
TRUE,
TRUE,
],
[
'it',
FALSE,
TRUE,
FALSE,
],
[
'en',
FALSE,
TRUE,
FALSE,
],
[
'en',
TRUE,
TRUE,
],
[
'it',
TRUE,
TRUE,
],
[
'en',
FALSE,
],
[
'it',
FALSE,
],
[
'en',
TRUE,
],
[
'it',
TRUE,
],
],
FALSE,
];
$sets['Alternative behavior - Untranslatable fields affect only default translation'] = [
[
[
'en',
TRUE,
TRUE,
],
[
'it',
FALSE,
TRUE,
FALSE,
],
[
'en',
FALSE,
TRUE,
],
[
'it',
TRUE,
TRUE,
FALSE,
],
[
'it',
FALSE,
],
[
'it',
TRUE,
],
[
'en',
TRUE,
TRUE,
],
[
'it',
FALSE,
],
[
'en',
FALSE,
],
[
'it',
TRUE,
],
[
'en',
TRUE,
TRUE,
],
],
TRUE,
];
return $sets;
}
public function testUntranslatableFields($sequence, $default_translation_affected) {
$this->state
->set('entity_test.untranslatable_fields.default_translation_affected', $default_translation_affected);
$this->bundleInfo
->clearCachedBundles();
$entity = EntityTestMulRev::create();
$entity
->set('non_mul_field', 0);
$violations = $entity
->validate();
$this
->assertEmpty($violations);
$this
->doTestEditSequence($sequence);
}
protected function doTestEditSequence($sequence) {
$revision_id = NULL;
foreach ($sequence as $index => $step) {
$this->stepIndex = $index;
$revision_id = call_user_func_array([
$this,
'doEditStep',
], $step);
}
return $revision_id;
}
protected function doEditStep($active_langcode, $default_revision, $untranslatable_update = FALSE, $valid = TRUE) {
$this->stepInfo = [
$active_langcode,
$default_revision,
$untranslatable_update,
$valid,
];
$all_translations_affected = !$this->state
->get('entity_test.untranslatable_fields.default_translation_affected');
$previous_untranslatable_field_langcode = $all_translations_affected ? LanguageInterface::LANGCODE_DEFAULT : $active_langcode;
if (!isset($this->translations)) {
$this->translations[$active_langcode] = EntityTestMulRev::create();
$this->previousRevisionId[$active_langcode] = 0;
$this->previousUntranslatableFieldValue[$previous_untranslatable_field_langcode] = NULL;
}
if (!isset($this->translations[$active_langcode])) {
$this->translations[$active_langcode] = reset($this->translations)
->addTranslation($active_langcode);
$this->previousRevisionId[$active_langcode] = 0;
$this->previousUntranslatableFieldValue[$active_langcode] = NULL;
}
if ($valid) {
$entity =& $this->translations[$active_langcode];
$previous_revision_id =& $this->previousRevisionId[$active_langcode];
$previous_untranslatable_field_value =& $this->previousUntranslatableFieldValue[$previous_untranslatable_field_langcode];
}
else {
$entity = clone $this->translations[$active_langcode];
$previous_revision_id = $this->previousRevisionId[$active_langcode];
$previous_untranslatable_field_value = $this->previousUntranslatableFieldValue[$previous_untranslatable_field_langcode];
}
if (!$entity
->isNew()) {
$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 {
$latest_affected_revision_id = 1;
}
$previous_revision_id = (int) $entity
->getLoadedRevisionId();
$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);
$this
->assertEquals($default_revision, $entity
->isDefaultRevision());
$this
->assertEquals($translation
->getLoadedRevisionId(), $entity
->getLoadedRevisionId());
$this
->assertEquals($previous_label, $entity
->label(), $this
->formatMessage('Loaded translatable field value does not match the previous one.'));
}
$value = $entity
->get('non_mul_field')->value;
if (isset($previous_untranslatable_field_value)) {
$this
->assertEquals($previous_untranslatable_field_value, $value, $this
->formatMessage('Loaded untranslatable field value does not match the previous one.'));
}
elseif (!$entity
->isDefaultTranslation()) {
$default_revision = $this->storage
->loadUnchanged($entity
->id());
$expected_value = $default_revision
->get('non_mul_field')->value;
$this
->assertEquals($expected_value, $value, $this
->formatMessage('Loaded untranslatable field value does not match the previous one.'));
}
$label = $this
->generateNewEntityLabel($entity, $previous_revision_id, TRUE);
$entity
->set('name', $label);
if ($untranslatable_update) {
$prev = 0;
if (isset($previous_untranslatable_field_value)) {
preg_match('/^\\d+ -> (\\d+)$/', $previous_untranslatable_field_value, $matches);
$prev = $matches[1];
}
$value = $prev . ' -> ' . ($entity
->getLoadedRevisionId() + 1);
$entity
->set('non_mul_field', $value);
$previous_untranslatable_field_value = $value;
}
$violations = $entity
->validate();
$messages = [];
foreach ($violations as $violation) {
$messages[] = $violation
->getMessage();
}
$this
->assertEquals($valid, !$violations
->count(), $this
->formatMessage('Validation does not match the expected result: %s', implode(', ', $messages)));
if ($valid) {
$entity
->save();
$entity = $this->storage
->loadRevision($entity
->getRevisionId());
$entity = $entity
->getTranslation($active_langcode);
$default_entity = $this->storage
->loadUnchanged($entity
->id());
foreach ($entity
->getTranslationLanguages() as $langcode => $language) {
$translation = $entity
->getTranslation($langcode);
$rta_expected = $langcode == $active_langcode || $untranslatable_update && $all_translations_affected;
$this
->assertEquals($rta_expected, $translation
->isRevisionTranslationAffected(), $this
->formatMessage("'{$langcode}' translation incorrectly affected"));
$label_expected = $label;
if ($langcode !== $active_langcode) {
$default_translation = $default_entity
->hasTranslation($langcode) ? $default_entity
->getTranslation($langcode) : $default_entity;
$label_expected = $default_translation
->label();
}
$this
->assertEquals($label_expected, $translation
->label(), $this
->formatMessage("Incorrect '{$langcode}' translation label"));
}
}
return $entity
->getRevisionId();
}
protected function generateNewEntityLabel(ContentEntityInterface $revision, $previous_revision_id, $next = FALSE) {
$language_label = $revision
->language()
->getName();
$revision_type = $revision
->isDefaultRevision() ? 'Default' : 'Pending';
$revision_id = $next ? $this->storage
->getLatestRevisionId($revision
->id()) + 1 : $revision
->getLoadedRevisionId();
return sprintf('%s (%s %d -> %d)', $language_label, $revision_type, $previous_revision_id, $revision_id);
}
protected function formatMessage($message) {
$args = func_get_args();
array_shift($args);
$params = array_merge($args, $this->stepInfo);
array_unshift($params, $this->stepIndex + 1);
array_unshift($params, '[Step %d] ' . $message . ' (langcode: %s, default_revision: %d, untranslatable_update: %d, valid: %d)');
return call_user_func_array('sprintf', $params);
}
public function testMultipleTranslationChanges() {
$this->state
->set('entity_test.untranslatable_fields.default_translation_affected', TRUE);
$this->bundleInfo
->clearCachedBundles();
$entity = EntityTestMulRev::create();
$entity
->get('name')->value = 'Test 1.1 EN';
$entity
->get('non_mul_field')->value = 'Test 1.1';
$this->storage
->save($entity);
$revision = $this->storage
->createRevision($entity
->addTranslation('it'));
$revision
->get('name')->value = 'Test 1.2 IT';
$this->storage
->save($revision);
$revision = $this->storage
->createRevision($revision
->getTranslation('en'), FALSE);
$revision
->get('non_mul_field')->value = 'Test 1.3';
$revision
->getTranslation('it')
->get('name')->value = 'Test 1.3 IT';
$violations = $revision
->validate();
$this
->assertCount(1, $violations);
$this
->assertEquals('Non-translatable fields can only be changed when updating the original language.', $violations[0]
->getMessage());
}
public function testInternalProperties() {
$entity = EntityTestMulRev::create();
$this
->doTestInternalProperties($entity);
$entity = EntityTestMulRev::create();
$entity
->save();
$this
->doTestInternalProperties($entity);
$translation = EntityTestMulRev::create()
->addTranslation('it');
$translation
->save();
$this
->doTestInternalProperties($translation);
}
protected function doTestInternalProperties(ContentEntityInterface $entity) {
$this
->assertFalse($entity
->isValidationRequired());
$entity
->setValidationRequired(TRUE);
$this
->assertTrue($entity
->isValidationRequired());
$new_revision = $this->storage
->createRevision($entity);
$this
->assertTrue($new_revision
->isValidationRequired());
}
public function testRemovedTranslations() {
$entity = EntityTestMulRev::create([
'name' => 'Test 1.1 EN',
]);
$this->storage
->save($entity);
$it_revision = $this->storage
->createRevision($entity
->addTranslation('it'));
$it_revision
->set('name', 'Test 1.2 IT');
$this->storage
->save($it_revision);
$en_revision = $this->storage
->createRevision($it_revision
->getUntranslated(), FALSE);
$en_revision
->set('name', 'Test 1.3 EN');
$this->storage
->save($en_revision);
$it_revision = $this->storage
->createRevision($it_revision);
$en_revision = $it_revision
->getUntranslated();
$en_revision
->removeTranslation('it');
$this->storage
->save($en_revision);
$revision_id = $this->storage
->getLatestTranslationAffectedRevisionId($entity
->id(), 'en');
$en_revision = $this->storage
->loadRevision($revision_id);
$en_revision = $this->storage
->createRevision($en_revision);
$en_revision
->set('name', 'Test 1.5 EN');
$this->storage
->save($en_revision);
$en_revision = $this->storage
->loadRevision($en_revision
->getRevisionId());
$this
->assertFalse($en_revision
->hasTranslation('it'));
}
public function testCreateRevisionHook() {
$entity = EntityTestMulRev::create();
$entity
->get('name')->value = 'revision_create_test_en';
$this->storage
->save($entity);
$translation = $entity
->addTranslation('it');
$translation
->set('name', 'revision_create_test_it');
$revision = $this->storage
->createRevision($translation, FALSE, TRUE);
$this
->assertEquals('revision_create_test_it_altered', $revision
->get('name')->value);
$data = $this->state
->get('entity_test.hooks');
$this
->assertEquals('revision_create_test_it', $data['entity_test_mulrev_revision_create']['entity']
->get('name')->value);
$this
->assertEquals('revision_create_test_it_altered', $data['entity_test_mulrev_revision_create']['new_revision']
->get('name')->value);
$this
->assertFalse($data['entity_test_mulrev_revision_create']['entity']
->isNewRevision());
$this
->assertTrue($data['entity_test_mulrev_revision_create']['new_revision']
->isNewRevision());
$this
->assertTrue($data['entity_test_mulrev_revision_create']['entity']
->isDefaultRevision());
$this
->assertFalse($data['entity_test_mulrev_revision_create']['new_revision']
->isDefaultRevision());
$this
->assertTrue($data['entity_test_mulrev_revision_create']['keep_untranslatable_fields']);
$this
->assertEquals('revision_create_test_it', $data['entity_revision_create']['entity']
->get('name')->value);
$this
->assertEquals('revision_create_test_it_altered', $data['entity_revision_create']['new_revision']
->get('name')->value);
$this
->assertFalse($data['entity_revision_create']['entity']
->isNewRevision());
$this
->assertTrue($data['entity_revision_create']['new_revision']
->isNewRevision());
$this
->assertTrue($data['entity_revision_create']['entity']
->isDefaultRevision());
$this
->assertFalse($data['entity_revision_create']['new_revision']
->isDefaultRevision());
$this
->assertTrue($data['entity_revision_create']['keep_untranslatable_fields']);
$translation
->isDefaultRevision(FALSE);
$this->storage
->createRevision($translation);
$data = $this->state
->get('entity_test.hooks');
$this
->assertFalse($data['entity_revision_create']['entity']
->isNewRevision());
$this
->assertTrue($data['entity_revision_create']['new_revision']
->isNewRevision());
$this
->assertFalse($data['entity_revision_create']['entity']
->isDefaultRevision());
$this
->assertTrue($data['entity_revision_create']['new_revision']
->isDefaultRevision());
$this
->assertNull($data['entity_revision_create']['keep_untranslatable_fields']);
$this
->assertFalse($data['entity_test_mulrev_revision_create']['entity']
->isNewRevision());
$this
->assertTrue($data['entity_test_mulrev_revision_create']['new_revision']
->isNewRevision());
$this
->assertFalse($data['entity_test_mulrev_revision_create']['entity']
->isDefaultRevision());
$this
->assertTrue($data['entity_test_mulrev_revision_create']['new_revision']
->isDefaultRevision());
$this
->assertNull($data['entity_test_mulrev_revision_create']['keep_untranslatable_fields']);
}
}