Same filename and directory in other branches
  1. 7.x-1.x form_example/form_example.test

test file for form_example module.

File

form_example/form_example.test
View source
<?php

/**
 * @file
 * test file for form_example module.
 */

/**
 * Default test case for the form_example module.
 */
class FormExampleTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Form Example',
      'description' => 'Various tests on the form_example module.',
      'group' => 'Examples',
    );
  }
  function setUp() {
    parent::setUp('form_example');
  }

  /**
   * Test each tutorial
   */
  function testTutorials() {

    // Tutorial #1
    $this
      ->drupalGet('examples/form_example/tutorial');
    $this
      ->assertText(t('#10'));

    // #2
    $this
      ->drupalPost('examples/form_example/tutorial/2', array(
      'name' => t('name'),
    ), t('Submit'));

    // #4
    $this
      ->drupalPost('examples/form_example/tutorial/4', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
    ), t('Submit'));
    $this
      ->drupalPost('examples/form_example/tutorial/4', array(), t('Submit'));
    $this
      ->assertText(t('First name field is required'));
    $this
      ->assertText(t('Last name field is required'));

    // #5
    $this
      ->drupalPost('examples/form_example/tutorial/5', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
    ), t('Submit'));
    $this
      ->assertText(t('Please enter your first name'));
    $this
      ->drupalPost('examples/form_example/tutorial/4', array(), t('Submit'));
    $this
      ->assertText(t('First name field is required'));
    $this
      ->assertText(t('Last name field is required'));

    // #6
    $this
      ->drupalPost('examples/form_example/tutorial/6', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1955,
    ), t('Submit'));
    $this
      ->assertNoText(t('Enter a year between 1900 and 2000'));
    $this
      ->drupalPost('examples/form_example/tutorial/6', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1855,
    ), t('Submit'));
    $this
      ->assertText(t('Enter a year between 1900 and 2000'));

    // #7
    $this
      ->drupalPost('examples/form_example/tutorial/7', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1955,
    ), t('Submit'));
    $this
      ->assertText(t('The form has been submitted. name="firstname lastname", year of birth=1955'));
    $this
      ->drupalPost('examples/form_example/tutorial/7', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1855,
    ), t('Submit'));
    $this
      ->assertText(t('Enter a year between 1900 and 2000'));

    // #8
    $this
      ->drupalPost('examples/form_example/tutorial/8', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1955,
    ), t('Submit'));
    $this
      ->assertText(t('The form has been submitted. name="firstname lastname", year of birth=1955'));
    $this
      ->drupalPost('examples/form_example/tutorial/8', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1855,
    ), t('Submit'));
    $this
      ->assertText(t('Enter a year between 1900 and 2000'));
    $this
      ->drupalPost('examples/form_example/tutorial/8', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1855,
    ), t('Reset form'));
    $this
      ->assertNoText(t('Enter a year between 1900 and 2000'));

    // #9
    $this
      ->drupalPost('examples/form_example/tutorial/9', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1955,
    ), t('Add another name'));
    $this
      ->assertText(t('Name #2'));
    $this
      ->drupalPost(NULL, array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1955,
      'first2' => t('firstname2'),
      'last2' => t('lastname2'),
      'year_of_birth2' => 1956,
    ), t('Submit'));
    $this
      ->assertText(t('The form has been submitted. name="firstname lastname", year of birth=1955'));
    $this
      ->assertText(t('Second name: name="firstname2 lastname2", year of birth=1956'));

    // #10
    $this
      ->drupalPost('examples/form_example/tutorial/10', array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1955,
    ), t('Add another name'));
    $this
      ->assertText(t('Name #2'));
    $this
      ->drupalPost(NULL, array(
      'first' => t('firstname'),
      'last' => t('lastname'),
      'year_of_birth' => 1955,
      'first2' => t('firstname2'),
      'last2' => t('lastname2'),
      'year_of_birth2' => 1956,
    ), t('Next >>'));
    $this
      ->drupalPost(NULL, array(
      'color' => t('green'),
    ), t('Submit'));
    $this
      ->assertText(t('The form has been submitted. name="firstname lastname", year of birth=1955'));
    $this
      ->assertText(t('Second name: name="firstname2 lastname2", year of birth=1956'));
    $this
      ->assertText(t('And the favorite color is green'));

    // #11
    // Get sample images.
    $images = $this
      ->drupalGetTestFiles('image');
    foreach ($images as $image) {
      $this
        ->drupalPost('examples/form_example/tutorial/11', array(
        'files[file]' => $image->filename,
      ), t('Submit'));
      $this
        ->assertText(t('The form has been submitted and the image has been saved, filename: @filename.', array(
        '@filename' => $image->basename,
      )));
    }

    // Try invalid files
    $files = $this
      ->drupalGetTestFiles('html');
    foreach ($files as $file) {
      $this
        ->drupalPost('examples/form_example/tutorial/11', array(
        'files[file]' => $file->filename,
      ), t('Submit'));

      // Can't find a quick way to rebuild the whole validation string, so check
      // only that file has not been submitted.
      $this
        ->assertText(t('The selected file @filename could not be uploaded.', array(
        '@filename' => $file->basename,
      )));
    }
  }

  /**
   * Test the element_example form for correct behavior.
   */
  function testElementExample() {

    // Make one basic POST with a set of values and check for correct responses.
    $edit = array(
      'form_example_textfield' => $this
        ->randomName(),
      'form_example_checkbox' => TRUE,
      'form_example_element_discrete[areacode]' => sprintf('%03d', rand(0, 999)),
      'form_example_element_discrete[prefix]' => sprintf('%03d', rand(0, 999)),
      'form_example_element_discrete[extension]' => sprintf('%04d', rand(0, 9999)),
      'form_example_element_combined[areacode]' => sprintf('%03d', rand(0, 999)),
      'form_example_element_combined[prefix]' => sprintf('%03d', rand(0, 999)),
      'form_example_element_combined[extension]' => sprintf('%04d', rand(0, 9999)),
    );
    $this
      ->drupalPost('examples/form_example/element_example', $edit, t('Submit'));
    $this
      ->assertText(t('form_example_textfield has value @value', array(
      '@value' => $edit['form_example_textfield'],
    )));
    $this
      ->assertText(t('form_example_checkbox has value 1'));
    $this
      ->assertPattern(t('/areacode.*!areacode/', array(
      '!areacode' => $edit['form_example_element_discrete[areacode]'],
    )));
    $this
      ->assertPattern(t('/prefix.*!prefix/', array(
      '!prefix' => $edit['form_example_element_discrete[prefix]'],
    )));
    $this
      ->assertPattern(t('/extension.*!extension/', array(
      '!extension' => $edit['form_example_element_discrete[extension]'],
    )));
    $this
      ->assertText(t('form_example_element_combined has value @value', array(
      '@value' => $edit['form_example_element_combined[areacode]'] . $edit['form_example_element_combined[prefix]'] . $edit['form_example_element_combined[extension]'],
    )));

    // Now flip the checkbox and check for correct behavior.
    $edit['form_example_checkbox'] = FALSE;
    $this
      ->drupalPost('examples/form_example/element_example', $edit, t('Submit'));
    $this
      ->assertText(t('form_example_checkbox has value 0'));
  }

}

Classes

Namesort descending Description
FormExampleTestCase Default test case for the form_example module.