function EntityReferenceFieldDefaultValueTest::testEntityReferenceDefaultConfigValue

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldDefaultValueTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceFieldDefaultValueTest::testEntityReferenceDefaultConfigValue()
  2. 10 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldDefaultValueTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceFieldDefaultValueTest::testEntityReferenceDefaultConfigValue()
  3. 11.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldDefaultValueTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceFieldDefaultValueTest::testEntityReferenceDefaultConfigValue()

Tests that dependencies due to default values can be removed.

See also

\Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::onDependencyRemoval()

File

core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldDefaultValueTest.php, line 126

Class

EntityReferenceFieldDefaultValueTest
Tests entity reference field default values storage in CMI.

Namespace

Drupal\Tests\field\Functional\EntityReference

Code

public function testEntityReferenceDefaultConfigValue() {
    // Create a node to be referenced.
    $referenced_node_type = $this->drupalCreateContentType([
        'type' => 'referenced_config_to_delete',
    ]);
    $referenced_node_type2 = $this->drupalCreateContentType([
        'type' => 'referenced_config_to_preserve',
    ]);
    $field_name = mb_strtolower($this->randomMachineName());
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'node',
        'type' => 'entity_reference',
        'settings' => [
            'target_type' => 'node_type',
        ],
        'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'reference_content',
        'settings' => [
            'handler' => 'default',
            'handler_settings' => [
                'sort' => [
                    'field' => '_none',
                ],
            ],
        ],
    ]);
    $field->save();
    // Set created node as default_value.
    $field_edit = [
        'default_value_input[' . $field_name . '][0][target_id]' => $referenced_node_type->label() . ' (' . $referenced_node_type->id() . ')',
        'default_value_input[' . $field_name . '][1][target_id]' => $referenced_node_type2->label() . ' (' . $referenced_node_type2->id() . ')',
    ];
    $this->drupalGet('admin/structure/types/manage/reference_content/fields/node.reference_content.' . $field_name);
    $this->submitForm($field_edit, 'Save settings');
    // Check that the field has a dependency on the default value.
    $config_entity = $this->config('field.field.node.reference_content.' . $field_name)
        ->get();
    $this->assertContains($referenced_node_type->getConfigDependencyName(), $config_entity['dependencies']['config'], 'The node type referenced_config_to_delete is a dependency of the field.');
    $this->assertContains($referenced_node_type2->getConfigDependencyName(), $config_entity['dependencies']['config'], 'The node type referenced_config_to_preserve is a dependency of the field.');
    // Check that the field does not have a dependency on the default value
    // after deleting the node type.
    $referenced_node_type->delete();
    $config_entity = $this->config('field.field.node.reference_content.' . $field_name)
        ->get();
    $this->assertNotContains($referenced_node_type->getConfigDependencyName(), $config_entity['dependencies']['config'], 'The node type referenced_config_to_delete not a dependency of the field.');
    $this->assertContains($referenced_node_type2->getConfigDependencyName(), $config_entity['dependencies']['config'], 'The node type referenced_config_to_preserve is a dependency of the field.');
}

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