You are here

function FormsFormStorageTestCase::testFormStatePersist in Drupal 7

Tests a form using form state without using 'storage' to pass data from the constructor to a submit handler. The data has to persist even when caching gets activated, what may happen when a modules alter the form and adds #ajax properties.

File

modules/simpletest/tests/form.test, line 1244
Unit tests for the Drupal Form API.

Class

FormsFormStorageTestCase
Test the form storage on a multistep form.

Code

function testFormStatePersist() {

  // Test the form one time with caching activated and one time without.
  $run_options = array(
    array(),
    array(
      'query' => array(
        'cache' => 1,
      ),
    ),
  );
  foreach ($run_options as $options) {
    $this
      ->drupalPost('form-test/state-persist', array(), t('Submit'), $options);

    // The submit handler outputs the value in $form_state, assert it's there.
    $this
      ->assertText('State persisted.');

    // Test it again, but first trigger a validation error, then test.
    $this
      ->drupalPost('form-test/state-persist', array(
      'title' => '',
    ), t('Submit'), $options);
    $this
      ->assertText(t('!name field is required.', array(
      '!name' => 'title',
    )));

    // Submit the form again triggering no validation error.
    $this
      ->drupalPost(NULL, array(
      'title' => 'foo',
    ), t('Submit'), $options);
    $this
      ->assertText('State persisted.');

    // Now post to the rebuilt form and verify it's still there afterwards.
    $this
      ->drupalPost(NULL, array(
      'title' => 'bar',
    ), t('Submit'), $options);
    $this
      ->assertText('State persisted.');
  }
}