1 form.test FormValidationTestCase::testValidate()

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

File

core/modules/simpletest/tests/form.test, line 772
Unit tests for the Backdrop Form API.

Class

FormValidationTestCase
Test form validation handlers.

Code

function testValidate() {
  $this->backdropGet('form-test/validate');
  // Verify that #element_validate handlers can alter the form and submitted
  // form values.
  $edit = array(
    'name' => 'element_validate',
  );
  $this->backdropPost(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->backdropPost(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->backdropPost(NULL, $edit, 'Save');
  $this->assertNoFieldByName('name', t('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->backdropPost(NULL, array(), 'Save');
  $this->assertNoFieldByName('name', t('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->backdropLogin($this->backdropCreateUser());
  $this->backdropGet('form-test/validate');
  $edit = array(
    'name' => 'validate',
    'form_token' => 'invalid token'
  );
  $this->backdropPost(NULL, $edit, 'Save');
  $this->assertNoFieldByName('name', '#value changed by #validate', 'Form element #value was not altered.');
  $this->assertNoText('Name value: value changed by form_set_value() in #validate', 'Form element value in $form_state was not altered.');
  $this->assertText('The form has become outdated.');
}