You are here

public function EntityReferenceItemTest::testSelectionHandlerSettings in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php \Drupal\field\Tests\EntityReference\EntityReferenceItemTest::testSelectionHandlerSettings()

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

File

core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php, line 306
Contains \Drupal\field\Tests\EntityReference\EntityReferenceItemTest.

Class

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

Namespace

Drupal\field\Tests\EntityReference

Code

public function testSelectionHandlerSettings() {
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $field_storage = FieldStorageConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'entity_reference',
    'settings' => array(
      '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(array(
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
  ));
  $field
    ->save();
  $field = FieldConfig::load($field
    ->id());
  $this
    ->assertEqual($field
    ->getSetting('handler'), 'default:entity_test');

  // 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
    ->assertEqual($field
    ->getSetting('handler'), 'default:entity_test_rev');

  // Change the handler to another, non-derivative plugin.
  $field
    ->setSetting('handler', 'views');
  $field
    ->save();
  $field = FieldConfig::load($field
    ->id());
  $this
    ->assertEqual($field
    ->getSetting('handler'), 'views');

  // 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
    ->assertEqual($field
    ->getSetting('handler'), 'views');
}