You are here

public function ComplexWidgetTest::testReferencingExistingEntities in Inline Entity Form 8

Tests if referencing existing entities work.

File

tests/src/FunctionalJavascript/ComplexWidgetTest.php, line 416

Class

ComplexWidgetTest
IEF complex field widget tests.

Namespace

Drupal\Tests\inline_entity_form\FunctionalJavascript

Code

public function testReferencingExistingEntities() {

  // Get the xpath selectors for the input fields in this test.
  $node_field_xpath = $this
    ->getXpathForNthInputByLabelText('Node', 1);
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();

  // Allow addition of existing nodes.
  $this
    ->updateSetting('allow_existing', TRUE);

  // Create three ief_reference_type entities.
  $referenceNodes = $this
    ->createReferenceContent();

  // Create a node for every bundle available.
  $bundle_nodes = $this
    ->createNodeForEveryBundle();

  // Create ief_test_complex node with first ief_reference_type node and first
  // node from bundle nodes.
  $this
    ->drupalCreateNode([
    'type' => 'ief_test_complex',
    'title' => 'Some title',
    'multi' => [
      1,
    ],
    'all_bundles' => key($bundle_nodes),
  ]);

  // Remove first node since we already added it.
  unset($bundle_nodes[key($bundle_nodes)]);
  $parent_node = $this
    ->drupalGetNodeByTitle('Some title', TRUE);

  // Add remaining existing reference nodes.
  $this
    ->drupalGet('node/' . $parent_node
    ->id() . '/edit');
  for ($i = 2; $i <= 3; $i++) {
    $this
      ->openMultiExistingForm();
    $title = 'Some reference ' . $i;
    $assert_session
      ->elementExists('xpath', $node_field_xpath)
      ->setValue($title . ' (' . $referenceNodes[$title] . ')');
    $page
      ->pressButton('Add node');
    $this
      ->waitForRowByTitle($title);
  }

  // Add all remaining nodes from all bundles.
  foreach ($bundle_nodes as $id => $title) {
    $all_bundles_fieldset = $assert_session
      ->elementExists('css', 'fieldset[data-drupal-selector="edit-all-bundles"]');
    $assert_session
      ->buttonExists('Add existing node', $all_bundles_fieldset)
      ->press();
    $this
      ->assertNotEmpty($assert_session
      ->waitForElement('xpath', $node_field_xpath));
    $assert_session
      ->elementExists('xpath', $node_field_xpath)
      ->setValue($title . ' (' . $id . ')');
    $page
      ->pressButton('Add node');
    $this
      ->waitForRowByTitle($title);
  }

  // Save the node.
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('IEF test complex Some title has been updated.');

  // Check if entities are referenced.
  $this
    ->drupalGet('node/' . $parent_node
    ->id() . '/edit');
  $assert_session
    ->elementsCount('css', 'fieldset[data-drupal-selector="edit-multi"] tr.ief-row-entity', 3);
  $this
    ->assertRowByTitle('Some reference 1');
  $this
    ->assertRowByTitle('Some reference 2');
  $this
    ->assertRowByTitle('Some reference 3');

  // Check if all remaining nodes from all bundles are referenced.
  $assert_session
    ->elementsCount('css', 'fieldset[data-drupal-selector="edit-all-bundles"] tr.ief-row-entity', 12);
  foreach ($bundle_nodes as $title) {
    $this
      ->assertRowByTitle($title);
  }
}