You are here

function FieldAttachOtherTest::testEntityCache in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/FieldAttachOtherTest.php \Drupal\field\Tests\FieldAttachOtherTest::testEntityCache()

Test entity cache.

Complements unit test coverage in \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest.

File

core/modules/field/src/Tests/FieldAttachOtherTest.php, line 165
Contains \Drupal\field\Tests\FieldAttachOtherTest.

Class

FieldAttachOtherTest
Tests other Field API functions.

Namespace

Drupal\field\Tests

Code

function testEntityCache() {

  // Initialize random values and a test entity.
  $entity_init = entity_create('entity_test', array(
    'type' => $this->fieldTestData->field
      ->getTargetBundle(),
  ));
  $values = $this
    ->_generateTestFieldValues($this->fieldTestData->field_storage
    ->getCardinality());

  // Non-cacheable entity type.
  $entity_type = 'entity_test';
  $cid = "values:{$entity_type}:" . $entity_init
    ->id();

  // Check that no initial cache entry is present.
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Non-cached: no initial cache entry');

  // Save, and check that no cache entry is present.
  $entity = clone $entity_init;
  $entity->{$this->fieldTestData->field_name}
    ->setValue($values);
  $entity = $this
    ->entitySaveReload($entity);
  $cid = "values:{$entity_type}:" . $entity
    ->id();
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Non-cached: no cache entry on insert and load');

  // Cacheable entity type.
  $entity_type = 'entity_test_rev';
  $this
    ->createFieldWithStorage('_2', $entity_type);
  $entity_init = entity_create($entity_type, array(
    'type' => $entity_type,
  ));

  // Check that no initial cache entry is present.
  $cid = "values:{$entity_type}:" . $entity
    ->id();
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Cached: no initial cache entry');

  // Save, and check that no cache entry is present.
  $entity = clone $entity_init;
  $entity->{$this->fieldTestData->field_name_2} = $values;
  $entity
    ->save();
  $cid = "values:{$entity_type}:" . $entity
    ->id();
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Cached: no cache entry on insert');

  // Load, and check that a cache entry is present with the expected values.
  $controller = $this->container
    ->get('entity.manager')
    ->getStorage($entity
    ->getEntityTypeId());
  $controller
    ->resetCache();
  $cached_entity = $controller
    ->load($entity
    ->id());
  $cache = \Drupal::cache('entity')
    ->get($cid);
  $this
    ->assertEqual($cache->data, $cached_entity, 'Cached: correct cache entry on load');

  // Update with different values, and check that the cache entry is wiped.
  $values = $this
    ->_generateTestFieldValues($this->fieldTestData->field_storage_2
    ->getCardinality());
  $entity->{$this->fieldTestData->field_name_2} = $values;
  $entity
    ->save();
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Cached: no cache entry on update');

  // Load, and check that a cache entry is present with the expected values.
  $controller
    ->resetCache();
  $cached_entity = $controller
    ->load($entity
    ->id());
  $cache = \Drupal::cache('entity')
    ->get($cid);
  $this
    ->assertEqual($cache->data, $cached_entity, 'Cached: correct cache entry on load');

  // Create a new revision, and check that the cache entry is wiped.
  $values = $this
    ->_generateTestFieldValues($this->fieldTestData->field_storage_2
    ->getCardinality());
  $entity->{$this->fieldTestData->field_name_2} = $values;
  $entity
    ->setNewRevision();
  $entity
    ->save();
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Cached: no cache entry on new revision creation');

  // Load, and check that a cache entry is present with the expected values.
  $controller
    ->resetCache();
  $cached_entity = $controller
    ->load($entity
    ->id());
  $cache = \Drupal::cache('entity')
    ->get($cid);
  $this
    ->assertEqual($cache->data, $cached_entity, 'Cached: correct cache entry on load');

  // Delete, and check that the cache entry is wiped.
  $entity
    ->delete();
  $this
    ->assertFalse(\Drupal::cache('entity')
    ->get($cid), 'Cached: no cache entry after delete');
}