function EntityRevisionsTest::testLoadedRevisionId

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

Tests getLoadedRevisionId() returns the correct ID throughout the process.

File

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

Class

EntityRevisionsTest
Tests the loaded Revision of an entity.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testLoadedRevisionId() : void {
    // Create a basic EntityTestMulRev entity and save it.
    $entity = EntityTestMulRev::create();
    $entity->save();
    // Load the created entity and create a new revision.
    $loaded = EntityTestMulRev::load($entity->id());
    $loaded->setNewRevision(TRUE);
    // Before saving, the loaded Revision ID should be the same as the created
    // entity, not the same as the loaded entity (which does not have a revision
    // ID yet).
    $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
    $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
    $this->assertNull($loaded->getRevisionId());
    // After updating the loaded Revision ID the result should be the same.
    $loaded->updateLoadedRevisionId();
    $this->assertEquals($entity->getRevisionId(), $loaded->getLoadedRevisionId());
    $this->assertNotEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
    $this->assertNull($loaded->getRevisionId());
    $loaded->save();
    // In entity_test_entity_update() the loaded Revision ID was stored in
    // state. This should be the same as we had before calling $loaded->save().
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */
    $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
    $this->assertEquals($entity->getRevisionId(), $loadedRevisionId);
    $this->assertNotEquals($loaded->getRevisionId(), $loadedRevisionId);
    // The revision ID and loaded Revision ID should be different for the two
    // versions of the entity, but the same for a saved entity.
    $this->assertNotEquals($loaded->getRevisionId(), $entity->getRevisionId());
    $this->assertNotEquals($loaded->getLoadedRevisionId(), $entity->getLoadedRevisionId());
    $this->assertEquals($entity->getRevisionId(), $entity->getLoadedRevisionId());
    $this->assertEquals($loaded->getRevisionId(), $loaded->getLoadedRevisionId());
}

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