CacheTest.php in Field Encryption 3.0.x        
                          
                  
                        
  
  
  
  
  
File
  tests/src/Functional/CacheTest.php
  
    View source  
  <?php
namespace Drupal\Tests\field_encrypt\Functional;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\field\Entity\FieldStorageConfig;
class CacheTest extends FieldEncryptTestBase {
  
  public static $modules = [
    'dynamic_page_cache',
  ];
  
  protected $entityTypeManager;
  
  protected function setUp() {
    parent::setUp();
    $this->entityTypeManager = $this->container
      ->get('entity_type.manager');
    
    $this
      ->setFieldStorageSettings(TRUE);
    
    $this
      ->createTestNode();
  }
  
  public function testDynamicPageCache() {
    
    $this
      ->drupalGet('node/' . $this->testNode
      ->id());
    $this
      ->assertEquals('UNCACHEABLE', $this
      ->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Page with encrypted fields is uncacheable.');
    
    $this
      ->drupalGet('admin/config/system/field-encrypt');
    $this
      ->submitForm([
      'make_entities_uncacheable' => FALSE,
    ], 'Save configuration');
    
    $this
      ->drupalGet('node/' . $this->testNode
      ->id());
    $this
      ->assertEquals('MISS', $this
      ->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Dynamic Page Cache MISS.');
    
    $this
      ->drupalGet('node/' . $this->testNode
      ->id());
    $this
      ->assertEquals('HIT', $this
      ->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Dynamic Page Cache HIT.');
  }
  
  public function testEntityCache() {
    
    $entity_type = $this->testNode
      ->getEntityTypeId();
    $cid = "values:{$entity_type}:" . $this->testNode
      ->id();
    
    $definition = $this->entityTypeManager
      ->getDefinition('node');
    $this
      ->assertFalse($definition
      ->isPersistentlyCacheable());
    $this
      ->assertFalse($definition
      ->isRenderCacheable());
    $this
      ->assertTrue($definition
      ->isStaticallyCacheable());
    
    $this
      ->assertFalse(\Drupal::cache('entity')
      ->get($cid), 'Entity cache: no initial cache.');
    $controller = $this->entityTypeManager
      ->getStorage($entity_type);
    $controller
      ->load($this->testNode
      ->id());
    
    $this
      ->assertFalse(\Drupal::cache('entity')
      ->get($cid), 'Entity cache: entity is not in persistent cache.');
    
    $this
      ->config('field_encrypt.settings')
      ->set('make_entities_uncacheable', FALSE)
      ->save();
    
    \Drupal::service('entity.memory_cache')
      ->deleteAll();
    
    $definition = $this->entityTypeManager
      ->getDefinition('node');
    $this
      ->assertTrue($definition
      ->isPersistentlyCacheable());
    $this
      ->assertTrue($definition
      ->isRenderCacheable());
    $this
      ->assertTrue($definition
      ->isStaticallyCacheable());
    
    $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.');
  }
  
  protected function setFieldStorageSettings($encryption = TRUE) {
    $fields = [
      'node.field_test_single' => [
        'properties' => [
          'value' => 'value',
          'summary' => 'summary',
        ],
      ],
      'node.field_test_multi' => [
        'properties' => [
          'value' => 'value',
        ],
      ],
    ];
    foreach ($fields as $field => $settings) {
      $field_storage = FieldStorageConfig::load($field);
      if ($encryption) {
        $field_storage
          ->setThirdPartySetting('field_encrypt', 'encrypt', TRUE);
        $field_storage
          ->setThirdPartySetting('field_encrypt', 'properties', $settings['properties']);
      }
      else {
        $field_storage
          ->unsetThirdPartySetting('field_encrypt', 'encrypt');
        $field_storage
          ->unsetThirdPartySetting('field_encrypt', 'properties');
      }
      $field_storage
        ->save();
    }
  }
}