function RevisionRevertFormTest::testPrepareRevision

Same name in other branches
  1. 11.x core/tests/Drupal/FunctionalTests/Entity/RevisionRevertFormTest.php \Drupal\FunctionalTests\Entity\RevisionRevertFormTest::testPrepareRevision()

Tests the revert process.

@covers ::prepareRevision

1 call to RevisionRevertFormTest::testPrepareRevision()
RevisionRevertFormTest::testFormRevisionRevert in core/tests/Drupal/FunctionalTests/Entity/RevisionRevertFormTest.php
Test form revision revert.

File

core/tests/Drupal/FunctionalTests/Entity/RevisionRevertFormTest.php, line 289

Class

RevisionRevertFormTest
Tests reverting a revision with revision revert form.

Namespace

Drupal\FunctionalTests\Entity

Code

protected function testPrepareRevision() : void {
    $user = $this->createUser();
    $this->drupalLogin($user);
    
    /** @var \Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog $entity */
    $entity = EntityTestWithRevisionLog::create([
        'type' => 'entity_test_revlog',
        'name' => 'revert',
    ]);
    $date = new \DateTime('11 January 2009 4:00:00pm');
    $entity->setRevisionCreationTime($date->getTimestamp());
    $entity->isDefaultRevision(TRUE);
    $entity->setNewRevision();
    $entity->save();
    $revisionCreationTime = $date->modify('+1 hour')
        ->getTimestamp();
    $entity->setRevisionCreationTime($revisionCreationTime);
    $entity->setRevisionUserId(0);
    $entity->isDefaultRevision(FALSE);
    $entity->setNewRevision();
    $entity->save();
    $targetRevertRevisionId = $entity->getRevisionId();
    // Create a another revision so the previous revision can be reverted to.
    $entity->setRevisionCreationTime($date->modify('+1 hour')
        ->getTimestamp());
    $entity->isDefaultRevision(FALSE);
    $entity->setNewRevision();
    $entity->save();
    $count = $this->countRevisions($entity->getEntityTypeId());
    // Load the revision to be copied.
    
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());
    
    /** @var \Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog $targetRevision */
    $targetRevision = $storage->loadRevision($targetRevertRevisionId);
    $this->drupalGet($targetRevision->toUrl('revision-revert-form'));
    $this->submitForm([], 'Revert');
    // Load the new latest revision.
    
    /** @var \Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog $latestRevision */
    $latestRevision = $storage->loadUnchanged($entity->id());
    $this->assertEquals($count + 1, $this->countRevisions($entity->getEntityTypeId()));
    $this->assertEquals('Copy of the revision from <em class="placeholder">Sun, 01/11/2009 - 17:00</em>.', $latestRevision->getRevisionLogMessage());
    $this->assertGreaterThan($revisionCreationTime, $latestRevision->getRevisionCreationTime());
    $this->assertEquals($user->id(), $latestRevision->getRevisionUserId());
    $this->assertTrue($latestRevision->isDefaultRevision());
}

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