You are here

public function ComplexWidgetRevisionsTest::testRevisionsAtDepth in Inline Entity Form 8

Tests saving entity reference revisions' field types at depth.

@dataProvider testRevisionsAtDepthDataProvider

File

tests/src/FunctionalJavascript/ComplexWidgetRevisionsTest.php, line 81

Class

ComplexWidgetRevisionsTest
IEF complex entity reference revisions tests.

Namespace

Drupal\Tests\inline_entity_form\FunctionalJavascript

Code

public function testRevisionsAtDepth(bool $inner_widget_adds_revisions) {
  $level_2_display_config = $this->entityFormDisplayStorage
    ->load('node.err_level_2.default');
  $component = $level_2_display_config
    ->getComponent('field_level_3_items');
  $component['settings']['revision'] = $inner_widget_adds_revisions;
  $level_2_display_config
    ->setComponent('field_level_3_items', $component);
  $level_2_display_config
    ->save();

  // 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);

  // Get the xpath selectors for the buttons in this test.
  $first_add_new_node_button_xpath = $this
    ->getXpathForButtonWithValue('Add new node', 1);
  $first_create_node_button_xpath = $this
    ->getXpathForButtonWithValue('Create node', 1);
  $first_edit_button_xpath = $this
    ->getXpathForButtonWithValue('Edit', 1);
  $first_update_button_xpath = $this
    ->getXpathForButtonWithValue('Update node', 1);
  $this
    ->drupalGet($this->formContentAddUrl);
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Open up level 2 and 3 IEF forms.
  $assert_session
    ->elementExists('xpath', $first_add_new_node_button_xpath)
    ->press();
  $assert_session
    ->waitForElementRemoved('xpath', $first_add_new_node_button_xpath);
  $assert_session
    ->elementExists('xpath', $first_add_new_node_button_xpath)
    ->press();

  // Fill in and save level 3 IEF form.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $double_nested_title_field_xpath));
  $assert_session
    ->elementExists('xpath', $double_nested_title_field_xpath)
    ->setValue('Level 3');
  $assert_session
    ->elementExists('xpath', $first_create_node_button_xpath)
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $first_edit_button_xpath));

  // Fill in and save level 2 IEF form.
  $assert_session
    ->elementExists('xpath', $nested_title_field_xpath)
    ->setValue('Level 2');
  $assert_session
    ->elementExists('xpath', $first_create_node_button_xpath)
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $first_edit_button_xpath));

  // Save the top level entity.
  $assert_session
    ->elementExists('xpath', $top_title_field_xpath)
    ->setValue('Level 1');
  $assert_session
    ->waitForElementRemoved('xpath', $first_edit_button_xpath);
  $page
    ->pressButton('Save');

  // Re-edit the created node to test for revisions.
  $node = $this
    ->drupalGetNodeByTitle('Level 1');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Open up level 2 and 3 IEF forms.
  $assert_session
    ->elementExists('xpath', $first_edit_button_xpath)
    ->press();
  $assert_session
    ->waitForElementRemoved('xpath', $first_edit_button_xpath);
  $assert_session
    ->elementExists('xpath', $first_edit_button_xpath)
    ->press();

  // Change level 3 IEF node title.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $double_nested_title_field_xpath));
  $assert_session
    ->elementExists('xpath', $double_nested_title_field_xpath)
    ->setValue('Level 3.1');
  $assert_session
    ->elementExists('xpath', $first_update_button_xpath)
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $first_edit_button_xpath));

  // Change level 2 IEF node title.
  $assert_session
    ->elementExists('xpath', $nested_title_field_xpath)
    ->setValue('Level 2.1');
  $assert_session
    ->elementExists('xpath', $first_update_button_xpath)
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $first_edit_button_xpath));

  // Save the top level entity.
  $assert_session
    ->elementExists('xpath', $top_title_field_xpath)
    ->setValue('Level 1.1');
  $assert_session
    ->waitForElementRemoved('xpath', $first_edit_button_xpath);
  $page
    ->pressButton('Save');

  // Assert that the entities are correctly saved.
  $assert_session
    ->pageTextContains('Level 1.1 has been updated.');
  $assert_session
    ->pageTextContains('Level 2.1');
  $assert_session
    ->pageTextContains('Level 3.1');

  // Load the current revision id of the Level 2 node.
  $node_level_2 = $this
    ->drupalGetNodeByTitle('Level 2.1');
  $node_level_2_vid = $node_level_2
    ->getLoadedRevisionId();

  // Load the current revision id of the Level 3 node.
  $node_level_3 = $this
    ->drupalGetNodeByTitle('Level 3.1');
  $node_level_3_vid = $node_level_3
    ->getLoadedRevisionId();

  // Re-edit the created node to test for revisions.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Open up level 2 and 3 IEF forms.
  $assert_session
    ->elementExists('xpath', $first_edit_button_xpath)
    ->press();
  $assert_session
    ->waitForElementRemoved('xpath', $first_edit_button_xpath);
  $assert_session
    ->elementExists('xpath', $first_edit_button_xpath)
    ->press();

  // Change level 3 IEF node title.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $double_nested_title_field_xpath));
  $assert_session
    ->elementExists('xpath', $double_nested_title_field_xpath)
    ->setValue('Level 3.2');
  $assert_session
    ->elementExists('xpath', $first_update_button_xpath)
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $first_edit_button_xpath));

  // Change level 2 IEF node title.
  $assert_session
    ->elementExists('xpath', $nested_title_field_xpath)
    ->setValue('Level 2.2');
  $assert_session
    ->elementExists('xpath', $first_update_button_xpath)
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $first_edit_button_xpath));

  // Save the top level entity.
  $assert_session
    ->elementExists('xpath', $top_title_field_xpath)
    ->setValue('Level 1.2');
  $assert_session
    ->waitForElementRemoved('xpath', $first_edit_button_xpath);
  $page
    ->pressButton('Save');

  // Assert that the entities are correctly saved.
  $assert_session
    ->pageTextContains('Level 1.2 has been updated.');
  $assert_session
    ->pageTextContains('Level 2.2');
  $assert_session
    ->pageTextContains('Level 3.2');

  // Clear node cache.
  $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->resetCache();

  // Load the current revision id of the Level 2 node.
  $node_level_2 = $this
    ->drupalGetNodeByTitle('Level 2.2');
  $node_level_2_vid_new = $node_level_2
    ->getLoadedRevisionId();

  // Assert that a new revision created.
  $this
    ->assertNotEquals($node_level_2_vid, $node_level_2_vid_new);

  // Load the current revision id of the Level 3 node.
  $node_level_3 = $this
    ->drupalGetNodeByTitle('Level 3.2');
  $node_level_3_vid_new = $node_level_3
    ->getLoadedRevisionId();

  // Assert that (no) new revision created.
  if ($inner_widget_adds_revisions) {
    $this
      ->assertNotEquals($node_level_3_vid, $node_level_3_vid_new);
  }
  else {
    $this
      ->assertEquals($node_level_3_vid, $node_level_3_vid_new);
  }
}