function ContentEntityCacheTest::testDelete

Tests deleting an entity or an entity revision.

File

core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCacheTest.php, line 258

Class

ContentEntityCacheTest
Tests the entity static cache when used by content entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testDelete() : void {
  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  $storage = $entity_type_manager->getStorage($this->revEntityTypeId);
  $rev_entity_type = $entity_type_manager->getDefinition($this->revEntityTypeId);
  $this->assertTrue($rev_entity_type->isStaticallyCacheable());
  $this->assertTrue($rev_entity_type->isPersistentlyCacheable());
  $this->assertTrue($rev_entity_type->isRevisionable());
  // Create an entity with three revisions by ensuring that each of the
  // revisions remains in the persistent entity revision cache.
  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $storage->create();
  $entity->save();
  $first_rev_id = $entity->getRevisionId();
  $entity = $storage->loadRevision($first_rev_id);
  $entity->setNewRevision();
  $entity->save();
  $second_rev_id = $entity->getRevisionId();
  $entity = $storage->loadRevision($second_rev_id);
  $entity->setNewRevision();
  $entity->save();
  $third_rev_id = $entity->getRevisionId();
  // Delete the first revision and ensure that it cannot be loaded.
  $storage->deleteRevision($first_rev_id);
  $this->assertNull($storage->loadRevision($first_rev_id));
  // Delete the entity and ensure that no revision can be loaded.
  $entity->delete();
  $this->assertNull($storage->loadRevision($first_rev_id));
  $this->assertNull($storage->loadRevision($second_rev_id));
  $this->assertNull($storage->loadRevision($third_rev_id));
}

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