You are here

public function ComplexWidgetTest::testNestedCreateAndEditWontClash in Inline Entity Form 8

Tests that create and edit of nested data won#t clash.

When creating, then editing a nested IEF, the internal widget state must use the same IEF ID on create and edit. Otherwise on saving, the entity will be saved twice, and cause a WSOD.

@dataProvider simpleFalseTrueDataProvider

File

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

Class

ComplexWidgetTest
IEF complex field widget tests.

Namespace

Drupal\Tests\inline_entity_form\FunctionalJavascript

Code

public function testNestedCreateAndEditWontClash(bool $required) {

  // Get the xpath selectors for the input fields in this test.
  $top_title_field_xpath = $this
    ->getXpathForNthInputByLabelText('Title', 1);
  $nested_title_field_xpath = $this
    ->getXpathForNthInputByLabelText('Title', 2);
  $double_nested_title_field_xpath = $this
    ->getXpathForNthInputByLabelText('Title', 3);
  $second_edit_button_xpath = $this
    ->getXpathForButtonWithValue('Edit', 2);
  $assert_session = $this
    ->assertSession();
  $this
    ->setupNestedComplexForm($required);
  $required_string = $required ? ' required' : ' unrequired';
  $title_1 = 'Rationality' . $required_string;
  $title_1_2 = 'Drain' . $required_string;
  $title_1_2_3 = 'Drain within a drain' . $required_string;
  $title_i_2_3a = "Drone within a drain" . $required_string;
  $title_1_2a = 'Drone' . $required_string;
  $title_1_2a_3 = 'Drain within a drone' . $required_string;
  $title_i_2a_3a = "Drone within a drain" . $required_string;
  $this
    ->elementWithXpath($top_title_field_xpath)
    ->setValue($title_1);
  $this
    ->elementWithXpath($nested_title_field_xpath)
    ->setValue($title_1_2);
  $this
    ->elementWithXpath($double_nested_title_field_xpath)
    ->setValue($title_1_2_3);

  // Close all subforms.
  $this
    ->elementWithXpath($this
    ->buttonXpath('Create node 3'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Add new node 3'));
  $this
    ->elementWithXpath($this
    ->buttonXpath('Create node 2'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Add new node 2'));

  // Re-open all subforms and add a second node 3.
  $this
    ->elementWithXpath($this
    ->buttonXpath('Edit'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Update node 2'));
  $this
    ->elementWithXpath($this
    ->buttonXpath('Add new node 3'))
    ->press();
  $this
    ->assertNotNull($assert_session
    ->waitForButton('Create node 3'));
  $assert_session
    ->elementExists('xpath', $double_nested_title_field_xpath)
    ->setValue($title_i_2_3a);
  $this
    ->elementWithXpath($this
    ->buttonXpath('Create node 3'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Add new node 3'));
  $this
    ->elementWithXpath($this
    ->buttonXpath('Update node 2'))
    ->press();
  $this
    ->waitForXpathRemoved($this
    ->buttonXpath('Update node 2'));

  // Repeat. Add node 2a and 2a_3.
  $this
    ->elementWithXpath($this
    ->buttonXpath('Add new node 2'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Create node 2'));
  if (!$required) {
    $this
      ->elementWithXpath($this
      ->buttonXpath('Add new node 3'))
      ->press();
    $this
      ->waitForXpath($this
      ->buttonXpath('Create node 3'));
  }
  $assert_session
    ->elementExists('xpath', $nested_title_field_xpath)
    ->setValue($title_1_2a);
  $assert_session
    ->elementExists('xpath', $double_nested_title_field_xpath)
    ->setValue($title_1_2a_3);

  // Close all subforms.
  $this
    ->elementWithXpath($this
    ->buttonXpath('Create node 3'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Add new node 3'));
  $this
    ->elementWithXpath($this
    ->buttonXpath('Create node 2'))
    ->press();
  $this
    ->waitForXpathRemoved($this
    ->buttonXpath('Create node 2'));

  // Re-open all subforms and add a second node 2a_3a.
  $this
    ->waitForXpath($second_edit_button_xpath)
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Update node 2'));
  $this
    ->elementWithXpath($this
    ->buttonXpath('Add new node 3'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Create node 3'));
  $assert_session
    ->elementExists('xpath', $double_nested_title_field_xpath)
    ->setValue($title_i_2a_3a);
  $this
    ->elementWithXpath($this
    ->buttonXpath('Create node 3'))
    ->press();
  $this
    ->waitForXpath($this
    ->buttonXpath('Add new node 3'));

  // Save everything and assert message.
  $this
    ->elementWithXpath($this
    ->buttonXpath('Save'))
    ->press();
  $this
    ->htmlOutput();
  $assert_session
    ->pageTextContains("IEF test nested 1 {$title_1} has been created.");
}