1 field.test FieldAttachStorageTestCase::testFieldHasData()

Test field_has_data().

File

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

Class

FieldAttachStorageTestCase
Unit test class for storage-related field_attach_* functions.

Code

function testFieldHasData() {
  $entity_type = 'test_entity';
  $langcode = LANGUAGE_NONE;

  $field_name = 'field_1';
  $field = array('field_name' => $field_name, 'type' => 'test_field');
  $field = field_create_field($field);

  $this->assertFalse(field_has_data($field), "No data should be detected.");

  $revision_table = _field_sql_storage_revision_tablename($field);

  $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $field_name . '_value');

  $eid = 0;

  // Insert values into the field revision table.
  $query = db_insert($revision_table)->fields($columns);
  $query->values(array($entity_type, $eid, 0, 0, $langcode, 1));
  $query->execute();

  $this->assertTrue(field_has_data($field), "Revision data only should be detected.");

  $field_name = 'field_2';
  $field = array('field_name' => $field_name, 'type' => 'test_field');
  $field = field_create_field($field);

  $this->assertFalse(field_has_data($field), "No data should be detected.");

  $table = _field_sql_storage_tablename($field);
  $columns = array('entity_type', 'entity_id', 'revision_id', 'delta', 'language', $field_name . '_value');

  $eid = 1;

  // Insert values into the field table.
  $query = db_insert($table)->fields($columns);
  $query->values(array($entity_type, $eid, 0, 0, $langcode, 1));
  $query->execute();

  $this->assertTrue(field_has_data($field), "Values only in field table should be detected.");
}