function ValidationTest::testValidate

Tests form alterations by #element_validate, #validate, and form_set_value().

File

drupal/core/modules/system/lib/Drupal/system/Tests/Form/ValidationTest.php, line 35
Definition of Drupal\system\Tests\Form\ValidationTest.

Class

ValidationTest
Test 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
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertFieldByName('name', '#value changed by #element_validate', 'Form element #value was altered.');
  $this
    ->assertText('Name value: value changed by form_set_value() 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
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertFieldByName('name', '#value changed by #validate', 'Form element #value was altered.');
  $this
    ->assertText('Name value: value changed by form_set_value() 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
    ->drupalPost(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
    ->drupalPost(NULL, array(), 'Save');
  $this
    ->assertNoFieldByName('name', 'Form element was hidden.');
  $this
    ->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');
}