common_test.module

Helper module for the Common tests.

File

drupal-7.x/modules/simpletest/tests/common_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the Common tests.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function common_test_menu() {
  10. $items['common-test/drupal_goto'] = array(
  11. 'title' => 'Drupal Goto',
  12. 'page callback' => 'common_test_drupal_goto_land',
  13. 'access arguments' => array('access content'),
  14. 'type' => MENU_CALLBACK,
  15. );
  16. $items['common-test/drupal_goto/fail'] = array(
  17. 'title' => 'Drupal Goto',
  18. 'page callback' => 'common_test_drupal_goto_land_fail',
  19. 'access arguments' => array('access content'),
  20. 'type' => MENU_CALLBACK,
  21. );
  22. $items['common-test/drupal_goto/redirect'] = array(
  23. 'title' => 'Drupal Goto',
  24. 'page callback' => 'common_test_drupal_goto_redirect',
  25. 'access arguments' => array('access content'),
  26. 'type' => MENU_CALLBACK,
  27. );
  28. $items['common-test/drupal_goto/redirect_advanced'] = array(
  29. 'title' => 'Drupal Goto',
  30. 'page callback' => 'common_test_drupal_goto_redirect_advanced',
  31. 'access arguments' => array('access content'),
  32. 'type' => MENU_CALLBACK,
  33. );
  34. $items['common-test/drupal_goto/redirect_fail'] = array(
  35. 'title' => 'Drupal Goto Failure',
  36. 'page callback' => 'drupal_goto',
  37. 'page arguments' => array('common-test/drupal_goto/fail'),
  38. 'access arguments' => array('access content'),
  39. 'type' => MENU_CALLBACK,
  40. );
  41. $items['common-test/destination'] = array(
  42. 'title' => 'Drupal Get Destination',
  43. 'page callback' => 'common_test_destination',
  44. 'access arguments' => array('access content'),
  45. 'type' => MENU_CALLBACK,
  46. );
  47. $items['common-test/query-string'] = array(
  48. 'title' => 'Test querystring',
  49. 'page callback' => 'common_test_js_and_css_querystring',
  50. 'access arguments' => array('access content'),
  51. 'type' => MENU_CALLBACK,
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Redirect using drupal_goto().
  57. */
  58. function common_test_drupal_goto_redirect() {
  59. drupal_goto('common-test/drupal_goto');
  60. }
  61. /**
  62. * Redirect using drupal_goto().
  63. */
  64. function common_test_drupal_goto_redirect_advanced() {
  65. drupal_goto('common-test/drupal_goto', array('query' => array('foo' => '123')), 301);
  66. }
  67. /**
  68. * Landing page for drupal_goto().
  69. */
  70. function common_test_drupal_goto_land() {
  71. print "drupal_goto";
  72. }
  73. /**
  74. * Fail landing page for drupal_goto().
  75. */
  76. function common_test_drupal_goto_land_fail() {
  77. print "drupal_goto_fail";
  78. }
  79. /**
  80. * Implements hook_drupal_goto_alter().
  81. */
  82. function common_test_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  83. if ($path == 'common-test/drupal_goto/fail') {
  84. $path = 'common-test/drupal_goto/redirect';
  85. }
  86. }
  87. /**
  88. * Print destination query parameter.
  89. */
  90. function common_test_destination() {
  91. $destination = drupal_get_destination();
  92. print "The destination: " . check_plain($destination['destination']);
  93. }
  94. /**
  95. * Applies #printed to an element to help test #pre_render.
  96. */
  97. function common_test_drupal_render_printing_pre_render($elements) {
  98. $elements['#printed'] = TRUE;
  99. return $elements;
  100. }
  101. /**
  102. * Implements hook_TYPE_alter().
  103. */
  104. function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  105. // Alter first argument.
  106. if (is_array($data)) {
  107. $data['foo'] = 'Drupal';
  108. }
  109. elseif (is_object($data)) {
  110. $data->foo = 'Drupal';
  111. }
  112. // Alter second argument, if present.
  113. if (isset($arg2)) {
  114. if (is_array($arg2)) {
  115. $arg2['foo'] = 'Drupal';
  116. }
  117. elseif (is_object($arg2)) {
  118. $arg2->foo = 'Drupal';
  119. }
  120. }
  121. // Try to alter third argument, if present.
  122. if (isset($arg3)) {
  123. if (is_array($arg3)) {
  124. $arg3['foo'] = 'Drupal';
  125. }
  126. elseif (is_object($arg3)) {
  127. $arg3->foo = 'Drupal';
  128. }
  129. }
  130. }
  131. /**
  132. * Implements hook_TYPE_alter() on behalf of Bartik theme.
  133. *
  134. * Same as common_test_drupal_alter_alter(), but here, we verify that themes
  135. * can also alter and come last.
  136. */
  137. function bartik_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  138. // Alter first argument.
  139. if (is_array($data)) {
  140. $data['foo'] .= ' theme';
  141. }
  142. elseif (is_object($data)) {
  143. $data->foo .= ' theme';
  144. }
  145. // Alter second argument, if present.
  146. if (isset($arg2)) {
  147. if (is_array($arg2)) {
  148. $arg2['foo'] .= ' theme';
  149. }
  150. elseif (is_object($arg2)) {
  151. $arg2->foo .= ' theme';
  152. }
  153. }
  154. // Try to alter third argument, if present.
  155. if (isset($arg3)) {
  156. if (is_array($arg3)) {
  157. $arg3['foo'] .= ' theme';
  158. }
  159. elseif (is_object($arg3)) {
  160. $arg3->foo .= ' theme';
  161. }
  162. }
  163. }
  164. /**
  165. * Implements hook_TYPE_alter() on behalf of block module.
  166. *
  167. * This is for verifying that drupal_alter(array(TYPE1, TYPE2), ...) allows
  168. * hook_module_implements_alter() to affect the order in which module
  169. * implementations are executed.
  170. */
  171. function block_drupal_alter_foo_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) {
  172. $data['foo'] .= ' block';
  173. }
  174. /**
  175. * Implements hook_module_implements_alter().
  176. *
  177. * @see block_drupal_alter_foo_alter()
  178. */
  179. function common_test_module_implements_alter(&$implementations, $hook) {
  180. // For drupal_alter(array('drupal_alter', 'drupal_alter_foo'), ...), make the
  181. // block module implementations run after all the other modules. Note that
  182. // when drupal_alter() is called with an array of types, the first type is
  183. // considered primary and controls the module order.
  184. if ($hook == 'drupal_alter_alter' && isset($implementations['block'])) {
  185. $group = $implementations['block'];
  186. unset($implementations['block']);
  187. $implementations['block'] = $group;
  188. }
  189. }
  190. /**
  191. * Implements hook_theme().
  192. */
  193. function common_test_theme() {
  194. return array(
  195. 'common_test_foo' => array(
  196. 'variables' => array('foo' => 'foo', 'bar' => 'bar'),
  197. ),
  198. );
  199. }
  200. /**
  201. * Theme function for testing drupal_render() theming.
  202. */
  203. function theme_common_test_foo($variables) {
  204. return $variables['foo'] . $variables['bar'];
  205. }
  206. /**
  207. * Implements hook_library_alter().
  208. */
  209. function common_test_library_alter(&$libraries, $module) {
  210. if ($module == 'system' && isset($libraries['farbtastic'])) {
  211. // Change the title of Farbtastic to "Farbtastic: Altered Library".
  212. $libraries['farbtastic']['title'] = 'Farbtastic: Altered Library';
  213. // Make Farbtastic depend on jQuery Form to test library dependencies.
  214. $libraries['farbtastic']['dependencies'][] = array('system', 'form');
  215. }
  216. }
  217. /**
  218. * Implements hook_library().
  219. *
  220. * Adds Farbtastic in a different version.
  221. */
  222. function common_test_library() {
  223. $libraries['farbtastic'] = array(
  224. 'title' => 'Custom Farbtastic Library',
  225. 'website' => 'http://code.google.com/p/farbtastic/',
  226. 'version' => '5.3',
  227. 'js' => array(
  228. 'misc/farbtastic/farbtastic.js' => array(),
  229. ),
  230. 'css' => array(
  231. 'misc/farbtastic/farbtastic.css' => array(),
  232. ),
  233. );
  234. return $libraries;
  235. }
  236. /**
  237. * Adds a JavaScript file and a CSS file with a query string appended.
  238. */
  239. function common_test_js_and_css_querystring() {
  240. drupal_add_js(drupal_get_path('module', 'node') . '/node.js');
  241. drupal_add_css(drupal_get_path('module', 'node') . '/node.css');
  242. // A relative URI may have a query string.
  243. drupal_add_css('/' . drupal_get_path('module', 'node') . '/node-fake.css?arg1=value1&arg2=value2');
  244. return '';
  245. }
  246. /**
  247. * Implements hook_cron().
  248. *
  249. * System module should handle if a module does not catch an exception and keep
  250. * cron going.
  251. *
  252. * @see common_test_cron_helper()
  253. *
  254. */
  255. function common_test_cron() {
  256. throw new Exception(t('Uncaught exception'));
  257. }