Projet

Général

Profil

Paste
Télécharger (13,9 ko) Statistiques
| Branche: | Révision:

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

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
  $items['system-test/get-destination'] = array(
110
    'title' => 'Test $_GET[\'destination\']',
111
    'page callback' => 'system_test_get_destination',
112
    'access callback' => TRUE,
113
    'type' => MENU_CALLBACK,
114
  );
115

    
116
  $items['system-test/request-destination'] = array(
117
    'title' => 'Test $_REQUEST[\'destination\']',
118
    'page callback' => 'system_test_request_destination',
119
    'access callback' => TRUE,
120
    'type' => MENU_CALLBACK,
121
  );
122

    
123
  return $items;
124
}
125

    
126
function system_test_sleep($seconds) {
127
  sleep($seconds);
128
}
129

    
130
function system_test_basic_auth_page() {
131
  // The Authorization HTTP header is forwarded via Drupal's .htaccess file even
132
  // for PHP CGI SAPIs.
133
  if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
134
    $authorization_header = $_SERVER['HTTP_AUTHORIZATION'];
135
  }
136
  // If using CGI on Apache with mod_rewrite, the forwarded HTTP header appears
137
  // in the redirected HTTP headers. See
138
  // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/ServerBag.php#L61
139
  elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
140
    $authorization_header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
141
  }
142
  // Resemble PHP_AUTH_USER and PHP_AUTH_PW for a Basic authentication from
143
  // the HTTP_AUTHORIZATION header. See
144
  // http://www.php.net/manual/features.http-auth.php
145
  list($user, $pw) = explode(':', base64_decode(substr($authorization_header, 6)));
146
  $output = t('Username is @username.', array('@username' => $user));
147
  $output .= t('Password is @password.', array('@password' => $pw));
148
  return $output;
149
}
150

    
151
function system_test_redirect($code) {
152
  $code = (int) $code;
153
  if ($code != 200) {
154
    // Header names are case-insensitive.
155
    header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
156
    exit;
157
  }
158
  return '';
159
}
160

    
161
/**
162
 * Menu callback; sends a redirect header to itself until $count argument is 0.
163
 *
164
 * Emulates the variable number of redirects (given by initial $count argument)
165
 * to the final destination URL by continuous sending of 301 HTTP redirect
166
 * headers to itself together with decrementing the $count parameter until the
167
 * $count parameter reaches 0. After that it returns an empty string to render
168
 * the final destination page.
169
 *
170
 * @param $count
171
 *   The count of redirects left until the final destination page.
172
 *
173
 * @returns
174
 *   The location redirect if the $count > 0, otherwise an empty string.
175
 */
176
function system_test_multiple_redirects($count) {
177
  $count = (int) $count;
178
  if ($count > 0) {
179
    header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
180
    exit;
181
  }
182
  return '';
183
}
184

    
185
function system_test_set_header() {
186
  drupal_add_http_header($_GET['name'], $_GET['value']);
187
  return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
188
}
189

    
190
function system_test_redirect_noscheme() {
191
  header("Location: localhost/path", TRUE, 301);
192
  exit;
193
}
194

    
195
function system_test_redirect_noparse() {
196
  header("Location: http:///path", TRUE, 301);
197
  exit;
198
}
199

    
200
function system_test_redirect_invalid_scheme() {
201
  header("Location: ftp://localhost/path", TRUE, 301);
202
  exit;
203
}
204

    
205
/**
206
 * Implements hook_modules_installed().
207
 */
208
function system_test_modules_installed($modules) {
209
  if (variable_get('test_verbose_module_hooks')) {
210
    foreach ($modules as $module) {
211
      drupal_set_message(t('hook_modules_installed fired for @module', array('@module' => $module)));
212
    }
213
  }
214
}
215

    
216
/**
217
 * Implements hook_modules_enabled().
218
 */
219
function system_test_modules_enabled($modules) {
220
  if (variable_get('test_verbose_module_hooks')) {
221
    foreach ($modules as $module) {
222
      drupal_set_message(t('hook_modules_enabled fired for @module', array('@module' => $module)));
223
    }
224
  }
225
}
226

    
227
/**
228
 * Implements hook_modules_disabled().
229
 */
230
function system_test_modules_disabled($modules) {
231
  if (variable_get('test_verbose_module_hooks')) {
232
    foreach ($modules as $module) {
233
      drupal_set_message(t('hook_modules_disabled fired for @module', array('@module' => $module)));
234
    }
235
  }
236
}
237

    
238
/**
239
 * Implements hook_modules_uninstalled().
240
 */
241
function system_test_modules_uninstalled($modules) {
242
  if (variable_get('test_verbose_module_hooks')) {
243
    foreach ($modules as $module) {
244
      drupal_set_message(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
245
    }
246
  }
247
}
248

    
249
/**
250
 * Implements hook_boot().
251
 */
252
function system_test_boot() {
253
  watchdog('system_test', 'hook_boot');
254
}
255

    
256
/**
257
 * Implements hook_init().
258
 */
259
function system_test_init() {
260
  // Used by FrontPageTestCase to get the results of drupal_is_front_page().
261
  if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
262
    drupal_set_message(t('On front page.'));
263
  }
264
}
265

    
266
/**
267
 * Implements hook_exit().
268
 */
269
function system_test_exit() {
270
  watchdog('system_test', 'hook_exit');
271
}
272

    
273
/**
274
 * Implements hook_system_info_alter().
275
 */
276
function system_test_system_info_alter(&$info, $file, $type) {
277
  // We need a static otherwise the last test will fail to alter common_test.
278
  static $test;
279
  if (($dependencies = variable_get('dependencies', array())) || $test) {
280
    if ($file->name == 'module_test') {
281
      $info['hidden'] = FALSE;
282
      $info['dependencies'][] = array_shift($dependencies);
283
      variable_set('dependencies', $dependencies);
284
      $test = TRUE;
285
    }
286
    if ($file->name == 'common_test') {
287
      $info['hidden'] = FALSE;
288
      $info['version'] = '7.x-2.4-beta3';
289
    }
290
  }
291

    
292
  // Make the system_dependencies_test visible by default.
293
  if ($file->name == 'system_dependencies_test') {
294
    $info['hidden'] = FALSE;
295
  }
296
  if (in_array($file->name, array(
297
    'system_incompatible_module_version_dependencies_test',
298
    'system_incompatible_core_version_dependencies_test',
299
    'system_incompatible_module_version_test',
300
    'system_incompatible_core_version_test',
301
  ))) {
302
    $info['hidden'] = FALSE;
303
  }
304
  if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
305
    $info['hidden'] = FALSE;
306
  }
307
}
308

    
309
/**
310
 * Try to acquire a named lock and report the outcome.
311
 */
312
function system_test_lock_acquire() {
313
  if (lock_acquire('system_test_lock_acquire')) {
314
    lock_release('system_test_lock_acquire');
315
    return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
316
  }
317
  else {
318
    return 'FALSE: Lock not acquired in system_test_lock_acquire()';
319
  }
320
}
321

    
322
/**
323
 * Try to acquire a specific lock, and then exit.
324
 */
325
function system_test_lock_exit() {
326
  if (lock_acquire('system_test_lock_exit', 900)) {
327
    echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
328
    // The shut-down function should release the lock.
329
    exit();
330
  }
331
  else {
332
    return 'FALSE: Lock not acquired in system_test_lock_exit()';
333
  }
334
}
335

    
336
/**
337
 * Implements hook_page_build().
338
 */
339
function system_test_page_build(&$page) {
340
  $menu_item = menu_get_item();
341
  $main_content_display = &drupal_static('system_main_content_added', FALSE);
342

    
343
  if ($menu_item['path'] == 'system-test/main-content-handling') {
344
    $page['footer'] = drupal_set_page_content();
345
    $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
346
  }
347
  elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
348
    drupal_set_page_content();
349
    $main_content_display = FALSE;
350
  }
351
  elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
352
    drupal_set_page_content();
353
  }
354
}
355

    
356
/**
357
 * Menu callback to test main content fallback().
358
 */
359
function system_test_main_content_fallback() {
360
  return t('Content to test main content fallback');
361
}
362

    
363
/**
364
 * A simple page callback which adds a register shutdown function.
365
 */
366
function system_test_page_shutdown_functions($arg1, $arg2) {
367
  drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
368
}
369

    
370
/**
371
 * Dummy shutdown function which registers another shutdown function.
372
 */
373
function _system_test_first_shutdown_function($arg1, $arg2) {
374
  // Output something, page has already been printed and the session stored
375
  // so we can't use drupal_set_message.
376
  print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
377
  drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
378
}
379

    
380
/**
381
 * Dummy shutdown function.
382
 */
383
function _system_test_second_shutdown_function($arg1, $arg2) {
384
  // Output something, page has already been printed and the session stored
385
  // so we can't use drupal_set_message.
386
  print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
387

    
388
  // Throw an exception with an HTML tag. Since this is called in a shutdown
389
  // function, it will not bubble up to the default exception handler but will
390
  // be caught in _drupal_shutdown_function() and be displayed through
391
  // _drupal_render_exception_safe().
392
  throw new Exception('Drupal is <blink>awesome</blink>.');
393
}
394

    
395
/**
396
 * Implements hook_filetransfer_info().
397
 */
398
function system_test_filetransfer_info() {
399
  return array(
400
    'system_test' => array(
401
      'title' => t('System Test FileTransfer'),
402
      'file' => 'system_test.module',  // Should be a .inc, but for test, ok.
403
      'class' => 'SystemTestFileTransfer',
404
      'weight' => -10,
405
    ),
406
  );
407
}
408

    
409
/**
410
 * Mock FileTransfer object to test the settings form functionality.
411
 */
412
class SystemTestFileTransfer {
413
  public static function factory() {
414
    return new SystemTestFileTransfer;
415
  }
416

    
417
  public function getSettingsForm() {
418
    $form = array();
419
    $form['system_test_username'] = array(
420
      '#type' => 'textfield',
421
      '#title' => t('System Test Username'),
422
    );
423
    return $form;
424
  }
425
}
426

    
427
/**
428
 * Page callback to initialize authorize.php during testing.
429
 *
430
 * @see system_authorized_init().
431
 */
432
function system_test_authorize_init_page($page_title) {
433
  $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
434
  system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
435
  drupal_goto($authorize_url);
436
}
437

    
438
/**
439
 * Page callback to print out $_GET['destination'] for testing.
440
 */
441
function system_test_get_destination() {
442
  if (isset($_GET['destination'])) {
443
    print $_GET['destination'];
444
  }
445
  // No need to render the whole page, we are just interested in this bit of
446
  // information.
447
  exit;
448
}
449

    
450
/**
451
 * Page callback to print out $_REQUEST['destination'] for testing.
452
 */
453
function system_test_request_destination() {
454
  if (isset($_REQUEST['destination'])) {
455
    print $_REQUEST['destination'];
456
  }
457
  // No need to render the whole page, we are just interested in this bit of
458
  // information.
459
  exit;
460
}