You are here

simpletest_example.test in Examples for Developers 6

Same filename and directory in other branches
  1. 7 simpletest_example/simpletest_example.test

An example of SimpleTest tests to accompany the tutorial at http://drupal.org/node/395012.

File

simpletest_example/simpletest_example.test
View source
<?php

/**
 * @file
 * An example of SimpleTest tests to accompany the tutorial at
 * http://drupal.org/node/395012.
 */

/**
 * Tests the SimpleTest Example module's content type.
 */
class SimpleTestExampleTestCase extends DrupalWebTestCase {

  /**
   * User with rights to post SimpleTest Example content.
   */
  protected $privileged_user;

  /**
   * getInfo() returns properties that are displayed in the test selection form.
   */
  public static function getInfo() {
    return array(
      'name' => 'SimpleTest Example',
      'description' => 'Ensure that the simpletest_example content type provided functions properly.',
      'group' => 'Examples',
    );
  }

  /**
   * setUp() performs any pre-requisite tasks that need to happen.
   */
  public function setUp() {

    // Enable any modules required for the test.
    parent::setUp('simpletest_example');

    // Create and log in our privileged user.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'create simpletest_example',
      'edit own simpletest_example',
    ));
    $this
      ->drupalLogin($this->privileged_user);
  }

  /**
   * Create a simpletest_example node using the node form.
   */
  public function testSimpleTestExampleCreate() {

    // Create node to edit.
    $edit = array();
    $edit['title'] = $this
      ->randomName(8);
    $edit['body'] = $this
      ->randomName(16);
    $this
      ->drupalPost('node/add/simpletest-example', $edit, t('Save'));
    $this
      ->assertText(t('simpletest_example page @title has been created.', array(
      '@title' => $edit['title'],
    )));
  }

  /**
   * Create a simpletest_example node and then see if our user can edit it.
   */
  public function testSimpleTestExampleEdit() {
    $settings = array(
      'type' => 'simpletest_example',
      'title' => $this
        ->randomName(32),
      'body' => $this
        ->randomName(64),
    );
    $node = $this
      ->drupalCreateNode($settings);

    // For debugging, we might output the node structure with $this->verbose().
    // It would only be output if the testing settings had 'verbose' set.
    $this
      ->verbose('Node created: ' . var_export($node, TRUE));
    if (!$this
      ->runningOnTestbot()) {

      // Make sure we don't get a 401 unauthorized response when editing.
      $this
        ->drupalGet("node/{$node->nid}/edit");
      $this
        ->assertResponse(200, t('User is allowed to edit the content.'));

      // Looking for title text in the page to determine whether we were
      // successful opening edit form.
      $this
        ->assertText(t("@title", array(
        '@title' => $settings['title'],
      )), "Found title in edit form");
    }
  }

  /**
   * Detect if we're running on Drupal.org's automated testbot.
   *
   * This is used inside the edit test so that the testbot skips intentionally
   * failing tests. Otherwise, it would mark this module as broken.
   *
   * For more information on the testbot, see http://qa.drupal.org/.
   *
   * @return boolean
   *   TRUE if running on testbot.
   */
  public function runningOnTestbot() {
    $testbot_code_directory = "../checkout";
    return file_exists($testbot_code_directory);
  }

}

Classes

Namesort descending Description
SimpleTestExampleTestCase Tests the SimpleTest Example module's content type.