Project

General

Profile

Paste
Download (18.2 KB) Statistics
| Branch: | Revision:

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

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
  $items['system-test/drupal-get-filename'] = array(
131
    'title' => 'Test drupal_get_filename()',
132
    'page callback' => 'system_test_drupal_get_filename',
133
    'access callback' => TRUE,
134
    'type' => MENU_CALLBACK,
135
  );
136

    
137
  $items['system-test/drupal-get-filename-with-schema-rebuild'] = array(
138
    'title' => 'Test drupal_get_filename() with a schema rebuild',
139
    'page callback' => 'system_test_drupal_get_filename_with_schema_rebuild',
140
    'access callback' => TRUE,
141
    'type' => MENU_CALLBACK,
142
  );
143

    
144
  return $items;
145
}
146

    
147
function system_test_sleep($seconds) {
148
  sleep($seconds);
149
}
150

    
151
function system_test_basic_auth_page() {
152
  // The Authorization HTTP header is forwarded via Drupal's .htaccess file even
153
  // for PHP CGI SAPIs.
154
  if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
155
    $authorization_header = $_SERVER['HTTP_AUTHORIZATION'];
156
  }
157
  // If using CGI on Apache with mod_rewrite, the forwarded HTTP header appears
158
  // in the redirected HTTP headers. See
159
  // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/ServerBag.php#L61
160
  elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
161
    $authorization_header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
162
  }
163
  // Resemble PHP_AUTH_USER and PHP_AUTH_PW for a Basic authentication from
164
  // the HTTP_AUTHORIZATION header. See
165
  // http://www.php.net/manual/features.http-auth.php
166
  list($user, $pw) = explode(':', base64_decode(substr($authorization_header, 6)));
167
  $output = t('Username is @username.', array('@username' => $user));
168
  $output .= t('Password is @password.', array('@password' => $pw));
169
  return $output;
170
}
171

    
172
function system_test_redirect($code) {
173
  $code = (int) $code;
174
  if ($code != 200) {
175
    // Header names are case-insensitive.
176
    header("locaTION: " . url('system-test/redirect/200', array('absolute' => TRUE)), TRUE, $code);
177
    exit;
178
  }
179
  return '';
180
}
181

    
182
/**
183
 * Menu callback; sends a redirect header to itself until $count argument is 0.
184
 *
185
 * Emulates the variable number of redirects (given by initial $count argument)
186
 * to the final destination URL by continuous sending of 301 HTTP redirect
187
 * headers to itself together with decrementing the $count parameter until the
188
 * $count parameter reaches 0. After that it returns an empty string to render
189
 * the final destination page.
190
 *
191
 * @param $count
192
 *   The count of redirects left until the final destination page.
193
 *
194
 * @returns
195
 *   The location redirect if the $count > 0, otherwise an empty string.
196
 */
197
function system_test_multiple_redirects($count) {
198
  $count = (int) $count;
199
  if ($count > 0) {
200
    header("location: " . url('system-test/multiple-redirects/' . --$count, array('absolute' => TRUE)), TRUE, 301);
201
    exit;
202
  }
203
  return '';
204
}
205

    
206
function system_test_set_header() {
207
  drupal_add_http_header($_GET['name'], $_GET['value']);
208
  return t('The following header was set: %name: %value', array('%name' => $_GET['name'], '%value' => $_GET['value']));
209
}
210

    
211
function system_test_redirect_noscheme() {
212
  header("Location: localhost/path", TRUE, 301);
213
  exit;
214
}
215

    
216
function system_test_redirect_noparse() {
217
  header("Location: http:///path", TRUE, 301);
218
  exit;
219
}
220

    
221
function system_test_redirect_invalid_scheme() {
222
  header("Location: ftp://localhost/path", TRUE, 301);
223
  exit;
224
}
225

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

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

    
248
/**
249
 * Implements hook_modules_disabled().
250
 */
251
function system_test_modules_disabled($modules) {
252
  if (variable_get('test_verbose_module_hooks')) {
253
    foreach ($modules as $module) {
254
      drupal_set_message(t('hook_modules_disabled fired for @module', array('@module' => $module)));
255
    }
256
  }
257
}
258

    
259
/**
260
 * Implements hook_modules_uninstalled().
261
 */
262
function system_test_modules_uninstalled($modules) {
263
  if (variable_get('test_verbose_module_hooks')) {
264
    foreach ($modules as $module) {
265
      drupal_set_message(t('hook_modules_uninstalled fired for @module', array('@module' => $module)));
266
    }
267
  }
268
}
269

    
270
/**
271
 * Implements hook_boot().
272
 */
273
function system_test_boot() {
274
  watchdog('system_test', 'hook_boot');
275
}
276

    
277
/**
278
 * Implements hook_init().
279
 */
280
function system_test_init() {
281
  // Used by FrontPageTestCase to get the results of drupal_is_front_page().
282
  if (variable_get('front_page_output', 0) && drupal_is_front_page()) {
283
    drupal_set_message(t('On front page.'));
284
  }
285
}
286

    
287
/**
288
 * Implements hook_exit().
289
 */
290
function system_test_exit() {
291
  watchdog('system_test', 'hook_exit');
292
}
293

    
294
/**
295
 * Implements hook_system_info_alter().
296
 */
297
function system_test_system_info_alter(&$info, $file, $type) {
298
  // We need a static otherwise the last test will fail to alter common_test.
299
  static $test;
300
  if (($dependencies = variable_get('dependencies', array())) || $test) {
301
    if ($file->name == 'module_test') {
302
      $info['hidden'] = FALSE;
303
      $info['dependencies'][] = array_shift($dependencies);
304
      variable_set('dependencies', $dependencies);
305
      $test = TRUE;
306
    }
307
    if ($file->name == 'common_test') {
308
      $info['hidden'] = FALSE;
309
      $info['version'] = '7.x-2.4-beta3';
310
    }
311
  }
312

    
313
  if ($file->name == 'system_project_namespace_test') {
314
    $info['hidden'] = FALSE;
315
  }
316
  // Make the system_dependencies_test visible by default.
317
  if ($file->name == 'system_dependencies_test') {
318
    $info['hidden'] = FALSE;
319
  }
320
  if (in_array($file->name, array(
321
    'system_incompatible_module_version_dependencies_test',
322
    'system_incompatible_core_version_dependencies_test',
323
    'system_incompatible_module_version_test',
324
    'system_incompatible_core_version_test',
325
  ))) {
326
    $info['hidden'] = FALSE;
327
  }
328
  if ($file->name == 'requirements1_test' || $file->name == 'requirements2_test') {
329
    $info['hidden'] = FALSE;
330
  }
331
}
332

    
333
/**
334
 * Try to acquire a named lock and report the outcome.
335
 */
336
function system_test_lock_acquire() {
337
  if (lock_acquire('system_test_lock_acquire')) {
338
    lock_release('system_test_lock_acquire');
339
    return 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
340
  }
341
  else {
342
    return 'FALSE: Lock not acquired in system_test_lock_acquire()';
343
  }
344
}
345

    
346
/**
347
 * Try to acquire a specific lock, and then exit.
348
 */
349
function system_test_lock_exit() {
350
  if (lock_acquire('system_test_lock_exit', 900)) {
351
    echo 'TRUE: Lock successfully acquired in system_test_lock_exit()';
352
    // The shut-down function should release the lock.
353
    exit();
354
  }
355
  else {
356
    return 'FALSE: Lock not acquired in system_test_lock_exit()';
357
  }
358
}
359

    
360
/**
361
 * Implements hook_page_build().
362
 */
363
function system_test_page_build(&$page) {
364
  $menu_item = menu_get_item();
365
  $main_content_display = &drupal_static('system_main_content_added', FALSE);
366

    
367
  if ($menu_item['path'] == 'system-test/main-content-handling') {
368
    $page['footer'] = drupal_set_page_content();
369
    $page['footer']['main']['#markup'] = '<div id="system-test-content">' . $page['footer']['main']['#markup'] . '</div>';
370
  }
371
  elseif ($menu_item['path'] == 'system-test/main-content-fallback') {
372
    drupal_set_page_content();
373
    $main_content_display = FALSE;
374
  }
375
  elseif ($menu_item['path'] == 'system-test/main-content-duplication') {
376
    drupal_set_page_content();
377
  }
378
}
379

    
380
/**
381
 * Menu callback to test main content fallback().
382
 */
383
function system_test_main_content_fallback() {
384
  return t('Content to test main content fallback');
385
}
386

    
387
/**
388
 * A simple page callback which adds a register shutdown function.
389
 */
390
function system_test_page_shutdown_functions($arg1, $arg2) {
391
  drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
392
}
393

    
394
/**
395
 * Dummy shutdown function which registers another shutdown function.
396
 */
397
function _system_test_first_shutdown_function($arg1, $arg2) {
398
  // Output something, page has already been printed and the session stored
399
  // so we can't use drupal_set_message.
400
  print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
401
  drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
402
}
403

    
404
/**
405
 * Dummy shutdown function.
406
 */
407
function _system_test_second_shutdown_function($arg1, $arg2) {
408
  // Output something, page has already been printed and the session stored
409
  // so we can't use drupal_set_message.
410
  print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
411

    
412
  // Throw an exception with an HTML tag. Since this is called in a shutdown
413
  // function, it will not bubble up to the default exception handler but will
414
  // be caught in _drupal_shutdown_function() and be displayed through
415
  // _drupal_render_exception_safe().
416
  throw new Exception('Drupal is <blink>awesome</blink>.');
417
}
418

    
419
/**
420
 * Implements hook_filetransfer_info().
421
 */
422
function system_test_filetransfer_info() {
423
  return array(
424
    'system_test' => array(
425
      'title' => t('System Test FileTransfer'),
426
      'file' => 'system_test.module',  // Should be a .inc, but for test, ok.
427
      'class' => 'SystemTestFileTransfer',
428
      'weight' => -10,
429
    ),
430
  );
431
}
432

    
433
/**
434
 * Mock FileTransfer object to test the settings form functionality.
435
 */
436
class SystemTestFileTransfer {
437
  public static function factory() {
438
    return new SystemTestFileTransfer;
439
  }
440

    
441
  public function getSettingsForm() {
442
    $form = array();
443
    $form['system_test_username'] = array(
444
      '#type' => 'textfield',
445
      '#title' => t('System Test Username'),
446
    );
447
    return $form;
448
  }
449
}
450

    
451
/**
452
 * Page callback to initialize authorize.php during testing.
453
 *
454
 * @see system_authorized_init().
455
 */
456
function system_test_authorize_init_page($page_title) {
457
  $authorize_url = $GLOBALS['base_url'] . '/authorize.php';
458
  system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
459
  drupal_goto($authorize_url);
460
}
461

    
462
/**
463
 * Sets two messages and removes the first one before the messages are displayed.
464
 */
465
function system_test_drupal_set_message() {
466
  // Set two messages.
467
  drupal_set_message('First message (removed).');
468
  drupal_set_message('Second message (not removed).');
469

    
470
  // Remove the first.
471
  unset($_SESSION['messages']['status'][0]);
472

    
473
  return '';
474
}
475

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

    
488
/**
489
 * Page callback to print out $_REQUEST['destination'] for testing.
490
 */
491
function system_test_request_destination() {
492
  if (isset($_REQUEST['destination'])) {
493
    print $_REQUEST['destination'];
494
  }
495
  // No need to render the whole page, we are just interested in this bit of
496
  // information.
497
  exit;
498
}
499

    
500
/**
501
 * Page callback to run drupal_get_filename() on a particular module.
502
 */
503
function system_test_drupal_get_filename() {
504
  // Prevent SimpleTest from failing as a result of the expected PHP warnings
505
  // this function causes. Any warnings will be recorded in the database logs
506
  // for examination by the tests.
507
  define('SIMPLETEST_COLLECT_ERRORS', FALSE);
508

    
509
  $module_name = variable_get('system_test_drupal_get_filename_test_module_name');
510
  drupal_get_filename('module', $module_name);
511

    
512
  return '';
513
}
514

    
515
/**
516
 * Page callback to run drupal_get_filename() and do a schema rebuild.
517
 */
518
function system_test_drupal_get_filename_with_schema_rebuild() {
519
  // Prevent SimpleTest from failing as a result of the expected PHP warnings
520
  // this function causes.
521
  define('SIMPLETEST_COLLECT_ERRORS', FALSE);
522

    
523
  // Record the original database tables from drupal_get_schema().
524
  variable_set('system_test_drupal_get_filename_with_schema_rebuild_original_tables', array_keys(drupal_get_schema(NULL, TRUE)));
525

    
526
  // Trigger system_test_schema() and system_test_watchdog() to perform an
527
  // attempted recursive rebuild when drupal_get_schema() is called. See
528
  // BootstrapGetFilenameWebTestCase::testRecursiveRebuilds().
529
  variable_set('system_test_drupal_get_filename_attempt_recursive_rebuild', TRUE);
530
  drupal_get_schema(NULL, TRUE);
531

    
532
  return '';
533
}
534

    
535
/**
536
 * Implements hook_watchdog().
537
 */
538
function system_test_watchdog($log_entry) {
539
  // If an attempted recursive schema rebuild has been triggered by
540
  // system_test_drupal_get_filename_with_schema_rebuild(), perform the rebuild
541
  // in response to the missing file message triggered by system_test_schema().
542
  if (!variable_get('system_test_drupal_get_filename_attempt_recursive_rebuild')) {
543
    return;
544
  }
545
  if ($log_entry['type'] != 'php' || $log_entry['severity'] != WATCHDOG_WARNING) {
546
    return;
547
  }
548
  $module_name = variable_get('system_test_drupal_get_filename_test_module_name');
549
  if (!isset($log_entry['variables']['!message']) || strpos($log_entry['variables']['!message'], format_string('The following module is missing from the file system: %name', array('%name' => $module_name))) === FALSE) {
550
    return;
551
  }
552
  variable_set('system_test_drupal_get_filename_with_schema_rebuild_final_tables', array_keys(drupal_get_schema()));
553
}
554

    
555
/**
556
 * Implements hook_module_implements_alter().
557
 */
558
function system_test_module_implements_alter(&$implementations, $hook) {
559
  // For BootstrapGetFilenameWebTestCase::testRecursiveRebuilds() to work
560
  // correctly, this module's hook_schema() implementation cannot be either the
561
  // first implementation (since that would trigger a potential recursive
562
  // rebuild before anything is in the drupal_get_schema() cache) or the last
563
  // implementation (since that would trigger a potential recursive rebuild
564
  // after the cache is already complete). So put it somewhere in the middle.
565
  if ($hook == 'schema') {
566
    $group = $implementations['system_test'];
567
    unset($implementations['system_test']);
568
    $count = count($implementations);
569
    $implementations = array_merge(array_slice($implementations, 0, $count / 2, TRUE), array('system_test' => $group), array_slice($implementations, $count / 2, NULL, TRUE));
570
  }
571
}