Tests field widget default values on entity forms.
function testFieldFormDefaultValue() {
$this->field = $this->field_single;
$this->field_name = $this->field['field_name'];
$this->instance['field_name'] = $this->field_name;
$default = rand(1, 127);
$this->instance['default_value'] = array(
array(
'value' => $default,
),
);
field_create_field($this->field);
field_create_instance($this->instance);
entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
->setComponent($this->field_name)
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Display creation form.
$this
->drupalGet('test-entity/add/test_bundle');
// Test that the default value is displayed correctly.
$this
->assertFieldByXpath("//input[@name='{$this->field_name}[{$langcode}][0][value]' and @value='{$default}']");
// Try to submit an empty value.
$edit = array(
"{$this->field_name}[{$langcode}][0][value]" => '',
);
$this
->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
$id = $match[1];
$this
->assertRaw(t('test_entity @id has been created.', array(
'@id' => $id,
)), 'Entity was created.');
$entity = field_test_entity_test_load($id);
$this
->assertTrue(empty($entity->{$this->field_name}), 'Field is now empty.');
}