View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\language\Entity\ConfigurableLanguage;
class EntityRevisionsTest extends EntityKernelTestBase {
public static $modules = [
'system',
'entity_test',
'language',
'content_translation',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test_mulrev');
}
public function testLoadedRevisionId() {
$entity = EntityTestMulRev::create();
$entity
->save();
$loaded = EntityTestMulRev::load($entity
->id());
$loaded
->setNewRevision(TRUE);
$this
->assertEquals($entity
->getRevisionId(), $loaded
->getLoadedRevisionId());
$this
->assertNotEquals($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
$this
->assertSame(NULL, $loaded
->getRevisionId());
$loaded
->updateLoadedRevisionId();
$this
->assertEquals($entity
->getRevisionId(), $loaded
->getLoadedRevisionId());
$this
->assertNotEquals($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
$this
->assertSame(NULL, $loaded
->getRevisionId());
$loaded
->save();
$loadedRevisionId = \Drupal::state()
->get('entity_test.loadedRevisionId');
$this
->assertEquals($entity
->getRevisionId(), $loadedRevisionId);
$this
->assertNotEquals($loaded
->getRevisionId(), $loadedRevisionId);
$this
->assertNotEquals($loaded
->getRevisionId(), $entity
->getRevisionId());
$this
->assertNotEquals($loaded
->getLoadedRevisionId(), $entity
->getLoadedRevisionId());
$this
->assertEquals($entity
->getRevisionId(), $entity
->getLoadedRevisionId());
$this
->assertEquals($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
}
public function testLoadedRevisionIdWithNoNewRevision() {
$entity = EntityTestMulRev::create();
$entity
->save();
$loaded = EntityTestMulRev::load($entity
->id());
$loaded
->setNewRevision(TRUE);
$loaded
->save();
$loaded
->set('name', 'dublin');
$this
->assertEquals($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
$loaded
->save();
$loadedRevisionId = \Drupal::state()
->get('entity_test.loadedRevisionId');
$this
->assertEquals($loaded
->getRevisionId(), $loadedRevisionId);
$this
->assertEquals($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
$clone = clone $loaded;
$this
->assertSame($loaded
->getLoadedRevisionId(), $clone
->getLoadedRevisionId());
$duplicate = $loaded
->createDuplicate();
$this
->assertSame(NULL, $duplicate
->getLoadedRevisionId());
}
public function testTranslatedLoadedRevisionId() {
ConfigurableLanguage::createFromLangcode('fr')
->save();
$entity = EntityTestMulRev::create();
$entity
->save();
$loaded = EntityTestMulRev::load($entity
->id());
$loaded
->setNewRevision(TRUE);
$loaded
->save();
$french = $loaded
->addTranslation('fr');
$this
->assertEquals($french
->getRevisionId(), $french
->getLoadedRevisionId());
$this
->assertEquals($loaded
->getRevisionId(), $french
->getLoadedRevisionId());
$this
->assertEquals($loaded
->getLoadedRevisionId(), $french
->getLoadedRevisionId());
$french
->save();
$this
->assertEquals($french
->getRevisionId(), $french
->getLoadedRevisionId());
$this
->assertEquals($loaded
->getRevisionId(), $french
->getLoadedRevisionId());
$this
->assertEquals($loaded
->getLoadedRevisionId(), $french
->getLoadedRevisionId());
$first_revision_id = $french
->getRevisionId();
$french
->setNewRevision();
$this
->assertEquals($first_revision_id, $french
->getLoadedRevisionId());
$this
->assertEquals($first_revision_id, $loaded
->getLoadedRevisionId());
$this
->assertNotEquals($french
->getRevisionId(), $french
->getLoadedRevisionId());
$this
->assertGreaterThan($french
->getRevisionId(), $french
->getLoadedRevisionId());
$this
->assertNotEquals($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
$this
->assertGreaterThan($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
$french
->save();
$this
->assertEquals($french
->getRevisionId(), $french
->getLoadedRevisionId());
$this
->assertEquals($loaded
->getRevisionId(), $loaded
->getLoadedRevisionId());
}
public function testSaveInHookEntityInsert() {
$entity = EntityTestMulRev::create([
'name' => 'EntityLoadedRevisionTest',
]);
$entity
->save();
$loadedRevisionId = \Drupal::state()
->get('entity_test.loadedRevisionId');
$this
->assertEquals($entity
->getLoadedRevisionId(), $loadedRevisionId);
$this
->assertEquals($entity
->getRevisionId(), $entity
->getLoadedRevisionId());
}
public function testIsLatestRevision() {
$entity = EntityTestMulRev::create();
$entity
->save();
$this
->assertTrue($entity
->isLatestRevision());
$pending_revision = EntityTestMulRev::load($entity
->id());
$pending_revision
->setNewRevision(TRUE);
$pending_revision
->isDefaultRevision(FALSE);
$this
->assertTrue($pending_revision
->isLatestRevision());
$pending_revision
->save();
$this
->assertTrue($pending_revision
->isLatestRevision());
$default_revision = EntityTestMulRev::load($entity
->id());
$this
->assertFalse($default_revision
->isLatestRevision());
}
public function testIsLatestAffectedRevisionTranslation() {
ConfigurableLanguage::createFromLangcode('it')
->save();
$entity = EntityTestMulRev::create();
$entity
->setName($this
->randomString());
$entity
->save();
$this
->assertTrue($entity
->isLatestTranslationAffectedRevision());
$pending_revision = EntityTestMulRev::load($entity
->id());
$pending_revision
->setName($this
->randomString());
$pending_revision
->setNewRevision(TRUE);
$pending_revision
->isDefaultRevision(FALSE);
$storage = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
$this
->assertNull($storage
->getLatestTranslationAffectedRevisionId($entity
->id(), 'it'));
$this
->assertEquals($pending_revision
->getLoadedRevisionId(), $storage
->getLatestRevisionId($entity
->id()));
$this
->assertTrue($pending_revision
->isLatestTranslationAffectedRevision());
$pending_revision
->save();
$this
->assertTrue($pending_revision
->isLatestTranslationAffectedRevision());
$default_revision = EntityTestMulRev::load($entity
->id());
$this
->assertFalse($default_revision
->isLatestRevision());
$this
->assertFalse($default_revision
->isLatestTranslationAffectedRevision());
$en_revision = clone $pending_revision;
$it_revision = $pending_revision
->addTranslation('it');
$it_revision
->setName($this
->randomString());
$it_revision
->setNewRevision(TRUE);
$it_revision
->isDefaultRevision(FALSE);
$it_revision->original = $storage
->loadRevision($it_revision
->getLoadedRevisionId());
$it_revision
->save();
$this
->assertTrue($it_revision
->isLatestRevision());
$this
->assertTrue($it_revision
->isLatestTranslationAffectedRevision());
$this
->assertFalse($en_revision
->isLatestRevision());
$this
->assertTrue($en_revision
->isLatestTranslationAffectedRevision());
}
}