You are here

function FormExampleTestCase::testElementExample in Examples for Developers 6

Same name and namespace in other branches
  1. 7 form_example/form_example.test \FormExampleTestCase::testElementExample()

Test the element_example form for correct behavior.

File

form_example/form_example.test, line 142
test file for form_example module.

Class

FormExampleTestCase
Default test case for the form_example module.

Code

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'));
}