Projet

Général

Profil

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

root / drupal7 / modules / simpletest / tests / system_test.module @ 76597ebf

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
  // The Authorization HTTP header is forwarded via Drupal's .htaccess file even
118
  // for PHP CGI SAPIs.
119
  if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
120
    $authorization_header = $_SERVER['HTTP_AUTHORIZATION'];
121
  }
122
  // If using CGI on Apache with mod_rewrite, the forwarded HTTP header appears
123
  // in the redirected HTTP headers. See
124
  // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/ServerBag.php#L61
125
  elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
126
    $authorization_header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
127
  }
128
  // Resemble PHP_AUTH_USER and PHP_AUTH_PW for a Basic authentication from
129
  // the HTTP_AUTHORIZATION header. See
130
  // http://www.php.net/manual/features.http-auth.php
131
  list($user, $pw) = explode(':', base64_decode(substr($authorization_header, 6)));
132
  $output = t('Username is @username.', array('@username' => $user));
133
  $output .= t('Password is @password.', array('@password' => $pw));
134
  return $output;
135
}
136

    
137
function system_test_redirect($code) {
138
  $code = (int) $code;
139
  if ($code != 200) {
140
    // Header names are case-insensitive.
141
    header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
142
    exit;
143
  }
144
  return '';
145
}
146

    
147
/**
148
 * Menu callback; sends a redirect header to itself until $count argument is 0.
149
 *
150
 * Emulates the variable number of redirects (given by initial $count argument)
151
 * to the final destination URL by continuous sending of 301 HTTP redirect
152
 * headers to itself together with decrementing the $count parameter until the
153
 * $count parameter reaches 0. After that it returns an empty string to render
154
 * the final destination page.
155
 *
156
 * @param $count
157
 *   The count of redirects left until the final destination page.
158
 *
159
 * @returns
160
 *   The location redirect if the $count > 0, otherwise an empty string.
161
 */
162
function system_test_multiple_redirects($count) {
163
  $count = (int) $count;
164
  if ($count > 0) {
165
    header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
166
    exit;
167
  }
168
  return '';
169
}
170

    
171
function system_test_set_header() {
172
  drupal_add_http_header($_GET['name'], $_GET['value']);
173
  return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
174
}
175

    
176
function system_test_redirect_noscheme() {
177
  header("Location: localhost/path", TRUE, 301);
178
  exit;
179
}
180

    
181
function system_test_redirect_noparse() {
182
  header("Location: http:///path", TRUE, 301);
183
  exit;
184
}
185

    
186
function system_test_redirect_invalid_scheme() {
187
  header("Location: ftp://localhost/path", TRUE, 301);
188
  exit;
189
}
190

    
191
/**
192
 * Implements hook_modules_installed().
193
 */
194
function system_test_modules_installed($modules) {
195
  if (variable_get('test_verbose_module_hooks')) {
196
    foreach ($modules as $module) {
197
      drupal_set_message(t('hook_modules_installed fired for @module', array('@module' => $module)));
198
    }
199
  }
200
}
201

    
202
/**
203
 * Implements hook_modules_enabled().
204
 */
205
function system_test_modules_enabled($modules) {
206
  if (variable_get('test_verbose_module_hooks')) {
207
    foreach ($modules as $module) {
208
      drupal_set_message(t('hook_modules_enabled fired for @module', array('@module' => $module)));
209
    }
210
  }
211
}
212

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

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

    
235
/**
236
 * Implements hook_boot().
237
 */
238
function system_test_boot() {
239
  watchdog('system_test', 'hook_boot');
240
}
241

    
242
/**
243
 * Implements hook_init().
244
 */
245
function system_test_init() {
246
  // Used by FrontPageTestCase to get the results of drupal_is_front_page().
247
  if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
248
    drupal_set_message(t('On front page.'));
249
  }
250
}
251

    
252
/**
253
 * Implements hook_exit().
254
 */
255
function system_test_exit() {
256
  watchdog('system_test', 'hook_exit');
257
}
258

    
259
/**
260
 * Implements hook_system_info_alter().
261
 */
262
function system_test_system_info_alter(&$info, $file, $type) {
263
  // We need a static otherwise the last test will fail to alter common_test.
264
  static $test;
265
  if (($dependencies = variable_get('dependencies', array())) || $test) {
266
    if ($file->name == 'module_test') {
267
      $info['hidden'] = FALSE;
268
      $info['dependencies'][] = array_shift($dependencies);
269
      variable_set('dependencies', $dependencies);
270
      $test = TRUE;
271
    }
272
    if ($file->name == 'common_test') {
273
      $info['hidden'] = FALSE;
274
      $info['version'] = '7.x-2.4-beta3';
275
    }
276
  }
277

    
278
  // Make the system_dependencies_test visible by default.
279
  if ($file->name == 'system_dependencies_test') {
280
    $info['hidden'] = FALSE;
281
  }
282
  if (in_array($file->name, array(
283
    'system_incompatible_module_version_dependencies_test',
284
    'system_incompatible_core_version_dependencies_test',
285
    'system_incompatible_module_version_test',
286
    'system_incompatible_core_version_test',
287
  ))) {
288
    $info['hidden'] = FALSE;
289
  }
290
  if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
291
    $info['hidden'] = FALSE;
292
  }
293
}
294

    
295
/**
296
 * Try to acquire a named lock and report the outcome.
297
 */
298
function system_test_lock_acquire() {
299
  if (lock_acquire('system_test_lock_acquire')) {
300
    lock_release('system_test_lock_acquire');
301
    return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
302
  }
303
  else {
304
    return 'FALSE: Lock not acquired in system_test_lock_acquire()';
305
  }
306
}
307

    
308
/**
309
 * Try to acquire a specific lock, and then exit.
310
 */
311
function system_test_lock_exit() {
312
  if (lock_acquire('system_test_lock_exit', 900)) {
313
    echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
314
    // The shut-down function should release the lock.
315
    exit();
316
  }
317
  else {
318
    return 'FALSE: Lock not acquired in system_test_lock_exit()';
319
  }
320
}
321

    
322
/**
323
 * Implements hook_page_build().
324
 */
325
function system_test_page_build(&$page) {
326
  $menu_item = menu_get_item();
327
  $main_content_display = &drupal_static('system_main_content_added', FALSE);
328

    
329
  if ($menu_item['path'] == 'system-test/main-content-handling') {
330
    $page['footer'] = drupal_set_page_content();
331
    $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
332
  }
333
  elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
334
    drupal_set_page_content();
335
    $main_content_display = FALSE;
336
  }
337
  elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
338
    drupal_set_page_content();
339
  }
340
}
341

    
342
/**
343
 * Menu callback to test main content fallback().
344
 */
345
function system_test_main_content_fallback() {
346
  return t('Content to test main content fallback');
347
}
348

    
349
/**
350
 * A simple page callback which adds a register shutdown function.
351
 */
352
function system_test_page_shutdown_functions($arg1, $arg2) {
353
  drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
354
}
355

    
356
/**
357
 * Dummy shutdown function which registers another shutdown function.
358
 */
359
function _system_test_first_shutdown_function($arg1, $arg2) {
360
  // Output something, page has already been printed and the session stored
361
  // so we can't use drupal_set_message.
362
  print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
363
  drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
364
}
365

    
366
/**
367
 * Dummy shutdown function.
368
 */
369
function _system_test_second_shutdown_function($arg1, $arg2) {
370
  // Output something, page has already been printed and the session stored
371
  // so we can't use drupal_set_message.
372
  print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
373

    
374
  // Throw an exception with an HTML tag. Since this is called in a shutdown
375
  // function, it will not bubble up to the default exception handler but will
376
  // be caught in _drupal_shutdown_function() and be displayed through
377
  // _drupal_render_exception_safe().
378
  throw new Exception('Drupal is <blink>awesome</blink>.');
379
}
380

    
381
/**
382
 * Implements hook_filetransfer_info().
383
 */
384
function system_test_filetransfer_info() {
385
  return array(
386
    'system_test' => array(
387
      'title' => t('System Test FileTransfer'),
388
      'file' => 'system_test.module',  // Should be a .inc, but for test, ok.
389
      'class' => 'SystemTestFileTransfer',
390
      'weight' => -10,
391
    ),
392
  );
393
}
394

    
395
/**
396
 * Mock FileTransfer object to test the settings form functionality.
397
 */
398
class SystemTestFileTransfer {
399
  public static function factory() {
400
    return new SystemTestFileTransfer;
401
  }
402

    
403
  public function getSettingsForm() {
404
    $form = array();
405
    $form['system_test_username'] = array(
406
      '#type' => 'textfield',
407
      '#title' => t('System Test Username'),
408
    );
409
    return $form;
410
  }
411
}
412

    
413
/**
414
 * Page callback to initialize authorize.php during testing.
415
 *
416
 * @see system_authorized_init().
417
 */
418
function system_test_authorize_init_page($page_title) {
419
  $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
420
  system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
421
  drupal_goto($authorize_url);
422
}