Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 284 lines (229 sloc) 10.883 kb
198855e @njt1982 Re-merging Drupal 6 work up to Drupal 7, incuding a SimpleTest.
njt1982 authored
1 <?php
2
3 define('ERROR_MESSAGE', 'ERROR<br />Expected Path: !expected_path<br />Expected Status Code: !expected_status<br />Location: !location<br />Status: !status');
4 define('SUCCESS_MESSAGE', 'SUCCESS<br />Expected Path: !expected_path<br />Expected Status Code: !expected_status<br />Location: !location<br />Status: !status');
5
6 /**
7 * @file
8 * Global Redirect functionality tests
9 */
10
11 class GlobalRedirectTestCase extends DrupalWebTestCase {
12
13
14 function setUp() {
15 parent::setUp('path', 'globalredirect', 'taxonomy', 'forum');
16
17 // Create a user
18 $user = $this->drupalCreateUser(array(
19 'access content',
20 'create page content',
21 'create url aliases',
22 ));
23 $this->drupalLogin($user);
24
25 // Create a dummy node
26 $node = array(
27 'type' => 'page',
28 'title' => 'Test Page Node',
29 'path' => array('alias' => 'test-node'),
30 );
31
32 // Save the node
33 $node = $this->drupalCreateNode($node);
34
35 // Create an alias for the create story path - this is used in the "redirect with permissions testing" test.
36 $path = array('source' => 'node/add/article', 'alias' => 'node-add-artice');
37 path_save($path);
38
39
40 // The forum vocab should already be created - should be term 1?
41 $forum_term = (object)array(
42 'vid' => variable_get('forum_nav_vocabulary', 0),
43 'name' => 'Test Forum Term',
44 );
45 taxonomy_term_save($forum_term);
46
47
48
49 // Create another test vocab (with a default module) - should be vocab 2?
50 $vocab = (object)array(
51 'name' => 'test vocab',
52 'multiple' => 0,
53 'required' => 0,
54 'hierarchy' => 0,
55 'module' => 'taxonomy',
56 'nodes' => array('page', 'story'),
57 );
58 taxonomy_vocabulary_save($vocab);
59 $vocab = taxonomy_vocabulary_load($vocab->vid, TRUE);
60
61 // Create a test term - Should be term 2?
62 $term = (object)array(
63 'vid' => $vocab->vid,
64 'name' => 'Test Term',
65 'path' => array('alias' => 'test-term'),
66 );
67 taxonomy_term_save($term);
68 $term = taxonomy_term_load($term->tid);
69 }
70
71
72
73 protected function _globalredirect_batch_test() {
74 // Get the settings
75 $settings = _globalredirect_get_settings();
76 $this->assert('pass', '<pre>' . print_r($settings, TRUE) . '</pre>');
77
78 // Array of request => "array of expected data" pairs.
79 // The array must have a return-code key (with a numeric HTTP code as a value (eg 301 or 200).
80 // The array may also have an expected-path key with a value representing the expected path. If this is ommitted, the request is passed through url().
81 $test_paths = array(
82 // "" is the frontpage. Should NOT redirect. -- Test for normal requests
83 array(
84 'request' => '',
85 'return-code' => 200,
86 'expected-path' => '',
87 ),
88
89 // "node" is the default frontpage. Should redirect to base path. --- Test for frontpage redirect
90 array(
91 'request' => 'node',
92 'return-code' => 301,
93 'expected-path' => '',
94 ),
95
96 // "node/1" has been defined above as having an alias ("test-node"). Should 301 redirect to the alias. --- Test for source path request on aliased path
97 array(
98 'request' => 'node/1',
99 'return-code' => 301,
100 ),
101
102 // "node/add/article" has an alias, however the redirect depends on the menu_check setting --- Test for access request to secured url
103 array(
104 'request' => 'node/add/article',
105 'return-code' => $settings['menu_check'] ? 403 : 301,
106 ),
107
108 // "user/[uid]/" has no alias, but the redirect depends on the $deslash_check setting --- Test for deslashing redirect
109 array(
110 'request' => 'user/' . $this->loggedInUser->uid . '/',
111 'return-code' => $settings['deslash'] ? 301 : 200,
112 'expected-path' => 'user/' . $this->loggedInUser->uid,
113 ),
114
115 // NonClean to Clean check 1 --- This should always redirect as we're requesting an aliased path in unaliased form (albeit also unclean)
116 array(
117 'request' => url('<front>', array('absolute' => TRUE)),
118 'options' => array('query' => array('q' => 'node/1'), 'external' => TRUE),
119 'return-code' => 301,
120 'expected-path' => 'test-node',
121 ),
122
123 // NonClean to Clean check 2 --- This may or may not redirect, depending on the state of nonclean_to_clean as we're requesting an unaliased path
124 array(
125 'request' => url('<front>', array('absolute' => TRUE)),
126 'options' => array('query' => array('q' => 'node/add/page'), 'external' => TRUE),
127 'return-code' => $settings['nonclean_to_clean'] ? 301 : 200,
128 'expected-path' => $settings['nonclean_to_clean'] ? 'node/add/page' : '?q=node/add/page',
129 ),
130
131 // Case Sensitive URL Check --- This may or may not redirect, depending on the state of case_sensitive_urls as we're requesting an aliased path in the wrong case
132 array(
133 'request' => 'Test-Node',
134 'options' => array('external' => TRUE),
135 'return-code' => $settings['case_sensitive_urls'] ? 301 : 200,
136 'expected-path' => $settings['case_sensitive_urls'] ? 'test-node' : 'Test-Node',
137 ),
138
139
140 // Term Path Handler Check --- This may or may not redirect, depending on the state of term_path_handler as we're requesting an aliased path with the wrong source
141 array(
142 'request' => 'taxonomy/term/1', // TODO: WE're assumeing term 1 is the forum tid created in setUp() - bad?
143 'return-code' => $settings['term_path_handler'] ? 301 : 200,
144 'expected-path' => $settings['term_path_handler'] ? 'forum/1' : 'taxonomy/term/1',
145 ),
146
147
148 // Trailing Zero Check --- This may or may not redirect, depending on the state of trailing_zero, as we're requesting an aliased path with a trailing zero source
149 // If 1, we trim ALL trailing /0... If 0 (disabled) or 2 (taxonomy term only), then a 200 response should be issued.
150 array(
151 'request' => 'node/1/0',
152 'return-code' => $settings['trailing_zero'] == 1 ? 301 : 200,
153 'expected-path' => $settings['trailing_zero'] == 1 ? 'test-node' : 'node/1/0',
154 ),
155
156 // Trailing Zero Check --- This may or may not redirect, depending on the state of trailing_zero, as we're requesting an aliased path with a trailing zero source
157 // If not disabled, then we should trim from taxonomy/term/1/0
158 array(
159 'request' => 'taxonomy/term/2/0',
160 'return-code' => $settings['trailing_zero'] > 0 ? 301 : 200,
161 'expected-path' => $settings['trailing_zero'] > 0 ? 'test-term' : 'taxonomy/term/2/0',
162 ),
163 );
164
165
166 // Foreach of the above, lets check they redirect correctly
167 foreach ($test_paths as $path) {
168 $path['options'] = isset($path['options']) ? $path['options'] + array('absolute' => TRUE) : array('absolute' => TRUE);
169
170 $request_path = url($path['request'], $path['options'] + array('alias' => TRUE));
171
172
173 // Display a message tellingthe user what we're testing
174 $this->pass(t('Requesting: !path', array('!path' => $request_path)));
175
176 // Do a HEAD request (don't care about the body). The alias=>TRUE is to tell Drupal not to lookup the alias - this is a raw request.
177 $this->drupalHead($request_path, array('alias' => TRUE));
178
179
180 // Grab the headers from the request
181 $headers = $this->drupalGetHeaders(TRUE);
182
183
184 // Build a nice array of results
185 $result = array(
186 '!expected_status' => $path['return-code'],
187 '!location' => isset($headers[0]['location']) ? $headers[0]['location'] : 'N/A',
188 '!status' => $headers[0][':status'],
189 );
190
191
192 // If the expected result is not a redirect, then there is no expected path in the location header.
193 if ($path['return-code'] != 301) {
194 $result['!expected_path'] = 'N/A';
195 }
196 else {
197 $url_options = array('absolute' => TRUE);
198
199 if (isset($path['expected-path'])) {
200 // If we have an expected path provided, use this and tell url() not to do an alias lookup
201 $expected = $path['expected-path'];
202 $url_options['alias'] = TRUE;
203 }
204 else {
205 // Otherwise set the path to the request and let url() do an alias lookup.
206 $expected = $path['request'];
207 }
208 $result['!expected_path'] = url($expected, $url_options);
209 }
210
211
212 // First test - is the status as expected? (Note: The expected status must be cast to string for strpos to work)
213 if (strpos($result['!status'], (string)$result['!expected_status']) !== FALSE) {
214 // Ok, we have a status and the status contains the appropriate response code (eg, 200, 301, 403 or 404).
215
216 // Next test (if expected return code is 301) - is the location set, and is it as expected?
217 if ($result['!expected_status'] == 301 && $result['!location'] == $result['!expected_path']) {
218 // We have redirect and ended up in the right place - a PASS!!!
219 $this->pass(t(SUCCESS_MESSAGE, $result), 'GlobalRedirect');
220 }
221 elseif ($result['!expected_status'] != 301) {
222 // We weren't supposed to redirect - this is good!
223 $this->pass(t(SUCCESS_MESSAGE, $result), 'GlobalRedirect');
224 }
225 else {
226 // In this case either the return-code or the returned location is unexpected
227 $this->fail(t(ERROR_MESSAGE, $result), 'GlobalRedirect');
228 $this->fail('<pre>' . print_r($headers, TRUE) . '</pre>');
229 }
230 }
231 else {
232 // The status either wasn't present or was not as expected
233 $this->fail(t(ERROR_MESSAGE, $result), 'GlobalRedirect');
234 $this->fail('<pre>' . print_r($headers, TRUE) . '</pre>');
235 }
236 }
237 }
238 }
239
240
241 class GlobalRedirectTestCaseDefault extends GlobalRedirectTestCase {
7789596 @davereid Issue #1045842 by pillarsdotnet, Dave Reid: Merged the master branch …
davereid authored
242 public static function getInfo() {
198855e @njt1982 Re-merging Drupal 6 work up to Drupal 7, incuding a SimpleTest.
njt1982 authored
243 return array(
244 'name' => '1. Global Redirect - Default Settings',
245 'description' => 'Ensure that Global Redirect functions correctly',
246 'group' => 'Global Redirect',
247 );
248 }
7789596 @davereid Issue #1045842 by pillarsdotnet, Dave Reid: Merged the master branch …
davereid authored
249
198855e @njt1982 Re-merging Drupal 6 work up to Drupal 7, incuding a SimpleTest.
njt1982 authored
250 function testGlobalRedirect() {
251 variable_set('globalredirect_settings', array(
252 'deslash' => 1,
253 'menu_check' => 0,
254 'nonclean_to_clean' => 1,
255 'case_sensitive_urls' => 1,
256 'term_path_handler' => 0,
257 ));
258 $this->_globalredirect_batch_test();
259 }
260 }
261
262
263 class GlobalRedirectTestCaseConfigAlpha extends GlobalRedirectTestCase {
7789596 @davereid Issue #1045842 by pillarsdotnet, Dave Reid: Merged the master branch …
davereid authored
264 public static function getInfo() {
198855e @njt1982 Re-merging Drupal 6 work up to Drupal 7, incuding a SimpleTest.
njt1982 authored
265 return array(
266 'name' => '2. Global Redirect - Config Alpha',
267 'description' => 'Ensure that Global Redirect functions correctly',
268 'group' => 'Global Redirect',
269 );
270 }
7789596 @davereid Issue #1045842 by pillarsdotnet, Dave Reid: Merged the master branch …
davereid authored
271
198855e @njt1982 Re-merging Drupal 6 work up to Drupal 7, incuding a SimpleTest.
njt1982 authored
272 function testGlobalRedirect() {
273 variable_set('globalredirect_settings', array(
274 'deslash' => 0,
275 'menu_check' => 1,
276 'nonclean_to_clean' => 0,
277 'case_sensitive_urls' => 0,
278 'term_path_handler' => 1,
279 ));
280
281 $this->_globalredirect_batch_test();
282 }
283 }
Something went wrong with that request. Please try again.