You are here

function RebuildTest::testPreserveFormActionAfterAJAX in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Form/RebuildTest.php \Drupal\system\Tests\Form\RebuildTest::testPreserveFormActionAfterAJAX()

Tests that a form's action is retained after an Ajax submission.

The 'action' attribute of a form should not change after an Ajax submission followed by a non-Ajax submission, which triggers a validation error.

File

core/modules/system/src/Tests/Form/RebuildTest.php, line 73
Contains \Drupal\system\Tests\Form\RebuildTest.

Class

RebuildTest
Tests functionality of \Drupal\Core\Form\FormBuilderInterface::rebuildForm().

Namespace

Drupal\system\Tests\Form

Code

function testPreserveFormActionAfterAJAX() {

  // Create a multi-valued field for 'page' nodes to use for Ajax testing.
  $field_name = 'field_ajax_test';
  entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'text',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ))
    ->save();
  entity_create('field_config', array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'bundle' => 'page',
  ))
    ->save();
  entity_get_form_display('node', 'page', 'default')
    ->setComponent($field_name, array(
    'type' => 'text_textfield',
  ))
    ->save();

  // Log in a user who can create 'page' nodes.
  $this->webUser = $this
    ->drupalCreateUser(array(
    'create page content',
  ));
  $this
    ->drupalLogin($this->webUser);

  // Get the form for adding a 'page' node. Submit an "add another item" Ajax
  // submission and verify it worked by ensuring the updated page has two text
  // field items in the field for which we just added an item.
  $this
    ->drupalGet('node/add/page');
  $this
    ->drupalPostAjaxForm(NULL, array(), array(
    'field_ajax_test_add_more' => t('Add another item'),
  ), NULL, array(), array(), 'node-page-form');
  $this
    ->assert(count($this
    ->xpath('//div[contains(@class, "field--name-field-ajax-test")]//input[@type="text"]')) == 2, 'AJAX submission succeeded.');

  // Submit the form with the non-Ajax "Save" button, leaving the title field
  // blank to trigger a validation error, and ensure that a validation error
  // occurred, because this test is for testing what happens when a form is
  // re-rendered without being re-built, which is what happens when there's
  // a validation error.
  $this
    ->drupalPostForm(NULL, array(), t('Save'));
  $this
    ->assertText('Title field is required.', 'Non-AJAX submission correctly triggered a validation error.');

  // Ensure that the form contains two items in the multi-valued field, so we
  // know we're testing a form that was correctly retrieved from cache.
  $this
    ->assert(count($this
    ->xpath('//form[contains(@id, "node-page-form")]//div[contains(@class, "js-form-item-field-ajax-test")]//input[@type="text"]')) == 2, 'Form retained its state from cache.');

  // Ensure that the form's action is correct.
  $forms = $this
    ->xpath('//form[contains(@class, "node-page-form")]');
  $this
    ->assertEqual(1, count($forms));

  // Strip query params off the action before asserting.
  $url = parse_url($forms[0]['action'])['path'];
  $this
    ->assertEqual(Url::fromRoute('node.add', [
    'node_type' => 'page',
  ])
    ->toString(), $url);
}