Projet

Général

Profil

Paste
Télécharger (24,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / bootstrap.test @ f7a2490e

1
<?php
2

    
3
class BootstrapIPAddressTestCase extends DrupalWebTestCase {
4

    
5
  public static function getInfo() {
6
    return array(
7
      'name' => 'IP address and HTTP_HOST test',
8
      'description' => 'Get the IP address from the current visitor from the server variables, check hostname validation.',
9
      'group' => 'Bootstrap'
10
    );
11
  }
12

    
13
  function setUp() {
14
    $this->oldserver = $_SERVER;
15

    
16
    $this->remote_ip = '127.0.0.1';
17
    $this->proxy_ip = '127.0.0.2';
18
    $this->proxy2_ip = '127.0.0.3';
19
    $this->forwarded_ip = '127.0.0.4';
20
    $this->cluster_ip = '127.0.0.5';
21
    $this->untrusted_ip = '0.0.0.0';
22

    
23
    drupal_static_reset('ip_address');
24

    
25
    $_SERVER['REMOTE_ADDR'] = $this->remote_ip;
26
    unset($_SERVER['HTTP_X_FORWARDED_FOR']);
27
    unset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']);
28

    
29
    parent::setUp();
30
  }
31

    
32
  function tearDown() {
33
    $_SERVER = $this->oldserver;
34
    drupal_static_reset('ip_address');
35
    parent::tearDown();
36
  }
37

    
38
  /**
39
   * test IP Address and hostname
40
   */
41
  function testIPAddressHost() {
42
    // Test the normal IP address.
43
    $this->assertTrue(
44
      ip_address() == $this->remote_ip,
45
      'Got remote IP address.'
46
    );
47

    
48
    // Proxy forwarding on but no proxy addresses defined.
49
    variable_set('reverse_proxy', 1);
50
    $this->assertTrue(
51
      ip_address() == $this->remote_ip,
52
      'Proxy forwarding without trusted proxies got remote IP address.'
53
    );
54

    
55
    // Proxy forwarding on and proxy address not trusted.
56
    variable_set('reverse_proxy_addresses', array($this->proxy_ip, $this->proxy2_ip));
57
    drupal_static_reset('ip_address');
58
    $_SERVER['REMOTE_ADDR'] = $this->untrusted_ip;
59
    $this->assertTrue(
60
      ip_address() == $this->untrusted_ip,
61
      'Proxy forwarding with untrusted proxy got remote IP address.'
62
    );
63

    
64
    // Proxy forwarding on and proxy address trusted.
65
    $_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
66
    $_SERVER['HTTP_X_FORWARDED_FOR'] = $this->forwarded_ip;
67
    drupal_static_reset('ip_address');
68
    $this->assertTrue(
69
      ip_address() == $this->forwarded_ip,
70
      'Proxy forwarding with trusted proxy got forwarded IP address.'
71
    );
72

    
73
    // Multi-tier architecture with comma separated values in header.
74
    $_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
75
    $_SERVER['HTTP_X_FORWARDED_FOR'] = implode(', ', array($this->untrusted_ip, $this->forwarded_ip, $this->proxy2_ip));
76
    drupal_static_reset('ip_address');
77
    $this->assertTrue(
78
      ip_address() == $this->forwarded_ip,
79
      'Proxy forwarding with trusted 2-tier proxy got forwarded IP address.'
80
    );
81

    
82
    // Custom client-IP header.
83
    variable_set('reverse_proxy_header', 'HTTP_X_CLUSTER_CLIENT_IP');
84
    $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] = $this->cluster_ip;
85
    drupal_static_reset('ip_address');
86
    $this->assertTrue(
87
      ip_address() == $this->cluster_ip,
88
      'Cluster environment got cluster client IP.'
89
    );
90

    
91
    // Verifies that drupal_valid_http_host() prevents invalid characters.
92
    $this->assertFalse(drupal_valid_http_host('security/.drupal.org:80'), 'HTTP_HOST with / is invalid');
93
    $this->assertFalse(drupal_valid_http_host('security\\.drupal.org:80'), 'HTTP_HOST with \\ is invalid');
94
    $this->assertFalse(drupal_valid_http_host('security<.drupal.org:80'), 'HTTP_HOST with &lt; is invalid');
95
    $this->assertFalse(drupal_valid_http_host('security..drupal.org:80'), 'HTTP_HOST with .. is invalid');
96
    // IPv6 loopback address
97
    $this->assertTrue(drupal_valid_http_host('[::1]:80'), 'HTTP_HOST containing IPv6 loopback is valid');
98
  }
99
}
100

    
101
class BootstrapPageCacheTestCase extends DrupalWebTestCase {
102

    
103
  public static function getInfo() {
104
    return array(
105
      'name' => 'Page cache test',
106
      'description' => 'Enable the page cache and test it with various HTTP requests.',
107
      'group' => 'Bootstrap'
108
    );
109
  }
110

    
111
  function setUp() {
112
    parent::setUp('system_test');
113
  }
114

    
115
  /**
116
   * Test support for requests containing If-Modified-Since and If-None-Match headers.
117
   */
118
  function testConditionalRequests() {
119
    variable_set('cache', 1);
120

    
121
    // Fill the cache.
122
    $this->drupalGet('');
123

    
124
    $this->drupalHead('');
125
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
126
    $etag = $this->drupalGetHeader('ETag');
127
    $last_modified = $this->drupalGetHeader('Last-Modified');
128

    
129
    $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
130
    $this->assertResponse(304, 'Conditional request returned 304 Not Modified.');
131

    
132
    $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag));
133
    $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
134

    
135
    $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag));
136
    $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
137

    
138
    $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified));
139
    $this->assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.');
140
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
141

    
142
    $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC1123, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag));
143
    $this->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.');
144
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
145

    
146
    $user = $this->drupalCreateUser();
147
    $this->drupalLogin($user);
148
    $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
149
    $this->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.');
150
    $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Absense of Page was not cached.');
151
  }
152

    
153
  /**
154
   * Test cache headers.
155
   */
156
  function testPageCache() {
157
    variable_set('cache', 1);
158

    
159
    // Fill the cache.
160
    $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
161
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
162
    $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary header was sent.');
163
    $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
164
    $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
165
    $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
166

    
167
    // Check cache.
168
    $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
169
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
170
    $this->assertEqual($this->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary: Cookie header was sent.');
171
    $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'public, max-age=0', 'Cache-Control header was sent.');
172
    $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
173
    $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
174

    
175
    // Check replacing default headers.
176
    $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT')));
177
    $this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', 'Default header was replaced.');
178
    $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent')));
179
    $this->assertEqual($this->drupalGetHeader('Vary'), 'User-Agent,Accept-Encoding', 'Default header was replaced.');
180

    
181
    // Check that authenticated users bypass the cache.
182
    $user = $this->drupalCreateUser();
183
    $this->drupalLogin($user);
184
    $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
185
    $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
186
    $this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.');
187
    $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, must-revalidate, post-check=0, pre-check=0', 'Cache-Control header was sent.');
188
    $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
189
    $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
190

    
191
  }
192

    
193
  /**
194
   * Test page compression.
195
   *
196
   * The test should pass even if zlib.output_compression is enabled in php.ini,
197
   * .htaccess or similar, or if compression is done outside PHP, e.g. by the
198
   * mod_deflate Apache module.
199
   */
200
  function testPageCompression() {
201
    variable_set('cache', 1);
202

    
203
    // Fill the cache and verify that output is compressed.
204
    $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
205
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
206
    $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
207
    $this->assertRaw('</html>', 'Page was gzip compressed.');
208

    
209
    // Verify that cached output is compressed.
210
    $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
211
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
212
    $this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.');
213
    $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
214
    $this->assertRaw('</html>', 'Page was gzip compressed.');
215

    
216
    // Verify that a client without compression support gets an uncompressed page.
217
    $this->drupalGet('');
218
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
219
    $this->assertFalse($this->drupalGetHeader('Content-Encoding'), 'A Content-Encoding header was not sent.');
220
    $this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), 'Site title matches.');
221
    $this->assertRaw('</html>', 'Page was not compressed.');
222

    
223
    // Disable compression mode.
224
    variable_set('page_compression', FALSE);
225

    
226
    // Verify if cached page is still available for a client with compression support.
227
    $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
228
    $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
229
    $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support enabled).');
230

    
231
    // Verify if cached page is still available for a client without compression support.
232
    $this->drupalGet('');
233
    $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support disabled).');
234
  }
235
}
236

    
237
class BootstrapVariableTestCase extends DrupalWebTestCase {
238

    
239
  function setUp() {
240
    parent::setUp('system_test');
241
  }
242

    
243
  public static function getInfo() {
244
    return array(
245
      'name' => 'Variable test',
246
      'description' => 'Make sure the variable system functions correctly.',
247
      'group' => 'Bootstrap'
248
    );
249
  }
250

    
251
  /**
252
   * testVariable
253
   */
254
  function testVariable() {
255
    // Setting and retrieving values.
256
    $variable = $this->randomName();
257
    variable_set('simpletest_bootstrap_variable_test', $variable);
258
    $this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), 'Setting and retrieving values');
259

    
260
    // Make sure the variable persists across multiple requests.
261
    $this->drupalGet('system-test/variable-get');
262
    $this->assertText($variable, 'Variable persists across multiple requests');
263

    
264
    // Deleting variables.
265
    $default_value = $this->randomName();
266
    variable_del('simpletest_bootstrap_variable_test');
267
    $variable = variable_get('simpletest_bootstrap_variable_test', $default_value);
268
    $this->assertIdentical($variable, $default_value, 'Deleting variables');
269
  }
270

    
271
  /**
272
   * Makes sure that the default variable parameter is passed through okay.
273
   */
274
  function testVariableDefaults() {
275
    // Tests passing nothing through to the default.
276
    $this->assertIdentical(NULL, variable_get('simpletest_bootstrap_variable_test'), 'Variables are correctly defaulting to NULL.');
277

    
278
    // Tests passing 5 to the default parameter.
279
    $this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), 'The default variable parameter is passed through correctly.');
280
  }
281

    
282
}
283

    
284
/**
285
 * Test hook_boot() and hook_exit().
286
 */
287
class HookBootExitTestCase extends DrupalWebTestCase {
288

    
289
  public static function getInfo() {
290
    return array(
291
      'name' => 'Boot and exit hook invocation',
292
      'description' => 'Test that hook_boot() and hook_exit() are called correctly.',
293
      'group' => 'Bootstrap',
294
    );
295
  }
296

    
297
  function setUp() {
298
    parent::setUp('system_test', 'dblog');
299
  }
300

    
301
  /**
302
   * Test calling of hook_boot() and hook_exit().
303
   */
304
  function testHookBootExit() {
305
    // Test with cache disabled. Boot and exit should always fire.
306
    variable_set('cache', 0);
307
    $this->drupalGet('');
308
    $calls = 1;
309
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with disabled cache.'));
310
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with disabled cache.'));
311

    
312
    // Test with normal cache. Boot and exit should be called.
313
    variable_set('cache', 1);
314
    $this->drupalGet('');
315
    $calls++;
316
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with normal cache.'));
317
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with normal cache.'));
318

    
319
    // Boot and exit should not fire since the page is cached.
320
    variable_set('page_cache_invoke_hooks', FALSE);
321
    $this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
322
    $this->drupalGet('');
323
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot not called with aggressive cache and a cached page.'));
324
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit not called with aggressive cache and a cached page.'));
325

    
326
    // Test with page cache cleared, boot and exit should be called.
327
    $this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
328
    $this->drupalGet('');
329
    $calls++;
330
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_boot'))->fetchField(), $calls, t('hook_boot called with aggressive cache and no cached page.'));
331
    $this->assertEqual(db_query('SELECT COUNT(*) FROM {watchdog} WHERE type = :type AND message = :message', array(':type' => 'system_test', ':message' => 'hook_exit'))->fetchField(), $calls, t('hook_exit called with aggressive cache and no cached page.'));
332
  }
333
}
334

    
335
/**
336
 * Test drupal_get_filename()'s availability.
337
 */
338
class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
339

    
340
  public static function getInfo() {
341
    return array(
342
      'name' => 'Get filename test',
343
      'description' => 'Test that drupal_get_filename() works correctly when the file is not found in the database.',
344
      'group' => 'Bootstrap',
345
    );
346
  }
347

    
348
  /**
349
   * Test that drupal_get_filename() works correctly when the file is not found in the database.
350
   */
351
  function testDrupalGetFilename() {
352
    // Reset the static cache so we can test the "db is not active" code of
353
    // drupal_get_filename().
354
    drupal_static_reset('drupal_get_filename');
355

    
356
    // Retrieving the location of a module.
357
    $this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
358

    
359
    // Retrieving the location of a theme.
360
    $this->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
361

    
362
    // Retrieving the location of a theme engine.
363
    $this->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
364

    
365
    // Retrieving the location of a profile. Profiles are a special case with
366
    // a fixed location and naming.
367
    $this->assertIdentical(drupal_get_filename('profile', 'standard'), 'profiles/standard/standard.profile', t('Retrieve install profile location.'));
368

    
369
    // When a file is not found in the database cache, drupal_get_filename()
370
    // searches several locations on the filesystem, including the DRUPAL_ROOT
371
    // directory. We use the '.script' extension below because this is a
372
    // non-existent filetype that will definitely not exist in the database.
373
    // Since there is already a scripts directory, drupal_get_filename() will
374
    // automatically check there for 'script' files, just as it does for (e.g.)
375
    // 'module' files in modules.
376
    $this->assertIdentical(drupal_get_filename('script', 'test'), 'scripts/test.script', t('Retrieve test script location.'));
377
  }
378
}
379

    
380
class BootstrapTimerTestCase extends DrupalUnitTestCase {
381

    
382
  public static function getInfo() {
383
    return array(
384
      'name' => 'Timer test',
385
      'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
386
      'group' => 'Bootstrap',
387
    );
388
  }
389

    
390
  /**
391
   * Test timer_read() to ensure it properly accumulates time when the timer
392
   * started and stopped multiple times.
393
   * @return
394
   */
395
  function testTimer() {
396
    timer_start('test');
397
    sleep(1);
398
    $this->assertTrue(timer_read('test') >= 1000, 'Timer measured 1 second of sleeping while running.');
399
    sleep(1);
400
    timer_stop('test');
401
    $this->assertTrue(timer_read('test') >= 2000, 'Timer measured 2 seconds of sleeping after being stopped.');
402
    timer_start('test');
403
    sleep(1);
404
    $this->assertTrue(timer_read('test') >= 3000, 'Timer measured 3 seconds of sleeping after being restarted.');
405
    sleep(1);
406
    $timer = timer_stop('test');
407
    $this->assertTrue(timer_read('test') >= 4000, 'Timer measured 4 seconds of sleeping after being stopped for a second time.');
408
    $this->assertEqual($timer['count'], 2, 'Timer counted 2 instances of being started.');
409
  }
410
}
411

    
412
/**
413
 * Test that resetting static variables works.
414
 */
415
class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
416

    
417
  public static function getInfo() {
418
    return array(
419
      'name' => 'Resettable static variables test',
420
      'description' => 'Test that drupal_static() and drupal_static_reset() work.',
421
      'group' => 'Bootstrap',
422
    );
423
  }
424

    
425
  /**
426
   * Test that a variable reference returned by drupal_static() gets reset when
427
   * drupal_static_reset() is called.
428
   */
429
  function testDrupalStatic() {
430
    $name = __CLASS__ . '_' . __METHOD__;
431
    $var = &drupal_static($name, 'foo');
432
    $this->assertEqual($var, 'foo', 'Variable returned by drupal_static() was set to its default.');
433

    
434
    // Call the specific reset and the global reset each twice to ensure that
435
    // multiple resets can be issued without odd side effects.
436
    $var = 'bar';
437
    drupal_static_reset($name);
438
    $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of name-specific reset.');
439
    $var = 'bar';
440
    drupal_static_reset($name);
441
    $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of name-specific reset.');
442
    $var = 'bar';
443
    drupal_static_reset();
444
    $this->assertEqual($var, 'foo', 'Variable was reset after first invocation of global reset.');
445
    $var = 'bar';
446
    drupal_static_reset();
447
    $this->assertEqual($var, 'foo', 'Variable was reset after second invocation of global reset.');
448
  }
449
}
450

    
451
/**
452
 * Test miscellaneous functions in bootstrap.inc.
453
 */
454
class BootstrapMiscTestCase extends DrupalUnitTestCase {
455

    
456
  public static function getInfo() {
457
    return array(
458
      'name' => 'Miscellaneous bootstrap unit tests',
459
      'description' => 'Test miscellaneous functions in bootstrap.inc.',
460
      'group' => 'Bootstrap',
461
    );
462
  }
463

    
464
  /**
465
   * Test miscellaneous functions in bootstrap.inc.
466
   */
467
  function testMisc() {
468
    // Test drupal_array_merge_deep().
469
    $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => 'X', 'class' => array('a', 'b')), 'language' => 'en');
470
    $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('c', 'd')), 'html' => TRUE);
471
    $expected = array('fragment' => 'y', 'attributes' => array('title' => 'Y', 'class' => array('a', 'b', 'c', 'd')), 'language' => 'en', 'html' => TRUE);
472
    $this->assertIdentical(drupal_array_merge_deep($link_options_1, $link_options_2), $expected, 'drupal_array_merge_deep() returned a properly merged array.');
473
  }
474

    
475
  /**
476
   * Tests that the drupal_check_memory_limit() function works as expected.
477
   */
478
  function testCheckMemoryLimit() {
479
    $memory_limit = ini_get('memory_limit');
480
    // Test that a very reasonable amount of memory is available.
481
    $this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.');
482

    
483
    // Get the available memory and multiply it by two to make it unreasonably
484
    // high.
485
    $twice_avail_memory = ($memory_limit * 2) . 'MB';
486

    
487
    // The function should always return true if the memory limit is set to -1.
488
    $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied');
489

    
490
    // Test that even though we have 30MB of memory available - the function
491
    // returns FALSE when given an upper limit for how much memory can be used.
492
    $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.');
493

    
494
    // Test that an equal amount of memory to the amount requested returns TRUE.
495
    $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.');
496
  }
497
}
498

    
499
/**
500
 * Tests for overriding server variables via the API.
501
 */
502
class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
503
  public static function getInfo() {
504
    return array(
505
      'name' => 'Overriding server variables',
506
      'description' => 'Test that drupal_override_server_variables() works correctly.',
507
      'group' => 'Bootstrap',
508
    );
509
  }
510

    
511
  /**
512
   * Test providing a direct URL to to drupal_override_server_variables().
513
   */
514
  function testDrupalOverrideServerVariablesProvidedURL() {
515
    $tests = array(
516
      'http://example.com' => array(
517
        'HTTP_HOST' => 'example.com',
518
        'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
519
      ),
520
      'http://example.com/index.php' => array(
521
        'HTTP_HOST' => 'example.com',
522
        'SCRIPT_NAME' => '/index.php',
523
      ),
524
      'http://example.com/subdirectory/index.php' => array(
525
        'HTTP_HOST' => 'example.com',
526
        'SCRIPT_NAME' => '/subdirectory/index.php',
527
      ),
528
    );
529
    foreach ($tests as $url => $expected_server_values) {
530
      // Remember the original value of $_SERVER, since the function call below
531
      // will modify it.
532
      $original_server = $_SERVER;
533
      // Call drupal_override_server_variables() and ensure that all expected
534
      // $_SERVER variables were modified correctly.
535
      drupal_override_server_variables(array('url' => $url));
536
      foreach ($expected_server_values as $key => $value) {
537
        $this->assertIdentical($_SERVER[$key], $value);
538
      }
539
      // Restore the original value of $_SERVER.
540
      $_SERVER = $original_server;
541
    }
542
  }
543
}