Test field cache.
function testFieldAttachCache() {
// Initialize random values and a test entity.
$entity_init = field_test_create_entity(1, 1, $this->instance['bundle']);
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$values = $this
->_generateTestFieldValues($this->field['cardinality']);
// Non-cacheable entity type.
$entity_type = 'test_entity';
$cid = "field:{$entity_type}:{$entity_init->ftid}";
// Check that no initial cache entry is present.
$this
->assertFalse(cache('field')
->get($cid), 'Non-cached: no initial cache entry');
// Save, and check that no cache entry is present.
$entity = clone $entity_init;
$entity->{$this->field_name}[$langcode] = $values;
field_attach_insert($entity);
$this
->assertFalse(cache('field')
->get($cid), 'Non-cached: no cache entry on insert');
// Load, and check that no cache entry is present.
$entity = clone $entity_init;
field_attach_load($entity_type, array(
$entity->ftid => $entity,
));
$this
->assertFalse(cache('field')
->get($cid), 'Non-cached: no cache entry on load');
// Cacheable entity type.
$entity_type = 'test_cacheable_entity';
$entity_init = entity_create($entity_type, array(
'ftid' => 1,
'ftvid' => 1,
'fttype' => $this->instance['bundle'],
));
$cid = "field:{$entity_type}:{$entity_init->ftid}";
$instance = $this->instance;
$instance['entity_type'] = $entity_type;
field_create_instance($instance);
// Check that no initial cache entry is present.
$this
->assertFalse(cache('field')
->get($cid), 'Cached: no initial cache entry');
// Save, and check that no cache entry is present.
$entity = clone $entity_init;
$entity->{$this->field_name}[$langcode] = $values;
field_attach_insert($entity);
$this
->assertFalse(cache('field')
->get($cid), 'Cached: no cache entry on insert');
// Load a single field, and check that no cache entry is present.
$entity = clone $entity_init;
field_attach_load($entity_type, array(
$entity->ftid => $entity,
), FIELD_LOAD_CURRENT, array(
'field_id' => $this->field_id,
));
$cache = cache('field')
->get($cid);
$this
->assertFalse(cache('field')
->get($cid), 'Cached: no cache entry on loading a single field');
// Load, and check that a cache entry is present with the expected values.
$entity = clone $entity_init;
field_attach_load($entity_type, array(
$entity->ftid => $entity,
));
$cache = cache('field')
->get($cid);
$this
->assertEqual($cache->data[$this->field_name][$langcode], $values, 'Cached: correct cache entry on load');
// Update with different values, and check that the cache entry is wiped.
$values = $this
->_generateTestFieldValues($this->field['cardinality']);
$entity = clone $entity_init;
$entity->{$this->field_name}[$langcode] = $values;
field_attach_update($entity);
$this
->assertFalse(cache('field')
->get($cid), 'Cached: no cache entry on update');
// Load, and check that a cache entry is present with the expected values.
$entity = clone $entity_init;
field_attach_load($entity_type, array(
$entity->ftid => $entity,
));
$cache = cache('field')
->get($cid);
$this
->assertEqual($cache->data[$this->field_name][$langcode], $values, 'Cached: correct cache entry on load');
// Create a new revision, and check that the cache entry is wiped.
$entity_init = entity_create($entity_type, array(
'ftid' => 1,
'ftvid' => 2,
'fttype' => $this->instance['bundle'],
));
$values = $this
->_generateTestFieldValues($this->field['cardinality']);
$entity = clone $entity_init;
$entity->{$this->field_name}[$langcode] = $values;
field_attach_update($entity);
$cache = cache('field')
->get($cid);
$this
->assertFalse(cache('field')
->get($cid), 'Cached: no cache entry on new revision creation');
// Load, and check that a cache entry is present with the expected values.
$entity = clone $entity_init;
field_attach_load($entity_type, array(
$entity->ftid => $entity,
));
$cache = cache('field')
->get($cid);
$this
->assertEqual($cache->data[$this->field_name][$langcode], $values, 'Cached: correct cache entry on load');
// Delete, and check that the cache entry is wiped.
field_attach_delete($entity);
$this
->assertFalse(cache('field')
->get($cid), 'Cached: no cache entry after delete');
}