You are here

public function EntityReferenceFieldDefaultValueTest::testEntityReferenceDefaultConfigValue in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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 122

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
    ->drupalPostForm('admin/structure/types/manage/reference_content/fields/node.reference_content.' . $field_name, $field_edit, t('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.');
}