-
Notifications
You must be signed in to change notification settings - Fork 3
/
redirect.test
228 lines (197 loc) · 8.53 KB
/
redirect.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
109
110
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file
* Unit tests for the redirect module.
*/
class RedirectTestHelper extends BackdropWebTestCase {
function setUp(array $modules = array()) {
array_unshift($modules, 'redirect');
parent::setUp($modules);
}
protected function assertRedirect($redirect) {
$source_url = url($redirect->source, array('absolute' => TRUE) + $redirect->source_options);
$redirect_url = url($redirect->redirect, array('absolute' => TRUE) + $redirect->redirect_options);
$this->backdropGet($source_url);
$this->assertEqual($this->getUrl(), $redirect_url, t('Page %source was redirected to %redirect.', array('%source' => $source_url, '%redirect' => $redirect_url)));
// Reload the redirect.
if (!empty($redirect->rid)) {
return redirect_load($redirect->rid);
}
}
protected function assertNoRedirect($redirect) {
$source_url = url($redirect->source, array('absolute' => TRUE) + $redirect->source_options);
$this->backdropGet($source_url);
$this->assertEqual($this->getUrl(), $source_url, t('Page %url was not redirected.', array('%url' => $source_url)));
}
/**
* Add an URL redirection
*
* @param $source
* A source path.
* @param $redirect
* A redirect path.
*/
protected function addRedirect($source_path, $redirect_path, array $redirect = array()) {
$source_parsed = redirect_parse_url($source_path);
$redirect['source'] = $source_parsed['url'];
if (isset($source_parsed['query'])) {
$redirect['source_options']['query'] = $source_parsed['query'];
}
$redirect_parsed = redirect_parse_url($redirect_path);
$redirect['redirect'] = $redirect_parsed['url'];
if (isset($redirect_parsed['query'])) {
$redirect['redirect_options']['query'] = $redirect_parsed['query'];
}
if (isset($redirect_parsed['fragment'])) {
$redirect['redirect_options']['fragment'] = $redirect_parsed['fragment'];
}
$redirect_object = new Redirect($redirect);
redirect_save($redirect_object);
return $redirect_object;
}
protected function assertPageCached($url, array $options = array()) {
$options['absolute'] = TRUE;
$url = url($url, $options);
$cache = cache_get($url, 'cache_page');
$this->assertTrue($cache, t('Page %url was cached.', array('%url' => $url)));
return $cache;
}
protected function assertPageNotCached($url, array $options = array()) {
$options['absolute'] = TRUE;
$url = url($url, $options);
$cache = cache_get($url, 'cache_page');
$this->assertFalse($cache, t('Page %url was not cached.', array('%url' => $url)));
}
protected function assertHeader($name, $expected, $headers = NULL) {
if (!isset($headers)) {
$headers = $this->backdropGetHeaders();
$name = strtolower($name);
}
return $this->assertIdentical($headers[$name], $expected);
}
}
class RedirectUnitTest extends RedirectTestHelper {
/**
* Test the redirect_compare_array_recursive() function.
*/
function testCompareArrayRecursive() {
$haystack = array('a' => 'aa', 'b' => 'bb', 'c' => array('c1' => 'cc1', 'c2' => 'cc2'));
$cases = array(
array('query' => array('a' => 'aa', 'b' => 'invalid'), 'result' => FALSE),
array('query' => array('b' => 'bb', 'b' => 'bb'), 'result' => TRUE),
array('query' => array('b' => 'bb', 'c' => 'invalid'), 'result' => FALSE),
array('query' => array('b' => 'bb', 'c' => array()), 'result' => TRUE),
array('query' => array('b' => 'bb', 'c' => array('invalid')), 'result' => FALSE),
array('query' => array('b' => 'bb', 'c' => array('c2' => 'invalid')), 'result' => FALSE),
array('query' => array('b' => 'bb', 'c' => array('c2' => 'cc2')), 'result' => TRUE),
);
foreach ($cases as $index => $case) {
$this->assertEqual($case['result'], redirect_compare_array_recursive($case['query'], $haystack));
}
}
/**
* Test redirect_sort_recursive().
*/
function testSortRecursive() {
$test_cases = array(
array(
'input' => array('b' => 'aa', 'c' => array('c2' => 'aa', 'c1' => 'aa'), 'a' => 'aa'),
'expected' => array('a' => 'aa', 'b' => 'aa', 'c' => array('c1' => 'aa', 'c2' => 'aa')),
'callback' => 'ksort',
),
);
foreach ($test_cases as $index => $test_case) {
$output = $test_case['input'];
redirect_sort_recursive($output, $test_case['callback']);
$this->assertIdentical($output, $test_case['expected']);
}
}
/**
* Test redirect_parse_url().
*/
function testParseURL() {
//$test_cases = array(
// array(
// 'input' => array('b' => 'aa', 'c' => array('c2' => 'aa', 'c1' => 'aa'), 'a' => 'aa'),
// 'expected' => array('a' => 'aa', 'b' => 'aa', 'c' => array('c1' => 'aa', 'c2' => 'aa')),
// ),
//);
//foreach ($test_cases as $index => $test_case) {
// $output = redirect_parse_url($test_case['input']);
// $this->assertIdentical($output, $test_case['expected']);
//}
}
}
class RedirectFunctionalTest extends RedirectTestHelper {
private $admin_user;
function setUp(array $modules = array()) {
parent::setUp($modules);
$this->admin_user = $this->backdropCreateUser(array('administer redirects', 'access site reports', 'access content', 'create post content', 'edit any post content', 'create url aliases'));
$this->backdropLogin($this->admin_user);
}
function test404Interface() {
// Check that 404 pages do get add redirect links for admin users.
$this->backdropGet('invalid-path1');
$this->backdropGet('invalid-path2');
$this->assertLink('Add URL redirect from this page to another location');
// Check that 403 pages do not get the add redirect link at all.
$this->backdropGet('admin/config/system/actions');
$this->assertNoLink('Add URL redirect from this page to another location');
$this->backdropGet('admin/reports/page-not-found');
$this->clickLink('Fix 404 pages with URL redirects');
// Check that normal users do not see the add redirect link on 404 pages.
$this->backdropLogout();
$this->backdropGet('invalid-path3');
$this->assertNoLink('Add an URL redirect from this page to another location');
}
function testPageCache() {
// Set up cache variables.
config_set('system.core', 'cache', 1);
$edit = array(
'redirect_page_cache' => TRUE,
'redirect_purge_inactive' => 604800,
);
$this->backdropPost('admin/config/search/redirect/settings', $edit, 'Save configuration');
$this->assertText('The configuration options have been saved.');
$this->backdropLogout();
// Add a new redirect.
$redirect = $this->addRedirect('redirect', 'node');
$this->assertEqual($redirect->access, 0);
$this->assertEqual($redirect->count, 0);
$this->assertPageNotCached('redirect');
// Perform the redirect and check that last_used
$redirect = $this->assertRedirect($redirect);
$this->assertEqual($redirect->count, 1);
$this->assertTrue($redirect->access > 0);
$cache = $this->assertPageCached('redirect');
$this->assertHeader('Location', url('node', array('absolute' => TRUE)), $cache->data['headers']);
$this->assertHeader('X-Redirect-ID', $redirect->rid, $cache->data['headers']);
// Set a redirect to not used in a while and disable running bootstrap
// hooks during cache page serve. Running cron to remove inactive redirects
// should not remove since they cannot be tracked.
$redirect->access = 1;
redirect_save($redirect);
variable_set('page_cache_invoke_hooks', FALSE);
$this->cronRun();
$this->assertRedirect($redirect);
$redirect->access = 1;
redirect_save($redirect);
variable_set('page_cache_invoke_hooks', TRUE);
$this->cronRun();
$this->assertNoRedirect($redirect);
}
function testPathChangeRedirects() {
// Create an initial post node with a path alias.
$node = $this->backdropCreateNode(array('type' => 'post', 'path' => array('alias' => 'first-alias')));
// Change the node's alias will create an automatic redirect from 'first-alias' to the node.
$this->backdropPost("node/{$node->nid}/edit", array('path[alias]' => 'second-alias'), 'Save');
//$redirect = redirect_load_by_source('first-alias');
//$this->assertRedirect($redirect);
$this->backdropPost("node/{$node->nid}/edit", array('path[alias]' => 'first-alias'), 'Save');
//$redirect = redirect_load_by_source('second-alias');
//$this->assertRedirect($redirect);
$this->backdropPost("node/{$node->nid}/edit", array('path[alias]' => 'second-alias'), 'Save');
//$redirect = redirect_load_by_source('first-alias');
//$this->assertRedirect($redirect);
}
}