You are here

public function EntityReferenceItemTest::testSelectionHandlerSettings in Drupal 9

Same name and namespace in other branches
  1. 8 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 364

Class

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

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testSelectionHandlerSettings() {
  $field_name = mb_strtolower($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'));
}