You are here

public function SimpleWidgetTest::testSimpleValidation in Inline Entity Form 8

Test Validation on Simple Widget.

File

tests/src/FunctionalJavascript/SimpleWidgetTest.php, line 130

Class

SimpleWidgetTest
Tests the IEF simple widget.

Namespace

Drupal\Tests\inline_entity_form\FunctionalJavascript

Code

public function testSimpleValidation() {

  // Get the xpath selectors for the fields in this test.
  $title_field_xpath = $this
    ->getXpathForNthInputByLabelText('Title', 1);
  $nested_title_field_xpath = $this
    ->getXpathForNthInputByLabelText('Title', 2);
  $positive_int_field_xpath = $this
    ->getXpathForNthInputByLabelText('Positive int', 1);
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalLogin($this->user);
  $host_node_title = 'Host Validation Node';
  $this
    ->drupalGet('node/add/ief_simple_single');

  // Assert inline entity field widget title found.
  $assert_session
    ->pageTextContains('Single node');

  // Assert inline entity field description found.
  $assert_session
    ->pageTextContains('Reference a single node.');

  // Assert positive int field found.
  $assert_session
    ->pageTextContains('Positive int');
  $assert_session
    ->elementExists('xpath', $title_field_xpath)
    ->setValue($host_node_title);
  $page
    ->pressButton('Save');

  // Assert title validation fires on Inline Entity Form widget.
  $assert_session
    ->pageTextNotContains('IEF simple single Host Validation Node has been created.');

  // Assert that we're still on form due to to validation error.
  $this
    ->assertSession()
    ->addressEquals('node/add/ief_simple_single');
  $child_title = 'Child node ' . $this
    ->randomString();
  $assert_session
    ->elementExists('xpath', $nested_title_field_xpath)
    ->setValue($child_title);
  $assert_session
    ->elementExists('xpath', $positive_int_field_xpath)
    ->setValue(-1);
  $page
    ->pressButton('Save');

  // Assert field validation fires on Inline Entity Form widget.
  $assert_session
    ->pageTextNotContains('IEF simple single Host Validation Node has been created.');

  // Assert that we're still on form due to to validation error.
  $this
    ->assertSession()
    ->addressEquals('node/add/ief_simple_single');
  $assert_session
    ->elementExists('xpath', $positive_int_field_xpath)
    ->setValue(1);
  $page
    ->pressButton('Save');

  // Assert title validation passes on Inline Entity Form widget.
  $assert_session
    ->pageTextNotContains('Title field is required.');

  // Assert field validation fires on Inline Entity Form widget.
  $assert_session
    ->pageTextNotContains('Positive int must be higher than or equal to 1');
  $assert_session
    ->pageTextContains('IEF simple single Host Validation Node has been created.');

  // Check that nodes were created correctly.
  $host_node = $this
    ->getNodeByTitle($host_node_title);
  $this
    ->assertNotNull($host_node, 'Host node created.');
  if (isset($host_node)) {

    // Assert that address is the canonical page after node add.
    $this
      ->assertSession()
      ->addressEquals($host_node
      ->toUrl('canonical', [
      'absolute' => TRUE,
    ])
      ->toString());
    $child_node = $this
      ->getNodeByTitle($child_title);
    $this
      ->assertNotNull($child_node);
    if (isset($child_node)) {
      $this
        ->assertSame($host_node->single[0]->target_id, $child_node
        ->id(), 'Child node is referenced');
      $this
        ->assertSame($child_node->positive_int[0]->value, '1', 'Child node int field correct.');
      $this
        ->assertSame($child_node
        ->bundle(), 'ief_test_custom', 'Child node is correct bundle.');
    }
  }
}