You are here

public function ComplexWidgetTest::testEntityEditingAndRemoving in Inline Entity Form 8

Tests if editing and removing entities work.

File

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

Class

ComplexWidgetTest
IEF complex field widget tests.

Namespace

Drupal\Tests\inline_entity_form\FunctionalJavascript

Code

public function testEntityEditingAndRemoving() {

  // Get the xpath selectors for the fields in this test.
  $inner_title_field_xpath = $this
    ->getXpathForNthInputByLabelText('Title', 2);
  $first_name_field_xpath = $this
    ->getXpathForNthInputByLabelText('First name', 1);
  $last_name_field_xpath = $this
    ->getXpathForNthInputByLabelText('Last name', 1);
  $first_delete_checkbox_xpath = $this
    ->getXpathForNthInputByLabelText('Delete this node from the system.', 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();
  $this
    ->drupalCreateNode([
    'type' => 'ief_test_complex',
    'title' => 'Some title',
    'multi' => array_values($referenceNodes),
  ]);
  $parent_node = $this
    ->drupalGetNodeByTitle('Some title');

  // Edit the second entity.
  $this
    ->drupalGet('node/' . $parent_node
    ->id() . '/edit');
  $assert_session
    ->elementExists('xpath', '(//input[@value="Edit"])[2]')
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $inner_title_field_xpath));
  $assert_session
    ->elementExists('xpath', $first_name_field_xpath)
    ->setValue('John');
  $assert_session
    ->elementExists('xpath', $last_name_field_xpath)
    ->setValue('Doe');
  $page
    ->pressButton('Update node');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementRemoved('css', 'div[data-drupal-selector="edit-multi-form-inline-entity-form-entities-1-form"]'));
  $this
    ->waitForRowByTitle('Some reference 2');

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

  // Checks values of changed entities.
  $node = $this
    ->drupalGetNodeByTitle('Some reference 2', TRUE);
  $this
    ->assertSame('John', $node->first_name->value, 'First name in reference node changed to John');
  $this
    ->assertSame('Doe', $node->last_name->value, 'Last name in reference node changed to Doe');

  // Delete the second entity.
  $this
    ->drupalGet('node/' . $parent_node
    ->id() . '/edit');
  $assert_session
    ->elementsCount('css', 'tr.ief-row-entity', 3);
  $assert_session
    ->elementExists('xpath', '(//input[@value="Remove"])[2]')
    ->press();
  $this
    ->assertNotEmpty($confirm_checkbox = $assert_session
    ->waitForElement('xpath', $first_delete_checkbox_xpath));
  $assert_session
    ->pageTextContains('Are you sure you want to remove Some reference 2?');
  $confirm_checkbox
    ->check();
  $assert_session
    ->elementExists('xpath', '(//input[@value="Remove"])[2]')
    ->press();
  $this
    ->waitForRowRemovedByTitle('Some reference 2');

  // Assert two rows show, instead of 3.
  $assert_session
    ->elementsCount('css', 'tr.ief-row-entity', 2);

  // Save the ief_test_complex node.
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('IEF test complex Some title has been updated.');
  $deleted_node = $this
    ->drupalGetNodeByTitle('Some reference 2');
  $this
    ->assertEmpty($deleted_node, 'The inline entity was deleted from the site.');

  // Checks that entity does nor appear in IEF.
  $this
    ->drupalGet('node/' . $parent_node
    ->id() . '/edit');

  // Assert 2 rows show, instead of 3.
  $assert_session
    ->elementsCount('css', 'tr.ief-row-entity', 2);
  $this
    ->assertRowByTitle('Some reference 1');
  $this
    ->assertNoRowByTitle('Some reference 2');
  $this
    ->assertRowByTitle('Some reference 3');

  // Delete the third entity reference only, don't delete the node. The third
  // entity now is second referenced entity because the second one was deleted
  // in previous step.
  $this
    ->drupalGet('node/' . $parent_node
    ->id() . '/edit');
  $assert_session
    ->elementsCount('css', 'tr.ief-row-entity', 2);
  $assert_session
    ->elementExists('xpath', '(//input[@value="Remove"])[2]')
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $first_delete_checkbox_xpath));
  $assert_session
    ->pageTextContains('Are you sure you want to remove Some reference 3?');
  $assert_session
    ->elementExists('xpath', '(//input[@value="Remove"])[2]')
    ->press();
  $this
    ->waitForRowRemovedByTitle('Some reference 3');

  // Assert only one row displays.
  $assert_session
    ->elementsCount('css', 'tr.ief-row-entity', 1);
  $this
    ->assertRowByTitle('Some reference 1');
  $this
    ->assertNoRowByTitle('Some reference 2');
  $this
    ->assertNoRowByTitle('Some reference 3');

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

  // Checks that entity is not deleted.
  $node = $this
    ->drupalGetNodeByTitle('Some reference 3');
  $this
    ->assertNotEmpty($node, 'Reference node not deleted');
}