simpletest_example.module

Tracking 8.x-1.x branch
  1. drupal
    1. 6 contributions/examples/simpletest_example/simpletest_example.module
    2. 7 contributions/examples/simpletest_example/simpletest_example.module
    3. 8 contributions/examples/simpletest_example/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.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Module file for simpletest_example
  5. */
  6. /**
  7. * @defgroup simpletest_example Example: Simpletest
  8. * @ingroup examples
  9. * @{
  10. * An example of simpletest tests to accompany the tutorial at
  11. * http://drupal.org/node/890654.
  12. *
  13. * This is built on a traditional node-type implementation.
  14. */
  15. /**
  16. * Implements hook_node_info().
  17. */
  18. function simpletest_example_node_info() {
  19. return array(
  20. 'simpletest_example' => array(
  21. 'name' => t('Simpletest Example Node Type'),
  22. 'module' => 'simpletest_example',
  23. 'base' => 'simpletest_example',
  24. 'description' => t('simpletest_example page node type.'),
  25. )
  26. );
  27. }
  28. /**
  29. * Implements hook_permission().
  30. *
  31. * In this case we're adding an addition permission that does the same
  32. * as the one the node module offers, just to demonstrate this error.
  33. */
  34. function simpletest_example_permission() {
  35. $perms = array();
  36. $perms['extra special edit any simpletest_example'] = array('title' => t('Extra special edit any Simpletest Example'), 'description' => t('Extra special edit any Simpletest Example'));
  37. return $perms;
  38. }
  39. /**
  40. * Implements hook_node_access().
  41. *
  42. * Demonstrates a bug that we'll find in our test.
  43. *
  44. * If this is running on the testbot, we don't want the error to show so will
  45. * work around it by testing to see if we're in the 'checkout' directory.
  46. */
  47. function simpletest_example_node_access($node, $op, $account) {
  48. // Don't get involved if this isn't a simpletest_example node, etc.
  49. $type = is_string($node) ? $node : $node->type;
  50. if ($type != 'simpletest_example' || ($op != 'update' && $op != 'delete')) {
  51. return NODE_ACCESS_IGNORE;
  52. }
  53. // This code has a BUG that we'll find in testing.
  54. // This is the incorrect version we'll use to demonstrate test failure.
  55. // The correct version should have ($op == 'update' || $op == 'delete').
  56. // The author had mistakenly always tested with User 1 so it always
  57. // allowed access and the bug wasn't noticed!
  58. if ( ($op == 'delete') && (user_access('extra special edit any simpletest_example', $account) && ($account->uid == $node->uid))) {
  59. return NODE_ACCESS_ALLOW;
  60. }
  61. return NODE_ACCESS_DENY;
  62. }
  63. /**
  64. * Implements hook_form().
  65. *
  66. * Form for the node type.
  67. */
  68. function simpletest_example_form($node, $form_state) {
  69. $type = node_type_get_type($node);
  70. $form = array();
  71. if ($type->has_title) {
  72. $form['title'] = array(
  73. '#type' => 'textfield',
  74. '#title' => check_plain($type->title_label),
  75. '#required' => TRUE,
  76. '#default_value' => $node->title,
  77. '#maxlength' => 255,
  78. '#weight' => -5,
  79. );
  80. }
  81. return $form;
  82. }
  83. /**
  84. * Implements hook_menu().
  85. *
  86. * Provides an explanation.
  87. */
  88. function simpletest_example_menu() {
  89. $items['examples/simpletest_example'] = array(
  90. 'title' => 'Simpletest Example',
  91. 'description' => 'Explain the simpletest example and allow the error logic to be executed.',
  92. 'page callback' => '_simpletest_example_explanation',
  93. 'access callback' => TRUE,
  94. );
  95. return $items;
  96. }
  97. /**
  98. * Returns an explanation of this module.
  99. */
  100. function _simpletest_example_explanation() {
  101. $explanation = t("This Simpletest Example is designed to give an introductory tutorial to writing
  102. a simpletest test. Please see the <a href='http://drupal.org/node/890654'>associated tutorial</a>.");
  103. return $explanation;
  104. }
  105. /**
  106. * A simple self-contained function used to demonstrate unit tests.
  107. *
  108. * @see SimpletestUnitTestExampleTestCase
  109. */
  110. function simpletest_example_empty_mysql_date($date_string) {
  111. if (empty($date_string) || $date_string == '0000-00-00' || $date_string == '0000-00-00 00:00:00') {
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * @} End of "defgroup simpletest_example".
  118. */