function FieldEntitySettingsTest::testEntityReferenceSettingsNormalized

Same name in other branches
  1. 10 core/modules/field/tests/src/Kernel/Entity/FieldEntitySettingsTest.php \Drupal\Tests\field\Kernel\Entity\FieldEntitySettingsTest::testEntityReferenceSettingsNormalized()

Tests entity reference settings are normalized on field creation and save.

File

core/modules/field/tests/src/Kernel/Entity/FieldEntitySettingsTest.php, line 94

Class

FieldEntitySettingsTest
Tests the ways that field entities handle their settings.

Namespace

Drupal\Tests\field\Kernel\Entity

Code

public function testEntityReferenceSettingsNormalized() : void {
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'test_reference',
        'type' => 'entity_reference',
        'entity_type' => 'entity_test_with_bundle',
        'cardinality' => 1,
        'settings' => [
            'target_type' => 'entity_test_with_bundle',
        ],
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'test',
        'label' => 'Test Reference',
        'settings' => [
            'handler' => 'default',
        ],
    ]);
    $this->assertSame('default:entity_test_with_bundle', $field->getSetting('handler'));
    // If the handler is changed, it should be normalized again on pre-save.
    $field->setSetting('handler', 'default')
        ->save();
    $this->assertSame('default:entity_test_with_bundle', $field->getSetting('handler'));
}

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