You are here

public function DynamicEntityReferenceTest::testFieldSettings in Dynamic Entity Reference 8

Same name in this branch
  1. 8 tests/src/Functional/DynamicEntityReferenceTest.php \Drupal\Tests\dynamic_entity_reference\Functional\DynamicEntityReferenceTest::testFieldSettings()
  2. 8 tests/src/FunctionalJavascript/DynamicEntityReferenceTest.php \Drupal\Tests\dynamic_entity_reference\FunctionalJavascript\DynamicEntityReferenceTest::testFieldSettings()
Same name and namespace in other branches
  1. 8.2 tests/src/Functional/DynamicEntityReferenceTest.php \Drupal\Tests\dynamic_entity_reference\Functional\DynamicEntityReferenceTest::testFieldSettings()

Tests field settings of dynamic entity reference field.

File

tests/src/Functional/DynamicEntityReferenceTest.php, line 81

Class

DynamicEntityReferenceTest
Ensures that Dynamic Entity References field works correctly.

Namespace

Drupal\Tests\dynamic_entity_reference\Functional

Code

public function testFieldSettings() {
  $assert_session = $this
    ->assertSession();

  // Add EntityTestBundle for EntityTestWithBundle.
  EntityTestBundle::create([
    'id' => 'test',
    'label' => 'Test label',
    'description' => 'My test description',
  ])
    ->save();
  $this
    ->drupalLogin($this->adminUser);

  // Add a new dynamic entity reference field.
  $this
    ->drupalGet('entity_test/structure/entity_test/fields/add-field');
  $edit = [
    'label' => 'Foobar',
    'field_name' => 'foobar',
    'new_storage_type' => 'dynamic_entity_reference',
  ];
  $this
    ->submitForm($edit, t('Save and continue'));
  $assert_session
    ->optionNotExists('settings[entity_type_ids][]', 'settings[entity_test_no_id][handler_settings][target_bundles][entity_test_no_id]');
  $assert_session
    ->optionNotExists('settings[entity_type_ids][]', 'settings[entity_test_no_id][handler_settings][target_bundles][entity_test_string_id]');
  $this
    ->submitForm([
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings[entity_type_ids][]' => [
      'user',
      'entity_test_label',
    ],
  ], t('Save field settings'));
  $assert_session
    ->fieldExists('default_value_input[field_foobar][0][target_type]');
  $assert_session
    ->optionExists('default_value_input[field_foobar][0][target_type]', 'entity_test');
  $assert_session
    ->optionNotExists('default_value_input[field_foobar][0][target_type]', 'user');
  $labels = $this->container
    ->get('entity_type.repository')
    ->getEntityTypeLabels(TRUE);
  $edit = [];
  $excluded_entity_type_ids = [
    'user',
    'file',
    'path_alias',
    'entity_test_label',
    'entity_test_no_id',
    'entity_test_no_bundle',
    'entity_test_string_id',
    'entity_test_computed_field',
    'entity_test_map_field',
    'entity_test_no_bundle_with_label',
  ];
  foreach ($labels[(string) t('Content', [], [
    'context' => 'Entity type group',
  ])] as $entity_type_id => $entity_type_label) {
    if (!in_array($entity_type_id, $excluded_entity_type_ids)) {
      if ($entity_type_id !== 'entity_test_with_bundle') {
        $edit["settings[{$entity_type_id}][handler_settings][target_bundles][{$entity_type_id}]"] = TRUE;
      }
      else {
        $edit['settings[entity_test_with_bundle][handler_settings][target_bundles][test]'] = TRUE;
      }
    }
  }
  $this
    ->submitForm($edit, t('Save settings'));
  $assert_session
    ->responseContains(t('Saved %name configuration', [
    '%name' => 'Foobar',
  ]));
  $excluded_entity_type_ids = FieldStorageConfig::loadByName('entity_test', 'field_foobar')
    ->getSetting('entity_type_ids');
  $this
    ->assertNotNull($excluded_entity_type_ids);
  $this
    ->assertSame(array_keys($excluded_entity_type_ids), [
    'user',
    'entity_test_label',
  ]);

  // Check the include entity settings.
  $this
    ->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar/storage');
  $this
    ->submitForm([
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings[exclude_entity_types]' => FALSE,
    'settings[entity_type_ids][]' => [],
  ], t('Save field settings'));
  $assert_session
    ->pageTextContains('Select at least one entity type ID.');
  $options = array_filter(array_keys($labels[(string) t('Content', [], [
    'context' => 'Entity type group',
  ])]), function ($entity_type_id) {
    return DynamicEntityReferenceItem::entityHasIntegerId($entity_type_id);
  });
  unset($options['entity_test_no_id']);
  $this
    ->submitForm([
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings[exclude_entity_types]' => TRUE,
    'settings[entity_type_ids][]' => $options,
  ], t('Save field settings'));
  $assert_session
    ->pageTextContains('Select at least one entity type ID.');
  $this
    ->submitForm([
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings[exclude_entity_types]' => FALSE,
    'settings[entity_type_ids][]' => [
      'user',
      'entity_test_label',
    ],
  ], t('Save field settings'));
  $this
    ->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar');
  $assert_session
    ->fieldExists('default_value_input[field_foobar][0][target_type]');
  $assert_session
    ->optionNotExists('default_value_input[field_foobar][0][target_type]', 'entity_test');
  $assert_session
    ->optionExists('default_value_input[field_foobar][0][target_type]', 'user');
  $edit = [
    'settings[entity_test_label][handler_settings][target_bundles][entity_test_label]' => 'entity_test_label',
  ];
  $this
    ->submitForm($edit, t('Save settings'));
  $assert_session
    ->responseContains(t('Saved %name configuration', [
    '%name' => 'Foobar',
  ]));
  $excluded_entity_type_ids = FieldStorageConfig::loadByName('entity_test', 'field_foobar')
    ->getSetting('entity_type_ids');
  $this
    ->assertNotNull($excluded_entity_type_ids);
  $this
    ->assertSame(array_keys($excluded_entity_type_ids), [
    'user',
    'entity_test_label',
  ]);

  // Check the default settings.
  $this
    ->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.field_foobar');
  $this
    ->submitForm([
    'default_value_input[field_foobar][0][target_type]' => 'user',
    'default_value_input[field_foobar][0][target_id]' => $this->adminUser
      ->label() . ' (' . $this->adminUser
      ->id() . ')',
  ], t('Save settings'));
  $field_config = FieldConfig::loadByName('entity_test', 'entity_test', 'field_foobar')
    ->toArray();
  $this
    ->assertEquals($field_config['default_value']['0'], [
    'target_type' => 'user',
    'target_uuid' => $this->adminUser
      ->uuid(),
  ]);
}