-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathSiteHealthTest.php
589 lines (529 loc) · 15.3 KB
/
SiteHealthTest.php
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
/**
* Test SiteHealthTest.
*
* @package AmpProject\AmpWP\Tests
*/
namespace AmpProject\AmpWP\Tests\Admin;
use AMP_Options_Manager;
use AmpProject\AmpWP\Admin\SiteHealth;
use AmpProject\AmpWP\AmpSlugCustomizationWatcher;
use AmpProject\AmpWP\AmpWpPluginFactory;
use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\QueryVar;
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
use AmpProject\AmpWP\Tests\TestCase;
use WP_REST_Server;
use WP_Error;
/**
* Test SiteHealthTest.
*
* @coversDefaultClass \AmpProject\AmpWP\Admin\SiteHealth
*/
class SiteHealthTest extends TestCase {
use HomeUrlLoopbackRequestMocking;
use PrivateAccess;
use MockAdminUser;
/**
* Whether external object cache is being used.
*
* @var bool
*/
private $was_wp_using_ext_object_cache;
/** @var WP_REST_Server */
private $original_wp_rest_server;
/**
* The tested instance.
*
* @var SiteHealth
*/
public $instance;
/**
* Sets up each test.
*
* @inheritDoc
*/
public function set_up() {
parent::set_up();
$injector = AmpWpPluginFactory::create()
->get_container()
->get( 'injector' );
$this->instance = $injector->make( SiteHealth::class );
remove_theme_support( 'amp' );
foreach ( get_post_types_by_support( 'amp' ) as $post_type ) {
remove_post_type_support( $post_type, 'amp' );
}
delete_option( AMP_Options_Manager::OPTION_NAME );
$this->was_wp_using_ext_object_cache = wp_using_ext_object_cache();
$this->original_wp_rest_server = isset( $GLOBALS['wp_rest_server'] ) ? $GLOBALS['wp_rest_server'] : null;
$GLOBALS['wp_rest_server'] = null;
$this->add_home_url_loopback_request_mocking();
}
/**
* Tears down after each test.
*
* @inheritDoc
*/
public function tear_down() {
wp_using_ext_object_cache( $this->was_wp_using_ext_object_cache );
$GLOBALS['wp_rest_server'] = $this->original_wp_rest_server;
unset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
parent::tear_down();
}
/**
* Test init.
*
* @covers ::register()
*/
public function test_register() {
$this->instance->register();
$this->assertEquals( 10, has_filter( 'site_status_tests', [ $this->instance, 'add_tests' ] ) );
$this->assertEquals( 10, has_filter( 'debug_information', [ $this->instance, 'add_debug_information' ] ) );
$this->assertEquals( 10, has_filter( 'site_status_test_result', [ $this->instance, 'modify_test_result' ] ) );
$this->assertEquals( 10, has_filter( 'site_status_test_php_modules', [ $this->instance, 'add_extensions' ] ) );
$this->assertEquals( 10, has_action( 'admin_print_styles-site-health.php', [ $this->instance, 'add_styles' ] ) );
$this->assertEquals( 10, has_action( 'admin_print_styles-tools_page_health-check', [ $this->instance, 'add_styles' ] ) );
}
/**
* Test add_tests.
*
* @covers ::add_tests()
*/
public function test_add_tests() {
$tests = $this->instance->add_tests( [] );
$this->assertArrayHasKey( 'direct', $tests );
$this->assertArrayHasKey( 'amp_curl_multi_functions', $tests['direct'] );
$this->assertArrayNotHasKey( 'amp_icu_version', $tests['direct'] );
$this->assertArrayHasKey( 'amp_xdebug_extension', $tests['direct'] );
$this->assertEquals( QueryVar::AMP, amp_get_slug() );
$this->assertArrayNotHasKey( 'amp_slug_definition_timing', $tests['direct'] );
// Test that the the ICU version test is added only when site URL is an IDN.
add_filter( 'site_url', [ self::class, 'get_idn' ], 10, 4 );
add_filter( 'amp_query_var', [ self::class, 'get_lite_query_var' ] );
$tests = $this->instance->add_tests( [] );
$this->assertArrayHasKey( 'amp_icu_version', $tests['direct'] );
$this->assertArrayHasKey( 'amp_slug_definition_timing', $tests['direct'] );
$this->assertArrayHasKey( 'amp_publisher_logo', $tests['direct'] );
remove_filter( 'site_url', [ self::class, 'get_idn' ] );
remove_filter( 'amp_query_var', [ self::class, 'get_lite_query_var' ] );
}
/**
* Test slug_definition_timing.
*
* @covers ::slug_definition_timing()
*/
public function test_slug_definition_timing() {
$data = [
'test' => 'amp_slug_definition_timing',
];
/** @var AmpSlugCustomizationWatcher $amp_slug_customization_watcher */
$amp_slug_customization_watcher = $this->get_private_property( $this->instance, 'amp_slug_customization_watcher' );
$this->set_private_property( $amp_slug_customization_watcher, 'is_customized_late', false );
$this->assertFalse( $amp_slug_customization_watcher->did_customize_late() );
$this->assertAssocArraySubset(
array_merge(
$data,
[
'label' => 'The AMP slug (query var) was defined early',
'status' => 'good',
'badge' => [
'label' => 'AMP',
'color' => 'green',
],
]
),
$this->instance->slug_definition_timing()
);
add_filter( 'amp_query_var', [ self::class, 'get_lite_query_var' ] );
/** @var AmpSlugCustomizationWatcher $amp_slug_customization_watcher */
$amp_slug_customization_watcher = $this->get_private_property( $this->instance, 'amp_slug_customization_watcher' );
$this->set_private_property( $amp_slug_customization_watcher, 'is_customized_late', true );
$this->assertTrue( $amp_slug_customization_watcher->did_customize_late() );
$this->assertAssocArraySubset(
array_merge(
$data,
[
'label' => 'The AMP slug (query var) was defined late',
'status' => 'recommended',
'badge' => [
'label' => 'AMP',
'color' => 'orange',
],
]
),
$this->instance->slug_definition_timing()
);
}
/**
* Test curl_multi_functions.
*
* @covers ::curl_multi_functions()
*/
public function test_curl_multi_functions() {
$this->assertAssocArraySubset(
[
'test' => 'amp_curl_multi_functions',
],
$this->instance->curl_multi_functions()
);
}
/**
* Test icu_version.
*
* @covers ::icu_version()
*/
public function test_icu_version() {
$this->assertAssocArraySubset(
[
'test' => 'amp_icu_version',
],
$this->instance->icu_version()
);
}
/**
* Test css_transient_caching.
*
* @covers ::css_transient_caching()
*/
public function test_css_transient_caching() {
wp_using_ext_object_cache( false );
$data = [
'test' => 'amp_css_transient_caching',
];
AMP_Options_Manager::update_option( Option::DISABLE_CSS_TRANSIENT_CACHING, false );
$this->assertAssocArraySubset(
array_merge(
$data,
[
'status' => 'good',
'badge' => [
'label' => 'AMP',
'color' => 'green',
],
]
),
$this->instance->css_transient_caching()
);
AMP_Options_Manager::update_option( Option::DISABLE_CSS_TRANSIENT_CACHING, true );
$this->assertAssocArraySubset(
array_merge(
$data,
[
'status' => 'recommended',
'badge' => [
'label' => 'AMP',
'color' => 'orange',
],
]
),
$this->instance->css_transient_caching()
);
wp_using_ext_object_cache( true );
$this->assertAssocArraySubset(
array_merge(
$data,
[
'status' => 'good',
'badge' => [
'label' => 'AMP',
'color' => 'blue',
],
]
),
$this->instance->css_transient_caching()
);
}
/**
* Test xdebug_extension.
*
* @covers ::xdebug_extension()
*/
public function test_xdebug_extension() {
$actual = $this->instance->xdebug_extension();
$this->assertEquals( 'amp_xdebug_extension', $actual['test'] );
$this->assertStringContainsString(
esc_html( 'The Xdebug extension can cause some of the AMP plugin’s processes to time out depending on your system resources and configuration. It should not be enabled on a live site (production environment).' ),
$actual['description']
);
}
/**
* Test add_debug_information.
*
* @covers ::add_debug_information()
*/
public function test_add_debug_information() {
$debug_info = $this->instance->add_debug_information( [] );
$this->assertArrayHasKey( 'amp_wp', $debug_info );
$this->assertArrayHasKey( 'fields', $debug_info['amp_wp'] );
$keys = [
'amp_mode_enabled',
'amp_templates_enabled',
'amp_serve_all_templates',
'amp_css_transient_caching_disabled',
'amp_css_transient_caching_threshold',
'amp_css_transient_caching_sampling_range',
'amp_css_transient_caching_transient_count',
'amp_slug_query_var',
'amp_slug_defined_late',
];
foreach ( $keys as $key ) {
$this->assertArrayHasKey( $key, $debug_info['amp_wp']['fields'], "Expected key: $key" );
$this->assertFalse( $debug_info['amp_wp']['fields'][ $key ]['private'], "Expected private for key: $key" );
}
}
/**
* Get test data for test_modify_test_result.
*
* @return array[] Test data.
*/
public function get_test_result() {
return [
'empty_result' => [
[],
],
'good_https_status_result' => [
[
'test' => 'https_status',
'status' => 'good',
'description' => '',
],
],
'recommended_https_status_result' => [
[
'test' => 'https_status',
'status' => 'recommended',
'description' => '',
],
[
'test' => 'https_status',
'status' => 'critical',
'description' => '<p>Additionally, AMP requires HTTPS for most components to work properly, including iframes and videos.</p>',
],
],
];
}
/**
* Test modify_test_result.
*
* @dataProvider get_test_result
*
* @covers ::modify_test_result()
*
* @param array $test_data Data from Site Health test.
* @param array $expected Expected modified test result.
*/
public function test_modify_test_result( $test_data, $expected = null ) {
$test_result = $this->instance->modify_test_result( $test_data );
if ( ! $expected ) {
$expected = $test_data;
}
$this->assertEquals( $expected, $test_result );
}
/**
* Gets the test data for test_get_supported_templates().
*
* @return array The test data.
*/
public function get_supported_templates_data() {
return [
'no_template_supported' => [
[],
[],
'standard',
'No template supported',
],
'only_singular' => [
[ 'post' ],
[ 'is_singular' ],
'transitional',
'post, is_singular',
],
'only_post' => [
[ 'post' ],
[],
'transitional',
'post',
],
'only_post_and_author' => [
[ 'post' ],
[ 'is_author' ],
'transitional',
'post, is_author',
],
'two_templates' => [
[ 'post' ],
[ 'is_singular', 'is_author' ],
'transitional',
'post, is_singular, is_author',
],
'three_templates' => [
[ 'post', 'page' ],
[ 'is_singular', 'is_author', 'is_tag' ],
'transitional',
'post, page, is_singular, is_author, is_tag',
],
'three_templates_reader_mode' => [
[ 'post', 'page' ],
[ 'is_singular', 'is_author', 'is_tag' ],
'reader',
'post, page',
],
];
}
/**
* Test add_debug_information.
*
* @dataProvider get_supported_templates_data
* @covers ::get_supported_templates()
*
* @param array $supported_content_types The supported content types, like 'post'.
* @param array $supported_templates The supported templates, like 'is_author'.
* @param string $theme_support The theme support, like 'standard'.
* @param string $expected The expected string of supported templates.
*/
public function test_get_supported_templates( $supported_content_types, $supported_templates, $theme_support, $expected ) {
remove_theme_support( 'amp' );
AMP_Options_Manager::update_option( Option::ALL_TEMPLATES_SUPPORTED, false );
AMP_Options_Manager::update_option( Option::SUPPORTED_TEMPLATES, $supported_templates );
AMP_Options_Manager::update_option( Option::THEME_SUPPORT, $theme_support );
AMP_Options_Manager::update_option( Option::SUPPORTED_POST_TYPES, $supported_content_types );
$this->assertEquals( $expected, $this->call_private_method( $this->instance, 'get_supported_templates' ) );
}
/**
* Gets the test data for test_get_serve_all_templates().
*
* @return array The test data.
*/
public function get_serve_all_templates_data() {
return [
'reader_mode_and_option_true' => [
'reader',
true,
'This option does not apply to Reader mode.',
],
'reader_mode_and_option_false' => [
'reader',
false,
'This option does not apply to Reader mode.',
],
'standard_mode_and_option_true' => [
'standard',
true,
'true',
],
'standard_mode_and_option_false' => [
'standard',
false,
'false',
],
'transitional_mode_and_option_true' => [
'transitional',
false,
'false',
],
'transitional_mode_and_option_false' => [
'transitional',
false,
'false',
],
];
}
/**
* Test get_serve_all_templates.
*
* @dataProvider get_serve_all_templates_data
* @covers ::get_serve_all_templates()
*
* @param string $theme_support The template mode, like 'standard'.
* @param bool $do_serve_all_templates Whether the option to serve all templates is true.
* @param string $expected The expected return value.
*/
public function test_get_serve_all_templates( $theme_support, $do_serve_all_templates, $expected ) {
AMP_Options_Manager::update_option( Option::THEME_SUPPORT, $theme_support );
AMP_Options_Manager::update_option( Option::ALL_TEMPLATES_SUPPORTED, $do_serve_all_templates );
$this->assertEquals( $expected, $this->call_private_method( $this->instance, 'get_serve_all_templates' ) );
}
/**
* Test add_extensions.
*
* @covers ::add_extensions()
*/
public function test_add_extensions() {
$this->assertEquals(
[
'json' => [
'extension' => 'json',
'function' => 'json_encode',
'required' => false,
],
'mbstring' => [
'extension' => 'mbstring',
'required' => false,
],
'zip' => [
'extension' => 'zip',
'required' => false,
],
],
$this->instance->add_extensions( [] )
);
// Test that the `intl` extension is added only when site URL is an IDN.
add_filter( 'site_url', [ self::class, 'get_idn' ], 10, 4 );
$extensions = $this->instance->add_extensions( [] );
$this->assertArrayHasKey( 'intl', $extensions );
$this->assertEquals(
[
'extension' => 'intl',
'function' => 'idn_to_utf8',
'required' => false,
],
$extensions['intl']
);
remove_filter( 'site_url', [ self::class, 'get_idn' ] );
}
/**
* Get an IDN for testing purposes.
*
* @return string
*/
public static function get_idn() {
return 'https://foo.xn--57h.bar.com';
}
/**
* Get an AMP query var for testing purposes.
*
* @return string
*/
public static function get_lite_query_var() {
return 'lite';
}
/**
* Assert that the expected is a subset of the actual superset.
*
* @param array $expected Subset.
* @param array $actual Superset.
*/
public function assertAssocArraySubset( $expected, $actual ) {
$this->assertEquals(
$expected,
wp_array_slice_assoc( $actual, array_keys( $expected ) )
);
}
/**
* @covers ::publisher_logo()
*/
public function test_publisher_logo() {
$output = $this->instance->publisher_logo();
$this->assertEquals( 'recommended', $output['status'] );
add_filter(
'amp_site_icon_url',
static function () {
return 'publisher-logo.png';
}
);
$output = $this->instance->publisher_logo();
$this->assertEquals( 'good', $output['status'] );
}
}