View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\language\Entity\ConfigurableLanguage;
class EntityRevisionTranslationTest extends EntityKernelTestBase {
public static $modules = [
'language',
];
protected function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('de')
->save();
ConfigurableLanguage::createFromLangcode('it')
->save();
$this
->installEntitySchema('entity_test_mul');
$this
->installEntitySchema('entity_test_mulrev');
}
public function testNewRevisionAfterTranslation() {
$user = $this
->createUser();
$entity = EntityTestMulRev::create([
'name' => $this
->randomString(),
'user_id' => $user
->id(),
'language' => 'en',
]);
$entity
->save();
$old_rev_id = $entity
->getRevisionId();
$translation = $entity
->addTranslation('de');
$translation
->setNewRevision();
$translation
->save();
$this
->assertTrue($translation
->getRevisionId() > $old_rev_id, 'The saved translation in new revision has a newer revision id.');
$this
->assertTrue($this
->reloadEntity($entity)
->getRevisionId() > $old_rev_id, 'The entity from the storage has a newer revision id.');
}
public function testRevertRevisionAfterTranslation() {
$user = $this
->createUser();
$storage = $this->entityTypeManager
->getStorage('entity_test_mulrev');
$entity = EntityTestMulRev::create([
'name' => $this
->randomString(),
'user_id' => $user
->id(),
'language' => 'en',
]);
$entity
->save();
$old_rev_id = $entity
->getRevisionId();
$translation = $entity
->addTranslation('de');
$translation
->setNewRevision();
$translation
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertTrue($entity
->hasTranslation('de'));
$entity = $storage
->loadRevision($old_rev_id);
$entity
->setNewRevision();
$entity
->isDefaultRevision(TRUE);
$entity
->save();
$entity = $this
->reloadEntity($entity);
$this
->assertFalse($entity
->hasTranslation('de'));
}
public function testTranslationValuesWhenSavingPendingRevisions() {
$user = $this
->createUser();
$storage = $this->entityTypeManager
->getStorage('entity_test_mulrev');
$entity = EntityTestMulRev::create([
'name' => 'default revision - en',
'user_id' => $user
->id(),
'language' => 'en',
]);
$entity
->addTranslation('de', [
'name' => 'default revision - de',
]);
$entity
->save();
$pending_revision = $this
->reloadEntity($entity);
$pending_revision
->setNewRevision();
$pending_revision
->isDefaultRevision(FALSE);
$pending_revision->name = 'pending revision - en';
$pending_revision
->save();
$pending_revision_translation = $pending_revision
->getTranslation('de');
$pending_revision_translation->name = 'pending revision - de';
$pending_revision_translation
->save();
$pending_revision_id = $pending_revision
->getRevisionId();
$pending_revision = $storage
->loadRevision($pending_revision_id);
$pending_revision->name = 'updated pending revision - en';
$pending_revision
->save();
$pending_revision = $storage
->loadRevision($pending_revision_id);
$this
->assertEquals($pending_revision->name->value, 'updated pending revision - en');
$this
->assertEquals($pending_revision
->getTranslation('de')->name->value, 'pending revision - de');
}
public function testDefaultRevision() {
$entity = EntityTestMulRev::create([
'name' => 'original',
'language' => 'en',
]);
$translation = $entity
->addTranslation('de');
$entity
->save();
$this
->assertTrue($entity
->isDefaultRevision());
$this
->assertTrue($translation
->isDefaultRevision());
$translation
->isDefaultRevision(FALSE);
$this
->assertFalse($entity
->isDefaultRevision());
$this
->assertFalse($translation
->isDefaultRevision());
}
public function testSetNewRevision() {
$user = $this
->createUser();
foreach (entity_test_entity_types(ENTITY_TEST_TYPES_REVISABLE) as $entity_type) {
$this
->installEntitySchema($entity_type);
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type);
$entity = $storage
->create([
'name' => 'foo',
'user_id' => $user
->id(),
]);
$entity
->save();
$entity_id = $entity
->id();
$entity_rev_id = $entity
->getRevisionId();
$entity = $storage
->loadUnchanged($entity_id);
$entity
->setNewRevision(TRUE);
$entity
->setNewRevision(FALSE);
$entity
->save();
$entity = $storage
->loadUnchanged($entity_id);
$this
->assertEquals($entity_rev_id, $entity
->getRevisionId(), 'A new entity revision was not created.');
}
}
public function testIsAnyStoredRevisionTranslated() {
$storage = $this->entityTypeManager
->getStorage('entity_test_mul');
$method = new \ReflectionMethod(get_class($storage), 'isAnyStoredRevisionTranslated');
$method
->setAccessible(TRUE);
$entity = EntityTestMul::create();
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity
->addTranslation('it');
$this
->assertNotEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity = EntityTestMul::create();
$entity
->save();
$entity
->addTranslation('it');
$this
->assertNotEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity
->save();
$entity
->removeTranslation('it');
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertTrue($method
->invoke($storage, $entity));
$entity
->save();
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity
->addTranslation('de');
$entity
->removeTranslation('de');
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity = EntityTestMul::create();
$entity
->save();
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity
->addTranslation('it');
$entity
->save();
$this
->assertNotEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertTrue($method
->invoke($storage, $entity));
$storage = $this->entityTypeManager
->getStorage('entity_test_mulrev');
$entity = EntityTestMulRev::create();
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity
->addTranslation('it');
$this
->assertNotEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$entity = EntityTestMulRev::create();
$entity
->save();
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertFalse($method
->invoke($storage, $entity));
$new_revision = $storage
->createRevision($entity, FALSE);
$new_revision
->addTranslation('it');
$new_revision
->save();
$entity = $storage
->loadUnchanged($entity
->id());
$this
->assertEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertNotEmpty($new_revision
->getTranslationLanguages(FALSE));
$this
->assertTrue($method
->invoke($storage, $entity));
$new_revision
->isDefaultRevision(TRUE);
$new_revision
->save();
$entity = $storage
->loadUnchanged($entity
->id());
$this
->assertNotEmpty($entity
->getTranslationLanguages(FALSE));
$this
->assertNotEmpty($new_revision
->getTranslationLanguages(FALSE));
$this
->assertTrue($method
->invoke($storage, $entity));
}
}