bootstrap.test 7.53 KB
Newer Older
1
<?php
2
// $Id$
3
4
5
6
7

class BootstrapIPAddressTestCase extends DrupalWebTestCase {

  function getInfo() {
    return array(
8
9
      'name' => t('IP address and HTTP_HOST test'),
      'description' => t('Get the IP address from the current visitor from the server variables, check hostname validation.'),
10
11
12
13
14
15
16
17
18
19
20
21
      'group' => t('Bootstrap')
    );
  }

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

    $this->remote_ip = '127.0.0.1';
    $this->proxy_ip = '127.0.0.2';
    $this->forwarded_ip = '127.0.0.3';
    $this->cluster_ip = '127.0.0.4';
    $this->untrusted_ip = '0.0.0.0';
22

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

27
28
    parent::setUp();
  }
29

30
31
32
33
34
35
  function tearDown() {
    $_SERVER = $this->oldserver;
    parent::tearDown();
  }

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

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

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

    // Proxy forwarding on and proxy address trusted.
    $_SERVER['REMOTE_ADDR'] = $this->proxy_ip;
    $_SERVER['HTTP_X_FORWARDED_FOR'] = $this->forwarded_ip;
    $this->assertTrue(
      ip_address(true) == $this->forwarded_ip,
      t('Proxy forwarding with trusted proxy got forwarded IP address')
66
67
    );

68
69
70
71
72
73
    // Cluster environment.
    $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] = $this->cluster_ip;
    $this->assertTrue(
      ip_address(true) == $this->cluster_ip,
      t('Cluster environment got cluster client IP')
    );
74
75
76
77
78
79
80
81
82
83
    $_SERVER['HTTP_HOST'] = 'security/.drupal.org:80';
    $this->assertFalse(drupal_valid_http_host(), t('HTTP_HOST with / is invalid'));
    $_SERVER['HTTP_HOST'] = 'security\\.drupal.org:80';
    $this->assertFalse(drupal_valid_http_host(), t('HTTP_HOST with \\ is invalid'));
    $_SERVER['HTTP_HOST'] = 'security<.drupal.org:80';
    $this->assertFalse(drupal_valid_http_host(), t('HTTP_HOST with &lt; is invalid'));
    $_SERVER['HTTP_HOST'] = 'security..drupal.org:80';
    $this->assertFalse(drupal_valid_http_host(), t('HTTP_HOST with .. is invalid'));
    $_SERVER['HTTP_HOST'] = '[::1]:80'; // IPv6 loopback address
    $this->assertTrue(drupal_valid_http_host(), t('HTTP_HOST containing IPv6 loopback is valid'));
84
85
  }
}
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

class BootstrapPageCacheTestCase extends DrupalWebTestCase {

  function getInfo() {
    return array(
      'name' => t('Page cache test'),
      'description' => t('Enable the page cache, submit a HEAD request and examine headers.'),
      'group' => t('Bootstrap')
    );
  }

  /**
   * Enable cache and examine HTTP headers.
   */
  function testPageCache() {
    global $base_url;
    variable_set('cache', 1);
    // Retrieve the front page, which has already been cached by $this->curlConnect();
    $this->drupalHead($base_url);
    $this->assertText('ETag: ', t('Verify presence of ETag header indicating that page caching is enabled.'));
  }

}
109
110

class BootstrapVariableTestCase extends DrupalWebTestCase {
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  function setUp() {
    parent::setUp('system_test');
  }

  function getInfo() {
    return array(
      'name' => t('Variable test'),
      'description' => t('Make sure the variable system functions correctly.'),
      'group' => t('Bootstrap')
    );
  }

  /**
   * testVariable
   */
  function testVariable() {
    // Setting and retrieving values.
    $variable = $this->randomName();
    variable_set('simpletest_bootstrap_variable_test', $variable);
    $this->assertIdentical($variable, variable_get('simpletest_bootstrap_variable_test', NULL), t('Setting and retrieving values'));

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

137
138
139
140
141
142
143
144
    // Deleting variables.
    $default_value = $this->randomName();
    variable_del('simpletest_bootstrap_variable_test');
    $variable = variable_get('simpletest_bootstrap_variable_test', $default_value);
    $this->assertIdentical($variable, $default_value, t('Deleting variables'));
  }

}
145
146
147
148
149
150
151
152

/**
 * Test hook_boot and hook_exit.
 */
class HookBootExitTestCase extends DrupalWebTestCase {

  function getInfo() {
    return array(
153
      'name' => t('Boot and exit hook invocation'),
154
155
156
157
158
159
160
161
162
163
164
165
166
167
      'description' => t('Test that hook_boot() and hook_exit() are called correctly.'),
      'group' => t('Bootstrap'),
    );
  }

  function setUp() {
    parent::setUp('system_test', 'dblog');
  }

  /**
   * Test calling of hook_boot() and hook_exit().
   */
  function testHookBootExit() {
    // Test with cache disabled. Boot and exit should always fire. Initialize
168
169
    // the number of hook calls to two since the first call to drupalGet
    // actually performs two HTTP requests.
170
171
172
173
174
175
176
177
178
179
180
181
182
    variable_set('cache', CACHE_DISABLED);
    $this->drupalGet('');
    $calls = 2;
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot called with disabled cache.'));
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit called with disabled cache.'));

    // Test with normal cache. Boot and exit should be called.
    variable_set('cache', CACHE_NORMAL);
    $this->drupalGet('');
    $calls++;
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot called with normal cache.'));
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit called with normal cache.'));

183
184
    // Test with aggressive cache. Boot and exit should not fire since the
    // page is cached.
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    variable_set('cache', CACHE_AGGRESSIVE);
    $this->assertTrue(cache_get(url('', array('absolute' => TRUE)), 'cache_page'), t('Page has been cached.'));
    $this->drupalGet('');
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot not called with agressive cache and a cached page.'));
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit not called with agressive cache and a cached page.'));

    // Test with aggressive cache and page cache cleared. Boot and exit should
    // be called.
    $this->assertTrue(db_delete('cache_page')->execute(), t('Page cache cleared.'));
    $this->drupalGet('');
    $calls++;
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_boot'")->fetchField(), $calls, t('hook_boot called with agressive cache and no cached page.'));
    $this->assertEqual(db_query("SELECT COUNT(*) FROM {watchdog} WHERE type = 'system_test' AND message = 'hook_exit'")->fetchField(), $calls, t('hook_exit called with agressive cache and no cached page.'));
  }
}