FieldInfoTestCase::testFieldInfo

  1. drupal
    1. 8
    2. 7
Versions
8 FieldInfoTestCase::testFieldInfo()

Test that field types and field definitions are correcly cached.

Code

modules/field/tests/field.test, line 1037

<?php
function testFieldInfo() {
  // Test that field_test module's fields, widgets, and formatters show up.

  $field_test_info = field_test_field_info();
  // We need to account for the existence of user_field_info_alter().
  foreach (array_keys($field_test_info) as $name) {
    $field_test_info[$name]['instance_settings']['user_register_form'] = FALSE;
  }
  $info = field_info_field_types();
  foreach ($field_test_info as $t_key => $field_type) {
    foreach ($field_type as $key => $val) {
      $this->assertEqual($info[$t_key][$key], $val, t("Field type $t_key key $key is $val"));
    }
    $this->assertEqual($info[$t_key]['module'], 'field_test',   t("Field type field_test module appears"));
  }

  $formatter_info = field_test_field_formatter_info();
  $info = field_info_formatter_types();
  foreach ($formatter_info as $f_key => $formatter) {
    foreach ($formatter as $key => $val) {
      $this->assertEqual($info[$f_key][$key], $val, t("Formatter type $f_key key $key is $val"));
    }
    $this->assertEqual($info[$f_key]['module'], 'field_test',   t("Formatter type field_test module appears"));
  }

  $widget_info = field_test_field_widget_info();
  $info = field_info_widget_types();
  foreach ($widget_info as $w_key => $widget) {
    foreach ($widget as $key => $val) {
      $this->assertEqual($info[$w_key][$key], $val, t("Widget type $w_key key $key is $val"));
    }
    $this->assertEqual($info[$w_key]['module'], 'field_test',   t("Widget type field_test module appears"));
  }

  $storage_info = field_test_field_storage_info();
  $info = field_info_storage_types();
  foreach ($storage_info as $s_key => $storage) {
    foreach ($storage as $key => $val) {
      $this->assertEqual($info[$s_key][$key], $val, t("Storage type $s_key key $key is $val"));
    }
    $this->assertEqual($info[$s_key]['module'], 'field_test',   t("Storage type field_test module appears"));
  }

  // Verify that no unexpected instances exist.
  $core_fields = field_info_fields();
  $instances = field_info_instances('test_entity', 'test_bundle');
  $this->assertTrue(empty($instances), t('With no instances, info bundles is empty.'));

  // Create a field, verify it shows up.
  $field = array(
    'field_name' => drupal_strtolower($this->randomName()), 
    'type' => 'test_field',
  );
  field_create_field($field);
  $fields = field_info_fields();
  $this->assertEqual(count($fields), count($core_fields) + 1, t('One new field exists'));
  $this->assertEqual($fields[$field['field_name']]['field_name'], $field['field_name'], t('info fields contains field name'));
  $this->assertEqual($fields[$field['field_name']]['type'], $field['type'], t('info fields contains field type'));
  $this->assertEqual($fields[$field['field_name']]['module'], 'field_test', t('info fields contains field module'));
  $settings = array('test_field_setting' => 'dummy test string');
  foreach ($settings as $key => $val) {
    $this->assertEqual($fields[$field['field_name']]['settings'][$key], $val, t("Field setting $key has correct default value $val"));
  }
  $this->assertEqual($fields[$field['field_name']]['cardinality'], 1, t('info fields contains cardinality 1'));
  $this->assertEqual($fields[$field['field_name']]['active'], 1, t('info fields contains active 1'));

  // Create an instance, verify that it shows up
  $instance = array(
    'field_name' => $field['field_name'], 
    'entity_type' => 'test_entity', 
    'bundle' => 'test_bundle', 
    'label' => $this->randomName(), 
    'description' => $this->randomName(), 
    'weight' => mt_rand(0, 127),
    // test_field has no instance settings 
    'widget' => array(
      'type' => 'test_field_widget', 
      'settings' => array(
        'test_setting' => 999,
      ),
    ),
  );
  field_create_instance($instance);

  $instances = field_info_instances('test_entity', $instance['bundle']);
  $this->assertEqual(count($instances), 1, t('One instance shows up in info when attached to a bundle.'));
  $this->assertTrue($instance < $instances[$instance['field_name']], t('Instance appears in info correctly'));
}
?>