function CrudTest::testReadFields

Tests reading field definitions.

File

drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php, line 198
Definition of Drupal\field\Tests\CrudTest.

Class

CrudTest

Namespace

Drupal\field\Tests

Code

function testReadFields() {
  $field_definition = array(
    'field_name' => 'field_1',
    'type' => 'test_field',
  );
  field_create_field($field_definition);

  // Check that 'single column' criteria works.
  $fields = field_read_fields(array(
    'field_name' => $field_definition['field_name'],
  ));
  $this
    ->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.');

  // Check that 'multi column' criteria works.
  $fields = field_read_fields(array(
    'field_name' => $field_definition['field_name'],
    'type' => $field_definition['type'],
  ));
  $this
    ->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.');
  $fields = field_read_fields(array(
    'field_name' => $field_definition['field_name'],
    'type' => 'foo',
  ));
  $this
    ->assertTrue(empty($fields), 'No field was found.');

  // Create an instance of the field.
  $instance_definition = array(
    'field_name' => $field_definition['field_name'],
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
  );
  field_create_instance($instance_definition);

  // Check that criteria spanning over the field_config_instance table work.
  $fields = field_read_fields(array(
    'entity_type' => $instance_definition['entity_type'],
    'bundle' => $instance_definition['bundle'],
  ));
  $this
    ->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.');
  $fields = field_read_fields(array(
    'entity_type' => $instance_definition['entity_type'],
    'field_name' => $instance_definition['field_name'],
  ));
  $this
    ->assertTrue(count($fields) == 1 && isset($fields[$field_definition['field_name']]), 'The field was properly read.');
}