Community Documentation

simpletest_example.module

  1. examples
    1. 6 simpletest_example.module
    2. 7 simpletest_example.module
    3. 8 simpletest_example.module

Module file for simpletest_example

Functions & methods

NameDescription
simpletest_example_empty_mysql_dateA simple self-contained function used to demonstrate unit tests.
simpletest_example_formImplements hook_form().
simpletest_example_menuImplements hook_menu().
simpletest_example_node_accessImplements hook_node_access().
simpletest_example_node_infoImplements hook_node_info().
simpletest_example_permissionImplements hook_permission().
_simpletest_example_explanationReturns an explanation of this module.
View source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

/**
 * @file
 * Module file for simpletest_example
 */

/**
 * @defgroup simpletest_example Example: Simpletest
 * @ingroup examples
 * @{
 * An example of simpletest tests to accompany the tutorial at
 * http://drupal.org/node/890654.
 *
 * This is built on a traditional node-type implementation.
 */

/**
 * Implements hook_node_info().
 */
function simpletest_example_node_info() {
  return array(
    'simpletest_example' => array(
      'name' => t('Simpletest Example Node Type'),
      'module' => 'simpletest_example',
      'base' => 'simpletest_example',
      'description' => t('simpletest_example page node type.'),
  )
  );
}

/**
 * Implements hook_permission().
 *
 * In this case we're adding an addition permission that does the same
 * as the one the node module offers, just to demonstrate this error.
 */
function simpletest_example_permission() {
  $perms = array();
  $perms['extra special edit any simpletest_example'] = array('title' => t('Extra special edit any Simpletest Example'), 'description' => t('Extra special edit any Simpletest Example'));
  return $perms;
}

/**
 * Implements hook_node_access().
 *
 * Demonstrates a bug that we'll find in our test.
 *
 * If this is running on the testbot, we don't want the error to show so will
 * work around it by testing to see if we're in the 'checkout' directory.
 */
function simpletest_example_node_access($node, $op, $account) {
  // Don't get involved if this isn't a simpletest_example node, etc.
  $type = is_string($node) ? $node : $node->type;
  if ($type != 'simpletest_example' || ($op != 'update' && $op != 'delete')) {
    return NODE_ACCESS_IGNORE;
  }

  // This code has a BUG that we'll find in testing.

  // This is the incorrect version we'll use to demonstrate test failure.
  // The correct version should have ($op == 'update' || $op == 'delete').
  // The author had mistakenly always tested with User 1 so it always
  // allowed access and the bug wasn't noticed!
  if ( ($op == 'delete')  && (user_access('extra special edit any simpletest_example', $account) && ($account->uid == $node->uid))) {
    return NODE_ACCESS_ALLOW;
  }

  return NODE_ACCESS_DENY;
}

/**
 * Implements hook_form().
 *
 * Form for the node type.
 */
function simpletest_example_form($node, $form_state) {
  $type = node_type_get_type($node);
  $form = array();
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => check_plain($type->title_label),
      '#required' => TRUE,
      '#default_value' => $node->title,
      '#maxlength' => 255,
      '#weight' => -5,
    );
  }
  return $form;
}

/**
 * Implements hook_menu().
 *
 * Provides an explanation.
 */
function simpletest_example_menu() {
  $items['examples/simpletest_example'] = array(
    'title'           => 'Simpletest Example',
    'description'     => 'Explain the simpletest example and allow the error logic to be executed.',
    'page callback'   => '_simpletest_example_explanation',
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * Returns an explanation of this module.
 */
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/890654'>associated tutorial</a>.");
  return $explanation;
}

/**
 * A simple self-contained function used to demonstrate unit tests.
 *
 * @see SimpletestUnitTestExampleTestCase
 */
function simpletest_example_empty_mysql_date($date_string) {
  if (empty($date_string) || $date_string == '0000-00-00' || $date_string == '0000-00-00 00:00:00') {
    return true;
  }
  return false;
}

/**
 * @} End of "defgroup simpletest_example".
 */
Login or register to post comments