cms/drupal/modules/update/tests/update_test.module
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Module for testing Update Manager functionality.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Implements hook_system_theme_info().
       
    10  */
       
    11 function update_test_system_theme_info() {
       
    12   $themes['update_test_basetheme'] = drupal_get_path('module', 'update_test') . '/themes/update_test_basetheme/update_test_basetheme.info';
       
    13   $themes['update_test_subtheme'] = drupal_get_path('module', 'update_test') . '/themes/update_test_subtheme/update_test_subtheme.info';
       
    14   $themes['update_test_admintheme'] = drupal_get_path('module', 'update_test') . '/themes/update_test_admintheme/update_test_admintheme.info';
       
    15   return $themes;
       
    16 }
       
    17 
       
    18 /**
       
    19  * Implements hook_menu().
       
    20  */
       
    21 function update_test_menu() {
       
    22   $items = array();
       
    23 
       
    24   $items['update-test'] = array(
       
    25     'title' => t('Update test'),
       
    26     'page callback' => 'update_test_mock_page',
       
    27     'access callback' => TRUE,
       
    28     'type' => MENU_CALLBACK,
       
    29   );
       
    30   $items['503-error'] = array(
       
    31     'title' => t('503 Service unavailable'),
       
    32     'page callback' => 'update_callback_service_unavailable',
       
    33     'access callback' => TRUE,
       
    34     'type' => MENU_CALLBACK,
       
    35   );
       
    36 
       
    37   return $items;
       
    38 }
       
    39 
       
    40 /**
       
    41  * Implements hook_system_info_alter().
       
    42  *
       
    43  * Checks the 'update_test_system_info' variable and sees if we need to alter
       
    44  * the system info for the given $file based on the setting. The setting is
       
    45  * expected to be a nested associative array. If the key '#all' is defined, its
       
    46  * subarray will include .info keys and values for all modules and themes on the
       
    47  * system. Otherwise, the settings array is keyed by the module or theme short
       
    48  * name ($file->name) and the subarrays contain settings just for that module or
       
    49  * theme.
       
    50  */
       
    51 function update_test_system_info_alter(&$info, $file) {
       
    52   $setting = variable_get('update_test_system_info', array());
       
    53   foreach (array('#all', $file->name) as $id) {
       
    54     if (!empty($setting[$id])) {
       
    55       foreach ($setting[$id] as $key => $value) {
       
    56         $info[$key] = $value;
       
    57       }
       
    58     }
       
    59   }
       
    60 }
       
    61 
       
    62 /**
       
    63  * Implements hook_update_status_alter().
       
    64  *
       
    65  * Checks the 'update_test_update_status' variable and sees if we need to alter
       
    66  * the update status for the given project based on the setting. The setting is
       
    67  * expected to be a nested associative array. If the key '#all' is defined, its
       
    68  * subarray will include .info keys and values for all modules and themes on the
       
    69  * system. Otherwise, the settings array is keyed by the module or theme short
       
    70  * name and the subarrays contain settings just for that module or theme.
       
    71  */
       
    72 function update_test_update_status_alter(&$projects) {
       
    73   $setting = variable_get('update_test_update_status', array());
       
    74   if (!empty($setting)) {
       
    75     foreach ($projects as $project_name => &$project) {
       
    76       foreach (array('#all', $project_name) as $id) {
       
    77         if (!empty($setting[$id])) {
       
    78           foreach ($setting[$id] as $key => $value) {
       
    79             $project[$key] = $value;
       
    80           }
       
    81         }
       
    82       }
       
    83     }
       
    84   }
       
    85 }
       
    86 
       
    87 /**
       
    88  * Page callback: Prints mock XML for the Update Manager module.
       
    89  *
       
    90  * The specific XML file to print depends on two things: the project we're
       
    91  * trying to fetch data for, and the desired "availability scenario" for that
       
    92  * project which we're trying to test. Before attempting to fetch this data (by
       
    93  * checking for updates on the available updates report), callers need to define
       
    94  * the 'update_test_xml_map' variable as an array, keyed by project name,
       
    95  * indicating which availability scenario to use for that project.
       
    96  *
       
    97  * @param $project_name
       
    98  *   The project short name the update manager is trying to fetch data for (the
       
    99  *   fetch URLs are of the form: [base_url]/[project_name]/[core_version]).
       
   100  *
       
   101  * @see update_test_menu()
       
   102  */
       
   103 function update_test_mock_page($project_name) {
       
   104   $xml_map = variable_get('update_test_xml_map', FALSE);
       
   105   if (isset($xml_map[$project_name])) {
       
   106     $availability_scenario = $xml_map[$project_name];
       
   107   }
       
   108   elseif (isset($xml_map['#all'])) {
       
   109     $availability_scenario = $xml_map['#all'];
       
   110   }
       
   111   else {
       
   112     // The test didn't specify (for example, the webroot has other modules and
       
   113     // themes installed but they're disabled by the version of the site
       
   114     // running the test. So, we default to a file we know won't exist, so at
       
   115     // least we'll get an empty page from readfile instead of a bunch of
       
   116     // Drupal page output.
       
   117     $availability_scenario = '#broken#';
       
   118   }
       
   119 
       
   120   $path = drupal_get_path('module', 'update_test');
       
   121   readfile("$path/$project_name.$availability_scenario.xml");
       
   122 }
       
   123 
       
   124 /**
       
   125  * Implements hook_archiver_info().
       
   126  */
       
   127 function update_test_archiver_info() {
       
   128   return array(
       
   129     'update_test_archiver' => array(
       
   130       // This is bogus, we only care about the extensions for now.
       
   131       'class' => 'ArchiverUpdateTest',
       
   132       'extensions' => array('update-test-extension'),
       
   133     ),
       
   134   );
       
   135 }
       
   136 
       
   137 /**
       
   138  * Implements hook_filetransfer_info().
       
   139  */
       
   140 function update_test_filetransfer_info() {
       
   141   // Define a mock file transfer method, to ensure that there will always be
       
   142   // at least one method available in the user interface (regardless of the
       
   143   // environment in which the update manager tests are run).
       
   144   return array(
       
   145     'system_test' => array(
       
   146       'title' => t('Update Test FileTransfer'),
       
   147       // This should be in an .inc file, but for testing purposes, it is OK to
       
   148       // leave it in the main module file.
       
   149       'file' => 'update_test.module',
       
   150       'class' => 'UpdateTestFileTransfer',
       
   151       'weight' => -20,
       
   152     ),
       
   153   );
       
   154 }
       
   155 
       
   156 /**
       
   157  * Mocks a FileTransfer object to test the settings form functionality.
       
   158  */
       
   159 class UpdateTestFileTransfer {
       
   160 
       
   161   /**
       
   162    * Returns an UpdateTestFileTransfer object.
       
   163    *
       
   164    * @return
       
   165    *   A new UpdateTestFileTransfer object.
       
   166    */
       
   167   public static function factory() {
       
   168     return new UpdateTestFileTransfer;
       
   169   }
       
   170 
       
   171   /**
       
   172    * Returns a settings form with a text field to input a username.
       
   173    */
       
   174   public function getSettingsForm() {
       
   175     $form = array();
       
   176     $form['udpate_test_username'] = array(
       
   177       '#type' => 'textfield',
       
   178       '#title' => t('Update Test Username'),
       
   179     );
       
   180     return $form;
       
   181   }
       
   182 }
       
   183 
       
   184 /**
       
   185  * Page callback: Displays an Error 503 (Service unavailable) page.
       
   186  *
       
   187  * @see update_test_menu()
       
   188  */
       
   189 function update_callback_service_unavailable() {
       
   190   drupal_add_http_header('Status', '503 Service unavailable');
       
   191   print "503 Service Temporarily Unavailable";
       
   192 }