You are here

function ValidationTest::testValidate in Zircon Profile 8.0

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

Tests #element_validate and #validate.

File

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

Class

ValidationTest
Tests form processing and alteration via form validation handlers.

Namespace

Drupal\system\Tests\Form

Code

function testValidate() {
  $this
    ->drupalGet('form-test/validate');

  // Verify that #element_validate handlers can alter the form and submitted
  // form values.
  $edit = array(
    'name' => 'element_validate',
  );
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertFieldByName('name', '#value changed by #element_validate', 'Form element #value was altered.');
  $this
    ->assertText('Name value: value changed by setValueForElement() in #element_validate', 'Form element value in $form_state was altered.');

  // Verify that #validate handlers can alter the form and submitted
  // form values.
  $edit = array(
    'name' => 'validate',
  );
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertFieldByName('name', '#value changed by #validate', 'Form element #value was altered.');
  $this
    ->assertText('Name value: value changed by setValueForElement() in #validate', 'Form element value in $form_state was altered.');

  // Verify that #element_validate handlers can make form elements
  // inaccessible, but values persist.
  $edit = array(
    'name' => 'element_validate_access',
  );
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertNoFieldByName('name', 'Form element was hidden.');
  $this
    ->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');

  // Verify that value for inaccessible form element persists.
  $this
    ->drupalPostForm(NULL, array(), 'Save');
  $this
    ->assertNoFieldByName('name', 'Form element was hidden.');
  $this
    ->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');

  // Verify that #validate handlers don't run if the CSRF token is invalid.
  $this
    ->drupalLogin($this
    ->drupalCreateUser());
  $this
    ->drupalGet('form-test/validate');
  $edit = array(
    'name' => 'validate',
    'form_token' => 'invalid token',
  );
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertNoFieldByName('name', '#value changed by #validate', 'Form element #value was not altered.');
  $this
    ->assertNoText('Name value: value changed by setValueForElement() in #validate', 'Form element value in $form_state was not altered.');
  $this
    ->assertText('The form has become outdated. Copy any unsaved work in the form below');
}