-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathtest-class-amp-theme-support.php
More file actions
1364 lines (1221 loc) · 47.7 KB
/
test-class-amp-theme-support.php
File metadata and controls
1364 lines (1221 loc) · 47.7 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Tests for Theme Support.
*
* @package AMP
* @since 0.7
*/
/**
* Tests for Theme Support.
*
* @covers AMP_Theme_Support
*/
class Test_AMP_Theme_Support extends WP_UnitTestCase {
/**
* The name of the tested class.
*
* @var string
*/
const TESTED_CLASS = 'AMP_Theme_Support';
/**
* Set up.
*/
public function setUp() {
parent::setUp();
AMP_Validation_Manager::reset_validation_results();
unset( $GLOBALS['current_screen'] );
remove_theme_support( 'amp' );
}
/**
* After a test method runs, reset any state in WordPress the test method might have changed.
*
* @global WP_Scripts $wp_scripts
*/
public function tearDown() {
global $wp_scripts;
$wp_scripts = null;
parent::tearDown();
AMP_Validation_Manager::reset_validation_results();
remove_theme_support( 'amp' );
remove_theme_support( 'custom-header' );
$_REQUEST = array(); // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification
$_SERVER['QUERY_STRING'] = '';
unset( $_SERVER['REQUEST_URI'] );
unset( $_SERVER['REQUEST_METHOD'] );
unset( $GLOBALS['content_width'] );
if ( isset( $GLOBALS['wp_customize'] ) ) {
$GLOBALS['wp_customize']->stop_previewing_theme();
}
AMP_Response_Headers::$headers_sent = array();
}
/**
* Test init.
*
* @covers AMP_Theme_Support::init()
* @covers AMP_Theme_Support::finish_init()
*/
public function test_init() {
$_REQUEST['__amp_source_origin'] = 'foo';
$_GET['__amp_source_origin'] = 'foo';
AMP_Theme_Support::init();
$this->assertNotEquals( 10, has_action( 'widgets_init', array( self::TESTED_CLASS, 'register_widgets' ) ) );
// Ensure that purge_amp_query_vars() didn't execute.
$this->assertTrue( isset( $_REQUEST['__amp_source_origin'] ) ); // WPCS: CSRF ok.
add_theme_support( 'amp' );
AMP_Theme_Support::init();
$this->assertEquals( 10, has_action( 'widgets_init', array( self::TESTED_CLASS, 'register_widgets' ) ) );
$this->assertEquals( PHP_INT_MAX, has_action( 'wp', array( self::TESTED_CLASS, 'finish_init' ) ) );
$this->assertFalse( isset( $_REQUEST['__amp_source_origin'] ) ); // WPCS: CSRF ok.
add_theme_support( 'amp', 'invalid_argumnet_type' );
$e = null;
try {
AMP_Theme_Support::init();
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertInstanceOf( 'PHPUnit_Framework_Error_Notice', $e );
$this->assertEquals( 'Expected AMP theme support arg to be array.', $e->getMessage() );
add_theme_support( 'amp', array(
'invalid_param_key' => array(),
) );
try {
AMP_Theme_Support::init();
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertEquals( 'Expected AMP theme support to only have template_dir and/or available_callback.', $e->getMessage() );
}
/**
* Test that amphtml link is added at the right time.
*
* @covers AMP_Theme_Support::finish_init()
*/
public function test_amphtml_link() {
$post_id = $this->factory()->post->create( array( 'post_title' => 'Test' ) );
add_theme_support( 'amp', array(
'template_dir' => '...',
'available_callback' => function() {
return is_singular();
},
) );
// Test paired mode singular.
remove_action( 'wp_head', 'amp_add_amphtml_link' );
$this->go_to( get_permalink( $post_id ) );
AMP_Theme_Support::finish_init();
$this->assertEquals( 10, has_action( 'wp_head', 'amp_add_amphtml_link' ) );
// Test paired mode homepage.
remove_action( 'wp_head', 'amp_add_amphtml_link' );
$this->go_to( home_url() );
AMP_Theme_Support::finish_init();
$this->assertEquals( 10, has_action( 'wp_head', 'amp_add_amphtml_link' ) );
// Test canonical.
remove_action( 'wp_head', 'amp_add_amphtml_link' );
remove_theme_support( 'amp' );
add_theme_support( 'amp' );
$this->go_to( get_permalink( $post_id ) );
AMP_Theme_Support::finish_init();
$this->assertFalse( has_action( 'wp_head', 'amp_add_amphtml_link' ) );
}
/**
* Test ensure_proper_amp_location for canonical.
*
* @covers AMP_Theme_Support::ensure_proper_amp_location()
*/
public function test_ensure_proper_amp_location_canonical() {
add_theme_support( 'amp' );
$e = null;
// Already canonical.
$_SERVER['REQUEST_URI'] = '/foo/bar/';
$this->assertFalse( AMP_Theme_Support::ensure_proper_amp_location( false ) );
// URL query param.
$_GET[ amp_get_slug() ] = '';
$_SERVER['REQUEST_URI'] = add_query_arg( amp_get_slug(), '', '/foo/bar' );
try {
$this->assertTrue( AMP_Theme_Support::ensure_proper_amp_location( false ) );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertTrue( isset( $e ) ); // wp_safe_redirect() modifies the headers, and causes an error.
$this->assertContains( 'headers already sent', $e->getMessage() );
$e = null;
// Endpoint.
unset( $_GET[ amp_get_slug() ] );
set_query_var( amp_get_slug(), '' );
$_SERVER['REQUEST_URI'] = '/2016/01/24/foo/amp/';
try {
$this->assertTrue( AMP_Theme_Support::ensure_proper_amp_location( false ) );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertContains( 'headers already sent', $e->getMessage() );
$e = null;
}
/**
* Test ensure_proper_amp_location for paired.
*
* @covers AMP_Theme_Support::ensure_proper_amp_location()
*/
public function test_ensure_proper_amp_location_paired() {
add_theme_support( 'amp', array(
'template_dir' => './',
) );
$e = null;
// URL query param, no redirection.
$_GET[ amp_get_slug() ] = '';
$_SERVER['REQUEST_URI'] = add_query_arg( amp_get_slug(), '', '/foo/bar' );
$this->assertFalse( AMP_Theme_Support::ensure_proper_amp_location( false ) );
// Endpoint, redirect.
unset( $_GET[ amp_get_slug() ] );
set_query_var( amp_get_slug(), '' );
$_SERVER['REQUEST_URI'] = '/2016/01/24/foo/amp/';
try {
$this->assertTrue( AMP_Theme_Support::ensure_proper_amp_location( false ) );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertContains( 'headers already sent', $e->getMessage() );
}
/**
* Test redirect_ampless_url.
*
* @covers AMP_Theme_Support::redirect_ampless_url()
*/
public function test_redirect_ampless_url() {
$e = null;
// Try AMP URL param.
$_SERVER['REQUEST_URI'] = add_query_arg( amp_get_slug(), '', '/foo/bar' );
try {
$this->assertTrue( AMP_Theme_Support::redirect_ampless_url() );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertTrue( isset( $e ) );
$this->assertContains( 'headers already sent', $e->getMessage() );
$e = null;
// Try AMP URL endpoint.
$_SERVER['REQUEST_URI'] = '/2016/01/24/foo/amp/';
try {
$this->assertTrue( AMP_Theme_Support::redirect_ampless_url() );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertTrue( isset( $e ) ); // wp_safe_redirect() modifies the headers, and causes an error.
$this->assertContains( 'headers already sent', $e->getMessage() );
$e = null;
// Make sure that if the URL doesn't have AMP that there should be no redirect.
$_SERVER['REQUEST_URI'] = '/foo/bar';
$this->assertFalse( AMP_Theme_Support::redirect_ampless_url() );
}
/**
* Test is_paired_available.
*
* @covers AMP_Theme_Support::is_paired_available()
*/
public function test_is_paired_available() {
// Establish initial state.
$post_id = $this->factory()->post->create( array( 'post_title' => 'Test' ) );
remove_theme_support( 'amp' );
query_posts( array( 'p' => $post_id ) ); // phpcs:ignore
$this->assertTrue( is_singular() );
// Paired support is not available if theme support is not present or canonical.
$this->assertFalse( AMP_Theme_Support::is_paired_available() );
add_theme_support( 'amp' );
$this->assertFalse( AMP_Theme_Support::is_paired_available() );
// Paired mode is available once template_dir is supplied.
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
) );
$this->assertTrue( AMP_Theme_Support::is_paired_available() );
// Paired mode not available when post does not support AMP.
add_filter( 'amp_skip_post', '__return_true' );
$this->assertFalse( AMP_Theme_Support::is_paired_available() );
$this->assertTrue( is_singular() );
query_posts( array( 's' => 'test' ) ); // phpcs:ignore
$this->assertTrue( is_search() );
$this->assertTrue( AMP_Theme_Support::is_paired_available() );
remove_filter( 'amp_skip_post', '__return_true' );
// Check that available_callback works.
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
'available_callback' => 'is_singular',
) );
query_posts( array( 'p' => $post_id ) ); // phpcs:ignore
$this->assertTrue( is_singular() );
$this->assertTrue( AMP_Theme_Support::is_paired_available() );
query_posts( array( 's' => $post_id ) ); // phpcs:ignore
$this->assertTrue( is_search() );
$this->assertFalse( AMP_Theme_Support::is_paired_available() );
}
/**
* Test is_customize_preview_iframe.
*
* @covers AMP_Theme_Support::is_customize_preview_iframe()
*/
public function test_is_customize_preview_iframe() {
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
$this->assertFalse( AMP_Theme_Support::is_customize_preview_iframe() );
$GLOBALS['wp_customize'] = new WP_Customize_Manager( array(
'messenger_channel' => 'baz',
) );
$this->assertFalse( AMP_Theme_Support::is_customize_preview_iframe() );
$GLOBALS['wp_customize']->start_previewing_theme();
$this->assertTrue( AMP_Theme_Support::is_customize_preview_iframe() );
}
/**
* Test register_paired_hooks.
*
* @covers AMP_Theme_Support::register_paired_hooks()
*/
public function test_register_paired_hooks() {
$template_types = array(
'paged',
'index',
'404',
'archive',
'author',
'category',
);
AMP_Theme_Support::register_paired_hooks();
foreach ( $template_types as $template_type ) {
$this->assertEquals( 10, has_filter( "{$template_type}_template_hierarchy", array( self::TESTED_CLASS, 'filter_paired_template_hierarchy' ) ) );
}
$this->assertEquals( 100, has_filter( 'template_include', array( self::TESTED_CLASS, 'filter_paired_template_include' ) ) );
}
/**
* Test validate_non_amp_theme.
*
* @global WP_Widget_Factory $wp_widget_factory
* @global WP_Scripts $wp_scripts
* @covers AMP_Theme_Support::prepare_response()
*/
public function test_validate_non_amp_theme() {
add_filter( 'amp_validation_error_sanitized', '__return_true' );
add_theme_support( 'amp' );
AMP_Theme_Support::init();
AMP_Theme_Support::finish_init();
ob_start();
?>
<!DOCTYPE html>
<html lang="en-US" class="no-js">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<?php wp_head(); ?>
</head>
<body>
<?php wp_print_scripts( 'amp-mathml' ); ?>
</body>
</html>
<?php
$original_html = trim( ob_get_clean() );
$sanitized_html = AMP_Theme_Support::prepare_response( $original_html );
// Invalid viewport meta tag is not present.
$this->assertNotContains( '<meta name="viewport" content="width=device-width">', $sanitized_html );
// Correct viewport meta tag was added.
$this->assertContains( '<meta name="viewport" content="width=device-width,minimum-scale=1">', $sanitized_html );
// MathML script was added.
$this->assertContains( '<script type="text/javascript" src="https://cdn.ampproject.org/v0/amp-mathml-latest.js" async custom-element="amp-mathml"></script>', $sanitized_html ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
}
/**
* Test add_hooks.
*
* @covers AMP_Theme_Support::add_hooks()
*/
public function test_add_hooks() {
AMP_Theme_Support::add_hooks();
$this->assertFalse( has_action( 'wp_head', 'wp_post_preview_js' ) );
$this->assertFalse( has_action( 'wp_head', 'print_emoji_detection_script' ) );
$this->assertFalse( has_action( 'wp_print_styles', 'print_emoji_styles' ) );
$this->assertFalse( has_action( 'wp_head', 'wp_oembed_add_host_js' ) );
$this->assertEquals( 0, has_action( 'wp_print_styles', array( self::TESTED_CLASS, 'print_amp_styles' ) ) );
$this->assertEquals( 20, has_action( 'wp_head', 'amp_add_generator_metadata' ) );
$this->assertEquals( 20, has_action( 'wp_head', 'amp_add_generator_metadata' ) );
$this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( self::TESTED_CLASS, 'enqueue_assets' ) ) );
$this->assertEquals( 1000, has_action( 'wp_enqueue_scripts', array( self::TESTED_CLASS, 'dequeue_customize_preview_scripts' ) ) );
$this->assertEquals( 10, has_filter( 'customize_partial_render', array( self::TESTED_CLASS, 'filter_customize_partial_render' ) ) );
$this->assertEquals( 10, has_action( 'wp_footer', 'amp_print_analytics' ) );
$this->assertEquals( 100, has_filter( 'show_admin_bar', '__return_false' ) );
$priority = defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : ~PHP_INT_MAX; // phpcs:ignore PHPCompatibility.PHP.NewConstants.php_int_minFound
$this->assertEquals( $priority, has_action( 'template_redirect', array( self::TESTED_CLASS, 'start_output_buffering' ) ) );
$this->assertEquals( PHP_INT_MAX, has_filter( 'wp_list_comments_args', array( self::TESTED_CLASS, 'set_comments_walker' ) ) );
$this->assertEquals( 10, has_filter( 'comment_form_defaults', array( self::TESTED_CLASS, 'filter_comment_form_defaults' ) ) );
$this->assertEquals( 10, has_filter( 'comment_reply_link', array( self::TESTED_CLASS, 'filter_comment_reply_link' ) ) );
$this->assertEquals( 10, has_filter( 'cancel_comment_reply_link', array( self::TESTED_CLASS, 'filter_cancel_comment_reply_link' ) ) );
$this->assertEquals( 100, has_action( 'comment_form', array( self::TESTED_CLASS, 'amend_comment_form' ) ) );
$this->assertFalse( has_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' ) );
$this->assertEquals( PHP_INT_MAX, has_filter( 'get_header_image_tag', array( self::TESTED_CLASS, 'amend_header_image_with_video_header' ) ) );
}
/**
* Test purge_amp_query_vars.
*
* @covers AMP_Theme_Support::purge_amp_query_vars()
*/
public function test_purge_amp_query_vars() {
// phpcs:disable WordPress.CSRF.NonceVerification.NoNonceVerification
$bad_query_vars = array(
'amp_latest_update_time' => '1517199956',
'amp_last_check_time' => '1517599126',
'__amp_source_origin' => home_url(),
);
$ok_query_vars = array(
'bar' => 'baz',
);
$all_query_vars = array_merge( $bad_query_vars, $ok_query_vars );
$_SERVER['QUERY_STRING'] = build_query( $all_query_vars );
remove_action( 'wp', 'amp_maybe_add_actions' );
$this->go_to( add_query_arg( $all_query_vars, home_url( '/foo/' ) ) );
$_REQUEST = $_GET;
foreach ( $all_query_vars as $key => $value ) {
$this->assertArrayHasKey( $key, $_GET );
$this->assertArrayHasKey( $key, $_REQUEST );
$this->assertContains( "$key=$value", $_SERVER['QUERY_STRING'] );
$this->assertContains( "$key=$value", $_SERVER['REQUEST_URI'] );
}
AMP_Theme_Support::$purged_amp_query_vars = array();
AMP_Theme_Support::purge_amp_query_vars();
$this->assertEqualSets( AMP_Theme_Support::$purged_amp_query_vars, $bad_query_vars );
foreach ( $bad_query_vars as $key => $value ) {
$this->assertArrayNotHasKey( $key, $_GET );
$this->assertArrayNotHasKey( $key, $_REQUEST );
$this->assertNotContains( "$key=$value", $_SERVER['QUERY_STRING'] );
$this->assertNotContains( "$key=$value", $_SERVER['REQUEST_URI'] );
}
foreach ( $ok_query_vars as $key => $value ) {
$this->assertArrayHasKey( $key, $_GET );
$this->assertArrayHasKey( $key, $_REQUEST );
$this->assertContains( "$key=$value", $_SERVER['QUERY_STRING'] );
$this->assertContains( "$key=$value", $_SERVER['REQUEST_URI'] );
}
// phpcs:enable WordPress.CSRF.NonceVerification.NoNonceVerification
}
/**
* Test handle_xhr_request().
*
* @covers AMP_Theme_Support::handle_xhr_request()
*/
public function test_handle_xhr_request() {
AMP_Theme_Support::purge_amp_query_vars();
AMP_Theme_Support::handle_xhr_request();
$this->assertEmpty( AMP_Response_Headers::$headers_sent );
$_GET['_wp_amp_action_xhr_converted'] = '1';
// Try bad source origin.
$_GET['__amp_source_origin'] = 'http://evil.example.com/';
$_SERVER['REQUEST_METHOD'] = 'POST';
AMP_Theme_Support::purge_amp_query_vars();
AMP_Theme_Support::handle_xhr_request();
$this->assertEmpty( AMP_Response_Headers::$headers_sent );
// Try home source origin.
$_GET['__amp_source_origin'] = home_url();
$_SERVER['REQUEST_METHOD'] = 'POST';
AMP_Theme_Support::purge_amp_query_vars();
AMP_Theme_Support::handle_xhr_request();
$this->assertCount( 1, AMP_Response_Headers::$headers_sent );
$this->assertEquals(
array(
'name' => 'AMP-Access-Control-Allow-Source-Origin',
'value' => home_url(),
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent[0]
);
$this->assertEquals( PHP_INT_MAX, has_filter( 'wp_redirect', array( 'AMP_Theme_Support', 'intercept_post_request_redirect' ) ) );
$this->assertEquals( PHP_INT_MAX, has_filter( 'comment_post_redirect', array( 'AMP_Theme_Support', 'filter_comment_post_redirect' ) ) );
$this->assertEquals(
array( 'AMP_Theme_Support', 'handle_wp_die' ),
apply_filters( 'wp_die_handler', '__return_true' )
);
}
/**
* Test filter_comment_post_redirect().
*
* @covers AMP_Theme_Support::filter_comment_post_redirect()
*/
public function test_filter_comment_post_redirect() {
add_filter( 'wp_doing_ajax', '__return_true' );
add_filter( 'wp_die_ajax_handler', function() {
return '__return_null';
} );
add_theme_support( 'amp' );
$post = $this->factory()->post->create_and_get();
$comment = $this->factory()->comment->create_and_get( array(
'comment_post_ID' => $post->ID,
) );
$url = get_comment_link( $comment );
// Test without comments_live_list.
$filtered_url = AMP_Theme_Support::filter_comment_post_redirect( $url, $comment );
$this->assertNotEquals(
strtok( $url, '#' ),
strtok( $filtered_url, '#' )
);
// Test with comments_live_list.
add_theme_support( 'amp', array(
'comments_live_list' => true,
) );
add_filter( 'amp_comment_posted_message', function( $message, WP_Comment $filter_comment ) {
return sprintf( '(comment=%d,approved=%d)', $filter_comment->comment_ID, $filter_comment->comment_approved );
}, 10, 2 );
// Test approved comment.
$comment->comment_approved = '1';
ob_start();
AMP_Theme_Support::filter_comment_post_redirect( $url, $comment );
$response = json_decode( ob_get_clean(), true );
$this->assertArrayHasKey( 'message', $response );
$this->assertEquals(
sprintf( '(comment=%d,approved=1)', $comment->comment_ID ),
$response['message']
);
// Test moderated comment.
$comment->comment_approved = '0';
ob_start();
AMP_Theme_Support::filter_comment_post_redirect( $url, $comment );
$response = json_decode( ob_get_clean(), true );
$this->assertArrayHasKey( 'message', $response );
$this->assertEquals(
sprintf( '(comment=%d,approved=0)', $comment->comment_ID ),
$response['message']
);
}
/**
* Test handle_wp_die().
*
* @covers AMP_Theme_Support::handle_wp_die()
*/
public function test_handle_wp_die() {
add_filter( 'wp_doing_ajax', '__return_true' );
add_filter( 'wp_die_ajax_handler', function() {
return '__return_null';
} );
ob_start();
AMP_Theme_Support::handle_wp_die( 'string' );
$this->assertEquals( '{"error":"string"}', ob_get_clean() );
ob_start();
$error = new WP_Error( 'code', 'The Message' );
AMP_Theme_Support::handle_wp_die( $error );
$this->assertEquals( '{"error":"The Message"}', ob_get_clean() );
}
/**
* Test intercept_post_request_redirect().
*
* @covers AMP_Theme_Support::intercept_post_request_redirect()
*/
public function test_intercept_post_request_redirect() {
add_theme_support( 'amp' );
$url = home_url( '', 'https' ) . ':443/?test=true#test';
add_filter( 'wp_doing_ajax', '__return_true' );
add_filter( 'wp_die_ajax_handler', function () {
return '__return_false';
} );
// Test redirecting to full URL with HTTPS protocol.
AMP_Response_Headers::$headers_sent = array();
ob_start();
AMP_Theme_Support::intercept_post_request_redirect( $url );
$this->assertEquals( '{"success":true}', ob_get_clean() );
$this->assertContains(
array(
'name' => 'AMP-Redirect-To',
'value' => $url,
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent
);
$this->assertContains(
array(
'name' => 'Access-Control-Expose-Headers',
'value' => 'AMP-Redirect-To',
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent
);
// Test redirecting to non-HTTPS URL.
AMP_Response_Headers::$headers_sent = array();
ob_start();
$url = home_url( '/', 'http' );
AMP_Theme_Support::intercept_post_request_redirect( $url );
$this->assertEquals( '{"success":true}', ob_get_clean() );
$this->assertContains(
array(
'name' => 'AMP-Redirect-To',
'value' => preg_replace( '#^\w+:#', '', $url ),
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent
);
$this->assertContains(
array(
'name' => 'Access-Control-Expose-Headers',
'value' => 'AMP-Redirect-To',
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent
);
// Test redirecting to host-less location.
AMP_Response_Headers::$headers_sent = array();
ob_start();
AMP_Theme_Support::intercept_post_request_redirect( '/new-location/' );
$this->assertEquals( '{"success":true}', ob_get_clean() );
$this->assertContains(
array(
'name' => 'AMP-Redirect-To',
'value' => set_url_scheme( home_url( '/new-location/' ), 'https' ),
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent
);
// Test redirecting to scheme-less location.
AMP_Response_Headers::$headers_sent = array();
ob_start();
$url = home_url( '/new-location/' );
AMP_Theme_Support::intercept_post_request_redirect( substr( $url, strpos( $url, ':' ) + 1 ) );
$this->assertEquals( '{"success":true}', ob_get_clean() );
$this->assertContains(
array(
'name' => 'AMP-Redirect-To',
'value' => set_url_scheme( home_url( '/new-location/' ), 'https' ),
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent
);
// Test redirecting to empty location.
AMP_Response_Headers::$headers_sent = array();
ob_start();
AMP_Theme_Support::intercept_post_request_redirect( '' );
$this->assertEquals( '{"success":true}', ob_get_clean() );
$this->assertContains(
array(
'name' => 'AMP-Redirect-To',
'value' => set_url_scheme( home_url(), 'https' ),
'replace' => true,
'status_code' => null,
),
AMP_Response_Headers::$headers_sent
);
}
/**
* Test register_widgets().
*
* @covers AMP_Theme_Support::register_widgets()
* @global WP_Widget_Factory $wp_widget_factory
*/
public function test_register_widgets() {
global $wp_widget_factory;
remove_all_actions( 'widgets_init' );
$wp_widget_factory->widgets = array();
wp_widgets_init();
AMP_Theme_Support::register_widgets();
$this->assertArrayNotHasKey( 'WP_Widget_Categories', $wp_widget_factory->widgets );
$this->assertArrayHasKey( 'AMP_Widget_Categories', $wp_widget_factory->widgets );
}
/**
* Test register_content_embed_handlers.
*
* @covers AMP_Theme_Support::register_content_embed_handlers()
* @global int $content_width
*/
public function test_register_content_embed_handlers() {
global $content_width;
$content_width = 1234;
$embed_handlers = AMP_Theme_Support::register_content_embed_handlers();
foreach ( $embed_handlers as $embed_handler ) {
$this->assertTrue( is_subclass_of( $embed_handler, 'AMP_Base_Embed_Handler' ) );
$reflection = new ReflectionObject( $embed_handler );
$args = $reflection->getProperty( 'args' );
$args->setAccessible( true );
$property = $args->getValue( $embed_handler );
$this->assertEquals( $content_width, $property['content_max_width'] );
}
}
/**
* Test set_comments_walker.
*
* @covers AMP_Theme_Support::set_comments_walker()
*/
public function test_set_comments_walker() {
$args = AMP_Theme_Support::set_comments_walker( array(
'walker' => null,
) );
$this->assertInstanceOf( 'AMP_Comment_Walker', $args['walker'] );
}
/**
* Test amend_comment_form().
*
* @covers AMP_Theme_Support::amend_comment_form()
*/
public function test_amend_comment_form() {
$post_id = $this->factory()->post->create();
$this->go_to( get_permalink( $post_id ) );
$this->assertTrue( is_singular() );
// Test native AMP.
add_theme_support( 'amp' );
$this->assertTrue( amp_is_canonical() );
ob_start();
AMP_Theme_Support::amend_comment_form();
$output = ob_get_clean();
$this->assertNotContains( '<input type="hidden" name="redirect_to"', $output );
$this->assertContains( '<div submit-success>', $output );
$this->assertContains( '<div submit-error>', $output );
// Test paired AMP.
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
) );
$this->assertFalse( amp_is_canonical() );
ob_start();
AMP_Theme_Support::amend_comment_form();
$output = ob_get_clean();
$this->assertContains( '<input type="hidden" name="redirect_to"', $output );
$this->assertContains( '<div submit-success>', $output );
$this->assertContains( '<div submit-error>', $output );
}
/**
* Test filter_paired_template_hierarchy.
*
* @covers AMP_Theme_Support::filter_paired_template_hierarchy()
*/
public function test_filter_paired_template_hierarchy() {
$template_dir = 'amp-templates';
add_theme_support( 'amp', array(
'template_dir' => $template_dir,
) );
$templates = array(
'single-post-example.php',
'single-post.php',
'single.php',
);
$filtered_templates = AMP_Theme_Support::filter_paired_template_hierarchy( $templates );
foreach ( $filtered_templates as $key => $filtered_template ) {
$this->assertEquals( $template_dir . '/' . $templates[ $key ], $filtered_template );
}
}
/**
* Test filter_paired_template_include.
*
* @covers AMP_Theme_Support::filter_paired_template_include()
*/
public function test_filter_paired_template_include() {
$template_dir = 'amp-templates';
$template = 'single.php';
add_theme_support( 'amp', array(
'template_dir' => $template_dir,
) );
$this->assertEquals( $template, AMP_Theme_Support::filter_paired_template_include( $template ) );
remove_theme_support( 'amp' );
try {
AMP_Theme_Support::filter_paired_template_include( $template );
} catch ( Exception $exception ) {
$e = $exception;
}
$this->assertTrue( isset( $e ) );
}
/**
* Test get_current_canonical_url.
*
* @covers AMP_Theme_Support::get_current_canonical_url()
*/
public function test_get_current_canonical_url() {
global $post, $wp;
$home_url = home_url( '/' );
$this->assertEquals( $home_url, AMP_Theme_Support::get_current_canonical_url() );
$added_query_vars = array(
'foo' => 'bar',
);
$wp->query_vars = $added_query_vars;
$this->assertEquals( add_query_arg( $added_query_vars, $home_url ), AMP_Theme_Support::get_current_canonical_url() );
$post = $this->factory()->post->create_and_get(); // WPCS: global override ok.
$this->go_to( get_permalink( $post ) );
$this->assertEquals( wp_get_canonical_url(), AMP_Theme_Support::get_current_canonical_url() );
}
/**
* Test get_comment_form_state_id.
*
* @covers AMP_Theme_Support::get_comment_form_state_id()
*/
public function test_get_comment_form_state_id() {
$post_id = 54;
$this->assertEquals( 'commentform_post_' . $post_id, AMP_Theme_Support::get_comment_form_state_id( $post_id ) );
$post_id = 91542;
$this->assertEquals( 'commentform_post_' . $post_id, AMP_Theme_Support::get_comment_form_state_id( $post_id ) );
}
/**
* Test filter_comment_form_defaults.
*
* @covers AMP_Theme_Support::filter_comment_form_defaults()
*/
public function test_filter_comment_form_defaults() {
global $post;
$post = $this->factory()->post->create_and_get(); // WPCS: global override ok.
$defaults = AMP_Theme_Support::filter_comment_form_defaults( array(
'title_reply_to' => 'Reply To',
'title_reply' => 'Reply',
'cancel_reply_before' => '',
'title_reply_before' => '',
) );
$this->assertContains( AMP_Theme_Support::get_comment_form_state_id( get_the_ID() ), $defaults['title_reply_before'] );
$this->assertContains( 'replyToName ?', $defaults['title_reply_before'] );
$this->assertContains( '</span>', $defaults['cancel_reply_before'] );
}
/**
* Test filter_comment_reply_link.
*
* @covers AMP_Theme_Support::filter_comment_reply_link()
*/
public function test_filter_comment_reply_link() {
global $post;
$post = $this->factory()->post->create_and_get(); // WPCS: global override ok.
$comment = $this->factory()->comment->create_and_get();
$link = sprintf( '<a href="%s">', get_comment_link( $comment ) );
$respond_id = '5234';
$reply_text = 'Reply';
$reply_to_text = 'Reply to';
$before = '<div class="reply">';
$after = '</div>';
$args = compact( 'respond_id', 'reply_text', 'reply_to_text', 'before', 'after' );
$comment = $this->factory()->comment->create_and_get();
update_option( 'comment_registration', true );
$filtered_link = AMP_Theme_Support::filter_comment_reply_link( $link, $args, $comment );
$this->assertEquals( $before . $link . $after, $filtered_link );
update_option( 'comment_registration', false );
$filtered_link = AMP_Theme_Support::filter_comment_reply_link( $link, $args, $comment );
$this->assertStringStartsWith( $before, $filtered_link );
$this->assertStringEndsWith( $after, $filtered_link );
$this->assertContains( AMP_Theme_Support::get_comment_form_state_id( get_the_ID() ), $filtered_link );
$this->assertContains( $comment->comment_author, $filtered_link );
$this->assertContains( $comment->comment_ID, $filtered_link );
$this->assertContains( 'tap:AMP.setState', $filtered_link );
$this->assertContains( $reply_text, $filtered_link );
$this->assertContains( $reply_to_text, $filtered_link );
$this->assertContains( $respond_id, $filtered_link );
}
/**
* Test filter_cancel_comment_reply_link.
*
* @covers AMP_Theme_Support::filter_cancel_comment_reply_link()
*/
public function test_filter_cancel_comment_reply_link() {
global $post;
$post = $this->factory()->post->create_and_get(); // WPCS: global override ok.
$url = get_permalink( $post );
$_SERVER['REQUEST_URI'] = $url;
$this->factory()->comment->create_and_get();
$formatted_link = get_cancel_comment_reply_link();
$link = remove_query_arg( 'replytocom' );
$text = 'Cancel your reply';
$filtered_link = AMP_Theme_Support::filter_cancel_comment_reply_link( $formatted_link, $link, $text );
$this->assertContains( $url, $filtered_link );
$this->assertContains( $text, $filtered_link );
$this->assertContains( '<a id="cancel-comment-reply-link"', $filtered_link );
$this->assertContains( '.values.comment_parent ==', $filtered_link );
$this->assertContains( 'tap:AMP.setState(', $filtered_link );
$filtered_link_no_text_passed = AMP_Theme_Support::filter_cancel_comment_reply_link( $formatted_link, $link, '' );
$this->assertContains( 'Click here to cancel reply.', $filtered_link_no_text_passed );
}
/**
* Test print_amp_styles.
*
* @covers AMP_Theme_Support::print_amp_styles()
*/
public function test_print_amp_styles() {
ob_start();
AMP_Theme_Support::print_amp_styles();
$output = ob_get_clean();
$this->assertContains( amp_get_boilerplate_code(), $output );
$this->assertContains( '<style amp-custom></style>', $output );
}
/**
* Test ensure_required_markup().
*
* @dataProvider get_script_data
* @covers AMP_Theme_Support::ensure_required_markup()
* @param string $script The value of the script.
* @param boolean $expected The expected result.
*/
public function test_ensure_required_markup( $script, $expected ) {
$page = '<html><head><script type="application/ld+json">%s</script></head><body>Test</body></html>';
$dom = new DOMDocument();
$dom->loadHTML( sprintf( $page, $script ) );
AMP_Theme_Support::ensure_required_markup( $dom );
$this->assertEquals( $expected, substr_count( $dom->saveHTML(), 'schema.org' ) );
}
/**
* Test dequeue_customize_preview_scripts.
*
* @covers AMP_Theme_Support::dequeue_customize_preview_scripts()
*/
public function test_dequeue_customize_preview_scripts() {
// Ensure AMP_Theme_Support::is_customize_preview_iframe() is true.
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
$GLOBALS['wp_customize'] = new WP_Customize_Manager( array(
'messenger_channel' => 'baz',
) );
$GLOBALS['wp_customize']->start_previewing_theme();
$customize_preview = 'customize-preview';
$preview_style = 'example-preview-style';
wp_enqueue_style( $preview_style, home_url( '/' ), array( $customize_preview ) );
AMP_Theme_Support::dequeue_customize_preview_scripts();
$this->assertTrue( wp_style_is( $preview_style ) );
$this->assertTrue( wp_style_is( $customize_preview ) );
wp_enqueue_style( $preview_style, home_url( '/' ), array( $customize_preview ) );
wp_enqueue_style( $customize_preview );
// Ensure AMP_Theme_Support::is_customize_preview_iframe() is false.
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
AMP_Theme_Support::dequeue_customize_preview_scripts();
$this->assertFalse( wp_style_is( $preview_style ) );
$this->assertFalse( wp_style_is( $customize_preview ) );
}
/**
* Test start_output_buffering.
*
* @covers AMP_Theme_Support::start_output_buffering()
* @covers AMP_Theme_Support::is_output_buffering()
* @covers AMP_Theme_Support::finish_output_buffering()
*/
public function test_start_output_buffering() {
if ( ! function_exists( 'newrelic_disable_autorum ' ) ) {
/**
* Define newrelic_disable_autorum to allow passing line.
*/
function newrelic_disable_autorum() {
return true;
}
}
add_theme_support( 'amp' );
AMP_Theme_Support::init();
AMP_Theme_Support::finish_init();
$initial_ob_level = ob_get_level();
AMP_Theme_Support::start_output_buffering();
$this->assertEquals( $initial_ob_level + 1, ob_get_level() );
ob_end_flush();
$this->assertEquals( $initial_ob_level, ob_get_level() );
}
/**
* Test finish_output_buffering.
*
* @covers AMP_Theme_Support::finish_output_buffering()
* @covers AMP_Theme_Support::is_output_buffering()
*/
public function test_finish_output_buffering() {
add_filter( 'amp_validation_error_sanitized', '__return_true' );
add_theme_support( 'amp' );