home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (simpletest_example.test)

This example Drupal source code file (simpletest_example.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, content, created, edit, function, node, php, public, return, simpletest_example, testbot_code_directory, the, title, type

The simpletest_example.test Drupal example source code

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

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal simpletest_example.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.