You are here

function EntityReferenceFieldDefaultValueTest::testEntityReferenceDefaultConfigValue in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/field/src/Tests/EntityReference/EntityReferenceFieldDefaultValueTest.php \Drupal\field\Tests\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/src/Tests/EntityReference/EntityReferenceFieldDefaultValueTest.php, line 115
Contains \Drupal\field\Tests\EntityReference\EntityReferenceFieldDefaultValueTest.

Class

EntityReferenceFieldDefaultValueTest
Tests entity reference field default values storage in CMI.

Namespace

Drupal\field\Tests\EntityReference

Code

function testEntityReferenceDefaultConfigValue() {

  // Create a node to be referenced.
  $referenced_node_type = $this
    ->drupalCreateContentType(array(
    'type' => 'referenced_config_to_delete',
  ));
  $referenced_node_type2 = $this
    ->drupalCreateContentType(array(
    'type' => 'referenced_config_to_preserve',
  ));
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'settings' => array(
      'target_type' => 'node_type',
    ),
    'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
  ));
  $field_storage
    ->save();
  $field = entity_create('field_config', array(
    'field_storage' => $field_storage,
    'bundle' => 'reference_content',
    'settings' => array(
      'handler' => 'default',
      'handler_settings' => array(
        'sort' => array(
          'field' => '_none',
        ),
      ),
    ),
  ));
  $field
    ->save();

  // Set created node as default_value.
  $field_edit = array(
    '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
    ->assertTrue(in_array($referenced_node_type
    ->getConfigDependencyName(), $config_entity['dependencies']['config'], TRUE), 'The node type referenced_config_to_delete is a dependency of the field.');
  $this
    ->assertTrue(in_array($referenced_node_type2
    ->getConfigDependencyName(), $config_entity['dependencies']['config'], TRUE), '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
    ->assertFalse(in_array($referenced_node_type
    ->getConfigDependencyName(), $config_entity['dependencies']['config'], TRUE), 'The node type referenced_config_to_delete not a dependency of the field.');
  $this
    ->assertTrue(in_array($referenced_node_type2
    ->getConfigDependencyName(), $config_entity['dependencies']['config'], TRUE), 'The node type referenced_config_to_preserve is a dependency of the field.');
}