simpletest_example.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 module creates a new node type called 'SimpleTest Example Node Type,'
  14. * so that we can test it.
  15. */
  16. /**
  17. * Implements hook_node_info().
  18. */
  19. function simpletest_example_node_info() {
  20. return array(
  21. 'simpletest_example' => array(
  22. 'name' => t('SimpleTest Example Node Type'),
  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. //
  55. // This is the incorrect version we'll use to demonstrate test failure.
  56. // The correct version should have ($op == 'update' || $op == 'delete').
  57. // The author had mistakenly always tested with User 1 so it always
  58. // allowed access and the bug wasn't noticed!
  59. if (($op == 'delete') && (user_access('extra special edit any simpletest_example', $account) && ($account->uid == $node->uid))) {
  60. return NODE_ACCESS_ALLOW;
  61. }
  62. return NODE_ACCESS_DENY;
  63. }
  64. /**
  65. * Implements hook_form().
  66. *
  67. * Form for the node type.
  68. */
  69. function simpletest_example_form($node, $form_state) {
  70. $type = node_type_get_type($node);
  71. $form = array();
  72. if ($type->has_title) {
  73. $form['title'] = array(
  74. '#type' => 'textfield',
  75. '#title' => check_plain($type->title_label),
  76. '#required' => TRUE,
  77. '#default_value' => $node->title,
  78. '#maxlength' => 255,
  79. '#weight' => -5,
  80. );
  81. }
  82. return $form;
  83. }
  84. /**
  85. * Implements hook_menu().
  86. *
  87. * Provides an explanation.
  88. */
  89. function simpletest_example_menu() {
  90. $items['examples/simpletest_example'] = array(
  91. 'title' => 'Simpletest Example',
  92. 'description' => 'Explain the simpletest example and allow the error logic to be executed.',
  93. 'page callback' => '_simpletest_example_explanation',
  94. 'access callback' => TRUE,
  95. );
  96. return $items;
  97. }
  98. /**
  99. * Returns an explanation of this module.
  100. */
  101. function _simpletest_example_explanation() {
  102. $explanation = t("This Simpletest Example is designed to give an introductory tutorial to writing
  103. a simpletest test. Please see the <a href='http://drupal.org/node/890654'>associated tutorial</a>.");
  104. return $explanation;
  105. }
  106. /**
  107. * A simple self-contained function used to demonstrate unit tests.
  108. *
  109. * @see SimpletestUnitTestExampleTestCase
  110. */
  111. function simpletest_example_empty_mysql_date($date_string) {
  112. if (empty($date_string) || $date_string == '0000-00-00' || $date_string == '0000-00-00 00:00:00') {
  113. return TRUE;
  114. }
  115. return FALSE;
  116. }
  117. /**
  118. * @} End of "defgroup simpletest_example".
  119. */