Tests that default value is correctly validated and saved.
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,
);
$instance = field_create_instance($instance);
entity_get_form_display('node', $this->type, 'default')
->setComponent($field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$admin_path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $instance
->id();
$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.');
field_info_cache_clear();
$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.');
// Change the widget to TestFieldWidgetNoDefault.
entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')
->setComponent($field_name, array(
'type' => 'test_field_widget_no_default',
))
->save();
$this
->drupalGet($admin_path);
$this
->assertNoFieldById($element_id, '', t('No default value was possible for widget that disables default value.'));
}