function FieldAttachStorageTestCase::testFieldHasData

Test field_has_data().

File

drupal/modules/field/tests/field.test, line 490
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.");
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
  );
  $instance = field_create_instance($instance);
  $table = _field_sql_storage_tablename($field);
  $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.");
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
  );
  $instance = field_create_instance($instance);
  $table = _field_sql_storage_tablename($field);
  $revision_table = _field_sql_storage_revision_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.");
}