function ContentEntityCacheTest::doTestLoadUnchanged

Helper method for ::testLoadUnchanged().

For revisionable entities both the loadUnchanged and loadRevisionUnchanged storage methods are tested and for non-revisionable entities only the loadUnchanged storage method is tested.

Parameters

string $entity_type_id: The entity type ID to test Storage::loadUnchanged() with.

1 call to ContentEntityCacheTest::doTestLoadUnchanged()
ContentEntityCacheTest::testLoadUnchanged in core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCacheTest.php
Tests that on loading unchanged entity a new object reference is returned.

File

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

Class

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

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestLoadUnchanged($entity_type_id) : void {
  foreach ([
    FALSE,
    TRUE,
  ] as $invalidate_entity_cache) {
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = $this->container
      ->get('entity_type.manager');
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $entity_type_manager->getStorage($entity_type_id);
    $entity = $storage->create();
    $entity->save();
    $entity = $storage->load($entity->id());
    // Invalidating the entity cache will lead to not retrieving the entity
    // from the persistent entity cache. This simulates e.g. a behavior where
    // in an entity insert hook a field config is created and saved and then
    // the cache tag "entity_field_info" will be invalidated leading to
    // invalidating the entities in the entity cache, which will prevent
    // loadUnchanged from retrieving the entity from the persistent cache,
    // which will test that the static entity cache has been reset properly,
    // otherwise if not then the same entity object reference will be
    // returned.
    if ($invalidate_entity_cache) {
      Cache::invalidateTags([
        'entity_field_info',
      ]);
    }
    $unchanged = $storage->loadUnchanged($entity->id());
    $message = $invalidate_entity_cache ? 'loadUnchanged returns a different entity object reference when the entity cache is invalidated before that.' : 'loadUnchanged returns a different entity object reference when the entity cache is not invalidated before that.';
    $this->assertNotSame($entity, $unchanged, $message);
    // For entities, the static cache will be cleared by loadUnchanged() for
    // backwards-compatibility.
    $this->assertNotSame($entity, $storage->load($entity->id()));
    // For revisionable entities test the same way the
    // Storage::loadRevisionUnchanged method as well.
    if ($entity_type->isRevisionable()) {
      $entity = $storage->loadRevision($entity->getRevisionId());
      if ($invalidate_entity_cache) {
        Cache::invalidateTags([
          'entity_field_info',
        ]);
      }
      $unchanged = $storage->loadRevisionUnchanged($entity->getRevisionId());
      $message = $invalidate_entity_cache ? 'loadRevisionUnchanged returns a different entity object reference when the entity cache is invalidated before that.' : 'loadRevisionUnchanged returns a different entity object reference when the entity cache is not invalidated before that.';
      $this->assertNotSame($entity, $unchanged, $message);
      // Make sure that calling loadRevisionUnchanged() does not clear the
      // static revision cache.
      $this->assertSame($entity, $storage->loadRevision($entity->getRevisionId()));
    }
  }
}

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