View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
class RevisionableContentEntityBaseTest extends EntityKernelTestBase {
protected static $modules = [
'entity_test_revlog',
'system',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test_mul_revlog');
}
public function testRevisionableContentEntity() {
$entity_type = 'entity_test_mul_revlog';
$definition = \Drupal::entityTypeManager()
->getDefinition($entity_type);
$user = User::create([
'name' => 'test name',
]);
$user
->save();
$entity = EntityTestMulWithRevisionLog::create([
'type' => $entity_type,
]);
$entity
->save();
$revision_ids[] = $entity
->getRevisionId();
$this
->assertItemsTableCount(1, $definition);
$entity
->setNewRevision(TRUE);
$random_timestamp = rand(100000000.0, 200000000.0);
$this
->createRevision($entity, $user, $random_timestamp, 'This is my log message');
$revision_id = $entity
->getRevisionId();
$revision_ids[] = $revision_id;
$storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mul_revlog');
$entity = $storage
->loadRevision($revision_id);
$this
->assertEquals($random_timestamp, $entity
->getRevisionCreationTime());
$this
->assertEquals($user
->id(), $entity
->getRevisionUserId());
$this
->assertEquals($user
->id(), $entity
->getRevisionUser()
->id());
$this
->assertEquals('This is my log message', $entity
->getRevisionLogMessage());
$random_timestamp = rand(100000000.0, 200000000.0);
$this
->createRevision($entity, $user, $random_timestamp, 'This is my log message');
$this
->assertItemsTableCount(3, $definition);
$revision_ids[] = $entity
->getRevisionId();
foreach (range(1, 3) as $count) {
$timestamp = rand(100000000.0, 200000000.0);
$this
->createRevision($entity, $user, $timestamp, 'This is my log message number: ' . $count);
$revision_ids[] = $entity
->getRevisionId();
}
$this
->assertItemsTableCount(6, $definition);
$this
->assertCount(6, $revision_ids);
foreach (range(0, 2) as $key) {
$storage
->deleteRevision($revision_ids[$key]);
}
$this
->assertItemsTableCount(3, $definition);
}
public function testWasDefaultRevision() {
$entity_type_id = 'entity_test_mul_revlog';
$entity = EntityTestMulWithRevisionLog::create([
'type' => $entity_type_id,
]);
$this
->assertEquals($entity
->isDefaultRevision(), $entity
->wasDefaultRevision());
$entity
->isDefaultRevision(FALSE);
$this
->assertEquals($entity
->isDefaultRevision(), $entity
->wasDefaultRevision());
$entity
->save();
$this
->assertTrue($entity
->wasDefaultRevision());
$entity
->setNewRevision();
$entity
->isDefaultRevision(FALSE);
$entity
->save();
$this
->assertFalse($entity
->wasDefaultRevision());
$entity
->setNewRevision();
$entity
->isDefaultRevision(TRUE);
$entity
->save();
$this
->assertTrue($entity
->wasDefaultRevision());
$entity
->setNewRevision();
$entity
->isDefaultRevision(FALSE);
$entity
->set('revision_default', TRUE);
$this
->assertTrue($entity
->wasDefaultRevision());
$entity
->save();
$this
->assertFalse($entity
->wasDefaultRevision());
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
foreach ([
TRUE,
FALSE,
TRUE,
FALSE,
] as $index => $expected) {
$revision = $storage
->loadRevision($index + 1);
$this
->assertEquals($expected, $revision
->wasDefaultRevision());
}
$entity = $storage
->loadUnchanged($entity
->id());
$this
->assertTrue($entity
->wasDefaultRevision());
$entity2 = EntityTestMulWithRevisionLog::create([
'type' => $entity_type_id,
]);
$entity2
->save();
$this
->assertTrue($entity2
->wasDefaultRevision());
$entity2
->isDefaultRevision(FALSE);
$entity2
->save();
$this
->assertTrue($entity2
->wasDefaultRevision());
}
protected function assertItemsTableCount($count, EntityTypeInterface $definition) {
$connection = Database::getConnection();
$this
->assertEquals(1, (int) $connection
->select($definition
->getBaseTable())
->countQuery()
->execute()
->fetchField());
$this
->assertEquals(1, (int) $connection
->select($definition
->getDataTable())
->countQuery()
->execute()
->fetchField());
$this
->assertEquals($count, (int) $connection
->select($definition
->getRevisionTable())
->countQuery()
->execute()
->fetchField());
$this
->assertEquals($count, (int) $connection
->select($definition
->getRevisionDataTable())
->countQuery()
->execute()
->fetchField());
}
protected function createRevision(EntityInterface $entity, UserInterface $user, $timestamp, $log_message) {
$entity
->setNewRevision(TRUE);
$entity
->setRevisionCreationTime($timestamp);
$entity
->setRevisionUserId($user
->id());
$entity
->setRevisionLogMessage($log_message);
$entity
->save();
}
}