1 form_example.test public FormExampleTestCase::testTutorials()

Test each tutorial.

File

modules/examples/form_example/tests/form_example.test, line 24
Test file for form_example module.

Class

FormExampleTestCase
Default test case for the form_example module.

Code

public function testTutorials() {
  // Tutorial #1
  $this->backdropGet('examples/form_example/tutorial');
  $this->assertText(t('#9'));

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

  // #4
  $this->backdropPost('examples/form_example/tutorial/4', 
  array('first' => t('firstname'), 'last' => t('lastname')), t('Submit'));
  $this->backdropPost('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->backdropPost('examples/form_example/tutorial/5', 
  array('first' => t('firstname'), 'last' => t('lastname')), t('Submit'));
  $this->assertText(t('Please enter your first name'));
  $this->backdropPost('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->backdropPost(
  '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->backdropPost(
  '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->backdropPost(
  '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->backdropPost(
  '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'));

  // Test tutorial #8.
  $this->backdropPost(
  'examples/form_example/tutorial/8', 
  array(
    'first' => t('firstname'),
    'last' => t('lastname'),
    'year_of_birth' => 1955,
  ), 
  t('Next >>')
  );

  $this->backdropPost(NULL, array('color' => t('green')), t('<< Back'));
  $this->backdropPost(NULL, array(), t('Next >>'));
  $this->backdropPost(NULL, array('color' => t('red')), t('Submit'));
  $this->assertText(t('The form has been submitted. name="firstname lastname", year of birth=1955'));
  $this->assertText(t('And the favorite color is red'));

  // #9
  $url = 'examples/form_example/tutorial/9';
  for ($i = 1; $i <= 4; $i++) {
    if ($i > 1) {
      // Later steps of multistep form take NULL.
      $url = NULL;
    }
    $this->backdropPost(
    $url, 
    array(
      "name[$i][first]" => "firstname $i",
      "name[$i][last]" => "lastname $i",
      "name[$i][year_of_birth]" => 1950 + $i,
    ), 
    t('Add another name')
    );
    $this->assertText(t('Name #@num', array('@num' => $i + 1)));
  }

  // Now remove the last name added (#5).
  $this->backdropPost(NULL, array(), t('Remove latest name'));
  $this->assertNoText("Name #5");

  $this->backdropPost(NULL, array(), t('Submit'));

  $this->assertText('Form 9 has been submitted');
  for ($i = 1; $i <= 4; $i++) {
    $this->assertText(t('@num: firstname @num lastname @num (@year)', array('@num' => $i, '@year' => 1950 + $i)));
  }

  // #10
  $url = 'examples/form_example/tutorial/10';

  $this->backdropPost($url, array(), t('Submit'));
  $this->assertText(t('No file was uploaded.'));

  // Get sample images.
  $images = $this->backdropGetTestFiles('image');
  foreach ($images as $image) {
    $this->backdropPost($url, array('files[file]' => backdrop_realpath($image->uri)), t('Submit'));
    $this->assertText(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $image->filename)));
  }
}