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