You are here

simpletest_example.module in Examples for Developers 6

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

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

This is built on a traditional node-type implementation.

File

simpletest_example/simpletest_example.module
View source
<?php

/**
 * @file
 * An example of simpletest tests to accompany the tutorial at
 * http://drupal.org/node/395012.
 *
 * This is built on a traditional node-type implementation.
 */

/**
 * @defgroup simpletest_example Example: Simpletest
 * @ingroup examples
 * @{
 * Creating tests. (drupal 6)
 *
 * This example accompanies the tutorial at
 * http://drupal.org/node/395012.
 *
 * This is built on a traditional node-type implementation.
 *
 * This example is part of the Examples for Developers Project which you can download
 * and experiment with here: http://drupal.org/project/examples
 */

/**
 * Implementation of hook_node_info().
 */
function simpletest_example_node_info() {
  return array(
    'simpletest_example' => array(
      'name' => t('simpletest_example page'),
      'module' => 'simpletest_example',
      'description' => t('simpletest_example page node type.'),
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function simpletest_example_perm() {
  return array(
    'create simpletest_example',
    'edit own simpletest_example',
  );
}

/**
 * Implementation of hook_access().
 */
function simpletest_example_access($op, $node) {
  global $user;
  if ($op == 'create') {
    return user_access('create simpletest_example');
  }

  // This code has a BUG that we'll find in testing
  // Correct version
  // if ($op == 'update' || $op == 'delete') {
  // Incorrect version we'll use to demonstrate test failure. We were always testing
  // with User 1, so it always allowed access and the bug wasn't noticed!
  if ($op == 'delete') {
    return user_access('edit own simpletest_example') && $user->uid == $node->uid;
  }
}

/**
 * Implementation of hook_form().
 */
function simpletest_example_form(&$node) {
  $type = node_get_types('type', $node);
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => check_plain($type->title_label),
      '#required' => TRUE,
      '#default_value' => $node->title,
      '#weight' => -5,
    );
  }
  if ($type->has_body) {
    $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  }
  return $form;
}

/**
 * Implementation of hook_menu().
 *
 * Provide an explanation.
 */
function simpletest_example_menu() {
  $items['examples/simpletest_example'] = array(
    'title' => 'SimpleTest Example',
    'description' => 'Explain the SimpleTest example.',
    'page callback' => '_simpletest_example_explanation',
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * Page to explain this example.
 */
function _simpletest_example_explanation() {
  $explanation = t("This SimpleTest Example is designed to give an introductory tutorial to writing a SimpleTest test. Please see the <a href='http://drupal.org/node/395012'>associated tutorial</a>.");
  return $explanation;
}

/**
 * @} End of "defgroup simpletest_example".
 */

Functions

Namesort descending Description
simpletest_example_access Implementation of hook_access().
simpletest_example_form Implementation of hook_form().
simpletest_example_menu Implementation of hook_menu().
simpletest_example_node_info Implementation of hook_node_info().
simpletest_example_perm Implementation of hook_perm().
_simpletest_example_explanation Page to explain this example.