-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtest-pgcache-redirect-cache.php
More file actions
229 lines (196 loc) · 5.38 KB
/
Copy pathtest-pgcache-redirect-cache.php
File metadata and controls
229 lines (196 loc) · 5.38 KB
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
229
<?php
/**
* Standalone regression test for page-cache redirect write decisions.
*
* Run with: php tests/test-pgcache-redirect-cache.php
*
* Exit code 0 = all pass, non-zero = failures.
*
* @package W3TC\Tests
* @since 2.10.0
*/
namespace {
if ( \realpath( __FILE__ ) !== \realpath( $_SERVER['SCRIPT_FILENAME'] ?? '' ) ) {
return;
}
}
namespace W3TC {
if ( ! \class_exists( __NAMESPACE__ . '\\Dispatcher', false ) ) {
/**
* Config stub for the standalone page-cache redirect test.
*/
class PgCacheRedirectTestConfigStub {
public function get_boolean( $key ) {
return false;
}
public function get_integer( $key ) {
return 0;
}
public function get_string( $key ) {
return '';
}
public function get_array( $key ) {
return array();
}
}
/**
* Dispatcher stub for the standalone page-cache redirect test.
*/
class Dispatcher {
public static function config() {
return new PgCacheRedirectTestConfigStub();
}
public static function component( $name ) {
return null;
}
}
/**
* Environment stub for the standalone page-cache redirect test.
*/
class Util_Environment {
public static function host_port() {
return 'example.com';
}
public static function is_https() {
return false;
}
public static function is_preview_mode() {
return false;
}
public static function parse_path( $path ) {
return $path;
}
}
}
}
namespace {
if ( ! \function_exists( 'w3tc_apply_filters' ) ) {
function w3tc_apply_filters( $tag, $value, ...$args ) {
return $value;
}
}
if ( ! \function_exists( 'wp_parse_url' ) ) {
function wp_parse_url( $url ) {
return \parse_url( $url );
}
}
require_once __DIR__ . '/../PgCache_ContentGrabber.php';
class W3TC_Test_PgCacheRedirectGrabber extends \W3TC\PgCache_ContentGrabber {
public function _passed_accept_files() {
return true;
}
}
$w3tc_pgcache_redirect_failures = 0;
function pgcr_assert_same( $label, $expected, $actual ) {
global $w3tc_pgcache_redirect_failures;
if ( $expected === $actual ) {
echo "[PASS] $label\n";
return;
}
++$w3tc_pgcache_redirect_failures;
echo "[FAIL] $label\n";
echo ' Expected: ' . \var_export( $expected, true ) . "\n";
echo ' Actual: ' . \var_export( $actual, true ) . "\n";
}
function pgcr_set_private_property( $object, $property, $value ) {
$reflection = new \ReflectionProperty( \W3TC\PgCache_ContentGrabber::class, $property );
$reflection->setAccessible( true );
$reflection->setValue( $object, $value );
}
function pgcr_get_private_property( $object, $property ) {
$reflection = new \ReflectionProperty( \W3TC\PgCache_ContentGrabber::class, $property );
$reflection->setAccessible( true );
return $reflection->getValue( $object );
}
function pgcr_can_write_cache( $object, $buffer, $headers ) {
$reflection = new \ReflectionMethod( \W3TC\PgCache_ContentGrabber::class, '_can_write_cache' );
$reflection->setAccessible( true );
return $reflection->invoke( $object, $buffer, $headers );
}
function pgcr_new_grabber( $enhanced_mode = false ) {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';
$grabber = new W3TC_Test_PgCacheRedirectGrabber();
pgcr_set_private_property( $grabber, '_caching', true );
pgcr_set_private_property( $grabber, '_enhanced_mode', $enhanced_mode );
pgcr_set_private_property(
$grabber,
'_request_url_fragments',
array(
'host' => 'example.com',
'path' => '/',
'querystring' => '',
)
);
pgcr_set_private_property(
$grabber,
'_page_key_extension',
array(
'useragent' => '',
'referrer' => '',
'cookie' => '',
'encryption' => '',
'group' => '',
'content_type' => '',
'compression' => false,
)
);
return $grabber;
}
$redirect_grabber = pgcr_new_grabber();
$redirect_headers = array(
'kv' => array(
'location' => 'http://127.0.0.1',
),
'plain' => array(
array(
'name' => 'Location',
'value' => 'http://127.0.0.1',
),
),
);
pgcr_assert_same(
'Redirect responses are not written to page cache',
false,
pgcr_can_write_cache( $redirect_grabber, '', $redirect_headers )
);
pgcr_assert_same(
'Redirect rejection reason is recorded',
'Redirect response',
pgcr_get_private_property( $redirect_grabber, 'cache_reject_reason' )
);
pgcr_assert_same(
'Redirect rejection status is recorded',
'miss_redirect',
pgcr_get_private_property( $redirect_grabber, 'process_status' )
);
$enhanced_redirect_grabber = pgcr_new_grabber( true );
pgcr_assert_same(
'Enhanced redirect responses can reach existing cleanup path',
true,
pgcr_can_write_cache( $enhanced_redirect_grabber, '', $redirect_headers )
);
pgcr_assert_same(
'Enhanced redirect rejection reason is recorded',
'Redirect response',
pgcr_get_private_property( $enhanced_redirect_grabber, 'cache_reject_reason' )
);
pgcr_assert_same(
'Enhanced redirect rejection status is recorded',
'miss_redirect',
pgcr_get_private_property( $enhanced_redirect_grabber, 'process_status' )
);
$html_grabber = pgcr_new_grabber();
$html_headers = array(
'kv' => array(),
'plain' => array(),
);
pgcr_assert_same(
'Non-empty HTML response can still be written',
true,
pgcr_can_write_cache( $html_grabber, '<html><body>ok</body></html>', $html_headers )
);
if ( $w3tc_pgcache_redirect_failures > 0 ) {
exit( 1 );
}
}