function ContentEntityCacheTest::testEntityLoad
Tests the static cache when loading content entities.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ ContentEntityCacheTest.php, line 55
Class
- ContentEntityCacheTest
- Tests the entity static cache when used by content entities.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testEntityLoad() : 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());
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $storage->create();
$entity->save();
$non_default_rev_id = $entity->getRevisionId();
$entity->setNewRevision();
$entity->save();
$persistent_cache = \Drupal::cache('entity');
$persistent_cache->deleteAll();
// Tests the three static cache rules for entity loading:
// 1. Loading an entity multiple times by its ID returns always the same
// entity object reference.
// 2. Loading an entity by its ID and by its default revision ID returns
// always the same entity object reference, if loaded by ID first.
// 3. Loading an entity multiple times by its revision ID returns always the
// same entity object reference.
$loaded = $storage->load($entity->id());
$this->assertSame($loaded, $storage->load($entity->id()));
$revision_id_cache = "values:{$entity->getEntityTypeId()}:revision:" . $entity->getRevisionId();
$this->assertFalse($persistent_cache->get($revision_id_cache));
$this->assertSame($loaded, $storage->loadRevision($entity->getRevisionId()));
// Because the revision is already in the static cache, loading it has not
// populated the revision cache.
$this->assertFalse($persistent_cache->get($revision_id_cache));
$this->assertSame($storage->loadRevision($non_default_rev_id), $storage->loadRevision($non_default_rev_id));
// Test that after resetting the entity cache then different object
// references will be returned.
$entity = $storage->load($entity->id());
$entity_default_revision = $storage->loadRevision($entity->getRevisionId());
$entity_non_default_revision = $storage->loadRevision($non_default_rev_id);
$storage->resetCache();
$this->assertNotSame($entity, $storage->load($entity->id()));
$this->assertNotSame($entity_default_revision, $storage->loadRevision($entity->getRevisionId()));
$this->assertNotSame($entity_non_default_revision, $storage->loadRevision($non_default_rev_id));
// Tests that the behavior for the three rules remains unchanged after
// resetting the entity cache.
$this->assertSame($storage->load($entity->id()), $storage->load($entity->id()));
$this->assertSame($storage->load($entity->id()), $storage->loadRevision($entity->getRevisionId()));
$this->assertSame($storage->loadRevision($non_default_rev_id), $storage->loadRevision($non_default_rev_id));
// Loading a revision does not populate the default revision static cache
// to prevent issues with preloading.
$storage->resetCache();
$this->assertFalse($persistent_cache->get($revision_id_cache));
$this->assertNotSame($storage->loadRevision($entity->getRevisionId()), $storage->load($entity->id()));
$this->assertNotFalse($persistent_cache->get($revision_id_cache));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.