You are here

public function ComplexWidgetTest::testEmptyField in Inline Entity Form 8

Tests if form behaves correctly when field is empty.

File

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

Class

ComplexWidgetTest
IEF complex field widget tests.

Namespace

Drupal\Tests\inline_entity_form\FunctionalJavascript

Code

public function testEmptyField() {

  // Get the xpath selectors for the input 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);

  // Get the xpath selectors for the fieldset labels in this test.
  $fieldset_label_all_bundles_xpath = $this
    ->getXpathForFieldsetLabel('All bundles', 1);
  $fieldset_label_multi_xpath = $this
    ->getXpathForFieldsetLabel('Multiple nodes', 1);
  $assert_session = $this
    ->assertSession();

  // Don't allow addition of existing nodes.
  $this
    ->updateSetting('allow_existing', FALSE);
  $this
    ->drupalGet($this->formContentAddUrl);

  // Assert fieldset title on inline form exists.
  $assert_session
    ->elementExists('xpath', $fieldset_label_all_bundles_xpath);
  $assert_session
    ->elementExists('xpath', $fieldset_label_multi_xpath);

  // Assert title field on inline form exists.
  $assert_session
    ->elementExists('xpath', $inner_title_field_xpath);

  // Assert first name field on inline form exists.
  $assert_session
    ->elementExists('xpath', $first_name_field_xpath);

  // Assert last name field on inline form exists.
  $assert_session
    ->elementExists('xpath', $last_name_field_xpath);
  $assert_session
    ->buttonExists('Create node');

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

  // Asserts 'Add new node' form elements.
  $this
    ->drupalGet($this->formContentAddUrl);
  $multi_fieldset = $assert_session
    ->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');

  // Assert fieldset titles.
  $assert_session
    ->elementExists('xpath', $fieldset_label_multi_xpath);

  // Assert title field does not appear.
  $assert_session
    ->elementNotExists('xpath', $inner_title_field_xpath);

  // Assert first name field does not appear.
  $assert_session
    ->elementNotExists('xpath', $first_name_field_xpath);

  // Assert last name field does not appear.
  $assert_session
    ->elementNotExists('xpath', $last_name_field_xpath);
  $assert_session
    ->buttonExists('Add existing node', $multi_fieldset);

  // Now submit 'Add new node' button in the 'Multiple nodes' fieldset.
  $multi_fieldset
    ->pressButton('Add new node');

  // Assert fieldset title.
  $assert_session
    ->elementExists('xpath', $fieldset_label_multi_xpath);

  // Assert title field on inline form exists.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $inner_title_field_xpath));

  // Assert first name field on inline form exists.
  $assert_session
    ->elementExists('xpath', $first_name_field_xpath);

  // Assert second name field on inline form exists.
  $assert_session
    ->elementExists('xpath', $last_name_field_xpath);
  $assert_session
    ->buttonExists('Create node');
  $assert_session
    ->buttonExists('Cancel');

  // Asserts 'Add existing node' form elements.
  $this
    ->drupalGet($this->formContentAddUrl);
  $multi_fieldset = $assert_session
    ->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
  $multi_fieldset
    ->pressButton('Add existing node');

  // Assert existing entity reference autocomplete field appears.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', $this
    ->getXpathForAutoCompleteInput()));
  $assert_session
    ->buttonExists('Add node');
  $assert_session
    ->buttonExists('Cancel');
}