You are here

public function EntityRevisionsTest::testNewRevisionRevert in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::testNewRevisionRevert()
  2. 10 core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::testNewRevisionRevert()

Tests manual revert of the revision ID value.

@covers \Drupal\Core\Entity\ContentEntityBase::getRevisionId @covers \Drupal\Core\Entity\ContentEntityBase::getLoadedRevisionId @covers \Drupal\Core\Entity\ContentEntityBase::setNewRevision @covers \Drupal\Core\Entity\ContentEntityBase::isNewRevision

File

core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php, line 229

Class

EntityRevisionsTest
Create a entity with revisions and test viewing, saving, reverting, and deleting revisions.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testNewRevisionRevert() {
  $entity = EntityTestMulRev::create([
    'name' => 'EntityLoadedRevisionTest',
  ]);
  $entity
    ->save();

  // Check that revision ID field is reset while the loaded revision ID is
  // preserved when flagging a new revision.
  $revision_id = $entity
    ->getRevisionId();
  $entity
    ->setNewRevision();
  $this
    ->assertNull($entity
    ->getRevisionId());
  $this
    ->assertEquals($revision_id, $entity
    ->getLoadedRevisionId());
  $this
    ->assertTrue($entity
    ->isNewRevision());

  // Check that after manually restoring the original revision ID, the entity
  // is stored without creating a new revision.
  $key = $entity
    ->getEntityType()
    ->getKey('revision');
  $entity
    ->set($key, $revision_id);
  $entity
    ->save();
  $this
    ->assertEquals($revision_id, $entity
    ->getRevisionId());
  $this
    ->assertEquals($revision_id, $entity
    ->getLoadedRevisionId());

  // Check that manually restoring the original revision ID causes the "new
  // revision" state to be reverted.
  $entity
    ->setNewRevision();
  $this
    ->assertNull($entity
    ->getRevisionId());
  $this
    ->assertEquals($revision_id, $entity
    ->getLoadedRevisionId());
  $this
    ->assertTrue($entity
    ->isNewRevision());
  $entity
    ->set($key, $revision_id);
  $this
    ->assertFalse($entity
    ->isNewRevision());
  $this
    ->assertEquals($revision_id, $entity
    ->getRevisionId());
  $this
    ->assertEquals($revision_id, $entity
    ->getLoadedRevisionId());

  // Check that flagging a new revision again works correctly.
  $entity
    ->setNewRevision();
  $this
    ->assertNull($entity
    ->getRevisionId());
  $this
    ->assertEquals($revision_id, $entity
    ->getLoadedRevisionId());
  $this
    ->assertTrue($entity
    ->isNewRevision());

  // Check that calling setNewRevision() on a new entity without a revision ID
  // and then with a revision ID does not unset the revision ID.
  $entity = EntityTestMulRev::create([
    'name' => 'EntityLoadedRevisionTest',
  ]);
  $entity
    ->set('revision_id', NULL);
  $entity
    ->set('revision_id', 5);
  $this
    ->assertTrue($entity
    ->isNewRevision());
  $entity
    ->setNewRevision();
  $this
    ->assertEquals(5, $entity
    ->get('revision_id')->value);
}