function EntityReferenceSettingsTest::testCustomTargetBundleDeletion

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceSettingsTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceSettingsTest::testCustomTargetBundleDeletion()
  2. 8.9.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceSettingsTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceSettingsTest::testCustomTargetBundleDeletion()
  3. 11.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceSettingsTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceSettingsTest::testCustomTargetBundleDeletion()

Tests that deletions of custom bundles are mirrored in field settings.

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceSettingsTest.php, line 153

Class

EntityReferenceSettingsTest
Tests entity reference field settings.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testCustomTargetBundleDeletion() : void {
    // Attach an entity reference field to $this->nodeType.
    $name = $this->randomMachineName();
    $label = $this->randomString();
    $handler_settings = [
        'target_bundles' => [
            $this->customBundle => $this->customBundle,
        ],
    ];
    $this->createEntityReferenceField('node', $this->nodeType
        ->id(), $name, $label, 'entity_test', 'default', $handler_settings);
    // Check that the 'target_bundle' setting contains the custom bundle.
    $field_config = FieldConfig::loadByName('node', $this->nodeType
        ->id(), $name);
    $actual_handler_settings = $field_config->getSetting('handler_settings');
    $this->assertEquals($handler_settings, $actual_handler_settings);
    // Delete the custom bundle.
    entity_test_delete_bundle($this->customBundle, 'entity_test');
    // Ensure that field_field_config_presave() logs the expected critical
    // error.
    $log_message = $this->container
        ->get($this->testLogServiceName)
        ->cleanLogs()[0];
    $this->assertEquals(RfcLogLevel::CRITICAL, $log_message[0]);
    $this->assertEquals('The %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', $log_message[1]);
    $this->assertEquals($field_config->getName(), $log_message[2]['%field_name']);
    $this->assertEquals('node', $log_message[2]['%entity_type']);
    $this->assertEquals($this->nodeType
        ->id(), $log_message[2]['%bundle']);
    // Check that the deleted bundle is no longer present in the
    // 'target_bundles' field setting.
    $field_config = FieldConfig::loadByName('node', $this->nodeType
        ->id(), $name);
    $handler_settings = $field_config->getSetting('handler_settings');
    $this->assertEmpty($handler_settings['target_bundles']);
}

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