forked from WordPress/performance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-optimization.php
More file actions
494 lines (448 loc) · 17.1 KB
/
test-optimization.php
File metadata and controls
494 lines (448 loc) · 17.1 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
/**
* Tests for optimization-detective plugin optimization.php.
*
* @package optimization-detective
*
* @noinspection PhpUnhandledExceptionInspection
* @todo There are "Cannot resolve ..." errors and "Element img doesn't have a required attribute src" warnings that should be excluded from inspection.
*/
class Test_OD_Optimization extends WP_UnitTestCase {
use Optimization_Detective_Test_Helpers;
/**
* @var string
*/
private $original_request_uri;
/**
* @var string
*/
private $original_request_method;
/**
* @var string
*/
private $default_mimetype;
public function set_up(): void {
parent::set_up();
$this->original_request_uri = $_SERVER['REQUEST_URI'];
$this->original_request_method = $_SERVER['REQUEST_METHOD'];
$this->default_mimetype = (string) ini_get( 'default_mimetype' );
}
public function tear_down(): void {
$_SERVER['REQUEST_URI'] = $this->original_request_uri;
$_SERVER['REQUEST_METHOD'] = $this->original_request_method;
ini_set( 'default_mimetype', $this->default_mimetype ); // phpcs:ignore WordPress.PHP.IniSet.Risky
unset( $GLOBALS['wp_customize'] );
parent::tear_down();
}
/**
* Make output is buffered and that it is also filtered.
*
* @covers ::od_buffer_output
*/
public function test_od_buffer_output(): void {
$original = 'Hello World!';
$expected = '¡Hola Mundo!';
// In order to test, a wrapping output buffer is required because ob_get_clean() does not invoke the output
// buffer callback. See <https://stackoverflow.com/a/61439514/93579>.
ob_start();
$filter_invoked = false;
add_filter(
'od_template_output_buffer',
function ( $buffer ) use ( $original, $expected, &$filter_invoked ) {
$this->assertSame( $original, $buffer );
$filter_invoked = true;
return $expected;
}
);
$original_ob_level = ob_get_level();
$template = sprintf( 'page-%s.php', wp_generate_uuid4() );
$this->assertSame( $template, od_buffer_output( $template ), 'Expected value to be passed through.' );
$this->assertSame( $original_ob_level + 1, ob_get_level(), 'Expected call to ob_start().' );
echo $original;
ob_end_flush(); // Flushing invokes the output buffer callback.
$buffer = ob_get_clean(); // Get the buffer from our wrapper output buffer.
$this->assertSame( $expected, $buffer );
$this->assertTrue( $filter_invoked );
}
/**
* Test that calling ob_flush() will not result in the buffer being processed and that ob_clean() will successfully prevent content from being processed.
*
* @covers ::od_buffer_output
*/
public function test_od_buffer_with_cleaning_and_attempted_flushing(): void {
$template_aborted = 'Before time began!';
$template_start = 'The beginning';
$template_middle = ', the middle';
$template_end = ', and the end!';
// In order to test, a wrapping output buffer is required because ob_get_clean() does not invoke the output
// buffer callback. See <https://stackoverflow.com/a/61439514/93579>.
$initial_level = ob_get_level();
$this->assertTrue( ob_start() );
$this->assertSame( $initial_level + 1, ob_get_level() );
$filter_count = 0;
add_filter(
'od_template_output_buffer',
function ( $buffer ) use ( $template_start, $template_middle, $template_end, &$filter_count ) {
$filter_count++;
$this->assertSame( $template_start . $template_middle . $template_end, $buffer );
return '<filtered>' . $buffer . '</filtered>';
}
);
od_buffer_output( '' );
$this->assertSame( $initial_level + 2, ob_get_level() );
echo $template_aborted;
$this->assertTrue( ob_clean() ); // By cleaning, the above should never be seen by the filter.
// This is the start of what will end up getting filtered.
echo $template_start;
// Attempt to flush the output, which will fail because the output buffer was opened without the flushable flag.
$this->assertFalse( ob_flush() );
// This will also be sent into the filter.
echo $template_middle;
$this->assertFalse( ob_flush() );
$this->assertSame( $initial_level + 2, ob_get_level() );
// Start a nested output buffer which will also end up getting sent into the filter.
$this->assertTrue( ob_start() );
echo $template_end;
$this->assertSame( $initial_level + 3, ob_get_level() );
$this->assertTrue( ob_flush() );
$this->assertTrue( ob_end_flush() );
$this->assertSame( $initial_level + 2, ob_get_level() );
// Close the output buffer opened by od_buffer_output(). This only works in the unit test because the removable flag was passed.
$this->assertTrue( ob_end_flush() );
$this->assertSame( $initial_level + 1, ob_get_level() );
$buffer = ob_get_clean(); // Get the buffer from our wrapper output buffer and close it.
$this->assertSame( $initial_level, ob_get_level() );
$this->assertSame( 1, $filter_count, 'Expected filter to be called once.' );
$this->assertSame(
'<filtered>' . $template_start . $template_middle . $template_end . '</filtered>',
$buffer,
'Excepted return value of filter to be the resulting value for the buffer.'
);
}
/**
* Data provider.
*
* @return array<string, mixed>
*/
public function data_provider_test_od_maybe_add_template_output_buffer_filter(): array {
return array(
'home_enabled' => array(
'set_up' => static function (): string {
return home_url( '/' );
},
'expected_has_filter' => true,
),
'home_disabled_by_filter' => array(
'set_up' => static function (): string {
add_filter( 'od_can_optimize_response', '__return_false' );
return home_url( '/' );
},
'expected_has_filter' => false,
),
'search_disabled' => array(
'set_up' => static function (): string {
return home_url( '/?s=foo' );
},
'expected_has_filter' => false,
),
'search_enabled_by_filter' => array(
'set_up' => static function (): string {
add_filter( 'od_can_optimize_response', '__return_true' );
return home_url( '/?s=foo' );
},
'expected_has_filter' => true,
),
'home_disabled_by_get_param' => array(
'set_up' => static function (): string {
return home_url( '/?optimization_detective_disabled=1' );
},
'expected_has_filter' => false,
),
'home_disabled_by_rest_api_unavailable' => array(
'set_up' => static function (): string {
update_option( 'od_rest_api_unavailable', '1' );
return home_url( '/' );
},
'expected_has_filter' => false,
),
);
}
/**
* Test od_maybe_add_template_output_buffer_filter().
*
* @dataProvider data_provider_test_od_maybe_add_template_output_buffer_filter
*
* @covers ::od_maybe_add_template_output_buffer_filter
* @covers ::od_can_optimize_response
* @covers ::od_is_rest_api_unavailable
*/
public function test_od_maybe_add_template_output_buffer_filter( Closure $set_up, bool $expected_has_filter ): void {
// There needs to be a post so that there is a post in the loop so that od_get_cache_purge_post_id() returns a post ID.
// Otherwise, od_can_optimize_response() will return false unless forced by a filter.
self::factory()->post->create();
$url = $set_up();
$this->go_to( $url );
remove_all_filters( 'od_template_output_buffer' ); // In case go_to() caused them to be added.
od_maybe_add_template_output_buffer_filter();
$this->assertSame( $expected_has_filter, has_filter( 'od_template_output_buffer' ) );
}
/**
* Test od_print_disabled_reasons().
*
* @covers ::od_print_disabled_reasons
*/
public function test_od_print_disabled_reasons(): void {
$this->assertSame( '', get_echo( 'od_print_disabled_reasons', array( array() ) ) );
$foo_message = get_echo( 'od_print_disabled_reasons', array( array( 'Foo' ) ) );
$this->assertStringContainsString( '<script', $foo_message );
$this->assertStringContainsString( 'console.info(', $foo_message );
$this->assertStringContainsString( '[Optimization Detective] Foo', $foo_message );
$foo_and_bar_messages = get_echo( 'od_print_disabled_reasons', array( array( 'Foo', 'Bar' ) ) );
$this->assertStringContainsString( '<script', $foo_and_bar_messages );
$this->assertStringContainsString( 'console.info(', $foo_and_bar_messages );
$this->assertStringContainsString( '[Optimization Detective] Foo', $foo_and_bar_messages );
$this->assertStringContainsString( '[Optimization Detective] Bar', $foo_and_bar_messages );
}
/**
* Data provider.
*
* @return array<string, mixed> Data.
*/
public function data_provider_test_od_can_optimize_response(): array {
return array(
'home_as_anonymous' => array(
'set_up' => function (): void {
$this->go_to( home_url( '/' ) );
$this->assertIsInt( od_get_cache_purge_post_id() );
},
'expected' => true,
),
'home_but_no_posts' => array(
'set_up' => function (): void {
$posts = get_posts();
foreach ( $posts as $post ) {
wp_delete_post( $post->ID, true );
}
$this->go_to( home_url( '/' ) );
$this->assertNull( od_get_cache_purge_post_id() );
},
'expected' => false, // This is because od_get_cache_purge_post_id() will return false.
),
'home_filtered_as_anonymous' => array(
'set_up' => static function (): string {
add_filter( 'od_can_optimize_response', '__return_false' );
return home_url( '/' );
},
'expected' => false,
),
'singular_as_anonymous' => array(
'set_up' => function (): string {
$posts = get_posts();
$this->assertInstanceOf( WP_Post::class, $posts[0] );
return get_permalink( $posts[0] );
},
'expected' => true,
),
'search_as_anonymous' => array(
'set_up' => static function (): string {
self::factory()->post->create( array( 'post_title' => 'Hello' ) );
return home_url( '?s=Hello' );
},
'expected' => false,
),
'home_customizer_preview_as_anonymous' => array(
'set_up' => static function (): string {
global $wp_customize;
require_once ABSPATH . 'wp-includes/class-wp-customize-manager.php';
$wp_customize = new WP_Customize_Manager();
$wp_customize->start_previewing_theme();
return home_url( '/' );
},
'expected' => false,
),
'singular_as_post_preview' => array(
'set_up' => static function (): string {
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
wp_set_current_user( $user_id );
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello',
'post_status' => 'draft',
'post_author' => $user_id,
)
);
return (string) get_preview_post_link( $post_id );
},
'expected' => false,
),
'home_post_request_as_anonymous' => array(
'set_up' => static function (): string {
$_SERVER['REQUEST_METHOD'] = 'POST';
return home_url( '/' );
},
'expected' => false,
),
'home_as_subscriber' => array(
'set_up' => static function (): string {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
return home_url( '/' );
},
'expected' => true,
),
'empty_author_page_as_anonymous' => array(
'set_up' => static function (): string {
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
return get_author_posts_url( $user_id );
},
'expected' => false,
),
'home_as_admin' => array(
'set_up' => static function (): string {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
return home_url( '/' );
},
'expected' => true,
),
);
}
/**
* Test od_can_optimize_response().
*
* @covers ::od_can_optimize_response
* @covers ::od_get_cache_purge_post_id
*
* @dataProvider data_provider_test_od_can_optimize_response
*/
public function test_od_can_optimize_response( Closure $set_up, bool $expected ): void {
// Make sure there is at least one post in the DB as otherwise od_get_cache_purge_post_id() will return false,
// causing od_can_optimize_response() to return false.
self::factory()->post->create();
$url = $set_up();
$this->go_to( $url );
$this->assertSame( $expected, od_can_optimize_response() );
}
/**
* Data provider.
*
* @return array<string, array{ directory: non-empty-string }> Test cases.
*/
public function data_provider_test_od_optimize_template_output_buffer(): array {
return $this->load_snapshot_test_cases( __DIR__ . '/test-cases' );
}
/**
* Test od_optimize_template_output_buffer().
*
* @covers ::od_optimize_template_output_buffer
* @covers ::od_is_response_html_content_type
* @covers OD_Tag_Visitor_Context::__construct
* @covers OD_Tag_Visitor_Context::__get
* @covers OD_Tag_Visitor_Context::track_tag
* @covers OD_Visited_Tag_State::__construct
* @covers OD_Visited_Tag_State::track_tag
* @covers OD_Visited_Tag_State::is_tag_tracked
* @covers OD_Visited_Tag_State::reset
* @covers OD_HTML_Tag_Processor::is_admin_bar
* @covers OD_Template_Optimization_Context::__construct
* @covers OD_Template_Optimization_Context::__get
*
* @dataProvider data_provider_test_od_optimize_template_output_buffer
*
* @param non-empty-string $directory Test case directory.
*
* @noinspection PhpDocMissingThrowsInspection
*/
public function test_od_optimize_template_output_buffer( string $directory ): void {
$this->assertSame( 0, did_action( 'od_register_tag_visitors' ) );
$this->assertSame( 0, did_action( 'od_start_template_optimization' ) );
$this->assertSame( 0, did_action( 'od_finish_template_optimization' ) );
$did_initialize = false;
add_action(
'od_register_tag_visitors',
static function () use ( &$did_initialize ): void {
$did_initialize = true;
}
);
add_action(
'od_register_tag_visitors',
function ( OD_Tag_Visitor_Registry $tag_visitor_registry ): void {
$tag_visitor_registry->register(
'img',
function ( OD_Tag_Visitor_Context $context ): bool {
$this->assertInstanceOf( OD_URL_Metric_Group_Collection::class, $context->url_metric_group_collection );
$this->assertInstanceOf( OD_HTML_Tag_Processor::class, $context->processor );
$this->assertInstanceOf( OD_Link_Collection::class, $context->link_collection );
$this->assertTrue( null === $context->url_metrics_id || $context->url_metrics_id > 0 );
$error = null;
$value = '';
try {
$value = $context->__get( 'invalid_param' );
} catch ( Error $e ) {
$error = $e;
}
$this->assertInstanceOf( Error::class, $error );
$this->assertSame( '', $value );
$this->assertFalse( $context->processor->is_tag_closer() );
return $context->processor->get_tag() === 'IMG';
}
);
}
);
add_action(
'od_register_tag_visitors',
function ( OD_Tag_Visitor_Registry $tag_visitor_registry ): void {
$tag_visitor_registry->register(
'video',
function ( OD_Tag_Visitor_Context $context ): void {
$this->assertFalse( $context->processor->is_tag_closer() );
if ( $context->processor->get_tag() === 'VIDEO' ) {
$context->track_tag();
}
}
);
}
);
$template_optimization_context = null;
add_action(
'od_start_template_optimization',
function ( OD_Template_Optimization_Context $context ) use ( &$template_optimization_context ): void {
$this->assertInstanceOf( OD_URL_Metric_Group_Collection::class, $context->url_metric_group_collection );
$this->assertTrue( is_int( $context->url_metrics_id ) || is_null( $context->url_metrics_id ) );
$this->assertIsArray( $context->normalized_query_vars );
$this->assertIsString( $context->url_metrics_slug );
$this->assertSame( od_get_url_metrics_slug( $context->normalized_query_vars ), $context->url_metrics_slug );
if ( is_int( $context->url_metrics_id ) ) {
$post = get_post( $context->url_metrics_id );
$this->assertInstanceOf( WP_Post::class, $post );
$this->assertSame( $post->post_name, $context->url_metrics_slug );
}
$this->assertInstanceOf( OD_Link_Collection::class, $context->link_collection );
$error = null;
$value = '';
try {
$value = $context->__get( 'invalid_param' );
} catch ( Error $e ) {
$error = $e;
}
$this->assertInstanceOf( Error::class, $error );
$this->assertSame( '', $value );
$template_optimization_context = $context;
}
);
add_action(
'od_finish_template_optimization',
function ( OD_Template_Optimization_Context $context ) use ( &$template_optimization_context ): void {
$this->assertSame( $template_optimization_context, $context );
$context->link_collection->add_link(
array(
'rel' => 'preconnect',
'href' => 'https://inserted-at-finish-template-optimization-action.example.net/',
)
);
}
);
$this->assert_snapshot_equals( $directory );
$this->assertSame( $did_initialize ? 1 : 0, did_action( 'od_register_tag_visitors' ) );
$this->assertSame( $did_initialize ? 1 : 0, did_action( 'od_start_template_optimization' ) );
$this->assertSame( $did_initialize ? 1 : 0, did_action( 'od_finish_template_optimization' ) );
}
}