You are here

menu_test.module in SimpleTest 7

Dummy module implementing hook menu.

File

tests/menu_test.module
View source
<?php

/**
 * @file
 * Dummy module implementing hook menu.
 */

/**
 * Implement hook_menu().
 */
function menu_test_menu() {

  // The name of the menu changes during the course of the test. Using a $_GET.
  $items['menu_name_test'] = array(
    'title' => 'Test menu_name router item',
    'page callback' => 'node_save',
    'menu_name' => menu_test_menu_name(),
  );

  // Use FALSE as 'title callback' to bypass t().
  $items['menu_no_title_callback'] = array(
    'title' => 'A title with @placeholder',
    'title callback' => FALSE,
    'title arguments' => array(
      '@placeholder' => 'some other text',
    ),
    'page callback' => 'menu_test_callback',
    'access arguments' => array(
      'access content',
    ),
  );

  // Hidden link for menu_link_maintain tests
  $items['menu_test_maintain/%'] = array(
    'title' => 'Menu maintain test',
    'page callback' => 'node_page_default',
    'access arguments' => array(
      'access content',
    ),
  );

  // Hierarchical tests.
  $items['menu-test/hierarchy/parent'] = array(
    'title' => 'Parent menu router',
    'page callback' => 'node_page_default',
  );
  $items['menu-test/hierarchy/parent/child'] = array(
    'title' => 'Child menu router',
    'page callback' => 'node_page_default',
  );
  $items['menu-test/hierarchy/parent/child2/child'] = array(
    'title' => 'Unattached subchild router',
    'page callback' => 'node_page_default',
  );

  // Theme callback tests.
  $items['menu-test/theme-callback/%'] = array(
    'title' => 'Page that displays different themes',
    'page callback' => 'menu_test_theme_page_callback',
    'access arguments' => array(
      'access content',
    ),
    'theme callback' => 'menu_test_theme_callback',
    'theme arguments' => array(
      2,
    ),
  );
  $items['menu-test/theme-callback/%/inheritance'] = array(
    'title' => 'Page that tests theme callback inheritance.',
    'page callback' => 'menu_test_theme_page_callback',
    'page arguments' => array(
      TRUE,
    ),
    'access arguments' => array(
      'access content',
    ),
  );
  return $items;
}

/**
 * Dummy callback for hook_menu() to point to.
 *
 * @return
 *  A random string.
 */
function menu_test_callback() {
  return $this
    ->randomName();
}

/**
 * Page callback to use when testing the theme callback functionality.
 *
 * @param $inherited
 *   An optional boolean to set to TRUE when the requested page is intended to
 *   inherit the theme of its parent.
 * @return
 *   A string describing the requested custom theme and actual theme being used
 *   for the current page request.
 */
function menu_test_theme_page_callback($inherited = FALSE) {
  global $theme_key;

  // Initialize the theme system so that $theme_key will be populated.
  drupal_theme_initialize();

  // Now check both the requested custom theme and the actual theme being used.
  $custom_theme = menu_get_custom_theme();
  $requested_theme = empty($custom_theme) ? 'NONE' : $custom_theme;
  $output = "Requested theme: {$requested_theme}. Actual theme: {$theme_key}.";
  if ($inherited) {
    $output .= ' Theme callback inheritance is being tested.';
  }
  return $output;
}

/**
 * Theme callback to use when testing the theme callback functionality.
 *
 * @param $argument
 *   The argument passed in from the URL.
 * @return
 *   The name of the custom theme to request for the current page.
 */
function menu_test_theme_callback($argument) {

  // Test using the variable administrative theme.
  if ($argument == 'use-admin-theme') {
    return variable_get('admin_theme');
  }
  elseif ($argument == 'use-stark-theme') {
    return 'stark';
  }
  elseif ($argument == 'use-fake-theme') {
    return 'fake_theme';
  }

  // For any other value of the URL argument, do not return anything. This
  // allows us to test that returning nothing from a theme callback function
  // causes the page to correctly fall back on using the main site theme.
}

/**
 * Helper function for the testMenuName() test. Used to change the menu_name
 * parameter of a menu.
 *
 * @param $new_name 
 *   If set, will change the menu_name value.
 * @return 
 *   The menu_name value to use.
 */
function menu_test_menu_name($new_name = '') {
  static $name = 'original';
  if ($new_name) {
    $name = $new_name;
  }
  return $name;
}

/**
 * Implement hook_menu_link_insert().
 *
 * @return
 *  A random string.
 */
function menu_test_menu_link_insert($item) {
  menu_test_static_variable('insert');
}

/**
 * Implement hook_menu_link_update().
 *
 * @return
 *  A random string.
 */
function menu_test_menu_link_update($item) {
  menu_test_static_variable('update');
}

/**
 * Implement hook_menu_link_delete().
 *
 * @return
 *  A random string.
 */
function menu_test_menu_link_delete($item) {
  menu_test_static_variable('delete');
}

/**
 * Static function for testing hook results.
 *
 * @param $value
 *   The value to set or NULL to return the current value.
 * @return
 *   A text string for comparison to test assertions.
 */
function menu_test_static_variable($value = NULL) {
  static $variable;
  if (!empty($value)) {
    $variable = $value;
  }
  return $variable;
}

Functions

Namesort descending Description
menu_test_callback Dummy callback for hook_menu() to point to.
menu_test_menu Implement hook_menu().
menu_test_menu_link_delete Implement hook_menu_link_delete().
menu_test_menu_link_insert Implement hook_menu_link_insert().
menu_test_menu_link_update Implement 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_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.