You are here

function ValidationTest::testValidateLimitErrors in Zircon Profile 8

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

Tests partial form validation through #limit_validation_errors.

File

core/modules/system/src/Tests/Form/ValidationTest.php, line 88
Contains \Drupal\system\Tests\Form\ValidationTest.

Class

ValidationTest
Tests form processing and alteration via form validation handlers.

Namespace

Drupal\system\Tests\Form

Code

function testValidateLimitErrors() {
  $edit = array(
    'test' => 'invalid',
    'test_numeric_index[0]' => 'invalid',
    'test_substring[foo]' => 'invalid',
  );
  $path = 'form-test/limit-validation-errors';

  // Render the form, and verify that the buttons with limited server-side
  // validation have the proper 'formnovalidate' attribute (to prevent
  // client-side validation by the browser).
  $this
    ->drupalGet($path);
  $expected = 'formnovalidate';
  foreach (array(
    'partial',
    'partial-numeric-index',
    'substring',
  ) as $type) {
    $element = $this
      ->xpath('//input[@id=:id and @formnovalidate=:expected]', array(
      ':id' => 'edit-' . $type,
      ':expected' => $expected,
    ));
    $this
      ->assertTrue(!empty($element), format_string('The @type button has the proper formnovalidate attribute.', array(
      '@type' => $type,
    )));
  }

  // The button with full server-side validation should not have the
  // 'formnovalidate' attribute.
  $element = $this
    ->xpath('//input[@id=:id and not(@formnovalidate)]', array(
    ':id' => 'edit-full',
  ));
  $this
    ->assertTrue(!empty($element), 'The button with full server-side validation does not have the formnovalidate attribute.');

  // Submit the form by pressing the 'Partial validate' button (uses
  // #limit_validation_errors) and ensure that the title field is not
  // validated, but the #element_validate handler for the 'test' field
  // is triggered.
  $this
    ->drupalPostForm($path, $edit, t('Partial validate'));
  $this
    ->assertNoText(t('@name field is required.', array(
    '@name' => 'Title',
  )));
  $this
    ->assertText('Test element is invalid');

  // Edge case of #limit_validation_errors containing numeric indexes: same
  // thing with the 'Partial validate (numeric index)' button and the
  // 'test_numeric_index' field.
  $this
    ->drupalPostForm($path, $edit, t('Partial validate (numeric index)'));
  $this
    ->assertNoText(t('@name field is required.', array(
    '@name' => 'Title',
  )));
  $this
    ->assertText('Test (numeric index) element is invalid');

  // Ensure something like 'foobar' isn't considered "inside" 'foo'.
  $this
    ->drupalPostForm($path, $edit, t('Partial validate (substring)'));
  $this
    ->assertNoText(t('@name field is required.', array(
    '@name' => 'Title',
  )));
  $this
    ->assertText('Test (substring) foo element is invalid');

  // Ensure not validated values are not available to submit handlers.
  $this
    ->drupalPostForm($path, array(
    'title' => '',
    'test' => 'valid',
  ), t('Partial validate'));
  $this
    ->assertText('Only validated values appear in the form values.');

  // Now test full form validation and ensure that the #element_validate
  // handler is still triggered.
  $this
    ->drupalPostForm($path, $edit, t('Full validate'));
  $this
    ->assertText(t('@name field is required.', array(
    '@name' => 'Title',
  )));
  $this
    ->assertText('Test element is invalid');
}