public function ManageFieldsFunctionalTest::testDefaultValue in Drupal 8
Same name and namespace in other branches
- 9 core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testDefaultValue()
- 10 core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testDefaultValue()
Tests that default value is correctly validated and saved.
File
- core/
modules/ field_ui/ tests/ src/ Functional/ ManageFieldsFunctionalTest.php, line 483
Class
- ManageFieldsFunctionalTest
- Tests the Field UI "Manage fields" screen.
Namespace
Drupal\Tests\field_ui\FunctionalCode
public function testDefaultValue() {
// Create a test field storage and field.
$field_name = 'test';
FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'test_field',
])
->save();
$field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'bundle' => $this->contentType,
]);
$field
->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', $this->contentType)
->setComponent($field_name)
->save();
$admin_path = 'admin/structure/types/manage/' . $this->contentType . '/fields/' . $field
->id();
$element_id = "edit-default-value-input-{$field_name}-0-value";
$element_name = "default_value_input[{$field_name}][0][value]";
$this
->drupalGet($admin_path);
$this
->assertFieldById($element_id, '', 'The default value widget was empty.');
// Check that invalid default values are rejected.
$edit = [
$element_name => '-1',
];
$this
->drupalPostForm($admin_path, $edit, t('Save settings'));
$this
->assertText("{$field_name} does not accept the value -1", 'Form validation failed.');
// Check that the default value is saved.
$edit = [
$element_name => '1',
];
$this
->drupalPostForm($admin_path, $edit, t('Save settings'));
$this
->assertText("Saved {$field_name} configuration", 'The form was successfully submitted.');
$field = FieldConfig::loadByName('node', $this->contentType, $field_name);
$this
->assertEqual($field
->getDefaultValueLiteral(), [
[
'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 = [
$element_name => '',
];
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->assertText("Saved {$field_name} configuration", 'The form was successfully submitted.');
$field = FieldConfig::loadByName('node', $this->contentType, $field_name);
$this
->assertEqual($field
->getDefaultValueLiteral(), [], 'The default value was correctly saved.');
// Check that the default value can be empty when the field is marked as
// required and can store unlimited values.
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
$field_storage
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$field_storage
->save();
$this
->drupalGet($admin_path);
$edit = [
'required' => 1,
];
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->drupalGet($admin_path);
$this
->drupalPostForm(NULL, [], t('Save settings'));
$this
->assertText("Saved {$field_name} configuration", 'The form was successfully submitted.');
$field = FieldConfig::loadByName('node', $this->contentType, $field_name);
$this
->assertEqual($field
->getDefaultValueLiteral(), [], 'The default value was correctly saved.');
// Check that the default widget is used when the field is hidden.
$display_repository
->getFormDisplay($field
->getTargetEntityTypeId(), $field
->getTargetBundle())
->removeComponent($field_name)
->save();
$this
->drupalGet($admin_path);
$this
->assertFieldById($element_id, '', 'The default value widget was displayed when field is hidden.');
}