function EntityRevisionsTest::testNewRevisionRevert

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::testNewRevisionRevert()
  2. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityRevisionsTest.php \Drupal\Tests\system\Functional\Entity\EntityRevisionsTest::testNewRevisionRevert()
  3. 11.x 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 231

Class

EntityRevisionsTest
Tests modifying an entity with revisions.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testNewRevisionRevert() : void {
    $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);
}

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