system_test.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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/drupal-set-message'] = array(
  77. 'title' => 'Set messages with drupal_set_message()',
  78. 'page callback' => 'system_test_drupal_set_message',
  79. 'access callback' => TRUE,
  80. 'type' => MENU_CALLBACK,
  81. );
  82. $items['system-test/main-content-handling'] = array(
  83. 'title' => 'Test main content handling',
  84. 'page callback' => 'system_test_main_content_fallback',
  85. 'access callback' => TRUE,
  86. 'type' => MENU_CALLBACK,
  87. );
  88. $items['system-test/main-content-fallback'] = array(
  89. 'title' => 'Test main content fallback',
  90. 'page callback' => 'system_test_main_content_fallback',
  91. 'access callback' => TRUE,
  92. 'type' => MENU_CALLBACK,
  93. );
  94. $items['system-test/main-content-duplication'] = array(
  95. 'title' => 'Test main content duplication',
  96. 'page callback' => 'system_test_main_content_fallback',
  97. 'access callback' => TRUE,
  98. 'type' => MENU_CALLBACK,
  99. );
  100. $items['system-test/shutdown-functions'] = array(
  101. 'title' => 'Test main content duplication',
  102. 'page callback' => 'system_test_page_shutdown_functions',
  103. 'access callback' => TRUE,
  104. 'type' => MENU_CALLBACK,
  105. );
  106. $items['system-test/get-destination'] = array(
  107. 'title' => 'Test $_GET[\'destination\']',
  108. 'page callback' => 'system_test_get_destination',
  109. 'access callback' => TRUE,
  110. 'type' => MENU_CALLBACK,
  111. );
  112. $items['system-test/request-destination'] = array(
  113. 'title' => 'Test $_REQUEST[\'destination\']',
  114. 'page callback' => 'system_test_request_destination',
  115. 'access callback' => TRUE,
  116. 'type' => MENU_CALLBACK,
  117. );
  118. return $items;
  119. }
  120. function system_test_sleep($seconds) {
  121. sleep($seconds);
  122. }
  123. function system_test_basic_auth_page() {
  124. // The Authorization HTTP header is forwarded via Drupal's .htaccess file even
  125. // for PHP CGI SAPIs.
  126. if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
  127. $authorization_header = $_SERVER['HTTP_AUTHORIZATION'];
  128. }
  129. // If using CGI on Apache with mod_rewrite, the forwarded HTTP header appears
  130. // in the redirected HTTP headers. See
  131. // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/ServerBag.php#L61
  132. elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
  133. $authorization_header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
  134. }
  135. // Resemble PHP_AUTH_USER and PHP_AUTH_PW for a Basic authentication from
  136. // the HTTP_AUTHORIZATION header. See
  137. // http://www.php.net/manual/features.http-auth.php
  138. list($user, $pw) = explode(':', base64_decode(substr($authorization_header, 6)));
  139. $output = t('Username is @username.', array('@username' => $user));
  140. $output .= t('Password is @password.', array('@password' => $pw));
  141. return $output;
  142. }
  143. function system_test_redirect($code) {
  144. $code = (int) $code;
  145. if ($code != 200) {
  146. // Header names are case-insensitive.
  147. header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
  148. exit;
  149. }
  150. return '';
  151. }
  152. /**
  153. * Menu callback; sends a redirect header to itself until $count argument is 0.
  154. *
  155. * Emulates the variable number of redirects (given by initial $count argument)
  156. * to the final destination URL by continuous sending of 301 HTTP redirect
  157. * headers to itself together with decrementing the $count parameter until the
  158. * $count parameter reaches 0. After that it returns an empty string to render
  159. * the final destination page.
  160. *
  161. * @param $count
  162. * The count of redirects left until the final destination page.
  163. *
  164. * @returns
  165. * The location redirect if the $count > 0, otherwise an empty string.
  166. */
  167. function system_test_multiple_redirects($count) {
  168. $count = (int) $count;
  169. if ($count > 0) {
  170. header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
  171. exit;
  172. }
  173. return '';
  174. }
  175. function system_test_set_header() {
  176. drupal_add_http_header($_GET['name'], $_GET['value']);
  177. return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
  178. }
  179. function system_test_redirect_noscheme() {
  180. header("Location: localhost/path", TRUE, 301);
  181. exit;
  182. }
  183. function system_test_redirect_noparse() {
  184. header("Location: http:///path", TRUE, 301);
  185. exit;
  186. }
  187. function system_test_redirect_invalid_scheme() {
  188. header("Location: ftp://localhost/path", TRUE, 301);
  189. exit;
  190. }
  191. /**
  192. * Implements hook_modules_installed().
  193. */
  194. function system_test_modules_installed($modules) {
  195. if (variable_get('test_verbose_module_hooks')) {
  196. foreach ($modules as $module) {
  197. drupal_set_message(t('hook_modules_installed fired for @module', array('@module' => $module)));
  198. }
  199. }
  200. }
  201. /**
  202. * Implements hook_modules_enabled().
  203. */
  204. function system_test_modules_enabled($modules) {
  205. if (variable_get('test_verbose_module_hooks')) {
  206. foreach ($modules as $module) {
  207. drupal_set_message(t('hook_modules_enabled fired for @module', array('@module' => $module)));
  208. }
  209. }
  210. }
  211. /**
  212. * Implements hook_modules_disabled().
  213. */
  214. function system_test_modules_disabled($modules) {
  215. if (variable_get('test_verbose_module_hooks')) {
  216. foreach ($modules as $module) {
  217. drupal_set_message(t('hook_modules_disabled fired for @module', array('@module' => $module)));
  218. }
  219. }
  220. }
  221. /**
  222. * Implements hook_modules_uninstalled().
  223. */
  224. function system_test_modules_uninstalled($modules) {
  225. if (variable_get('test_verbose_module_hooks')) {
  226. foreach ($modules as $module) {
  227. drupal_set_message(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
  228. }
  229. }
  230. }
  231. /**
  232. * Implements hook_boot().
  233. */
  234. function system_test_boot() {
  235. watchdog('system_test', 'hook_boot');
  236. }
  237. /**
  238. * Implements hook_init().
  239. */
  240. function system_test_init() {
  241. // Used by FrontPageTestCase to get the results of drupal_is_front_page().
  242. if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
  243. drupal_set_message(t('On front page.'));
  244. }
  245. }
  246. /**
  247. * Implements hook_exit().
  248. */
  249. function system_test_exit() {
  250. watchdog('system_test', 'hook_exit');
  251. }
  252. /**
  253. * Implements hook_system_info_alter().
  254. */
  255. function system_test_system_info_alter(&$info, $file, $type) {
  256. // We need a static otherwise the last test will fail to alter common_test.
  257. static $test;
  258. if (($dependencies = variable_get('dependencies', array())) || $test) {
  259. if ($file->name == 'module_test') {
  260. $info['hidden'] = FALSE;
  261. $info['dependencies'][] = array_shift($dependencies);
  262. variable_set('dependencies', $dependencies);
  263. $test = TRUE;
  264. }
  265. if ($file->name == 'common_test') {
  266. $info['hidden'] = FALSE;
  267. $info['version'] = '7.x-2.4-beta3';
  268. }
  269. }
  270. if ($file->name == 'system_project_namespace_test') {
  271. $info['hidden'] = FALSE;
  272. }
  273. // Make the system_dependencies_test visible by default.
  274. if ($file->name == 'system_dependencies_test') {
  275. $info['hidden'] = FALSE;
  276. }
  277. if (in_array($file->name, array(
  278. 'system_incompatible_module_version_dependencies_test',
  279. 'system_incompatible_core_version_dependencies_test',
  280. 'system_incompatible_module_version_test',
  281. 'system_incompatible_core_version_test',
  282. ))) {
  283. $info['hidden'] = FALSE;
  284. }
  285. if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
  286. $info['hidden'] = FALSE;
  287. }
  288. }
  289. /**
  290. * Try to acquire a named lock and report the outcome.
  291. */
  292. function system_test_lock_acquire() {
  293. if (lock_acquire('system_test_lock_acquire')) {
  294. lock_release('system_test_lock_acquire');
  295. return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
  296. }
  297. else {
  298. return 'FALSE: Lock not acquired in system_test_lock_acquire()';
  299. }
  300. }
  301. /**
  302. * Try to acquire a specific lock, and then exit.
  303. */
  304. function system_test_lock_exit() {
  305. if (lock_acquire('system_test_lock_exit', 900)) {
  306. echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
  307. // The shut-down function should release the lock.
  308. exit();
  309. }
  310. else {
  311. return 'FALSE: Lock not acquired in system_test_lock_exit()';
  312. }
  313. }
  314. /**
  315. * Implements hook_page_build().
  316. */
  317. function system_test_page_build(&$page) {
  318. $menu_item = menu_get_item();
  319. $main_content_display = &drupal_static('system_main_content_added', FALSE);
  320. if ($menu_item['path'] == 'system-test/main-content-handling') {
  321. $page['footer'] = drupal_set_page_content();
  322. $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
  323. }
  324. elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
  325. drupal_set_page_content();
  326. $main_content_display = FALSE;
  327. }
  328. elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
  329. drupal_set_page_content();
  330. }
  331. }
  332. /**
  333. * Menu callback to test main content fallback().
  334. */
  335. function system_test_main_content_fallback() {
  336. return t('Content to test main content fallback');
  337. }
  338. /**
  339. * A simple page callback which adds a register shutdown function.
  340. */
  341. function system_test_page_shutdown_functions($arg1, $arg2) {
  342. drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
  343. }
  344. /**
  345. * Dummy shutdown function which registers another shutdown function.
  346. */
  347. function _system_test_first_shutdown_function($arg1, $arg2) {
  348. // Output something, page has already been printed and the session stored
  349. // so we can't use drupal_set_message.
  350. print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  351. drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
  352. }
  353. /**
  354. * Dummy shutdown function.
  355. */
  356. function _system_test_second_shutdown_function($arg1, $arg2) {
  357. // Output something, page has already been printed and the session stored
  358. // so we can't use drupal_set_message.
  359. print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  360. // Throw an exception with an HTML tag. Since this is called in a shutdown
  361. // function, it will not bubble up to the default exception handler but will
  362. // be caught in _drupal_shutdown_function() and be displayed through
  363. // _drupal_render_exception_safe().
  364. throw new Exception('Drupal is <blink>awesome</blink>.');
  365. }
  366. /**
  367. * Implements hook_filetransfer_info().
  368. */
  369. function system_test_filetransfer_info() {
  370. return array(
  371. 'system_test' => array(
  372. 'title' => t('System Test FileTransfer'),
  373. 'file' => 'system_test.module', // Should be a .inc, but for test, ok.
  374. 'class' => 'SystemTestFileTransfer',
  375. 'weight' => -10,
  376. ),
  377. );
  378. }
  379. /**
  380. * Mock FileTransfer object to test the settings form functionality.
  381. */
  382. class SystemTestFileTransfer {
  383. public static function factory() {
  384. return new SystemTestFileTransfer;
  385. }
  386. public function getSettingsForm() {
  387. $form = array();
  388. $form['system_test_username'] = array(
  389. '#type' => 'textfield',
  390. '#title' => t('System Test Username'),
  391. );
  392. return $form;
  393. }
  394. }
  395. /**
  396. * Page callback to initialize authorize.php during testing.
  397. *
  398. * @see system_authorized_init().
  399. */
  400. function system_test_authorize_init_page($page_title) {
  401. $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
  402. system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
  403. drupal_goto($authorize_url);
  404. }
  405. /**
  406. * Sets two messages and removes the first one before the messages are displayed.
  407. */
  408. function system_test_drupal_set_message() {
  409. // Set two messages.
  410. drupal_set_message('First message (removed).');
  411. drupal_set_message('Second message (not removed).');
  412. // Remove the first.
  413. unset($_SESSION['messages']['status'][0]);
  414. return '';
  415. }
  416. /**
  417. * Page callback to print out $_GET['destination'] for testing.
  418. */
  419. function system_test_get_destination() {
  420. if (isset($_GET['destination'])) {
  421. print $_GET['destination'];
  422. }
  423. // No need to render the whole page, we are just interested in this bit of
  424. // information.
  425. exit;
  426. }
  427. /**
  428. * Page callback to print out $_REQUEST['destination'] for testing.
  429. */
  430. function system_test_request_destination() {
  431. if (isset($_REQUEST['destination'])) {
  432. print $_REQUEST['destination'];
  433. }
  434. // No need to render the whole page, we are just interested in this bit of
  435. // information.
  436. exit;
  437. }