function EntityReferenceItemTest::testSelectionHandlerSettings

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testSelectionHandlerSettings()
  2. 8.9.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testSelectionHandlerSettings()
  3. 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testSelectionHandlerSettings()

Tests that the 'handler' field setting stores the proper plugin ID.

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php, line 368

Class

EntityReferenceItemTest
Tests the new entity API for the entity reference field type.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testSelectionHandlerSettings() : void {
    $field_name = $this->randomMachineName();
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'entity_reference',
        'settings' => [
            'target_type' => 'entity_test',
        ],
    ]);
    $field_storage->save();
    // Do not specify any value for the 'handler' setting in order to verify
    // that the default handler with the correct derivative is used.
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'entity_test',
    ]);
    $field->save();
    $field = FieldConfig::load($field->id());
    $this->assertEquals('default:entity_test', $field->getSetting('handler'));
    // Change the target_type in the field storage, and check that the handler
    // was correctly reassigned in the field.
    $field_storage->setSetting('target_type', 'entity_test_rev');
    $field_storage->save();
    $field = FieldConfig::load($field->id());
    $this->assertEquals('default:entity_test_rev', $field->getSetting('handler'));
    // Change the handler to another, non-derivative plugin.
    $field->setSetting('handler', 'views');
    $field->save();
    $field = FieldConfig::load($field->id());
    $this->assertEquals('views', $field->getSetting('handler'));
    // Change the target_type in the field storage again, and check that the
    // non-derivative handler was unchanged.
    $field_storage->setSetting('target_type', 'entity_test_rev');
    $field_storage->save();
    $field = FieldConfig::load($field->id());
    $this->assertEquals('views', $field->getSetting('handler'));
    // Check that selection handlers aren't changed during sync.
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'entity_test',
        'settings' => [
            'handler' => 'fake:thing',
        ],
        'isSyncing' => TRUE,
    ]);
    $this->assertEquals('fake:thing', $field->getSetting('handler'));
}

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