You are here

public function FormExampleTestCase::testWizard in Examples for Developers 7

Test Wizard tutorial.

@TODO improve this using drupal_form_submit

File

form_example/form_example.test, line 195
Test file for form_example module.

Class

FormExampleTestCase
Default test case for the form_example module.

Code

public function testWizard() {

  // Check if the wizard is there.
  $this
    ->drupalGet('examples/form_example/wizard');
  $this
    ->assertText(t('Extensible wizard example'));
  $first_name = $this
    ->randomName(8);
  $last_name = $this
    ->randomName(8);
  $city = $this
    ->randomName(8);
  $aunts_name = $this
    ->randomName(8);

  // Submit the first step of the wizard.
  $options = array(
    'first_name' => $first_name,
    'last_name' => $last_name,
  );
  $this
    ->drupalPost('examples/form_example/wizard', $options, t('Next'));

  // A label city is created, and two buttons appear, Previous and Next.
  $this
    ->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));

  // Go back to the beginning and verify that the value is there.
  $this
    ->drupalPost(NULL, array(), t('Previous'));
  $this
    ->assertFieldByName('first_name', $first_name);
  $this
    ->assertFieldByName('last_name', $last_name);

  // Go next. We should keep our values.
  $this
    ->drupalPost(NULL, array(), t('Next'));
  $this
    ->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));

  // Try "San Francisco".
  $this
    ->drupalPost(NULL, array(
    'city' => 'San Francisco',
  ), t('Next'));
  $this
    ->assertText(t('You were warned not to enter "San Francisco"'));

  // Try the real city.
  $this
    ->drupalPost(NULL, array(
    'city' => $city,
  ), t('Next'));

  // Enter the Aunt's name, but then the previous button.
  $this
    ->drupalPost(NULL, array(
    'aunts_name' => $aunts_name,
  ), t('Previous'));
  $this
    ->assertFieldByName('city', $city);

  // Go to first step and re-check all fields.
  $this
    ->drupalPost(NULL, array(), t('Previous'));
  $this
    ->assertFieldByName('first_name', $first_name);
  $this
    ->assertFieldByName('last_name', $last_name);

  // Re-check second step.
  $this
    ->drupalPost(NULL, array(), t('Next'));
  $this
    ->assertText(t('Hint: Do not enter "San Francisco", and do not leave this out.'));
  $this
    ->assertFieldByName('city', $city);

  // Re-check third step.
  $this
    ->drupalPost(NULL, array(), t('Next'));
  $this
    ->assertFieldByName('aunts_name', $aunts_name);

  // Press finish and check for correct values.
  $this
    ->drupalPost(NULL, array(), t('Finish'));
  $this
    ->assertRaw(t('[first_name] => @first_name', array(
    '@first_name' => $first_name,
  )));
  $this
    ->assertRaw(t('[last_name] => @last_name', array(
    '@last_name' => $last_name,
  )));
  $this
    ->assertRaw(t('[city] => @city', array(
    '@city' => $city,
  )));
  $this
    ->assertRaw(t('[aunts_name] => @aunts_name', array(
    '@aunts_name' => $aunts_name,
  )));
}