system_test.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. $items['system-test/drupal-get-filename'] = array(
  119. 'title' => 'Test drupal_get_filename()',
  120. 'page callback' => 'system_test_drupal_get_filename',
  121. 'access callback' => TRUE,
  122. 'type' => MENU_CALLBACK,
  123. );
  124. $items['system-test/drupal-get-filename-with-schema-rebuild'] = array(
  125. 'title' => 'Test drupal_get_filename() with a schema rebuild',
  126. 'page callback' => 'system_test_drupal_get_filename_with_schema_rebuild',
  127. 'access callback' => TRUE,
  128. 'type' => MENU_CALLBACK,
  129. );
  130. return $items;
  131. }
  132. function system_test_sleep($seconds) {
  133. sleep($seconds);
  134. }
  135. function system_test_basic_auth_page() {
  136. // The Authorization HTTP header is forwarded via Drupal's .htaccess file even
  137. // for PHP CGI SAPIs.
  138. if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
  139. $authorization_header = $_SERVER['HTTP_AUTHORIZATION'];
  140. }
  141. // If using CGI on Apache with mod_rewrite, the forwarded HTTP header appears
  142. // in the redirected HTTP headers. See
  143. // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/ServerBag.php#L61
  144. elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
  145. $authorization_header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
  146. }
  147. // Resemble PHP_AUTH_USER and PHP_AUTH_PW for a Basic authentication from
  148. // the HTTP_AUTHORIZATION header. See
  149. // http://www.php.net/manual/features.http-auth.php
  150. list($user, $pw) = explode(':', base64_decode(substr($authorization_header, 6)));
  151. $output = t('Username is @username.', array('@username' => $user));
  152. $output .= t('Password is @password.', array('@password' => $pw));
  153. return $output;
  154. }
  155. function system_test_redirect($code) {
  156. $code = (int) $code;
  157. if ($code != 200) {
  158. // Header names are case-insensitive.
  159. header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
  160. exit;
  161. }
  162. return '';
  163. }
  164. /**
  165. * Menu callback; sends a redirect header to itself until $count argument is 0.
  166. *
  167. * Emulates the variable number of redirects (given by initial $count argument)
  168. * to the final destination URL by continuous sending of 301 HTTP redirect
  169. * headers to itself together with decrementing the $count parameter until the
  170. * $count parameter reaches 0. After that it returns an empty string to render
  171. * the final destination page.
  172. *
  173. * @param $count
  174. * The count of redirects left until the final destination page.
  175. *
  176. * @returns
  177. * The location redirect if the $count > 0, otherwise an empty string.
  178. */
  179. function system_test_multiple_redirects($count) {
  180. $count = (int) $count;
  181. if ($count > 0) {
  182. header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
  183. exit;
  184. }
  185. return '';
  186. }
  187. function system_test_set_header() {
  188. drupal_add_http_header($_GET['name'], $_GET['value']);
  189. return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
  190. }
  191. function system_test_redirect_noscheme() {
  192. header("Location: localhost/path", TRUE, 301);
  193. exit;
  194. }
  195. function system_test_redirect_noparse() {
  196. header("Location: http:///path", TRUE, 301);
  197. exit;
  198. }
  199. function system_test_redirect_invalid_scheme() {
  200. header("Location: ftp://localhost/path", TRUE, 301);
  201. exit;
  202. }
  203. /**
  204. * Implements hook_modules_installed().
  205. */
  206. function system_test_modules_installed($modules) {
  207. if (variable_get('test_verbose_module_hooks')) {
  208. foreach ($modules as $module) {
  209. drupal_set_message(t('hook_modules_installed fired for @module', array('@module' => $module)));
  210. }
  211. }
  212. }
  213. /**
  214. * Implements hook_modules_enabled().
  215. */
  216. function system_test_modules_enabled($modules) {
  217. if (variable_get('test_verbose_module_hooks')) {
  218. foreach ($modules as $module) {
  219. drupal_set_message(t('hook_modules_enabled fired for @module', array('@module' => $module)));
  220. }
  221. }
  222. }
  223. /**
  224. * Implements hook_modules_disabled().
  225. */
  226. function system_test_modules_disabled($modules) {
  227. if (variable_get('test_verbose_module_hooks')) {
  228. foreach ($modules as $module) {
  229. drupal_set_message(t('hook_modules_disabled fired for @module', array('@module' => $module)));
  230. }
  231. }
  232. }
  233. /**
  234. * Implements hook_modules_uninstalled().
  235. */
  236. function system_test_modules_uninstalled($modules) {
  237. if (variable_get('test_verbose_module_hooks')) {
  238. foreach ($modules as $module) {
  239. drupal_set_message(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
  240. }
  241. }
  242. }
  243. /**
  244. * Implements hook_boot().
  245. */
  246. function system_test_boot() {
  247. watchdog('system_test', 'hook_boot');
  248. }
  249. /**
  250. * Implements hook_init().
  251. */
  252. function system_test_init() {
  253. // Used by FrontPageTestCase to get the results of drupal_is_front_page().
  254. if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
  255. drupal_set_message(t('On front page.'));
  256. }
  257. }
  258. /**
  259. * Implements hook_exit().
  260. */
  261. function system_test_exit() {
  262. watchdog('system_test', 'hook_exit');
  263. }
  264. /**
  265. * Implements hook_system_info_alter().
  266. */
  267. function system_test_system_info_alter(&$info, $file, $type) {
  268. // We need a static otherwise the last test will fail to alter common_test.
  269. static $test;
  270. if (($dependencies = variable_get('dependencies', array())) || $test) {
  271. if ($file->name == 'module_test') {
  272. $info['hidden'] = FALSE;
  273. $info['dependencies'][] = array_shift($dependencies);
  274. variable_set('dependencies', $dependencies);
  275. $test = TRUE;
  276. }
  277. if ($file->name == 'common_test') {
  278. $info['hidden'] = FALSE;
  279. $info['version'] = '7.x-2.4-beta3';
  280. }
  281. }
  282. if ($file->name == 'system_project_namespace_test') {
  283. $info['hidden'] = FALSE;
  284. }
  285. // Make the system_dependencies_test visible by default.
  286. if ($file->name == 'system_dependencies_test') {
  287. $info['hidden'] = FALSE;
  288. }
  289. if (in_array($file->name, array(
  290. 'system_incompatible_module_version_dependencies_test',
  291. 'system_incompatible_core_version_dependencies_test',
  292. 'system_incompatible_module_version_test',
  293. 'system_incompatible_core_version_test',
  294. ))) {
  295. $info['hidden'] = FALSE;
  296. }
  297. if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
  298. $info['hidden'] = FALSE;
  299. }
  300. }
  301. /**
  302. * Try to acquire a named lock and report the outcome.
  303. */
  304. function system_test_lock_acquire() {
  305. if (lock_acquire('system_test_lock_acquire')) {
  306. lock_release('system_test_lock_acquire');
  307. return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
  308. }
  309. else {
  310. return 'FALSE: Lock not acquired in system_test_lock_acquire()';
  311. }
  312. }
  313. /**
  314. * Try to acquire a specific lock, and then exit.
  315. */
  316. function system_test_lock_exit() {
  317. if (lock_acquire('system_test_lock_exit', 900)) {
  318. echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
  319. // The shut-down function should release the lock.
  320. exit();
  321. }
  322. else {
  323. return 'FALSE: Lock not acquired in system_test_lock_exit()';
  324. }
  325. }
  326. /**
  327. * Implements hook_page_build().
  328. */
  329. function system_test_page_build(&$page) {
  330. $menu_item = menu_get_item();
  331. $main_content_display = &drupal_static('system_main_content_added', FALSE);
  332. if ($menu_item['path'] == 'system-test/main-content-handling') {
  333. $page['footer'] = drupal_set_page_content();
  334. $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
  335. }
  336. elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
  337. drupal_set_page_content();
  338. $main_content_display = FALSE;
  339. }
  340. elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
  341. drupal_set_page_content();
  342. }
  343. }
  344. /**
  345. * Menu callback to test main content fallback().
  346. */
  347. function system_test_main_content_fallback() {
  348. return t('Content to test main content fallback');
  349. }
  350. /**
  351. * A simple page callback which adds a register shutdown function.
  352. */
  353. function system_test_page_shutdown_functions($arg1, $arg2) {
  354. drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
  355. }
  356. /**
  357. * Dummy shutdown function which registers another shutdown function.
  358. */
  359. function _system_test_first_shutdown_function($arg1, $arg2) {
  360. // Output something, page has already been printed and the session stored
  361. // so we can't use drupal_set_message.
  362. print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  363. drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
  364. }
  365. /**
  366. * Dummy shutdown function.
  367. */
  368. function _system_test_second_shutdown_function($arg1, $arg2) {
  369. // Output something, page has already been printed and the session stored
  370. // so we can't use drupal_set_message.
  371. print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
  372. // Throw an exception with an HTML tag. Since this is called in a shutdown
  373. // function, it will not bubble up to the default exception handler but will
  374. // be caught in _drupal_shutdown_function() and be displayed through
  375. // _drupal_render_exception_safe().
  376. throw new Exception('Drupal is <blink>awesome</blink>.');
  377. }
  378. /**
  379. * Implements hook_filetransfer_info().
  380. */
  381. function system_test_filetransfer_info() {
  382. return array(
  383. 'system_test' => array(
  384. 'title' => t('System Test FileTransfer'),
  385. 'file' => 'system_test.module', // Should be a .inc, but for test, ok.
  386. 'class' => 'SystemTestFileTransfer',
  387. 'weight' => -10,
  388. ),
  389. );
  390. }
  391. /**
  392. * Mock FileTransfer object to test the settings form functionality.
  393. */
  394. class SystemTestFileTransfer {
  395. public static function factory() {
  396. return new SystemTestFileTransfer;
  397. }
  398. public function getSettingsForm() {
  399. $form = array();
  400. $form['system_test_username'] = array(
  401. '#type' => 'textfield',
  402. '#title' => t('System Test Username'),
  403. );
  404. return $form;
  405. }
  406. }
  407. /**
  408. * Page callback to initialize authorize.php during testing.
  409. *
  410. * @see system_authorized_init().
  411. */
  412. function system_test_authorize_init_page($page_title) {
  413. $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
  414. system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
  415. drupal_goto($authorize_url);
  416. }
  417. /**
  418. * Sets two messages and removes the first one before the messages are displayed.
  419. */
  420. function system_test_drupal_set_message() {
  421. // Set two messages.
  422. drupal_set_message('First message (removed).');
  423. drupal_set_message('Second message (not removed).');
  424. // Remove the first.
  425. unset($_SESSION['messages']['status'][0]);
  426. return '';
  427. }
  428. /**
  429. * Page callback to print out $_GET['destination'] for testing.
  430. */
  431. function system_test_get_destination() {
  432. if (isset($_GET['destination'])) {
  433. print $_GET['destination'];
  434. }
  435. // No need to render the whole page, we are just interested in this bit of
  436. // information.
  437. exit;
  438. }
  439. /**
  440. * Page callback to print out $_REQUEST['destination'] for testing.
  441. */
  442. function system_test_request_destination() {
  443. if (isset($_REQUEST['destination'])) {
  444. print $_REQUEST['destination'];
  445. }
  446. // No need to render the whole page, we are just interested in this bit of
  447. // information.
  448. exit;
  449. }
  450. /**
  451. * Page callback to run drupal_get_filename() on a particular module.
  452. */
  453. function system_test_drupal_get_filename() {
  454. // Prevent SimpleTest from failing as a result of the expected PHP warnings
  455. // this function causes. Any warnings will be recorded in the database logs
  456. // for examination by the tests.
  457. define('SIMPLETEST_COLLECT_ERRORS', FALSE);
  458. $module_name = variable_get('system_test_drupal_get_filename_test_module_name');
  459. drupal_get_filename('module', $module_name);
  460. return '';
  461. }
  462. /**
  463. * Page callback to run drupal_get_filename() and do a schema rebuild.
  464. */
  465. function system_test_drupal_get_filename_with_schema_rebuild() {
  466. // Prevent SimpleTest from failing as a result of the expected PHP warnings
  467. // this function causes.
  468. define('SIMPLETEST_COLLECT_ERRORS', FALSE);
  469. // Record the original database tables from drupal_get_schema().
  470. variable_set('system_test_drupal_get_filename_with_schema_rebuild_original_tables', array_keys(drupal_get_schema(NULL, TRUE)));
  471. // Trigger system_test_schema() and system_test_watchdog() to perform an
  472. // attempted recursive rebuild when drupal_get_schema() is called. See
  473. // BootstrapGetFilenameWebTestCase::testRecursiveRebuilds().
  474. variable_set('system_test_drupal_get_filename_attempt_recursive_rebuild', TRUE);
  475. drupal_get_schema(NULL, TRUE);
  476. return '';
  477. }
  478. /**
  479. * Implements hook_watchdog().
  480. */
  481. function system_test_watchdog($log_entry) {
  482. // If an attempted recursive schema rebuild has been triggered by
  483. // system_test_drupal_get_filename_with_schema_rebuild(), perform the rebuild
  484. // in response to the missing file message triggered by system_test_schema().
  485. if (!variable_get('system_test_drupal_get_filename_attempt_recursive_rebuild')) {
  486. return;
  487. }
  488. if ($log_entry['type'] != 'php' || $log_entry['severity'] != WATCHDOG_WARNING) {
  489. return;
  490. }
  491. $module_name = variable_get('system_test_drupal_get_filename_test_module_name');
  492. if (!isset($log_entry['variables']['!message']) || strpos($log_entry['variables']['!message'], format_string('The following module is missing from the file system: %name', array('%name' => $module_name))) === FALSE) {
  493. return;
  494. }
  495. variable_set('system_test_drupal_get_filename_with_schema_rebuild_final_tables', array_keys(drupal_get_schema()));
  496. }
  497. /**
  498. * Implements hook_module_implements_alter().
  499. */
  500. function system_test_module_implements_alter(&$implementations, $hook) {
  501. // For BootstrapGetFilenameWebTestCase::testRecursiveRebuilds() to work
  502. // correctly, this module's hook_schema() implementation cannot be either the
  503. // first implementation (since that would trigger a potential recursive
  504. // rebuild before anything is in the drupal_get_schema() cache) or the last
  505. // implementation (since that would trigger a potential recursive rebuild
  506. // after the cache is already complete). So put it somewhere in the middle.
  507. if ($hook == 'schema') {
  508. $group = $implementations['system_test'];
  509. unset($implementations['system_test']);
  510. $count = count($implementations);
  511. $implementations = array_merge(array_slice($implementations, 0, $count / 2, TRUE), array('system_test' => $group), array_slice($implementations, $count / 2, NULL, TRUE));
  512. }
  513. }