1 field_ui.test FieldUIManageFieldsTestCase::testDefaultValue()

Tests that default value is correctly validated and saved.

File

core/modules/field_ui/tests/field_ui.test, line 371
Tests for field_ui.module.

Class

FieldUIManageFieldsTestCase
Tests the functionality of the 'Manage fields' screen.

Code

function testDefaultValue() {
  // Create a test field and instance.
  $field_name = 'test';
  $field = array(
    'field_name' => $field_name,
    'type' => 'test_field'
  );
  field_create_field($field);
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'bundle' => $this->type,
  );
  field_create_instance($instance);

  $langcode = LANGUAGE_NONE;
  $admin_path = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields/' . $field_name;
  $element_id = "edit-$field_name-$langcode-0-value";
  $element_name = "{$field_name}[$langcode][0][value]";
  $this->backdropGet($admin_path);
  $this->assertFieldById($element_id, '', 'The default value widget was empty.');

  // Check that invalid default values are rejected.
  $edit = array($element_name => '-1');
  $this->backdropPost($admin_path, $edit, t('Save settings'));
  $this->assertText("$field_name does not accept the value -1", 'Form validation failed.');

  // Check that the default value is saved.
  $edit = array($element_name => '1');
  $this->backdropPost($admin_path, $edit, t('Save settings'));
  $this->assertText("Saved $field_name configuration", 'The form was successfully submitted.');
  $instance = field_info_instance('node', $field_name, $this->type);
  $this->assertEqual($instance['default_value'], array(array('value' => 1)), 'The default value was correctly saved.');

  // Check that the default value shows up in the form
  $this->backdropGet($admin_path);
  $this->assertFieldById($element_id, '1', 'The default value widget was displayed with the correct value.');

  // Check that the default value can be emptied.
  $edit = array($element_name => '');
  $this->backdropPost(NULL, $edit, t('Save settings'));
  $this->assertText("Saved $field_name configuration", 'The form was successfully submitted.');
  field_info_cache_clear();
  $instance = field_info_instance('node', $field_name, $this->type);
  $this->assertEqual($instance['default_value'], NULL, 'The default value was correctly saved.');
}