-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathtest-speculation-rules-plugin-api.php
More file actions
392 lines (341 loc) · 10.9 KB
/
Copy pathtest-speculation-rules-plugin-api.php
File metadata and controls
392 lines (341 loc) · 10.9 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* Tests for speculation-rules plugin API.
*
* @package speculation-rules
*/
class Test_Speculation_Rules_Plugin_API extends WP_UnitTestCase {
/** @var array<string, mixed> */
private $original_wp_theme_features = array();
/**
* Runs the routine before each test is executed.
*/
public function set_up(): void {
parent::set_up();
$this->original_wp_theme_features = $GLOBALS['_wp_theme_features'];
add_filter(
'template_directory_uri',
static function () {
return content_url( 'themes/template' );
}
);
add_filter(
'stylesheet_directory_uri',
static function () {
return content_url( 'themes/stylesheet' );
}
);
}
public function tear_down(): void {
$GLOBALS['_wp_theme_features'] = $this->original_wp_theme_features;
parent::tear_down();
}
/**
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules(): void {
$rules = plsr_get_speculation_rules();
$this->assertArrayHasKey( 'prerender', $rules );
$this->assertIsArray( $rules['prerender'] );
foreach ( $rules['prerender'] as $entry ) {
$this->assertIsArray( $entry );
$this->assertArrayHasKey( 'source', $entry );
$this->assertTrue( in_array( $entry['source'], array( 'list', 'document' ), true ) );
}
}
/**
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_href_exclude_paths(): void {
$rules = plsr_get_speculation_rules();
$href_exclude_paths = $rules['prerender'][0]['where']['and'][1]['not']['href_matches'];
$this->assertSameSets(
array(
'/wp-*.php',
'/wp-admin/*',
'/wp-content/uploads/*',
'/wp-content/*',
'/wp-content/plugins/*',
'/wp-content/themes/stylesheet/*',
'/wp-content/themes/template/*',
'/*\\?*(^|&)_wpnonce=*',
),
$href_exclude_paths,
'Snapshot: ' . var_export( $href_exclude_paths, true )
);
// Add filter that attempts to replace base exclude paths with a custom path to exclude.
add_filter(
'plsr_speculation_rules_href_exclude_paths',
static function () {
return array( 'custom-file.php' );
}
);
$rules = plsr_get_speculation_rules();
$href_exclude_paths = $rules['prerender'][0]['where']['and'][1]['not']['href_matches'];
// Ensure the base exclude paths are still present and that the custom path was formatted correctly.
$this->assertSameSets(
array(
'/wp-*.php',
'/wp-admin/*',
'/wp-content/uploads/*',
'/wp-content/*',
'/wp-content/plugins/*',
'/wp-content/themes/stylesheet/*',
'/wp-content/themes/template/*',
'/*\\?*(^|&)_wpnonce=*',
'/custom-file.php',
),
$href_exclude_paths,
'Snapshot: ' . var_export( $href_exclude_paths, true )
);
}
/**
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_href_exclude_paths_with_pretty_permalinks(): void {
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
$rules = plsr_get_speculation_rules();
$href_exclude_paths = $rules['prerender'][0]['where']['and'][1]['not']['href_matches'];
$this->assertSameSets(
array(
'/wp-*.php',
'/wp-admin/*',
'/wp-content/uploads/*',
'/wp-content/*',
'/wp-content/plugins/*',
'/wp-content/themes/stylesheet/*',
'/wp-content/themes/template/*',
'/*\\?(.+)',
),
$href_exclude_paths,
'Snapshot: ' . var_export( $href_exclude_paths, true )
);
}
/**
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_href_exclude_paths_with_mode(): void {
// Add filter that adds an exclusion only if the mode is 'prerender'.
add_filter(
'plsr_speculation_rules_href_exclude_paths',
static function ( $exclude_paths, $mode ) {
if ( 'prerender' === $mode ) {
$exclude_paths[] = '/products/*';
}
return $exclude_paths;
},
10,
2
);
$rules = plsr_get_speculation_rules();
$href_exclude_paths = $rules['prerender'][0]['where']['and'][1]['not']['href_matches'];
// Ensure the additional exclusion is present because the mode is 'prerender'.
// Also ensure keys are sequential starting from 0 (that is, that array_is_list()).
$this->assertSame(
array(
'/wp-*.php',
'/wp-admin/*',
'/wp-content/uploads/*',
'/wp-content/*',
'/wp-content/plugins/*',
'/wp-content/themes/stylesheet/*',
'/wp-content/themes/template/*',
'/*\\?*(^|&)_wpnonce=*',
'/products/*',
),
$href_exclude_paths,
'Snapshot: ' . var_export( $href_exclude_paths, true )
);
// Update mode to be 'prefetch'.
update_option( 'plsr_speculation_rules', array( 'mode' => 'prefetch' ) );
$rules = plsr_get_speculation_rules();
$href_exclude_paths = $rules['prefetch'][0]['where']['and'][1]['not']['href_matches'];
// Ensure the additional exclusion is not present because the mode is 'prefetch'.
$this->assertSame(
array(
'/wp-*.php',
'/wp-admin/*',
'/wp-content/uploads/*',
'/wp-content/*',
'/wp-content/plugins/*',
'/wp-content/themes/stylesheet/*',
'/wp-content/themes/template/*',
'/*\\?*(^|&)_wpnonce=*',
),
$href_exclude_paths,
'Snapshot: ' . var_export( $href_exclude_paths, true )
);
}
/**
* Tests filter that explicitly adds non-sequential keys.
*
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_with_filtering_bad_keys(): void {
add_filter(
'plsr_speculation_rules_href_exclude_paths',
static function ( array $exclude_paths ): array {
$exclude_paths[] = '/next/';
array_unshift( $exclude_paths, '/unshifted/' );
$exclude_paths[-1] = '/negative-one/';
$exclude_paths[100] = '/one-hundred/';
$exclude_paths['a'] = '/letter-a/';
return $exclude_paths;
}
);
$actual = plsr_get_speculation_rules()['prerender'][0]['where']['and'][1]['not']['href_matches'];
$this->assertSame(
array(
'/wp-*.php',
'/wp-admin/*',
'/wp-content/uploads/*',
'/wp-content/*',
'/wp-content/plugins/*',
'/wp-content/themes/stylesheet/*',
'/wp-content/themes/template/*',
'/*\\?*(^|&)_wpnonce=*',
'/unshifted/',
'/next/',
'/negative-one/',
'/one-hundred/',
'/letter-a/',
),
$actual,
'Snapshot: ' . var_export( $actual, true )
);
}
/**
* Tests scenario when the home_url and site_url have different paths.
*
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_different_home_and_site_urls(): void {
add_filter(
'site_url',
static function (): string {
return 'https://example.com/wp/';
}
);
add_filter(
'home_url',
static function (): string {
return 'https://example.com/blog/';
}
);
add_filter(
'plsr_speculation_rules_href_exclude_paths',
static function ( array $exclude_paths ): array {
$exclude_paths[] = '/store/*';
return $exclude_paths;
}
);
$actual = plsr_get_speculation_rules()['prerender'][0]['where']['and'][1]['not']['href_matches'];
$this->assertSame(
array(
'/wp/wp-*.php',
'/wp/wp-admin/*',
'/wp-content/uploads/*',
'/wp-content/*',
'/wp-content/plugins/*',
'/wp-content/themes/stylesheet/*',
'/wp-content/themes/template/*',
'/blog/*\\?*(^|&)_wpnonce=*',
'/blog/store/*',
),
$actual,
'Snapshot: ' . var_export( $actual, true )
);
}
/**
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_prerender(): void {
$rules = plsr_get_speculation_rules();
$this->assertArrayHasKey( 'prerender', $rules );
$this->assertCount( 4, $rules['prerender'][0]['where']['and'] );
}
/**
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_prerender_until_script(): void {
update_option( 'plsr_speculation_rules', array( 'mode' => 'prerender_until_script' ) );
$rules = plsr_get_speculation_rules();
$this->assertArrayHasKey( 'prerender_until_script', $rules );
$this->assertCount( 4, $rules['prerender_until_script'][0]['where']['and'] );
$this->assertSame(
'.no-prerender, .no-prerender_until_script',
$rules['prerender_until_script'][0]['where']['and'][3]['not']['selector_matches']
);
}
/**
* @covers ::plsr_get_speculation_rules
*/
public function test_plsr_get_speculation_rules_prefetch(): void {
update_option( 'plsr_speculation_rules', array( 'mode' => 'prefetch' ) );
$rules = plsr_get_speculation_rules();
$this->assertArrayHasKey( 'prefetch', $rules );
$this->assertCount( 3, $rules['prefetch'][0]['where']['and'] );
}
/**
* @covers ::plsr_get_speculation_rules
* @dataProvider data_plsr_get_speculation_rules_with_eagerness
*/
public function test_plsr_get_speculation_rules_with_eagerness( string $eagerness ): void {
update_option( 'plsr_speculation_rules', array( 'eagerness' => $eagerness ) );
$rules = plsr_get_speculation_rules();
$this->assertArrayHasKey( 'prerender', $rules );
$this->assertSame( $eagerness, $rules['prerender'][0]['eagerness'] );
}
/** @return array<int, string[]> */
public function data_plsr_get_speculation_rules_with_eagerness(): array {
return array(
array( 'conservative' ),
array( 'moderate' ),
array( 'eager' ),
);
}
/**
* @covers ::plsr_print_speculation_rules
*/
public function test_plsr_print_speculation_rules_without_html5_support(): void {
$this->enable_pretty_permalinks();
$output = get_echo( 'plsr_print_speculation_rules' );
$this->assertStringContainsString( '<script type="speculationrules">', $output );
$json = str_replace( array( '<script type="speculationrules">', '</script>' ), '', $output );
$rules = json_decode( $json, true );
$this->assertIsArray( $rules );
$this->assertArrayHasKey( 'prerender', $rules );
}
/**
* @covers ::plsr_print_speculation_rules
*/
public function test_plsr_print_speculation_rules_without_pretty_permalinks(): void {
$this->disable_pretty_permalinks();
$output = get_echo( 'plsr_print_speculation_rules' );
$this->assertSame( '', $output );
}
/**
* @covers ::plsr_print_speculation_rules
*/
public function test_plsr_print_speculation_rules_without_pretty_permalinks_but_opted_in(): void {
$this->disable_pretty_permalinks();
add_filter( 'plsr_enabled_without_pretty_permalinks', '__return_true' );
$output = get_echo( 'plsr_print_speculation_rules' );
$this->assertStringContainsString( '<script type="speculationrules">', $output );
}
/**
* @covers ::plsr_print_speculation_rules
*/
public function test_plsr_print_speculation_rules_for_logged_in_user(): void {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$this->enable_pretty_permalinks();
$output = get_echo( 'plsr_print_speculation_rules' );
$this->assertSame( '', $output );
}
private function enable_pretty_permalinks(): void {
update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
}
private function disable_pretty_permalinks(): void {
update_option( 'permalink_structure', '' );
}
}