You are here

public function ComplexWidgetTest::testEntityCreation in Inline Entity Form 8

Tests creation of entities.

File

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

Class

ComplexWidgetTest
IEF complex field widget tests.

Namespace

Drupal\Tests\inline_entity_form\FunctionalJavascript

Code

public function testEntityCreation() {

  // Get the xpath selectors for the input fields in this test.
  $first_title_field_xpath = $this
    ->getXpathForNthInputByLabelText('Title', 1);
  $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);
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();

  // Allow addition of existing nodes.
  $this
    ->updateSetting('allow_existing', TRUE);
  $this
    ->drupalGet($this->formContentAddUrl);
  $multi_fieldset = $assert_session
    ->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
  $multi_fieldset
    ->pressButton('Add new node');
  $this
    ->assertNotEmpty($create_node_button = $assert_session
    ->waitForButton('Create node'));
  $create_node_button
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('css', 'div[data-drupal-messages]'));
  $assert_session
    ->pageTextContains('First name field is required.');
  $assert_session
    ->pageTextContains('Last name field is required.');
  $assert_session
    ->pageTextContains('Title field is required.');

  // Create ief_reference_type node in IEF.
  $this
    ->drupalGet($this->formContentAddUrl);
  $multi_fieldset = $assert_session
    ->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
  $multi_fieldset
    ->pressButton('Add new node');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $inner_title_field_xpath));
  $assert_session
    ->elementExists('xpath', $inner_title_field_xpath)
    ->setValue('Some reference');
  $assert_session
    ->elementExists('xpath', $first_name_field_xpath)
    ->setValue('John');
  $assert_session
    ->elementExists('xpath', $last_name_field_xpath)
    ->setValue('Doe');
  $page
    ->pressButton('Create node');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('css', '.ief-row-entity'));

  // Tests if correct fields appear in the table.
  $assert_session
    ->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-label', 'Some reference');
  $assert_session
    ->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-status', 'Published');

  // Tests if edit and remove buttons appear.
  $multi_fieldset = $assert_session
    ->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
  $assert_session
    ->buttonExists('Edit', $multi_fieldset);
  $assert_session
    ->buttonExists('Remove', $multi_fieldset);

  // Test edit functionality.
  $assert_session
    ->buttonExists('Edit', $multi_fieldset)
    ->press();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $inner_title_field_xpath));
  $assert_session
    ->elementExists('xpath', $inner_title_field_xpath)
    ->setValue('Some changed reference');
  $page
    ->pressButton('Update node');
  $this
    ->waitForRowByTitle('Some changed reference');

  // Tests if correct fields appear in the table.
  $assert_session
    ->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-label', 'Some changed reference');
  $assert_session
    ->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-status', 'Published');

  // Tests if edit and remove buttons appear.
  $multi_fieldset = $assert_session
    ->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
  $assert_session
    ->buttonExists('Edit', $multi_fieldset);
  $assert_session
    ->buttonExists('Remove', $multi_fieldset);

  // Make sure unrelated AJAX submit doesn't save the referenced entity.
  // @todo restore this test.
  // @see https://www.drupal.org/project/inline_entity_form/issues/3088453
  $assert_session
    ->elementExists('xpath', $first_title_field_xpath)
    ->setValue('Some title');
  $page
    ->pressButton('Save');
  $assert_session
    ->pageTextContains('IEF test complex Some title has been created.');

  // Checks values of created entities.
  $node = $this
    ->drupalGetNodeByTitle('Some changed reference');
  $this
    ->assertNotEmpty($node, 'Created ief_reference_type node ' . $node
    ->label());
  $this
    ->assertSame('John', $node
    ->get('first_name')->value, 'First name in reference node set to John');
  $this
    ->assertSame('Doe', $node
    ->get('last_name')->value, 'Last name in reference node set to Doe');
  $parent_node = $this
    ->drupalGetNodeByTitle('Some title');
  $this
    ->assertNotEmpty($parent_node, 'Created ief_test_complex node ' . $parent_node
    ->label());
  $this
    ->assertSame($node
    ->id(), $parent_node->multi->target_id, 'Reference node id set to ' . $node
    ->id());
}