1. 7 modules/simpletest/tests/system_test.module
  2. 8 core/modules/system/tests/modules/system_test/system_test.module

File

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

Functions

Namesort descending Description
system_test_authorize_init_page Page callback to initialize authorize.php during testing.
system_test_basic_auth_page
system_test_boot Implements hook_boot().
system_test_exit Implements hook_exit().
system_test_filetransfer_info Implements hook_filetransfer_info().
system_test_get_destination Page callback to print out $_GET['destination'] for testing.
system_test_init Implements hook_init().
system_test_lock_acquire Try to acquire a named lock and report the outcome.
system_test_lock_exit Try to acquire a specific lock, and then exit.
system_test_main_content_fallback Menu callback to test main content fallback().
system_test_menu Implements hook_menu().
system_test_modules_disabled Implements hook_modules_disabled().
system_test_modules_enabled Implements hook_modules_enabled().
system_test_modules_installed Implements hook_modules_installed().
system_test_modules_uninstalled Implements hook_modules_uninstalled().
system_test_multiple_redirects Menu callback; sends a redirect header to itself until $count argument is 0.
system_test_page_build Implements hook_page_build().
system_test_page_shutdown_functions A simple page callback which adds a register shutdown function.
system_test_redirect
system_test_redirect_invalid_scheme
system_test_redirect_noparse
system_test_redirect_noscheme
system_test_request_destination Page callback to print out $_REQUEST['destination'] for testing.
system_test_set_header
system_test_sleep
system_test_system_info_alter Implements hook_system_info_alter().
_system_test_first_shutdown_function Dummy shutdown function which registers another shutdown function.
_system_test_second_shutdown_function Dummy shutdown function.

Classes

Namesort descending Description
SystemTestFileTransfer Mock FileTransfer object to test the settings form functionality.