function RevisionableContentEntityBaseTest::testRevisionableContentEntity

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php \Drupal\KernelTests\Core\Entity\RevisionableContentEntityBaseTest::testRevisionableContentEntity()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php \Drupal\KernelTests\Core\Entity\RevisionableContentEntityBaseTest::testRevisionableContentEntity()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php \Drupal\KernelTests\Core\Entity\RevisionableContentEntityBaseTest::testRevisionableContentEntity()

Tests the correct functionality CRUD operations of entity revisions.

File

core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php, line 41

Class

RevisionableContentEntityBaseTest
Test the revision system.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testRevisionableContentEntity() : void {
    $entity_type = 'entity_test_mul_revlog';
    $definition = \Drupal::entityTypeManager()->getDefinition($entity_type);
    $user = User::create([
        'name' => 'test name',
    ]);
    $user->save();
    
    /** @var \Drupal\entity_test_revlog\Entity\EntityTestMulWithRevisionLog $entity */
    $entity = EntityTestMulWithRevisionLog::create([
        'type' => $entity_type,
    ]);
    // Save the entity, this creates the first revision.
    $entity->save();
    $revision_ids[] = $entity->getRevisionId();
    $this->assertItemsTableCount(1, $definition);
    // Create the second revision.
    $entity->setNewRevision(TRUE);
    $random_timestamp = rand(100000000, 200000000);
    $this->createRevision($entity, $user, $random_timestamp, 'This is my log message');
    $revision_id = $entity->getRevisionId();
    $revision_ids[] = $revision_id;
    
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $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());
    // Create the third revision.
    $random_timestamp = rand(100000000, 200000000);
    $this->createRevision($entity, $user, $random_timestamp, 'This is my log message');
    $this->assertItemsTableCount(3, $definition);
    $revision_ids[] = $entity->getRevisionId();
    // Create another 3 revisions.
    foreach (range(1, 3) as $count) {
        $timestamp = rand(100000000, 200000000);
        $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);
    // Delete the first 3 revisions.
    foreach (range(0, 2) as $key) {
        $storage->deleteRevision($revision_ids[$key]);
    }
    // We should have only data for three revisions.
    $this->assertItemsTableCount(3, $definition);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.