Projet

Général

Profil

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

root / drupal7 / modules / simpletest / tests / system_test.module @ 582db59d

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/drupal-set-message'] = array(
82
    'title' => 'Set messages with drupal_set_message()',
83
    'page callback' => 'system_test_drupal_set_message',
84
    'access callback' => TRUE,
85
    'type' => MENU_CALLBACK,
86
  );
87

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

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

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

    
109
  $items['system-test/shutdown-functions'] = array(
110
    'title' => 'Test main content duplication',
111
    'page callback' => 'system_test_page_shutdown_functions',
112
    'access callback' => TRUE,
113
    'type' => MENU_CALLBACK,
114
  );
115

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

    
123
  $items['system-test/request-destination'] = array(
124
    'title' => 'Test $_REQUEST[\'destination\']',
125
    'page callback' => 'system_test_request_destination',
126
    'access callback' => TRUE,
127
    'type' => MENU_CALLBACK,
128
  );
129

    
130
  return $items;
131
}
132

    
133
function system_test_sleep($seconds) {
134
  sleep($seconds);
135
}
136

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

    
158
function system_test_redirect($code) {
159
  $code = (int) $code;
160
  if ($code != 200) {
161
    // Header names are case-insensitive.
162
    header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
163
    exit;
164
  }
165
  return '';
166
}
167

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

    
192
function system_test_set_header() {
193
  drupal_add_http_header($_GET['name'], $_GET['value']);
194
  return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
195
}
196

    
197
function system_test_redirect_noscheme() {
198
  header("Location: localhost/path", TRUE, 301);
199
  exit;
200
}
201

    
202
function system_test_redirect_noparse() {
203
  header("Location: http:///path", TRUE, 301);
204
  exit;
205
}
206

    
207
function system_test_redirect_invalid_scheme() {
208
  header("Location: ftp://localhost/path", TRUE, 301);
209
  exit;
210
}
211

    
212
/**
213
 * Implements hook_modules_installed().
214
 */
215
function system_test_modules_installed($modules) {
216
  if (variable_get('test_verbose_module_hooks')) {
217
    foreach ($modules as $module) {
218
      drupal_set_message(t('hook_modules_installed fired for @module', array('@module' => $module)));
219
    }
220
  }
221
}
222

    
223
/**
224
 * Implements hook_modules_enabled().
225
 */
226
function system_test_modules_enabled($modules) {
227
  if (variable_get('test_verbose_module_hooks')) {
228
    foreach ($modules as $module) {
229
      drupal_set_message(t('hook_modules_enabled fired for @module', array('@module' => $module)));
230
    }
231
  }
232
}
233

    
234
/**
235
 * Implements hook_modules_disabled().
236
 */
237
function system_test_modules_disabled($modules) {
238
  if (variable_get('test_verbose_module_hooks')) {
239
    foreach ($modules as $module) {
240
      drupal_set_message(t('hook_modules_disabled fired for @module', array('@module' => $module)));
241
    }
242
  }
243
}
244

    
245
/**
246
 * Implements hook_modules_uninstalled().
247
 */
248
function system_test_modules_uninstalled($modules) {
249
  if (variable_get('test_verbose_module_hooks')) {
250
    foreach ($modules as $module) {
251
      drupal_set_message(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
252
    }
253
  }
254
}
255

    
256
/**
257
 * Implements hook_boot().
258
 */
259
function system_test_boot() {
260
  watchdog('system_test', 'hook_boot');
261
}
262

    
263
/**
264
 * Implements hook_init().
265
 */
266
function system_test_init() {
267
  // Used by FrontPageTestCase to get the results of drupal_is_front_page().
268
  if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
269
    drupal_set_message(t('On front page.'));
270
  }
271
}
272

    
273
/**
274
 * Implements hook_exit().
275
 */
276
function system_test_exit() {
277
  watchdog('system_test', 'hook_exit');
278
}
279

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

    
299
  if ($file->name == 'system_project_namespace_test') {
300
    $info['hidden'] = FALSE;
301
  }
302
  // Make the system_dependencies_test visible by default.
303
  if ($file->name == 'system_dependencies_test') {
304
    $info['hidden'] = FALSE;
305
  }
306
  if (in_array($file->name, array(
307
    'system_incompatible_module_version_dependencies_test',
308
    'system_incompatible_core_version_dependencies_test',
309
    'system_incompatible_module_version_test',
310
    'system_incompatible_core_version_test',
311
  ))) {
312
    $info['hidden'] = FALSE;
313
  }
314
  if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
315
    $info['hidden'] = FALSE;
316
  }
317
}
318

    
319
/**
320
 * Try to acquire a named lock and report the outcome.
321
 */
322
function system_test_lock_acquire() {
323
  if (lock_acquire('system_test_lock_acquire')) {
324
    lock_release('system_test_lock_acquire');
325
    return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
326
  }
327
  else {
328
    return 'FALSE: Lock not acquired in system_test_lock_acquire()';
329
  }
330
}
331

    
332
/**
333
 * Try to acquire a specific lock, and then exit.
334
 */
335
function system_test_lock_exit() {
336
  if (lock_acquire('system_test_lock_exit', 900)) {
337
    echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
338
    // The shut-down function should release the lock.
339
    exit();
340
  }
341
  else {
342
    return 'FALSE: Lock not acquired in system_test_lock_exit()';
343
  }
344
}
345

    
346
/**
347
 * Implements hook_page_build().
348
 */
349
function system_test_page_build(&$page) {
350
  $menu_item = menu_get_item();
351
  $main_content_display = &drupal_static('system_main_content_added', FALSE);
352

    
353
  if ($menu_item['path'] == 'system-test/main-content-handling') {
354
    $page['footer'] = drupal_set_page_content();
355
    $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
356
  }
357
  elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
358
    drupal_set_page_content();
359
    $main_content_display = FALSE;
360
  }
361
  elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
362
    drupal_set_page_content();
363
  }
364
}
365

    
366
/**
367
 * Menu callback to test main content fallback().
368
 */
369
function system_test_main_content_fallback() {
370
  return t('Content to test main content fallback');
371
}
372

    
373
/**
374
 * A simple page callback which adds a register shutdown function.
375
 */
376
function system_test_page_shutdown_functions($arg1, $arg2) {
377
  drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
378
}
379

    
380
/**
381
 * Dummy shutdown function which registers another shutdown function.
382
 */
383
function _system_test_first_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('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
387
  drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
388
}
389

    
390
/**
391
 * Dummy shutdown function.
392
 */
393
function _system_test_second_shutdown_function($arg1, $arg2) {
394
  // Output something, page has already been printed and the session stored
395
  // so we can't use drupal_set_message.
396
  print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
397

    
398
  // Throw an exception with an HTML tag. Since this is called in a shutdown
399
  // function, it will not bubble up to the default exception handler but will
400
  // be caught in _drupal_shutdown_function() and be displayed through
401
  // _drupal_render_exception_safe().
402
  throw new Exception('Drupal is <blink>awesome</blink>.');
403
}
404

    
405
/**
406
 * Implements hook_filetransfer_info().
407
 */
408
function system_test_filetransfer_info() {
409
  return array(
410
    'system_test' => array(
411
      'title' => t('System Test FileTransfer'),
412
      'file' => 'system_test.module',  // Should be a .inc, but for test, ok.
413
      'class' => 'SystemTestFileTransfer',
414
      'weight' => -10,
415
    ),
416
  );
417
}
418

    
419
/**
420
 * Mock FileTransfer object to test the settings form functionality.
421
 */
422
class SystemTestFileTransfer {
423
  public static function factory() {
424
    return new SystemTestFileTransfer;
425
  }
426

    
427
  public function getSettingsForm() {
428
    $form = array();
429
    $form['system_test_username'] = array(
430
      '#type' => 'textfield',
431
      '#title' => t('System Test Username'),
432
    );
433
    return $form;
434
  }
435
}
436

    
437
/**
438
 * Page callback to initialize authorize.php during testing.
439
 *
440
 * @see system_authorized_init().
441
 */
442
function system_test_authorize_init_page($page_title) {
443
  $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
444
  system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
445
  drupal_goto($authorize_url);
446
}
447

    
448
/**
449
 * Sets two messages and removes the first one before the messages are displayed.
450
 */
451
function system_test_drupal_set_message() {
452
  // Set two messages.
453
  drupal_set_message('First message (removed).');
454
  drupal_set_message('Second message (not removed).');
455

    
456
  // Remove the first.
457
  unset($_SESSION['messages']['status'][0]);
458

    
459
  return '';
460
}
461

    
462
/**
463
 * Page callback to print out $_GET['destination'] for testing.
464
 */
465
function system_test_get_destination() {
466
  if (isset($_GET['destination'])) {
467
    print $_GET['destination'];
468
  }
469
  // No need to render the whole page, we are just interested in this bit of
470
  // information.
471
  exit;
472
}
473

    
474
/**
475
 * Page callback to print out $_REQUEST['destination'] for testing.
476
 */
477
function system_test_request_destination() {
478
  if (isset($_REQUEST['destination'])) {
479
    print $_REQUEST['destination'];
480
  }
481
  // No need to render the whole page, we are just interested in this bit of
482
  // information.
483
  exit;
484
}