FieldUIManageFieldsTestCase::testDefaultValue

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

Tests that default value is correctly validated and saved.

File

modules/field_ui/field_ui.test, line 286
Tests for field_ui.module.

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, '', t('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", t('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", t('The form was successfully submitted.'));
  $instance = field_info_instance('node', $field_name, $this->type);
  $this->assertEqual($instance['default_value'], array(array('value' => 1)), t('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', t('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", t('The form was successfully submitted.'));
  field_info_cache_clear();
  $instance = field_info_instance('node', $field_name, $this->type);
  $this->assertEqual($instance['default_value'], NULL, t('The default value was correctly saved.'));
}
Login or register to post comments