function FieldUIManageFieldsTestCase::testDefaultValue

7.x field_ui.test FieldUIManageFieldsTestCase::testDefaultValue()

Tests that default value is correctly validated and saved.

File

drupal-7.x/modules/field_ui/field_ui.test, line 286
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->drupalGet($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->drupalPost($admin_path, $edit, t('Save settings'));
  $this->assertText("$field_name does not accept the value -1", 'Form vaildation failed.');

  // Check that the default value is saved.
  $edit = array($element_name => '1');
  $this->drupalPost($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->drupalGet($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->drupalPost(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.');
}