1 field.test FieldAttachOtherTestCase::testFieldAttachCache()

Test field cache.

File

core/modules/field/tests/field.test, line 890
Tests for field.module.

Class

FieldAttachOtherTestCase
Unit test class for non-storage related field_attach_* functions.

Code

function testFieldAttachCache() {
  // Initialize random values and a test entity.
  $entity_init = field_test_create_entity(1, 1, $this->instance['bundle']);
  $langcode = LANGUAGE_NONE;
  $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_type, $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';
  $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_type, $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_name' => $this->field_name));
  $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_type, $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 = field_test_create_entity(1, 2, $this->instance['bundle']);
  $values = $this->_generateTestFieldValues($this->field['cardinality']);
  $entity = clone($entity_init);
  $entity->{$this->field_name}[$langcode] = $values;
  field_attach_update($entity_type, $entity);
  $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_type, $entity);
  $this->assertFalse(cache('field')->get($cid), 'Cached: no cache entry after delete');
}