You are here

public function CacheTest::testEntityCache in Field Encryption 3.0.x

Test caching of encrypted fields on entity level.

File

tests/src/Functional/CacheTest.php, line 68

Class

CacheTest
Tests field encryption caching.

Namespace

Drupal\Tests\field_encrypt\Functional

Code

public function testEntityCache() {

  // Check if entity with uncacheable fields is cached by the entity
  // storage.
  $entity_type = $this->testNode
    ->getEntityTypeId();
  $cid = "values:{$entity_type}:" . $this->testNode
    ->id();

  // Check whether node entities are marked as uncacheable.
  $definition = $this->entityTypeManager
    ->getDefinition('node');
  $this
    ->assertFalse($definition
    ->isPersistentlyCacheable());
  $this
    ->assertFalse($definition
    ->isRenderCacheable());
  $this
    ->assertTrue($definition
    ->isStaticallyCacheable());

  // Check that no initial cache entry is present.
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Entity cache: no initial cache.');
  $controller = $this->entityTypeManager
    ->getStorage($entity_type);
  $controller
    ->load($this->testNode
    ->id());

  // Check if entity gets cached.
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Entity cache: entity is not in persistent cache.');

  // Set encrypted field as cacheable.
  $this
    ->config('field_encrypt.settings')
    ->set('make_entities_uncacheable', FALSE)
    ->save();

  // Clear memory cache so the entity will now make it to the persistent
  // cache.
  \Drupal::service('entity.memory_cache')
    ->deleteAll();

  // Check whether node entities are marked as cacheable.
  $definition = $this->entityTypeManager
    ->getDefinition('node');
  $this
    ->assertTrue($definition
    ->isPersistentlyCacheable());
  $this
    ->assertTrue($definition
    ->isRenderCacheable());
  $this
    ->assertTrue($definition
    ->isStaticallyCacheable());

  // Load the node again. It should be cached now.
  $controller = $this->entityTypeManager
    ->getStorage($entity_type);
  $controller
    ->load($this->testNode
    ->id());
  $cache = \Drupal::cache('entity')
    ->get($cid);
  $this
    ->assertTrue(is_object($cache), 'Entity cache: entity is in persistent cache.');
}