Projet

Général

Profil

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

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

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
    // Verifies that host names are shorter than 1000 characters.
97
    $this->assertFalse(drupal_valid_http_host(str_repeat('x', 1001)), 'HTTP_HOST with more than 1000 characters is invalid.');
98
    $this->assertFalse(drupal_valid_http_host(str_repeat('.', 101)), 'HTTP_HOST with more than 100 subdomains is invalid.');
99
    $this->assertFalse(drupal_valid_http_host(str_repeat(':', 101)), 'HTTP_HOST with more than 100 portseparators is invalid.');
100

    
101
    // IPv6 loopback address
102
    $this->assertTrue(drupal_valid_http_host('[::1]:80'), 'HTTP_HOST containing IPv6 loopback is valid');
103
  }
104
}
105

    
106
class BootstrapPageCacheTestCase extends DrupalWebTestCase {
107

    
108
  public static function getInfo() {
109
    return array(
110
      'name' => 'Page cache test',
111
      'description' => 'Enable the page cache and test it with various HTTP requests.',
112
      'group' => 'Bootstrap'
113
    );
114
  }
115

    
116
  function setUp() {
117
    parent::setUp('system_test');
118
  }
119

    
120
  /**
121
   * Test support for requests containing If-Modified-Since and If-None-Match headers.
122
   */
123
  function testConditionalRequests() {
124
    variable_set('cache', 1);
125

    
126
    // Fill the cache.
127
    $this->drupalGet('');
128

    
129
    $this->drupalHead('');
130
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
131
    $etag = $this->drupalGetHeader('ETag');
132
    $last_modified = $this->drupalGetHeader('Last-Modified');
133

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

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

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

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

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

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

    
158
  /**
159
   * Test cache headers.
160
   */
161
  function testPageCache() {
162
    variable_set('cache', 1);
163

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

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

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

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

    
196
  }
197

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

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

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

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

    
228
    // Disable compression mode.
229
    variable_set('page_compression', FALSE);
230

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

    
236
    // Verify if cached page is still available for a client without compression support.
237
    $this->drupalGet('');
238
    $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support disabled).');
239
  }
240
}
241

    
242
class BootstrapVariableTestCase extends DrupalWebTestCase {
243

    
244
  function setUp() {
245
    parent::setUp('system_test');
246
  }
247

    
248
  public static function getInfo() {
249
    return array(
250
      'name' => 'Variable test',
251
      'description' => 'Make sure the variable system functions correctly.',
252
      'group' => 'Bootstrap'
253
    );
254
  }
255

    
256
  /**
257
   * testVariable
258
   */
259
  function testVariable() {
260
    // Setting and retrieving values.
261
    $variable = $this->randomName();
262
    variable_set('simpletest_bootstrap_variable_test', $variable);
263
    $this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test'), 'Setting and retrieving values');
264

    
265
    // Make sure the variable persists across multiple requests.
266
    $this->drupalGet('system-test/variable-get');
267
    $this->assertText($variable, 'Variable persists across multiple requests');
268

    
269
    // Deleting variables.
270
    $default_value = $this->randomName();
271
    variable_del('simpletest_bootstrap_variable_test');
272
    $variable = variable_get('simpletest_bootstrap_variable_test', $default_value);
273
    $this->assertIdentical($variable, $default_value, 'Deleting variables');
274
  }
275

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

    
283
    // Tests passing 5 to the default parameter.
284
    $this->assertIdentical(5, variable_get('simpletest_bootstrap_variable_test', 5), 'The default variable parameter is passed through correctly.');
285
  }
286

    
287
}
288

    
289
/**
290
 * Test hook_boot() and hook_exit().
291
 */
292
class HookBootExitTestCase extends DrupalWebTestCase {
293

    
294
  public static function getInfo() {
295
    return array(
296
      'name' => 'Boot and exit hook invocation',
297
      'description' => 'Test that hook_boot() and hook_exit() are called correctly.',
298
      'group' => 'Bootstrap',
299
    );
300
  }
301

    
302
  function setUp() {
303
    parent::setUp('system_test', 'dblog');
304
  }
305

    
306
  /**
307
   * Test calling of hook_boot() and hook_exit().
308
   */
309
  function testHookBootExit() {
310
    // Test with cache disabled. Boot and exit should always fire.
311
    variable_set('cache', 0);
312
    $this->drupalGet('');
313
    $calls = 1;
314
    $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.'));
315
    $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.'));
316

    
317
    // Test with normal cache. Boot and exit should be called.
318
    variable_set('cache', 1);
319
    $this->drupalGet('');
320
    $calls++;
321
    $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.'));
322
    $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.'));
323

    
324
    // Boot and exit should not fire since the page is cached.
325
    variable_set('page_cache_invoke_hooks', FALSE);
326
    $this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
327
    $this->drupalGet('');
328
    $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.'));
329
    $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.'));
330

    
331
    // Test with page cache cleared, boot and exit should be called.
332
    $this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
333
    $this->drupalGet('');
334
    $calls++;
335
    $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.'));
336
    $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.'));
337
  }
338
}
339

    
340
/**
341
 * Test drupal_get_filename()'s availability.
342
 */
343
class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
344

    
345
  public static function getInfo() {
346
    return array(
347
      'name' => 'Get filename test',
348
      'description' => 'Test that drupal_get_filename() works correctly when the file is not found in the database.',
349
      'group' => 'Bootstrap',
350
    );
351
  }
352

    
353
  /**
354
   * Test that drupal_get_filename() works correctly when the file is not found in the database.
355
   */
356
  function testDrupalGetFilename() {
357
    // Reset the static cache so we can test the "db is not active" code of
358
    // drupal_get_filename().
359
    drupal_static_reset('drupal_get_filename');
360

    
361
    // Retrieving the location of a module.
362
    $this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
363

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

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

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

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

    
385
class BootstrapTimerTestCase extends DrupalUnitTestCase {
386

    
387
  public static function getInfo() {
388
    return array(
389
      'name' => 'Timer test',
390
      'description' => 'Test that timer_read() works both when a timer is running and when a timer is stopped.',
391
      'group' => 'Bootstrap',
392
    );
393
  }
394

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

    
417
/**
418
 * Test that resetting static variables works.
419
 */
420
class BootstrapResettableStaticTestCase extends DrupalUnitTestCase {
421

    
422
  public static function getInfo() {
423
    return array(
424
      'name' => 'Resettable static variables test',
425
      'description' => 'Test that drupal_static() and drupal_static_reset() work.',
426
      'group' => 'Bootstrap',
427
    );
428
  }
429

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

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

    
456
/**
457
 * Test miscellaneous functions in bootstrap.inc.
458
 */
459
class BootstrapMiscTestCase extends DrupalUnitTestCase {
460

    
461
  public static function getInfo() {
462
    return array(
463
      'name' => 'Miscellaneous bootstrap unit tests',
464
      'description' => 'Test miscellaneous functions in bootstrap.inc.',
465
      'group' => 'Bootstrap',
466
    );
467
  }
468

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

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

    
488
    // Get the available memory and multiply it by two to make it unreasonably
489
    // high.
490
    $twice_avail_memory = ($memory_limit * 2) . 'MB';
491

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

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

    
499
    // Test that an equal amount of memory to the amount requested returns TRUE.
500
    $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.');
501
  }
502
}
503

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

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