You are here

public function DynamicEntityReferenceWidgetTest::testEntityReferenceOptionsSelectWidget in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/DynamicEntityReferenceWidgetTest.php \Drupal\Tests\dynamic_entity_reference\Functional\DynamicEntityReferenceWidgetTest::testEntityReferenceOptionsSelectWidget()

Tests option select widget.

File

tests/src/Functional/DynamicEntityReferenceWidgetTest.php, line 177

Class

DynamicEntityReferenceWidgetTest
Tests dynamic entity reference field widgets.

Namespace

Drupal\Tests\dynamic_entity_reference\Functional

Code

public function testEntityReferenceOptionsSelectWidget() {
  $assert_session = $this
    ->assertSession();
  $field_name = $this->fieldName;
  EntityFormDisplay::load('node.reference_content.default')
    ->setComponent($field_name, [
    'type' => 'dynamic_entity_reference_options_select',
  ])
    ->save();
  $this
    ->drupalLogin($this->adminUser);

  // Create a node to be referenced.
  $referenced_node = $this
    ->drupalCreateNode([
    'type' => 'referenced_content',
  ]);
  $title = $this
    ->randomMachineName();
  $edit = [
    'title[0][value]' => $title,
    $field_name => $referenced_node
      ->getEntityTypeId() . '-' . $referenced_node
      ->id(),
  ];
  $this
    ->drupalGet(Url::fromRoute('node.add', [
    'node_type' => 'reference_content',
  ]));

  // Only one bundle is configuerd, so optgroup should not be added to
  // the select element.
  $assert_session
    ->elementNotContains('css', '[name=' . $field_name . ']', 'optgroup');
  $this
    ->submitForm($edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($title);
  $assert_session
    ->responseContains(t('@type %title has been created.', [
    '@type' => 'reference_content',
    '%title' => $node
      ->toLink($node
      ->label())
      ->toString(),
  ]));
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => $title,
  ]);
  $reference_node = reset($nodes);
  $this
    ->assertEquals($reference_node
    ->get($field_name)
    ->offsetGet(0)->target_type, $referenced_node
    ->getEntityTypeId());
  $this
    ->assertEquals($reference_node
    ->get($field_name)
    ->offsetGet(0)->target_id, $referenced_node
    ->id());
  $field_config = FieldConfig::loadByName('node', 'reference_content', $this->fieldName);
  $node_setting = $field_config
    ->getSetting('node');
  $field_config
    ->setSetting('node', [
    'handler' => 'default',
    'handler_settings' => [
      'target_bundles' => [
        'referenced_content',
        'reference_content',
      ],
      'sort' => [
        'field' => '_none',
      ],
    ],
  ]);
  $field_config
    ->save();
  $this
    ->drupalGet(Url::fromRoute('node.add', [
    'node_type' => 'reference_content',
  ]));

  // Multiple target_bundles configured, optgroup should be added to the
  // select element.
  $assert_session
    ->elementContains('css', '[name=' . $field_name . ']', 'optgroup');
  $field_config
    ->setSetting('node', $node_setting);
}