Projet

Général

Profil

Paste
Télécharger (12,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / system_test.module @ f7a2490e

1
<?php
2

    
3
/**
4
 * Implements hook_menu().
5
 */
6
function system_test_menu() {
7
  $items['system-test/sleep/%'] = array(
8
    'page callback' => 'system_test_sleep',
9
    'page arguments' => array(2),
10
    'access callback' => TRUE,
11
    'type' => MENU_CALLBACK,
12
  );
13
  $items['system-test/auth'] = array(
14
    'page callback' => 'system_test_basic_auth_page',
15
    'access callback' => TRUE,
16
    'type' => MENU_CALLBACK,
17
  );
18
  $items['system-test/authorize-init/%'] = array(
19
    'page callback' => 'system_test_authorize_init_page',
20
    'page arguments' => array(2),
21
    'access arguments' => array('administer software updates'),
22
    'type' => MENU_CALLBACK,
23
  );
24
  $items['system-test/redirect/%'] = array(
25
    'title' => 'Redirect',
26
    'page callback' => 'system_test_redirect',
27
    'page arguments' => array(2),
28
    'access arguments' => array('access content'),
29
    'type' => MENU_CALLBACK,
30
  );
31
  $items['system-test/multiple-redirects/%'] = array(
32
    'title' => 'Redirect',
33
    'page callback' => 'system_test_multiple_redirects',
34
    'page arguments' => array(2),
35
    'access arguments' => array('access content'),
36
    'type' => MENU_CALLBACK,
37
  );
38
  $items['system-test/set-header'] = array(
39
    'page callback' => 'system_test_set_header',
40
    'access arguments' => array('access content'),
41
    'type' => MENU_CALLBACK,
42
  );
43
  $items['system-test/redirect-noscheme'] = array(
44
    'page callback' => 'system_test_redirect_noscheme',
45
    'access arguments' => array('access content'),
46
    'type' => MENU_CALLBACK,
47
  );
48
  $items['system-test/redirect-noparse'] = array(
49
    'page callback' => 'system_test_redirect_noparse',
50
    'access arguments' => array('access content'),
51
    'type' => MENU_CALLBACK,
52
  );
53
  $items['system-test/redirect-invalid-scheme'] = array(
54
    'page callback' => 'system_test_redirect_invalid_scheme',
55
    'access arguments' => array('access content'),
56
    'type' => MENU_CALLBACK,
57
  );
58

    
59
  $items['system-test/variable-get'] = array(
60
    'title' => 'Variable Get',
61
    'page callback' => 'variable_get',
62
    'page arguments' => array('simpletest_bootstrap_variable_test', NULL),
63
    'access arguments' => array('access content'),
64
    'type' => MENU_CALLBACK,
65
  );
66

    
67
  $items['system-test/lock-acquire'] = array(
68
    'title' => 'Lock acquire',
69
    'page callback' => 'system_test_lock_acquire',
70
    'access callback' => TRUE,
71
    'type' => MENU_CALLBACK,
72
  );
73

    
74
  $items['system-test/lock-exit'] = array(
75
    'title' => 'Lock acquire then exit',
76
    'page callback' => 'system_test_lock_exit',
77
    'access callback' => TRUE,
78
    'type' => MENU_CALLBACK,
79
  );
80

    
81
  $items['system-test/main-content-handling'] = array(
82
    'title' => 'Test main content handling',
83
    'page callback' => 'system_test_main_content_fallback',
84
    'access callback' => TRUE,
85
    'type' => MENU_CALLBACK,
86
  );
87

    
88
  $items['system-test/main-content-fallback'] = array(
89
    'title' => 'Test main content fallback',
90
    'page callback' => 'system_test_main_content_fallback',
91
    'access callback' => TRUE,
92
    'type' => MENU_CALLBACK,
93
  );
94

    
95
  $items['system-test/main-content-duplication'] = array(
96
    'title' => 'Test main content duplication',
97
    'page callback' => 'system_test_main_content_fallback',
98
    'access callback' => TRUE,
99
    'type' => MENU_CALLBACK,
100
  );
101

    
102
  $items['system-test/shutdown-functions'] = array(
103
    'title' => 'Test main content duplication',
104
    'page callback' => 'system_test_page_shutdown_functions',
105
    'access callback' => TRUE,
106
    'type' => MENU_CALLBACK,
107
  );
108

    
109
  return $items;
110
}
111

    
112
function system_test_sleep($seconds) {
113
  sleep($seconds);
114
}
115

    
116
function system_test_basic_auth_page() {
117
  $output = t('$_SERVER[\'PHP_AUTH_USER\'] is @username.', array('@username' => $_SERVER['PHP_AUTH_USER']));
118
  $output .= t('$_SERVER[\'PHP_AUTH_PW\'] is @password.', array('@password' => $_SERVER['PHP_AUTH_PW']));
119
  return $output;
120
}
121

    
122
function system_test_redirect($code) {
123
  $code = (int) $code;
124
  if ($code != 200) {
125
    // Header names are case-insensitive.
126
    header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
127
    exit;
128
  }
129
  return '';
130
}
131

    
132
/**
133
 * Menu callback; sends a redirect header to itself until $count argument is 0.
134
 *
135
 * Emulates the variable number of redirects (given by initial $count argument)
136
 * to the final destination URL by continuous sending of 301 HTTP redirect
137
 * headers to itself together with decrementing the $count parameter until the
138
 * $count parameter reaches 0. After that it returns an empty string to render
139
 * the final destination page.
140
 *
141
 * @param $count
142
 *   The count of redirects left until the final destination page.
143
 *
144
 * @returns
145
 *   The location redirect if the $count > 0, otherwise an empty string.
146
 */
147
function system_test_multiple_redirects($count) {
148
  $count = (int) $count;
149
  if ($count > 0) {
150
    header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
151
    exit;
152
  }
153
  return '';
154
}
155

    
156
function system_test_set_header() {
157
  drupal_add_http_header($_GET['name'], $_GET['value']);
158
  return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
159
}
160

    
161
function system_test_redirect_noscheme() {
162
  header("Location: localhost/path", TRUE, 301);
163
  exit;
164
}
165

    
166
function system_test_redirect_noparse() {
167
  header("Location: http:///path", TRUE, 301);
168
  exit;
169
}
170

    
171
function system_test_redirect_invalid_scheme() {
172
  header("Location: ftp://localhost/path", TRUE, 301);
173
  exit;
174
}
175

    
176
/**
177
 * Implements hook_modules_installed().
178
 */
179
function system_test_modules_installed($modules) {
180
  if (variable_get('test_verbose_module_hooks')) {
181
    foreach ($modules as $module) {
182
      drupal_set_message(t('hook_modules_installed fired for @module', array('@module' => $module)));
183
    }
184
  }
185
}
186

    
187
/**
188
 * Implements hook_modules_enabled().
189
 */
190
function system_test_modules_enabled($modules) {
191
  if (variable_get('test_verbose_module_hooks')) {
192
    foreach ($modules as $module) {
193
      drupal_set_message(t('hook_modules_enabled fired for @module', array('@module' => $module)));
194
    }
195
  }
196
}
197

    
198
/**
199
 * Implements hook_modules_disabled().
200
 */
201
function system_test_modules_disabled($modules) {
202
  if (variable_get('test_verbose_module_hooks')) {
203
    foreach ($modules as $module) {
204
      drupal_set_message(t('hook_modules_disabled fired for @module', array('@module' => $module)));
205
    }
206
  }
207
}
208

    
209
/**
210
 * Implements hook_modules_uninstalled().
211
 */
212
function system_test_modules_uninstalled($modules) {
213
  if (variable_get('test_verbose_module_hooks')) {
214
    foreach ($modules as $module) {
215
      drupal_set_message(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
216
    }
217
  }
218
}
219

    
220
/**
221
 * Implements hook_boot().
222
 */
223
function system_test_boot() {
224
  watchdog('system_test', 'hook_boot');
225
}
226

    
227
/**
228
 * Implements hook_init().
229
 */
230
function system_test_init() {
231
  // Used by FrontPageTestCase to get the results of drupal_is_front_page().
232
  if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
233
    drupal_set_message(t('On front page.'));
234
  }
235
}
236

    
237
/**
238
 * Implements hook_exit().
239
 */
240
function system_test_exit() {
241
  watchdog('system_test', 'hook_exit');
242
}
243

    
244
/**
245
 * Implements hook_system_info_alter().
246
 */
247
function system_test_system_info_alter(&$info, $file, $type) {
248
  // We need a static otherwise the last test will fail to alter common_test.
249
  static $test;
250
  if (($dependencies = variable_get('dependencies', array())) || $test) {
251
    if ($file->name == 'module_test') {
252
      $info['hidden'] = FALSE;
253
      $info['dependencies'][] = array_shift($dependencies);
254
      variable_set('dependencies', $dependencies);
255
      $test = TRUE;
256
    }
257
    if ($file->name == 'common_test') {
258
      $info['hidden'] = FALSE;
259
      $info['version'] = '7.x-2.4-beta3';
260
    }
261
  }
262

    
263
  // Make the system_dependencies_test visible by default.
264
  if ($file->name == 'system_dependencies_test') {
265
    $info['hidden'] = FALSE;
266
  }
267
  if (in_array($file->name, array(
268
    'system_incompatible_module_version_dependencies_test',
269
    'system_incompatible_core_version_dependencies_test',
270
    'system_incompatible_module_version_test',
271
    'system_incompatible_core_version_test',
272
  ))) {
273
    $info['hidden'] = FALSE;
274
  }
275
  if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
276
    $info['hidden'] = FALSE;
277
  }
278
}
279

    
280
/**
281
 * Try to acquire a named lock and report the outcome.
282
 */
283
function system_test_lock_acquire() {
284
  if (lock_acquire('system_test_lock_acquire')) {
285
    lock_release('system_test_lock_acquire');
286
    return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
287
  }
288
  else {
289
    return 'FALSE: Lock not acquired in system_test_lock_acquire()';
290
  }
291
}
292

    
293
/**
294
 * Try to acquire a specific lock, and then exit.
295
 */
296
function system_test_lock_exit() {
297
  if (lock_acquire('system_test_lock_exit', 900)) {
298
    echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
299
    // The shut-down function should release the lock.
300
    exit();
301
  }
302
  else {
303
    return 'FALSE: Lock not acquired in system_test_lock_exit()';
304
  }
305
}
306

    
307
/**
308
 * Implements hook_page_build().
309
 */
310
function system_test_page_build(&$page) {
311
  $menu_item = menu_get_item();
312
  $main_content_display = &drupal_static('system_main_content_added', FALSE);
313

    
314
  if ($menu_item['path'] == 'system-test/main-content-handling') {
315
    $page['footer'] = drupal_set_page_content();
316
    $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
317
  }
318
  elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
319
    drupal_set_page_content();
320
    $main_content_display = FALSE;
321
  }
322
  elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
323
    drupal_set_page_content();
324
  }
325
}
326

    
327
/**
328
 * Menu callback to test main content fallback().
329
 */
330
function system_test_main_content_fallback() {
331
  return t('Content to test main content fallback');
332
}
333

    
334
/**
335
 * A simple page callback which adds a register shutdown function.
336
 */
337
function system_test_page_shutdown_functions($arg1, $arg2) {
338
  drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
339
}
340

    
341
/**
342
 * Dummy shutdown function which registers another shutdown function.
343
 */
344
function _system_test_first_shutdown_function($arg1, $arg2) {
345
  // Output something, page has already been printed and the session stored
346
  // so we can't use drupal_set_message.
347
  print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
348
  drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
349
}
350

    
351
/**
352
 * Dummy shutdown function.
353
 */
354
function _system_test_second_shutdown_function($arg1, $arg2) {
355
  // Output something, page has already been printed and the session stored
356
  // so we can't use drupal_set_message.
357
  print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
358

    
359
  // Throw an exception with an HTML tag. Since this is called in a shutdown
360
  // function, it will not bubble up to the default exception handler but will
361
  // be caught in _drupal_shutdown_function() and be displayed through
362
  // _drupal_render_exception_safe().
363
  throw new Exception('Drupal is <blink>awesome</blink>.');
364
}
365

    
366
/**
367
 * Implements hook_filetransfer_info().
368
 */
369
function system_test_filetransfer_info() {
370
  return array(
371
    'system_test' => array(
372
      'title' => t('System Test FileTransfer'),
373
      'file' => 'system_test.module',  // Should be a .inc, but for test, ok.
374
      'class' => 'SystemTestFileTransfer',
375
      'weight' => -10,
376
    ),
377
  );
378
}
379

    
380
/**
381
 * Mock FileTransfer object to test the settings form functionality.
382
 */
383
class SystemTestFileTransfer {
384
  public static function factory() {
385
    return new SystemTestFileTransfer;
386
  }
387

    
388
  public function getSettingsForm() {
389
    $form = array();
390
    $form['system_test_username'] = array(
391
      '#type' => 'textfield',
392
      '#title' => t('System Test Username'),
393
    );
394
    return $form;
395
  }
396
}
397

    
398
/**
399
 * Page callback to initialize authorize.php during testing.
400
 *
401
 * @see system_authorized_init().
402
 */
403
function system_test_authorize_init_page($page_title) {
404
  $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
405
  system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
406
  drupal_goto($authorize_url);
407
}