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/FunctionalJavascript/DynamicEntityReferenceTest.php \Drupal\Tests\dynamic_entity_reference\FunctionalJavascript\DynamicEntityReferenceTest::testFieldSettings()

Tests field settings of dynamic entity reference field.

File

tests/src/FunctionalJavascript/DynamicEntityReferenceTest.php, line 93

Class

DynamicEntityReferenceTest
Ensures that Dynamic Entity References field works correctly.

Namespace

Drupal\Tests\dynamic_entity_reference\FunctionalJavascript

Code

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

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

  // We will query on the first two characters of the second username.
  $autocomplete_query = mb_substr($this->anotherUser
    ->label(), 0, 3);
  $this->testEntity = EntityTest::create([
    // Make this partially match the second user name.
    'name' => $autocomplete_query . $this
      ->randomMachineName(5),
    'type' => 'entity_test',
  ]);
  $this->testEntity
    ->save();
  $this
    ->drupalLogin($this->adminUser);

  // Add a new dynamic entity reference field.
  $this
    ->drupalGet('entity_test/structure/entity_test/fields/add-field');
  $select = $assert_session
    ->selectExists('new_storage_type');
  $select
    ->selectOption('dynamic_entity_reference');
  $label = $assert_session
    ->fieldExists('label');
  $label
    ->setValue('Foobar');

  // Wait for the machine name.
  $assert_session
    ->waitForElementVisible('css', '[name="label"] + * .machine-name-value');
  $this
    ->submitForm([], t('Save and continue'), 'field-ui-field-storage-add-form');
  $page = $this
    ->getSession()
    ->getPage();
  $entity_type_ids_select = $assert_session
    ->selectExists('settings[entity_type_ids][]', $page);
  $entity_type_ids_select
    ->selectOption('user');
  $entity_type_ids_select
    ->selectOption('entity_test', TRUE);
  $assert_session
    ->selectExists('cardinality', $page)
    ->selectOption(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $page
    ->uncheckField('settings[exclude_entity_types]');
  $this
    ->submitForm([], t('Save field settings'), 'field-storage-config-edit-form');
  $page = $this
    ->getSession()
    ->getPage();
  $autocomplete_field = $page
    ->findField('default_value_input[field_foobar][0][target_id]');
  $autocomplete_field_1 = $page
    ->findField('default_value_input[field_foobar][1][target_id]');
  $target_type_select = $assert_session
    ->selectExists('default_value_input[field_foobar][0][target_type]');
  $this
    ->assertSame($autocomplete_field
    ->getAttribute('data-autocomplete-path'), $this
    ->createAutoCompletePath('entity_test'));
  $this
    ->assertSame($autocomplete_field_1
    ->getAttribute('data-autocomplete-path'), $this
    ->createAutoCompletePath('entity_test'));
  $target_type_select
    ->selectOption('user');

  // Changing the selected value changes the autocomplete path for the
  // corresponding autocomplete field.
  $this
    ->assertSame($autocomplete_field
    ->getAttribute('data-autocomplete-path'), $this
    ->createAutoCompletePath('user'));

  // Changing the selected value of delta 0 doesn't change the autocomplete
  // path for delta 1 autocomplete field.
  $this
    ->assertSame($autocomplete_field_1
    ->getAttribute('data-autocomplete-path'), $this
    ->createAutoCompletePath('entity_test'));
  $target_type_select
    ->selectOption('entity_test');

  // Changing the selected value changes the autocomplete path for the
  // corresponding autocomplete field.
  $this
    ->assertSame($autocomplete_field
    ->getAttribute('data-autocomplete-path'), $this
    ->createAutoCompletePath('entity_test'));

  // Changing the selected value of delta 0 doesn't change the autocomplete
  // path for delta 1 autocomplete field.
  $this
    ->assertSame($autocomplete_field_1
    ->getAttribute('data-autocomplete-path'), $this
    ->createAutoCompletePath('entity_test'));
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->checkField('settings[entity_test][handler_settings][target_bundles][entity_test]');
  $assert_session
    ->assertWaitOnAjaxRequest(20000);
  $page
    ->checkField('settings[entity_test][handler_settings][auto_create]');
  $this
    ->submitForm([], t('Save settings'), 'field-config-edit-form');
  $assert_session
    ->pageTextContains('Saved Foobar configuration');
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $field_storage = FieldStorageConfig::loadByName('entity_test', 'field_foobar');
  $this
    ->assertEmpty($field_storage
    ->getSetting('exclude_entity_types'));
  $this
    ->assertEquals($field_storage
    ->getSetting('entity_type_ids'), [
    'entity_test' => 'entity_test',
    'user' => 'user',
  ]);
  $field_config = FieldConfig::loadByName('entity_test', 'entity_test', 'field_foobar');
  $settings = $field_config
    ->getSettings();
  $this
    ->assertEquals($settings['entity_test']['handler'], 'default:entity_test');
  $this
    ->assertNotEmpty($settings['entity_test']['handler_settings']);
  $this
    ->assertEquals($settings['entity_test']['handler_settings']['target_bundles'], [
    'entity_test' => 'entity_test',
  ]);
  $this
    ->assertTrue($settings['entity_test']['handler_settings']['auto_create']);
  $this
    ->assertEmpty($settings['entity_test']['handler_settings']['auto_create_bundle']);
  $this
    ->drupalGet('entity_test/add');
  $autocomplete_field = $page
    ->findField('field_foobar[0][target_id]');
  $entity_type_field = $page
    ->findField('field_foobar[0][target_type]');

  // Change to user.
  $entity_type_field
    ->selectOption('user');
  $this
    ->performAutocompleteQuery($autocomplete_query, $autocomplete_field);
  $this
    ->selectAutocompleteOption();
  $assert_session
    ->pageTextContains($this->anotherUser
    ->label());

  // Change to entity_test, this should automatically clear the autocomplete
  // field.
  $entity_type_field
    ->selectOption('entity_test');
  $this
    ->assertEmpty($autocomplete_field
    ->getValue());
  $this
    ->performAutocompleteQuery($autocomplete_query, $autocomplete_field);
  $this
    ->selectAutocompleteOption();
  $assert_session
    ->pageTextContains($this->testEntity
    ->label());
}