public function FieldAttachOtherTest::testEntityCache in Drupal 8
Same name and namespace in other branches
- 9 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityCache()
- 10 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityCache()
Test entity cache.
Complements unit test coverage in \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest.
File
- core/
modules/ field/ tests/ src/ Kernel/ FieldAttachOtherTest.php, line 165
Class
- FieldAttachOtherTest
- Tests other Field API functions.
Namespace
Drupal\Tests\field\KernelCode
public function testEntityCache() {
// Initialize random values and a test entity.
$entity_init = EntityTest::create([
'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 = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create([
'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_type.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');
}