function ManageFieldsFunctionalTest::testDefaultValue

Same name in other branches
  1. 9 core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testDefaultValue()
  2. 8.9.x core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::testDefaultValue()
  3. 11.x 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 23

Class

ManageFieldsFunctionalTest
Tests the Field UI "Manage fields" screen.

Namespace

Drupal\Tests\field_ui\Functional

Code

public function testDefaultValue() : void {
    // 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->assertSession()
        ->fieldValueEquals($element_id, '');
    // Check that invalid default values are rejected.
    $edit = [
        $element_name => '-1',
        'set_default_value' => '1',
    ];
    $this->drupalGet($admin_path);
    $this->submitForm($edit, 'Save settings');
    $this->assertSession()
        ->pageTextContains("{$field_name} does not accept the value -1");
    // Check that the default value is saved.
    $edit = [
        $element_name => '1',
        'set_default_value' => '1',
    ];
    $this->drupalGet($admin_path);
    $this->submitForm($edit, 'Save settings');
    $this->assertSession()
        ->pageTextContains("Saved {$field_name} configuration");
    $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
    $this->assertEquals([
        [
            'value' => 1,
        ],
    ], $field->getDefaultValueLiteral(), 'The default value was correctly saved.');
    // Check that the default value shows up in the form.
    $this->drupalGet($admin_path);
    $this->assertSession()
        ->fieldValueEquals($element_id, '1');
    // Check that the default value is left empty when "Set default value"
    // checkbox is not checked.
    $edit = [
        $element_name => '1',
        'set_default_value' => '0',
    ];
    $this->drupalGet($admin_path);
    $this->submitForm($edit, 'Save settings');
    $this->assertSession()
        ->pageTextContains("Saved {$field_name} configuration");
    $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
    $this->assertEquals([], $field->getDefaultValueLiteral(), 'The default value was removed.');
    // Check that the default value can be emptied.
    $this->drupalGet($admin_path);
    $edit = [
        $element_name => '',
    ];
    $this->submitForm($edit, 'Save settings');
    $this->assertSession()
        ->pageTextContains("Saved {$field_name} configuration");
    $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
    $this->assertEquals([], $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->submitForm($edit, 'Save settings');
    $this->drupalGet($admin_path);
    $this->submitForm([], 'Save settings');
    $this->assertSession()
        ->pageTextContains("Saved {$field_name} configuration");
    $field = FieldConfig::loadByName('node', $this->contentType, $field_name);
    $this->assertEquals([], $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->assertSession()
        ->fieldValueEquals($element_id, '');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.