system_test.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function system_test_menu() {
  6. $items['system-test/sleep/%'] = array(
  7. 'page callback' => 'system_test_sleep',
  8. 'page arguments' => array(2),
  9. 'access callback' => TRUE,
  10. 'type' => MENU_CALLBACK,
  11. );
  12. $items['system-test/auth'] = array(
  13. 'page callback' => 'system_test_basic_auth_page',
  14. 'access callback' => TRUE,
  15. 'type' => MENU_CALLBACK,
  16. );
  17. $items['system-test/authorize-init/%'] = array(
  18. 'page callback' => 'system_test_authorize_init_page',
  19. 'page arguments' => array(2),
  20. 'access arguments' => array('administer software updates'),
  21. 'type' => MENU_CALLBACK,
  22. );
  23. $items['system-test/redirect/%'] = array(
  24. 'title' => 'Redirect',
  25. 'page callback' => 'system_test_redirect',
  26. 'page arguments' => array(2),
  27. 'access arguments' => array('access content'),
  28. 'type' => MENU_CALLBACK,
  29. );
  30. $items['system-test/multiple-redirects/%'] = array(
  31. 'title' => 'Redirect',
  32. 'page callback' => 'system_test_multiple_redirects',
  33. 'page arguments' => array(2),
  34. 'access arguments' => array('access content'),
  35. 'type' => MENU_CALLBACK,
  36. );
  37. $items['system-test/set-header'] = array(
  38. 'page callback' => 'system_test_set_header',
  39. 'access arguments' => array('access content'),
  40. 'type' => MENU_CALLBACK,
  41. );
  42. $items['system-test/redirect-noscheme'] = array(
  43. 'page callback' => 'system_test_redirect_noscheme',
  44. 'access arguments' => array('access content'),
  45. 'type' => MENU_CALLBACK,
  46. );
  47. $items['system-test/redirect-noparse'] = array(
  48. 'page callback' => 'system_test_redirect_noparse',
  49. 'access arguments' => array('access content'),
  50. 'type' => MENU_CALLBACK,
  51. );
  52. $items['system-test/redirect-invalid-scheme'] = array(
  53. 'page callback' => 'system_test_redirect_invalid_scheme',
  54. 'access arguments' => array('access content'),
  55. 'type' => MENU_CALLBACK,
  56. );
  57. $items['system-test/variable-get'] = array(
  58. 'title' => 'Variable Get',
  59. 'page callback' => 'variable_get',
  60. 'page arguments' => array('simpletest_bootstrap_variable_test', NULL),
  61. 'access arguments' => array('access content'),
  62. 'type' => MENU_CALLBACK,
  63. );
  64. $items['system-test/lock-acquire'] = array(
  65. 'title' => 'Lock acquire',
  66. 'page callback' => 'system_test_lock_acquire',
  67. 'access callback' => TRUE,
  68. 'type' => MENU_CALLBACK,
  69. );
  70. $items['system-test/lock-exit'] = array(
  71. 'title' => 'Lock acquire then exit',
  72. 'page callback' => 'system_test_lock_exit',
  73. 'access callback' => TRUE,
  74. 'type' => MENU_CALLBACK,
  75. );
  76. $items['system-test/main-content-handling'] = array(
  77. 'title' => 'Test main content handling',
  78. 'page callback' => 'system_test_main_content_fallback',
  79. 'access callback' => TRUE,
  80. 'type' => MENU_CALLBACK,
  81. );
  82. $items['system-test/main-content-fallback'] = array(
  83. 'title' => 'Test main content fallback',
  84. 'page callback' => 'system_test_main_content_fallback',
  85. 'access callback' => TRUE,
  86. 'type' => MENU_CALLBACK,
  87. );
  88. $items['system-test/main-content-duplication'] = array(
  89. 'title' => 'Test main content duplication',
  90. 'page callback' => 'system_test_main_content_fallback',
  91. 'access callback' => TRUE,
  92. 'type' => MENU_CALLBACK,
  93. );
  94. $items['system-test/shutdown-functions'] = array(
  95. 'title' => 'Test main content duplication',
  96. 'page callback' => 'system_test_page_shutdown_functions',
  97. 'access callback' => TRUE,
  98. 'type' => MENU_CALLBACK,
  99. );
  100. return $items;
  101. }
  102. function system_test_sleep($seconds) {
  103. sleep($seconds);
  104. }
  105. function system_test_basic_auth_page() {
  106. $output = t('$_SERVER[\'PHP_AUTH_USER\'] is @username.', array('@username' => $_SERVER['PHP_AUTH_USER']));
  107. $output .= t('$_SERVER[\'PHP_AUTH_PW\'] is @password.', array('@password' => $_SERVER['PHP_AUTH_PW']));
  108. return $output;
  109. }
  110. function system_test_redirect($code) {
  111. $code = (int) $code;
  112. if ($code != 200) {
  113. // Header names are case-insensitive.
  114. header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
  115. exit;
  116. }
  117. return '';
  118. }
  119. /**
  120. * Menu callback; sends a redirect header to itself until $count argument is 0.
  121. *
  122. * Emulates the variable number of redirects (given by initial $count argument)
  123. * to the final destination URL by continuous sending of 301 HTTP redirect
  124. * headers to itself together with decrementing the $count parameter until the
  125. * $count parameter reaches 0. After that it returns an empty string to render
  126. * the final destination page.
  127. *
  128. * @param $count
  129. * The count of redirects left until the final destination page.
  130. *
  131. * @returns
  132. * The location redirect if the $count > 0, otherwise an empty string.
  133. */
  134. function system_test_multiple_redirects($count) {
  135. $count = (int) $count;
  136. if ($count > 0) {
  137. header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
  138. exit;
  139. }
  140. return '';
  141. }
  142. function system_test_set_header() {
  143. drupal_add_http_header($_GET['name'], $_GET['value']);
  144. return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
  145. }
  146. function system_test_redirect_noscheme() {
  147. header("Location: localhost/path", TRUE, 301);
  148. exit;
  149. }
  150. function system_test_redirect_noparse() {
  151. header("Location: http:///path", TRUE, 301);
  152. exit;
  153. }
  154. function system_test_redirect_invalid_scheme() {
  155. header("Location: ftp://localhost/path", TRUE, 301);
  156. exit;
  157. }
  158. /**
  159. * Implements hook_modules_installed().
  160. */
  161. function system_test_modules_installed($modules) {
  162. if (variable_get('test_verbose_module_hooks')) {
  163. foreach ($modules as $module) {
  164. drupal_set_message(t('hook_modules_installed fired for @module', array('@module' => $module)));
  165. }
  166. }
  167. }
  168. /**
  169. * Implements hook_modules_enabled().
  170. */
  171. function system_test_modules_enabled($modules) {
  172. if (variable_get('test_verbose_module_hooks')) {
  173. foreach ($modules as $module) {
  174. drupal_set_message(t('hook_modules_enabled fired for @module', array('@module' => $module)));
  175. }
  176. }
  177. }
  178. /**
  179. * Implements hook_modules_disabled().
  180. */
  181. function system_test_modules_disabled($modules) {
  182. if (variable_get('test_verbose_module_hooks')) {
  183. foreach ($modules as $module) {
  184. drupal_set_message(t('hook_modules_disabled fired for @module', array('@module' => $module)));
  185. }
  186. }
  187. }
  188. /**
  189. * Implements hook_modules_uninstalled().
  190. */
  191. function system_test_modules_uninstalled($modules) {
  192. if (variable_get('test_verbose_module_hooks')) {
  193. foreach ($modules as $module) {
  194. drupal_set_message(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
  195. }
  196. }
  197. }
  198. /**
  199. * Implements hook_boot().
  200. */
  201. function system_test_boot() {
  202. watchdog('system_test', 'hook_boot');
  203. }
  204. /**
  205. * Implements hook_init().
  206. */
  207. function system_test_init() {
  208. // Used by FrontPageTestCase to get the results of drupal_is_front_page().
  209. if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
  210. drupal_set_message(t('On front page.'));
  211. }
  212. }
  213. /**
  214. * Implements hook_exit().
  215. */
  216. function system_test_exit() {
  217. watchdog('system_test', 'hook_exit');
  218. }
  219. /**
  220. * Implements hook_system_info_alter().
  221. */
  222. function system_test_system_info_alter(&$info, $file, $type) {
  223. // We need a static otherwise the last test will fail to alter common_test.
  224. static $test;
  225. if (($dependencies = variable_get('dependencies', array())) || $test) {
  226. if ($file->name == 'module_test') {
  227. $info['hidden'] = FALSE;
  228. $info['dependencies'][] = array_shift($dependencies);
  229. variable_set('dependencies', $dependencies);
  230. $test = TRUE;
  231. }
  232. if ($file->name == 'common_test') {
  233. $info['hidden'] = FALSE;
  234. $info['version'] = '7.x-2.4-beta3';
  235. }
  236. }
  237. // Make the system_dependencies_test visible by default.
  238. if ($file->name == 'system_dependencies_test') {
  239. $info['hidden'] = FALSE;
  240. }
  241. if (in_array($file->name, array(
  242. 'system_incompatible_module_version_dependencies_test',
  243. 'system_incompatible_core_version_dependencies_test',
  244. 'system_incompatible_module_version_test',
  245. 'system_incompatible_core_version_test',
  246. ))) {
  247. $info['hidden'] = FALSE;
  248. }
  249. if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
  250. $info['hidden'] = FALSE;
  251. }
  252. }
  253. /**
  254. * Try to acquire a named lock and report the outcome.
  255. */
  256. function system_test_lock_acquire() {
  257. if (lock_acquire('system_test_lock_acquire')) {
  258. lock_release('system_test_lock_acquire');
  259. return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
  260. }
  261. else {
  262. return 'FALSE: Lock not acquired in system_test_lock_acquire()';
  263. }
  264. }
  265. /**
  266. * Try to acquire a specific lock, and then exit.
  267. */
  268. function system_test_lock_exit() {
  269. if (lock_acquire('system_test_lock_exit', 900)) {
  270. echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
  271. // The shut-down function should release the lock.
  272. exit();
  273. }
  274. else {
  275. return 'FALSE: Lock not acquired in system_test_lock_exit()';
  276. }
  277. }
  278. /**
  279. * Implements hook_page_build().
  280. */
  281. function system_test_page_build(&$page) {
  282. $menu_item = menu_get_item();
  283. $main_content_display = &drupal_static('system_main_content_added', FALSE);
  284. if ($menu_item['path'] == 'system-test/main-content-handling') {
  285. $page['footer'] = drupal_set_page_content();
  286. $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
  287. }
  288. elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
  289. drupal_set_page_content();
  290. $main_content_display = FALSE;
  291. }
  292. elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
  293. drupal_set_page_content();
  294. }
  295. }
  296. /**
  297. * Menu callback to test main content fallback().
  298. */
  299. function system_test_main_content_fallback() {
  300. return t('Content to test main content fallback');
  301. }
  302. /**
  303. * A simple page callback which adds a register shutdown function.
  304. */
  305. function system_test_page_shutdown_functions($arg1, $arg2) {
  306. drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
  307. }
  308. /**
  309. * Dummy shutdown function which registers another shutdown function.
  310. */
  311. function _system_test_first_shutdown_function($arg1, $arg2) {
  312. // Output something, page has already been printed and the session stored
  313. // so we can't use drupal_set_message.
  314. print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  315. drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
  316. }
  317. /**
  318. * Dummy shutdown function.
  319. */
  320. function _system_test_second_shutdown_function($arg1, $arg2) {
  321. // Output something, page has already been printed and the session stored
  322. // so we can't use drupal_set_message.
  323. print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  324. // Throw an exception with an HTML tag. Since this is called in a shutdown
  325. // function, it will not bubble up to the default exception handler but will
  326. // be caught in _drupal_shutdown_function() and be displayed through
  327. // _drupal_render_exception_safe().
  328. throw new Exception('Drupal is <blink>awesome</blink>.');
  329. }
  330. /**
  331. * Implements hook_filetransfer_info().
  332. */
  333. function system_test_filetransfer_info() {
  334. return array(
  335. 'system_test' => array(
  336. 'title' => t('System Test FileTransfer'),
  337. 'file' => 'system_test.module', // Should be a .inc, but for test, ok.
  338. 'class' => 'SystemTestFileTransfer',
  339. 'weight' => -10,
  340. ),
  341. );
  342. }
  343. /**
  344. * Mock FileTransfer object to test the settings form functionality.
  345. */
  346. class SystemTestFileTransfer {
  347. public static function factory() {
  348. return new SystemTestFileTransfer;
  349. }
  350. public function getSettingsForm() {
  351. $form = array();
  352. $form['system_test_username'] = array(
  353. '#type' => 'textfield',
  354. '#title' => t('System Test Username'),
  355. );
  356. return $form;
  357. }
  358. }
  359. /**
  360. * Page callback to initialize authorize.php during testing.
  361. *
  362. * @see system_authorized_init().
  363. */
  364. function system_test_authorize_init_page($page_title) {
  365. $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
  366. system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
  367. drupal_goto($authorize_url);
  368. }