You are here

public function FieldEncryptCacheTest::testEntityCache in Field Encryption 8.2

Test caching of encrypted fields on entity level.

File

tests/src/Functional/FieldEncryptCacheTest.php, line 67

Class

FieldEncryptCacheTest
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 uncacheable_entity_types setting.
  $types = \Drupal::state()
    ->get('uncacheable_entity_types');
  $this
    ->assertEquals([
    'node' => 'node',
  ], $types);

  // Check whether node entities are marked as uncacheable.
  $definition = $this->entityTypeManager
    ->getDefinition('node');
  $this
    ->assertFalse($definition
    ->isPersistentlyCacheable());
  $this
    ->assertFalse($definition
    ->isRenderCacheable());
  $this
    ->assertFalse($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
    ->setFieldStorageSettings(TRUE, FALSE, FALSE);

  // Check uncacheable_entity_types setting.
  $types = \Drupal::state()
    ->get('uncacheable_entity_types');
  $this
    ->assertEquals([], $types);

  // 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.');
}