system_test.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. // Make the system_dependencies_test visible by default.
  271. if ($file->name == 'system_dependencies_test') {
  272. $info['hidden'] = FALSE;
  273. }
  274. if (in_array($file->name, array(
  275. 'system_incompatible_module_version_dependencies_test',
  276. 'system_incompatible_core_version_dependencies_test',
  277. 'system_incompatible_module_version_test',
  278. 'system_incompatible_core_version_test',
  279. ))) {
  280. $info['hidden'] = FALSE;
  281. }
  282. if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
  283. $info['hidden'] = FALSE;
  284. }
  285. }
  286. /**
  287. * Try to acquire a named lock and report the outcome.
  288. */
  289. function system_test_lock_acquire() {
  290. if (lock_acquire('system_test_lock_acquire')) {
  291. lock_release('system_test_lock_acquire');
  292. return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
  293. }
  294. else {
  295. return 'FALSE: Lock not acquired in system_test_lock_acquire()';
  296. }
  297. }
  298. /**
  299. * Try to acquire a specific lock, and then exit.
  300. */
  301. function system_test_lock_exit() {
  302. if (lock_acquire('system_test_lock_exit', 900)) {
  303. echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
  304. // The shut-down function should release the lock.
  305. exit();
  306. }
  307. else {
  308. return 'FALSE: Lock not acquired in system_test_lock_exit()';
  309. }
  310. }
  311. /**
  312. * Implements hook_page_build().
  313. */
  314. function system_test_page_build(&$page) {
  315. $menu_item = menu_get_item();
  316. $main_content_display = &drupal_static('system_main_content_added', FALSE);
  317. if ($menu_item['path'] == 'system-test/main-content-handling') {
  318. $page['footer'] = drupal_set_page_content();
  319. $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
  320. }
  321. elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
  322. drupal_set_page_content();
  323. $main_content_display = FALSE;
  324. }
  325. elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
  326. drupal_set_page_content();
  327. }
  328. }
  329. /**
  330. * Menu callback to test main content fallback().
  331. */
  332. function system_test_main_content_fallback() {
  333. return t('Content to test main content fallback');
  334. }
  335. /**
  336. * A simple page callback which adds a register shutdown function.
  337. */
  338. function system_test_page_shutdown_functions($arg1, $arg2) {
  339. drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
  340. }
  341. /**
  342. * Dummy shutdown function which registers another shutdown function.
  343. */
  344. function _system_test_first_shutdown_function($arg1, $arg2) {
  345. // Output something, page has already been printed and the session stored
  346. // so we can't use drupal_set_message.
  347. print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  348. drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
  349. }
  350. /**
  351. * Dummy shutdown function.
  352. */
  353. function _system_test_second_shutdown_function($arg1, $arg2) {
  354. // Output something, page has already been printed and the session stored
  355. // so we can't use drupal_set_message.
  356. print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  357. // Throw an exception with an HTML tag. Since this is called in a shutdown
  358. // function, it will not bubble up to the default exception handler but will
  359. // be caught in _drupal_shutdown_function() and be displayed through
  360. // _drupal_render_exception_safe().
  361. throw new Exception('Drupal is <blink>awesome</blink>.');
  362. }
  363. /**
  364. * Implements hook_filetransfer_info().
  365. */
  366. function system_test_filetransfer_info() {
  367. return array(
  368. 'system_test' => array(
  369. 'title' => t('System Test FileTransfer'),
  370. 'file' => 'system_test.module', // Should be a .inc, but for test, ok.
  371. 'class' => 'SystemTestFileTransfer',
  372. 'weight' => -10,
  373. ),
  374. );
  375. }
  376. /**
  377. * Mock FileTransfer object to test the settings form functionality.
  378. */
  379. class SystemTestFileTransfer {
  380. public static function factory() {
  381. return new SystemTestFileTransfer;
  382. }
  383. public function getSettingsForm() {
  384. $form = array();
  385. $form['system_test_username'] = array(
  386. '#type' => 'textfield',
  387. '#title' => t('System Test Username'),
  388. );
  389. return $form;
  390. }
  391. }
  392. /**
  393. * Page callback to initialize authorize.php during testing.
  394. *
  395. * @see system_authorized_init().
  396. */
  397. function system_test_authorize_init_page($page_title) {
  398. $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
  399. system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
  400. drupal_goto($authorize_url);
  401. }
  402. /**
  403. * Sets two messages and removes the first one before the messages are displayed.
  404. */
  405. function system_test_drupal_set_message() {
  406. // Set two messages.
  407. drupal_set_message('First message (removed).');
  408. drupal_set_message('Second message (not removed).');
  409. // Remove the first.
  410. unset($_SESSION['messages']['status'][0]);
  411. return '';
  412. }
  413. /**
  414. * Page callback to print out $_GET['destination'] for testing.
  415. */
  416. function system_test_get_destination() {
  417. if (isset($_GET['destination'])) {
  418. print $_GET['destination'];
  419. }
  420. // No need to render the whole page, we are just interested in this bit of
  421. // information.
  422. exit;
  423. }
  424. /**
  425. * Page callback to print out $_REQUEST['destination'] for testing.
  426. */
  427. function system_test_request_destination() {
  428. if (isset($_REQUEST['destination'])) {
  429. print $_REQUEST['destination'];
  430. }
  431. // No need to render the whole page, we are just interested in this bit of
  432. // information.
  433. exit;
  434. }