function FormTest::testFieldFormDefaultValue in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/field/src/Tests/FormTest.php \Drupal\field\Tests\FormTest::testFieldFormDefaultValue()
Tests field widget default values on entity forms.
File
- core/
modules/ field/ src/ Tests/ FormTest.php, line 172 - Contains \Drupal\field\Tests\FormTest.
Class
- FormTest
- Tests field form handling.
Namespace
Drupal\field\TestsCode
function testFieldFormDefaultValue() {
$field_storage = $this->fieldStorageSingle;
$field_name = $field_storage['field_name'];
$this->field['field_name'] = $field_name;
$default = rand(1, 127);
$this->field['default_value'] = array(
array(
'value' => $default,
),
);
entity_create('field_storage_config', $field_storage)
->save();
entity_create('field_config', $this->field)
->save();
entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
->setComponent($field_name)
->save();
// Display creation form.
$this
->drupalGet('entity_test/add');
// Test that the default value is displayed correctly.
$this
->assertFieldByXpath("//input[@name='{$field_name}[0][value]' and @value='{$default}']");
// Try to submit an empty value.
$edit = array(
"{$field_name}[0][value]" => '',
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
$id = $match[1];
$this
->assertText(t('entity_test @id has been created.', array(
'@id' => $id,
)), 'Entity was created.');
$entity = entity_load('entity_test', $id);
$this
->assertTrue($entity->{$field_name}
->isEmpty(), 'Field is now empty.');
}