Projet

Général

Profil

Paste
Télécharger (5,96 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / update / tests / update_test.module @ db2d93dd

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
  return $themes;
15
}
16

    
17
/**
18
 * Implements hook_menu().
19
 */
20
function update_test_menu() {
21
  $items = array();
22

    
23
  $items['update-test'] = array(
24
    'title' => t('Update test'),
25
    'page callback' => 'update_test_mock_page',
26
    'access callback' => TRUE,
27
    'type' => MENU_CALLBACK,
28
  );
29
  $items['503-error'] = array(
30
    'title' => t('503 Service unavailable'),
31
    'page callback' => 'update_callback_service_unavailable',
32
    'access callback' => TRUE,
33
    'type' => MENU_CALLBACK,
34
  );
35

    
36
  return $items;
37
}
38

    
39
/**
40
 * Implements hook_system_info_alter().
41
 *
42
 * Checks the 'update_test_system_info' variable and sees if we need to alter
43
 * the system info for the given $file based on the setting. The setting is
44
 * expected to be a nested associative array. If the key '#all' is defined, its
45
 * subarray will include .info keys and values for all modules and themes on the
46
 * system. Otherwise, the settings array is keyed by the module or theme short
47
 * name ($file->name) and the subarrays contain settings just for that module or
48
 * theme.
49
 */
50
function update_test_system_info_alter(&$info, $file) {
51
  $setting = variable_get('update_test_system_info', array());
52
  foreach (array('#all', $file->name) as $id) {
53
    if (!empty($setting[$id])) {
54
      foreach ($setting[$id] as $key => $value) {
55
        $info[$key] = $value;
56
      }
57
    }
58
  }
59
}
60

    
61
/**
62
 * Implements hook_update_status_alter().
63
 *
64
 * Checks the 'update_test_update_status' variable and sees if we need to alter
65
 * the update status for the given project based on the setting. The setting is
66
 * expected to be a nested associative array. If the key '#all' is defined, its
67
 * subarray will include .info keys and values for all modules and themes on the
68
 * system. Otherwise, the settings array is keyed by the module or theme short
69
 * name and the subarrays contain settings just for that module or theme.
70
 */
71
function update_test_update_status_alter(&$projects) {
72
  $setting = variable_get('update_test_update_status', array());
73
  if (!empty($setting)) {
74
    foreach ($projects as $project_name => &$project) {
75
      foreach (array('#all', $project_name) as $id) {
76
        if (!empty($setting[$id])) {
77
          foreach ($setting[$id] as $key => $value) {
78
            $project[$key] = $value;
79
          }
80
        }
81
      }
82
    }
83
  }
84
}
85

    
86
/**
87
 * Page callback: Prints mock XML for the Update Manager module.
88
 *
89
 * The specific XML file to print depends on two things: the project we're
90
 * trying to fetch data for, and the desired "availability scenario" for that
91
 * project which we're trying to test. Before attempting to fetch this data (by
92
 * checking for updates on the available updates report), callers need to define
93
 * the 'update_test_xml_map' variable as an array, keyed by project name,
94
 * indicating which availability scenario to use for that project.
95
 *
96
 * @param $project_name
97
 *   The project short name the update manager is trying to fetch data for (the
98
 *   fetch URLs are of the form: [base_url]/[project_name]/[core_version]).
99
 *
100
 * @see update_test_menu()
101
 */
102
function update_test_mock_page($project_name) {
103
  $xml_map = variable_get('update_test_xml_map', FALSE);
104
  if (isset($xml_map[$project_name])) {
105
    $availability_scenario = $xml_map[$project_name];
106
  }
107
  elseif (isset($xml_map['#all'])) {
108
    $availability_scenario = $xml_map['#all'];
109
  }
110
  else {
111
    // The test didn't specify (for example, the webroot has other modules and
112
    // themes installed but they're disabled by the version of the site
113
    // running the test. So, we default to a file we know won't exist, so at
114
    // least we'll get an empty page from readfile instead of a bunch of
115
    // Drupal page output.
116
    $availability_scenario = '#broken#';
117
  }
118

    
119
  $path = drupal_get_path('module', 'update_test');
120
  readfile("$path/$project_name.$availability_scenario.xml");
121
}
122

    
123
/**
124
 * Implements hook_archiver_info().
125
 */
126
function update_test_archiver_info() {
127
  return array(
128
    'update_test_archiver' => array(
129
      // This is bogus, we only care about the extensions for now.
130
      'class' => 'ArchiverUpdateTest',
131
      'extensions' => array('update-test-extension'),
132
    ),
133
  );
134
}
135

    
136
/**
137
 * Implements hook_filetransfer_info().
138
 */
139
function update_test_filetransfer_info() {
140
  // Define a mock file transfer method, to ensure that there will always be
141
  // at least one method available in the user interface (regardless of the
142
  // environment in which the update manager tests are run).
143
  return array(
144
    'system_test' => array(
145
      'title' => t('Update Test FileTransfer'),
146
      // This should be in an .inc file, but for testing purposes, it is OK to
147
      // leave it in the main module file.
148
      'file' => 'update_test.module',
149
      'class' => 'UpdateTestFileTransfer',
150
      'weight' => -20,
151
    ),
152
  );
153
}
154

    
155
/**
156
 * Mocks a FileTransfer object to test the settings form functionality.
157
 */
158
class UpdateTestFileTransfer {
159

    
160
  /**
161
   * Returns an UpdateTestFileTransfer object.
162
   *
163
   * @return
164
   *   A new UpdateTestFileTransfer object.
165
   */
166
  public static function factory() {
167
    return new UpdateTestFileTransfer;
168
  }
169

    
170
  /**
171
   * Returns a settings form with a text field to input a username.
172
   */
173
  public function getSettingsForm() {
174
    $form = array();
175
    $form['udpate_test_username'] = array(
176
      '#type' => 'textfield',
177
      '#title' => t('Update Test Username'),
178
    );
179
    return $form;
180
  }
181
}
182

    
183
/**
184
 * Page callback: Displays an Error 503 (Service unavailable) page.
185
 *
186
 * @see update_test_menu()
187
 */
188
function update_callback_service_unavailable() {
189
  drupal_add_http_header('Status', '503 Service unavailable');
190
  print "503 Service Temporarily Unavailable";
191
}