menu_test.module

You are here

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

Dummy module implementing hook menu.

Functions

Namesort descending Description
menu_login_callback Menu callback to be used as a login path.
menu_test_argument_load Dummy argument loader for hook_menu() to point to.
menu_test_callback Dummy callback for hook_menu() to point to.
menu_test_custom_403_404_callback Callback for our custom 403 and 404 pages.
menu_test_custom_theme Implement hook_custom_theme().
menu_test_init Implements hook_init().
menu_test_menu Implements hook_menu().
menu_test_menu_link_delete Implements hook_menu_link_delete().
menu_test_menu_link_insert Implements hook_menu_link_insert().
menu_test_menu_link_update Implements hook_menu_link_update().
menu_test_menu_name Helper function for the testMenuName() test. Used to change the menu_name parameter of a menu.
menu_test_menu_site_status_alter Implements hook_menu_site_status_alter().
menu_test_menu_trail_callback Callback that test menu_test_menu_tree_set_path().
menu_test_other_argument_load Dummy argument loader for hook_menu() to point to.
menu_test_static_variable Static function for testing hook results.
menu_test_theme_callback Theme callback to use when testing the theme callback functionality.
menu_test_theme_page_callback Page callback to use when testing the theme callback functionality.
menu_test_title_callback Concatenates a string, by using the t() function and a case number.

File

modules/simpletest/tests/menu_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Dummy module implementing hook menu.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function menu_test_menu() {
  10. // The name of the menu changes during the course of the test. Using a $_GET.
  11. $items['menu_name_test'] = array(
  12. 'title' => 'Test menu_name router item',
  13. 'page callback' => 'node_save',
  14. 'menu_name' => menu_test_menu_name(),
  15. );
  16. // This item is of type MENU_CALLBACK with no parents to test title.
  17. $items['menu_callback_title'] = array(
  18. 'title' => 'Menu Callback Title',
  19. 'page callback' => 'menu_test_callback',
  20. 'type' => MENU_CALLBACK,
  21. 'access arguments' => array('access content'),
  22. );
  23. // Use FALSE as 'title callback' to bypass t().
  24. $items['menu_no_title_callback'] = array(
  25. 'title' => 'A title with @placeholder',
  26. 'title callback' => FALSE,
  27. 'title arguments' => array('@placeholder' => 'some other text'),
  28. 'page callback' => 'menu_test_callback',
  29. 'access arguments' => array('access content'),
  30. );
  31. // Hidden link for menu_link_maintain tests
  32. $items['menu_test_maintain/%'] = array(
  33. 'title' => 'Menu maintain test',
  34. 'page callback' => 'node_page_default',
  35. 'access arguments' => array('access content'),
  36. );
  37. // Hierarchical tests.
  38. $items['menu-test/hierarchy/parent'] = array(
  39. 'title' => 'Parent menu router',
  40. 'page callback' => 'node_page_default',
  41. );
  42. $items['menu-test/hierarchy/parent/child'] = array(
  43. 'title' => 'Child menu router',
  44. 'page callback' => 'node_page_default',
  45. );
  46. $items['menu-test/hierarchy/parent/child2/child'] = array(
  47. 'title' => 'Unattached subchild router',
  48. 'page callback' => 'node_page_default',
  49. );
  50. // Theme callback tests.
  51. $items['menu-test/theme-callback/%'] = array(
  52. 'title' => 'Page that displays different themes',
  53. 'page callback' => 'menu_test_theme_page_callback',
  54. 'access arguments' => array('access content'),
  55. 'theme callback' => 'menu_test_theme_callback',
  56. 'theme arguments' => array(2),
  57. );
  58. $items['menu-test/theme-callback/%/inheritance'] = array(
  59. 'title' => 'Page that tests theme callback inheritance.',
  60. 'page callback' => 'menu_test_theme_page_callback',
  61. 'page arguments' => array(TRUE),
  62. 'access arguments' => array('access content'),
  63. );
  64. $items['menu-test/no-theme-callback'] = array(
  65. 'title' => 'Page that displays different themes without using a theme callback.',
  66. 'page callback' => 'menu_test_theme_page_callback',
  67. 'access arguments' => array('access content'),
  68. );
  69. // Path containing "exotic" characters.
  70. $path = "menu-test/ -._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters.
  71. "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string.
  72. "éøïвβ中國書۞"; // Characters from various non-ASCII alphabets.
  73. $items[$path] = array(
  74. 'title' => '"Exotic" path',
  75. 'page callback' => 'menu_test_callback',
  76. 'access arguments' => array('access content'),
  77. );
  78. // Hidden tests; base parents.
  79. // Same structure as in Menu and Block modules. Since those structures can
  80. // change, we need to simulate our own in here.
  81. $items['menu-test'] = array(
  82. 'title' => 'Menu test root',
  83. 'page callback' => 'node_page_default',
  84. 'access arguments' => array('access content'),
  85. );
  86. $items['menu-test/hidden'] = array(
  87. 'title' => 'Hidden test root',
  88. 'page callback' => 'node_page_default',
  89. 'access arguments' => array('access content'),
  90. );
  91. // Hidden tests; one dynamic argument.
  92. $items['menu-test/hidden/menu'] = array(
  93. 'title' => 'Menus',
  94. 'page callback' => 'node_page_default',
  95. 'access arguments' => array('access content'),
  96. );
  97. $items['menu-test/hidden/menu/list'] = array(
  98. 'title' => 'List menus',
  99. 'type' => MENU_DEFAULT_LOCAL_TASK,
  100. 'weight' => -10,
  101. );
  102. $items['menu-test/hidden/menu/add'] = array(
  103. 'title' => 'Add menu',
  104. 'page callback' => 'node_page_default',
  105. 'access arguments' => array('access content'),
  106. 'type' => MENU_LOCAL_ACTION,
  107. );
  108. $items['menu-test/hidden/menu/settings'] = array(
  109. 'title' => 'Settings',
  110. 'page callback' => 'node_page_default',
  111. 'access arguments' => array('access content'),
  112. 'type' => MENU_LOCAL_TASK,
  113. 'weight' => 5,
  114. );
  115. $items['menu-test/hidden/menu/manage/%menu'] = array(
  116. 'title' => 'Customize menu',
  117. 'page callback' => 'node_page_default',
  118. 'access arguments' => array('access content'),
  119. );
  120. $items['menu-test/hidden/menu/manage/%menu/list'] = array(
  121. 'title' => 'List links',
  122. 'weight' => -10,
  123. 'type' => MENU_DEFAULT_LOCAL_TASK,
  124. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  125. );
  126. $items['menu-test/hidden/menu/manage/%menu/add'] = array(
  127. 'title' => 'Add link',
  128. 'page callback' => 'node_page_default',
  129. 'access arguments' => array('access content'),
  130. 'type' => MENU_LOCAL_ACTION,
  131. );
  132. $items['menu-test/hidden/menu/manage/%menu/edit'] = array(
  133. 'title' => 'Edit menu',
  134. 'page callback' => 'node_page_default',
  135. 'access arguments' => array('access content'),
  136. 'type' => MENU_LOCAL_TASK,
  137. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  138. );
  139. $items['menu-test/hidden/menu/manage/%menu/delete'] = array(
  140. 'title' => 'Delete menu',
  141. 'page callback' => 'node_page_default',
  142. 'access arguments' => array('access content'),
  143. );
  144. // Hidden tests; two dynamic arguments.
  145. $items['menu-test/hidden/block'] = array(
  146. 'title' => 'Blocks',
  147. 'page callback' => 'node_page_default',
  148. 'access arguments' => array('access content'),
  149. );
  150. $items['menu-test/hidden/block/list'] = array(
  151. 'title' => 'List',
  152. 'type' => MENU_DEFAULT_LOCAL_TASK,
  153. 'weight' => -10,
  154. );
  155. $items['menu-test/hidden/block/add'] = array(
  156. 'title' => 'Add block',
  157. 'page callback' => 'node_page_default',
  158. 'access arguments' => array('access content'),
  159. 'type' => MENU_LOCAL_ACTION,
  160. );
  161. $items['menu-test/hidden/block/manage/%/%'] = array(
  162. 'title' => 'Configure block',
  163. 'page callback' => 'node_page_default',
  164. 'access arguments' => array('access content'),
  165. );
  166. $items['menu-test/hidden/block/manage/%/%/configure'] = array(
  167. 'title' => 'Configure block',
  168. 'type' => MENU_DEFAULT_LOCAL_TASK,
  169. 'context' => MENU_CONTEXT_INLINE,
  170. );
  171. $items['menu-test/hidden/block/manage/%/%/delete'] = array(
  172. 'title' => 'Delete block',
  173. 'page callback' => 'node_page_default',
  174. 'access arguments' => array('access content'),
  175. 'type' => MENU_LOCAL_TASK,
  176. 'context' => MENU_CONTEXT_NONE,
  177. );
  178. // Breadcrumbs tests.
  179. // @see MenuBreadcrumbTestCase
  180. $base = array(
  181. 'page callback' => 'menu_test_callback',
  182. 'access callback' => TRUE,
  183. );
  184. // Local tasks: Second level below default local task.
  185. $items['menu-test/breadcrumb/tasks'] = array(
  186. 'title' => 'Breadcrumbs test: Local tasks',
  187. ) + $base;
  188. $items['menu-test/breadcrumb/tasks/first'] = array(
  189. 'title' => 'First',
  190. 'type' => MENU_DEFAULT_LOCAL_TASK,
  191. ) + $base;
  192. $items['menu-test/breadcrumb/tasks/second'] = array(
  193. 'title' => 'Second',
  194. 'type' => MENU_LOCAL_TASK,
  195. ) + $base;
  196. $items['menu-test/breadcrumb/tasks/first/first'] = array(
  197. 'title' => 'First first',
  198. 'type' => MENU_DEFAULT_LOCAL_TASK,
  199. ) + $base;
  200. $items['menu-test/breadcrumb/tasks/first/second'] = array(
  201. 'title' => 'First second',
  202. 'type' => MENU_LOCAL_TASK,
  203. ) + $base;
  204. $items['menu-test/breadcrumb/tasks/second/first'] = array(
  205. 'title' => 'Second first',
  206. 'type' => MENU_DEFAULT_LOCAL_TASK,
  207. ) + $base;
  208. $items['menu-test/breadcrumb/tasks/second/second'] = array(
  209. 'title' => 'Second second',
  210. 'type' => MENU_LOCAL_TASK,
  211. ) + $base;
  212. // Menu trail tests.
  213. // @see MenuTrailTestCase
  214. $items['menu-test/menu-trail'] = array(
  215. 'title' => 'Menu trail - Case 1',
  216. 'page callback' => 'menu_test_menu_trail_callback',
  217. 'access arguments' => array('access content'),
  218. );
  219. $items['admin/config/development/menu-trail'] = array(
  220. 'title' => 'Menu trail - Case 2',
  221. 'description' => 'Tests menu_tree_set_path()',
  222. 'page callback' => 'menu_test_menu_trail_callback',
  223. 'access arguments' => array('access administration pages'),
  224. );
  225. $items['menu-test/custom-403-page'] = array(
  226. 'title' => 'Custom 403 page',
  227. 'page callback' => 'menu_test_custom_403_404_callback',
  228. 'access arguments' => array('access content'),
  229. );
  230. $items['menu-test/custom-404-page'] = array(
  231. 'title' => 'Custom 404 page',
  232. 'page callback' => 'menu_test_custom_403_404_callback',
  233. 'access arguments' => array('access content'),
  234. );
  235. // File inheritance tests. This menu item should inherit the page callback
  236. // system_admin_menu_block_page() and therefore render its children as links
  237. // on the page.
  238. $items['admin/config/development/file-inheritance'] = array(
  239. 'title' => 'File inheritance',
  240. 'description' => 'Test file inheritance',
  241. 'access arguments' => array('access content'),
  242. );
  243. $items['admin/config/development/file-inheritance/inherit'] = array(
  244. 'title' => 'Inherit',
  245. 'description' => 'File inheritance test description',
  246. 'page callback' => 'menu_test_callback',
  247. 'access arguments' => array('access content'),
  248. );
  249. $items['menu_login_callback'] = array(
  250. 'title' => 'Used as a login path',
  251. 'page callback' => 'menu_login_callback',
  252. 'access callback' => TRUE,
  253. );
  254. $items['menu-title-test/case1'] = array(
  255. 'title' => 'Example title - Case 1',
  256. 'access callback' => TRUE,
  257. 'page callback' => 'menu_test_callback',
  258. );
  259. $items['menu-title-test/case2'] = array(
  260. 'title' => 'Example @sub1 - Case @op2',
  261. // If '2' is not in quotes, the argument becomes arg(2).
  262. 'title arguments' => array('@sub1' => 'title', '@op2' => '2'),
  263. 'access callback' => TRUE,
  264. 'page callback' => 'menu_test_callback',
  265. );
  266. $items['menu-title-test/case3'] = array(
  267. 'title' => 'Example title',
  268. 'title callback' => 'menu_test_title_callback',
  269. 'access callback' => TRUE,
  270. 'page callback' => 'menu_test_callback',
  271. );
  272. $items['menu-title-test/case4'] = array(
  273. // Title gets completely ignored. Good thing, too.
  274. 'title' => 'Bike sheds full of blue smurfs',
  275. 'title callback' => 'menu_test_title_callback',
  276. // If '4' is not in quotes, the argument becomes arg(4).
  277. 'title arguments' => array('Example title', '4'),
  278. 'access callback' => TRUE,
  279. 'page callback' => 'menu_test_callback',
  280. );
  281. // Load arguments inheritance test.
  282. $items['menu-test/arguments/%menu_test_argument/%'] = array(
  283. 'title' => 'Load arguments inheritance test',
  284. 'load arguments' => array(3),
  285. 'page callback' => 'menu_test_callback',
  286. 'access callback' => TRUE,
  287. );
  288. $items['menu-test/arguments/%menu_test_argument/%/default'] = array(
  289. 'title' => 'Default local task',
  290. 'type' => MENU_DEFAULT_LOCAL_TASK,
  291. );
  292. $items['menu-test/arguments/%menu_test_argument/%/task'] = array(
  293. 'title' => 'Local task',
  294. 'page callback' => 'menu_test_callback',
  295. 'access callback' => TRUE,
  296. 'type' => MENU_LOCAL_TASK,
  297. );
  298. // For this path, load arguments should be inherited for the first loader only.
  299. $items['menu-test/arguments/%menu_test_argument/%menu_test_other_argument/common-loader'] = array(
  300. 'title' => 'Local task',
  301. 'page callback' => 'menu_test_callback',
  302. 'access callback' => TRUE,
  303. 'type' => MENU_LOCAL_TASK,
  304. );
  305. // For these paths, no load arguments should be inherited.
  306. // Not on the same position.
  307. $items['menu-test/arguments/%/%menu_test_argument/different-loaders-1'] = array(
  308. 'title' => 'An item not sharing the same loader',
  309. 'page callback' => 'menu_test_callback',
  310. 'access callback' => TRUE,
  311. );
  312. // Not the same loader.
  313. $items['menu-test/arguments/%menu_test_other_argument/%/different-loaders-2'] = array(
  314. 'title' => 'An item not sharing the same loader',
  315. 'page callback' => 'menu_test_callback',
  316. 'access callback' => TRUE,
  317. );
  318. // Not the same loader.
  319. $items['menu-test/arguments/%/%/different-loaders-3'] = array(
  320. 'title' => 'An item not sharing the same loader',
  321. 'page callback' => 'menu_test_callback',
  322. 'access callback' => TRUE,
  323. );
  324. // Explict load arguments should not be overriden (even if empty).
  325. $items['menu-test/arguments/%menu_test_argument/%/explicit-arguments'] = array(
  326. 'title' => 'An item defining explicit load arguments',
  327. 'load arguments' => array(),
  328. 'page callback' => 'menu_test_callback',
  329. 'access callback' => TRUE,
  330. );
  331. return $items;
  332. }
  333. /**
  334. * Dummy argument loader for hook_menu() to point to.
  335. */
  336. function menu_test_argument_load($arg1) {
  337. return FALSE;
  338. }
  339. /**
  340. * Dummy argument loader for hook_menu() to point to.
  341. */
  342. function menu_test_other_argument_load($arg1) {
  343. return FALSE;
  344. }
  345. /**
  346. * Dummy callback for hook_menu() to point to.
  347. *
  348. * @return
  349. * A random string.
  350. */
  351. function menu_test_callback() {
  352. return 'This is menu_test_callback().';
  353. }
  354. /**
  355. * Callback that test menu_test_menu_tree_set_path().
  356. */
  357. function menu_test_menu_trail_callback() {
  358. $menu_path = variable_get('menu_test_menu_tree_set_path', array());
  359. if (!empty($menu_path)) {
  360. menu_tree_set_path($menu_path['menu_name'], $menu_path['path']);
  361. }
  362. return 'This is menu_test_menu_trail_callback().';
  363. }
  364. /**
  365. * Implements hook_init().
  366. */
  367. function menu_test_init() {
  368. // When requested by one of the MenuTrailTestCase tests, record the initial
  369. // active trail during Drupal's bootstrap (before the user is redirected to a
  370. // custom 403 or 404 page). See menu_test_custom_403_404_callback().
  371. if (variable_get('menu_test_record_active_trail', FALSE)) {
  372. variable_set('menu_test_active_trail_initial', menu_get_active_trail());
  373. }
  374. }
  375. /**
  376. * Callback for our custom 403 and 404 pages.
  377. */
  378. function menu_test_custom_403_404_callback() {
  379. // When requested by one of the MenuTrailTestCase tests, record the final
  380. // active trail now that the user has been redirected to the custom 403 or
  381. // 404 page. See menu_test_init().
  382. if (variable_get('menu_test_record_active_trail', FALSE)) {
  383. variable_set('menu_test_active_trail_final', menu_get_active_trail());
  384. }
  385. return 'This is menu_test_custom_403_404_callback().';
  386. }
  387. /**
  388. * Page callback to use when testing the theme callback functionality.
  389. *
  390. * @param $inherited
  391. * An optional boolean to set to TRUE when the requested page is intended to
  392. * inherit the theme of its parent.
  393. * @return
  394. * A string describing the requested custom theme and actual theme being used
  395. * for the current page request.
  396. */
  397. function menu_test_theme_page_callback($inherited = FALSE) {
  398. global $theme_key;
  399. // Initialize the theme system so that $theme_key will be populated.
  400. drupal_theme_initialize();
  401. // Now check both the requested custom theme and the actual theme being used.
  402. $custom_theme = menu_get_custom_theme();
  403. $requested_theme = empty($custom_theme) ? 'NONE' : $custom_theme;
  404. $output = "Custom theme: $requested_theme. Actual theme: $theme_key.";
  405. if ($inherited) {
  406. $output .= ' Theme callback inheritance is being tested.';
  407. }
  408. return $output;
  409. }
  410. /**
  411. * Theme callback to use when testing the theme callback functionality.
  412. *
  413. * @param $argument
  414. * The argument passed in from the URL.
  415. * @return
  416. * The name of the custom theme to request for the current page.
  417. */
  418. function menu_test_theme_callback($argument) {
  419. // Test using the variable administrative theme.
  420. if ($argument == 'use-admin-theme') {
  421. return variable_get('admin_theme');
  422. }
  423. // Test using a theme that exists, but may or may not be enabled.
  424. elseif ($argument == 'use-stark-theme') {
  425. return 'stark';
  426. }
  427. // Test using a theme that does not exist.
  428. elseif ($argument == 'use-fake-theme') {
  429. return 'fake_theme';
  430. }
  431. // For any other value of the URL argument, do not return anything. This
  432. // allows us to test that returning nothing from a theme callback function
  433. // causes the page to correctly fall back on using the main site theme.
  434. }
  435. /**
  436. * Implement hook_custom_theme().
  437. *
  438. * @return
  439. * The name of the custom theme to use for the current page.
  440. */
  441. function menu_test_custom_theme() {
  442. // If an appropriate variable has been set in the database, request the theme
  443. // that is stored there. Otherwise, do not attempt to dynamically set the
  444. // theme.
  445. if ($theme = variable_get('menu_test_hook_custom_theme_name', FALSE)) {
  446. return $theme;
  447. }
  448. }
  449. /**
  450. * Helper function for the testMenuName() test. Used to change the menu_name
  451. * parameter of a menu.
  452. *
  453. * @param $new_name
  454. * If set, will change the menu_name value.
  455. * @return
  456. * The menu_name value to use.
  457. */
  458. function menu_test_menu_name($new_name = '') {
  459. static $name = 'original';
  460. if ($new_name) {
  461. $name = $new_name;
  462. }
  463. return $name;
  464. }
  465. /**
  466. * Implements hook_menu_link_insert().
  467. *
  468. * @return
  469. * A random string.
  470. */
  471. function menu_test_menu_link_insert($item) {
  472. menu_test_static_variable('insert');
  473. }
  474. /**
  475. * Implements hook_menu_link_update().
  476. *
  477. * @return
  478. * A random string.
  479. */
  480. function menu_test_menu_link_update($item) {
  481. menu_test_static_variable('update');
  482. }
  483. /**
  484. * Implements hook_menu_link_delete().
  485. *
  486. * @return
  487. * A random string.
  488. */
  489. function menu_test_menu_link_delete($item) {
  490. menu_test_static_variable('delete');
  491. }
  492. /**
  493. * Static function for testing hook results.
  494. *
  495. * @param $value
  496. * The value to set or NULL to return the current value.
  497. * @return
  498. * A text string for comparison to test assertions.
  499. */
  500. function menu_test_static_variable($value = NULL) {
  501. static $variable;
  502. if (!empty($value)) {
  503. $variable = $value;
  504. }
  505. return $variable;
  506. }
  507. /**
  508. * Implements hook_menu_site_status_alter().
  509. */
  510. function menu_test_menu_site_status_alter(&$menu_site_status, $path) {
  511. // Allow access to ?q=menu_login_callback even if in maintenance mode.
  512. if ($menu_site_status == MENU_SITE_OFFLINE && $path == 'menu_login_callback') {
  513. $menu_site_status = MENU_SITE_ONLINE;
  514. }
  515. }
  516. /**
  517. * Menu callback to be used as a login path.
  518. */
  519. function menu_login_callback() {
  520. return 'This is menu_login_callback().';
  521. }
  522. /**
  523. * Concatenates a string, by using the t() function and a case number.
  524. *
  525. * @param $title
  526. * Title string.
  527. * @param $case_number
  528. * The current case number which is tests (defaults to 3).
  529. */
  530. function menu_test_title_callback($title, $case_no = 3) {
  531. return t($title) . ' - Case ' . $case_no;
  532. }