/
test-class-amp-validation-manager.php
2442 lines (2216 loc) · 85 KB
/
test-class-amp-validation-manager.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
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 AMP_Validation_Manager class.
*
* @package AMP
*/
// phpcs:disable Generic.Formatting.MultipleStatementAlignment.NotSameWarning
use AmpProject\AmpWP\Tests\AssertContainsCompatibility;
use AmpProject\AmpWP\Tests\HandleValidation;
use AmpProject\AmpWP\Tests\PrivateAccess;
use AmpProject\Dom\Document;
/**
* Tests for AMP_Validation_Manager class.
*
* @covers AMP_Validation_Manager
* @since 0.7
*/
class Test_AMP_Validation_Manager extends WP_UnitTestCase {
use AssertContainsCompatibility;
use HandleValidation;
use PrivateAccess;
/**
* The name of the tested class.
*
* @var string
*/
const TESTED_CLASS = 'AMP_Validation_Manager';
/**
* An instance of DOMElement to test.
*
* @var DOMElement
*/
public $node;
/**
* A tag that the sanitizer should strip.
*
* @var string
*/
public $disallowed_tag = '<script async src="https://example.com/script"></script>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
/**
* The name of a tag that the sanitizer should strip.
*
* @var string
*/
public $disallowed_tag_name = 'script';
/**
* The name of an attribute that the sanitizer should strip.
*
* @var string
*/
public $disallowed_attribute_name = 'onload';
/**
* A mock plugin name that outputs invalid markup.
*
* @var string
*/
public $plugin_name = 'foo-bar';
/**
* A valid image that sanitizers should not alter.
*
* @var string
*/
public $valid_amp_img = '<amp-img id="img-123" media="(min-width: 600x)" src="/assets/example.jpg" width="200" height="500" layout="responsive"></amp-img>';
/**
* The name of the tag to test.
*
* @var string
*/
const TAG_NAME = 'img';
/**
* Backed up $wp_registered_widgets.
*
* @var array
*/
protected $original_wp_registered_widgets;
/**
* Setup.
*
* @inheritdoc
* @global $wp_registered_widgets
*/
public function setUp() {
unset( $GLOBALS['wp_scripts'], $GLOBALS['wp_styles'] );
parent::setUp();
$dom_document = new Document( '1.0', 'utf-8' );
$this->node = $dom_document->createElement( self::TAG_NAME );
$dom_document->appendChild( $this->node );
AMP_Validation_Manager::reset_validation_results();
$this->original_wp_registered_widgets = $GLOBALS['wp_registered_widgets'];
if ( class_exists( 'WP_Block_Type_Registry' ) ) {
foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $block ) {
if ( 'amp/' === substr( $block->name, 0, 4 ) ) {
WP_Block_Type_Registry::get_instance()->unregister( $block->name );
}
}
}
delete_option( AMP_Options_Manager::OPTION_NAME ); // Make sure default reader mode option does not override theme support being added.
}
/**
* After a test method runs, reset any state in WordPress the test method might have changed.
*/
public function tearDown() {
$GLOBALS['wp_registered_widgets'] = $this->original_wp_registered_widgets;
remove_theme_support( AMP_Theme_Support::SLUG );
AMP_Theme_Support::read_theme_support();
AMP_Validation_Manager::$validation_error_status_overrides = [];
$_REQUEST = [];
unset( $GLOBALS['current_screen'] );
AMP_Validation_Manager::$is_validate_request = false;
AMP_Validation_Manager::$hook_source_stack = [];
AMP_Validation_Manager::$validation_results = [];
AMP_Validation_Manager::reset_validation_results();
parent::tearDown();
}
/**
* Test init.
*
* @covers AMP_Validation_Manager::init()
*/
public function test_init() {
add_theme_support( AMP_Theme_Support::SLUG );
AMP_Validation_Manager::init();
$this->assertTrue( post_type_exists( AMP_Validated_URL_Post_Type::POST_TYPE_SLUG ) );
$this->assertTrue( taxonomy_exists( AMP_Validation_Error_Taxonomy::TAXONOMY_SLUG ) );
$this->assertEquals( 10, has_action( 'save_post', self::TESTED_CLASS . '::handle_save_post_prompting_validation' ) );
$this->assertEquals( 10, has_action( 'enqueue_block_editor_assets', self::TESTED_CLASS . '::enqueue_block_validation' ) );
$this->assertEquals( 10, has_action( 'edit_form_top', self::TESTED_CLASS . '::print_edit_form_validation_status' ) );
$this->assertEquals( 10, has_action( 'all_admin_notices', self::TESTED_CLASS . '::print_plugin_notice' ) );
$this->assertEquals( 10, has_action( 'rest_api_init', self::TESTED_CLASS . '::add_rest_api_fields' ) );
$this->assertContains( AMP_Validation_Manager::VALIDATION_ERRORS_QUERY_VAR, wp_removable_query_args() );
$this->assertEquals( 101, has_action( 'admin_bar_menu', [ self::TESTED_CLASS, 'add_admin_bar_menu_items' ] ) );
$this->assertFalse( has_action( 'wp', [ self::TESTED_CLASS, 'wrap_widget_callbacks' ] ) );
}
/**
* Test init_validate_request without error.
*
* @covers AMP_Validation_Manager::init_validate_request()
*/
public function test_init_validate_request_without_error() {
$this->assertFalse( AMP_Validation_Manager::should_validate_response() );
AMP_Validation_Manager::init_validate_request();
$this->assertFalse( AMP_Validation_Manager::$is_validate_request );
$_GET[ AMP_Validation_Manager::VALIDATE_QUERY_VAR ] = wp_slash( AMP_Validation_Manager::get_amp_validate_nonce() ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$this->assertTrue( AMP_Validation_Manager::should_validate_response() );
AMP_Validation_Manager::init_validate_request();
$this->assertTrue( AMP_Validation_Manager::$is_validate_request );
}
/**
* Test init_validate_request without error.
*
* @covers AMP_Validation_Manager::init_validate_request()
*/
public function test_init_validate_request_with_error() {
$_GET[ AMP_Validation_Manager::VALIDATE_QUERY_VAR ] = 'bad';
$this->assertInstanceOf( 'WP_Error', AMP_Validation_Manager::should_validate_response() );
add_filter( 'wp_doing_ajax', '__return_true' );
ob_start();
$died = false;
add_filter(
'wp_die_ajax_handler',
function() use ( &$died ) {
return function() use ( &$died ) {
$died = true;
};
}
);
AMP_Validation_Manager::init_validate_request();
ob_end_clean();
$this->assertTrue( $died );
}
/**
* Test init when theme support is not present.
*
* @covers AMP_Validation_Manager::init()
*/
public function test_init_without_theme_support() {
remove_theme_support( AMP_Theme_Support::SLUG );
AMP_Validation_Manager::init();
$this->assertFalse( has_action( 'save_post', [ 'AMP_Validation_Manager', 'handle_save_post_prompting_validation' ] ) );
}
/**
* Test \AMP_Validation_Manager::post_supports_validation.
*
* @covers \AMP_Validation_Manager::post_supports_validation()
*/
public function test_post_supports_validation() {
// Ensure that posts can be validated even when theme support is absent.
remove_theme_support( AMP_Theme_Support::SLUG );
$this->assertFalse( AMP_Validation_Manager::post_supports_validation( $this->factory()->post->create() ) );
// Ensure normal case of validating published post when theme support present.
add_theme_support( AMP_Theme_Support::SLUG );
$this->assertTrue( AMP_Validation_Manager::post_supports_validation( $this->factory()->post->create() ) );
// Trashed posts are not validatable.
$this->assertFalse( AMP_Validation_Manager::post_supports_validation( $this->factory()->post->create( [ 'post_status' => 'trash' ] ) ) );
// An invalid post is not previewable.
$this->assertFalse( AMP_Validation_Manager::post_supports_validation( 0 ) );
// Ensure non-viewable posts do not support validation.
register_post_type( 'not_viewable', [ 'publicly_queryable' => false ] );
$post = $this->factory()->post->create( [ 'post_type' => 'not_viewable' ] );
$this->assertFalse( AMP_Validation_Manager::post_supports_validation( $post ) );
}
/**
* Test add_validation_hooks.
*
* Excessive CSS validation errors have special case handling.
* @see https://github.com/ampproject/amp-wp/issues/2326
*
* @covers AMP_Validation_Manager::is_sanitization_auto_accepted()
*/
public function test_is_sanitization_auto_accepted() {
$some_error = [
'node_name' => 'href',
'parent_name' => 'a',
'type' => 'html_attribute_error',
'code' => AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_ATTR,
];
$excessive_css_error = [
'node_name' => 'style',
'type' => 'css',
'code' => AMP_Style_Sanitizer::STYLESHEET_TOO_LONG,
];
remove_theme_support( AMP_Theme_Support::SLUG );
$this->accept_sanitization_by_default( false );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted() );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted( $some_error ) );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted( $excessive_css_error ) );
remove_theme_support( AMP_Theme_Support::SLUG );
$this->accept_sanitization_by_default( true );
$this->assertTrue( AMP_Validation_Manager::is_sanitization_auto_accepted() );
$this->assertTrue( AMP_Validation_Manager::is_sanitization_auto_accepted( $some_error ) );
$this->assertTrue( AMP_Validation_Manager::is_sanitization_auto_accepted( $excessive_css_error ) );
add_theme_support( AMP_Theme_Support::SLUG );
$this->accept_sanitization_by_default( false );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted() );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted( $some_error ) );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted( $excessive_css_error ) );
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$this->accept_sanitization_by_default( false );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted() );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted( $some_error ) );
$this->assertFalse( AMP_Validation_Manager::is_sanitization_auto_accepted( $excessive_css_error ) );
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$this->accept_sanitization_by_default( true );
$this->assertTrue( AMP_Validation_Manager::is_sanitization_auto_accepted() );
$this->assertTrue( AMP_Validation_Manager::is_sanitization_auto_accepted( $some_error ) );
$this->assertTrue( AMP_Validation_Manager::is_sanitization_auto_accepted( $excessive_css_error ) );
}
/**
* Test add_admin_bar_menu_items.
*
* @covers AMP_Validation_Manager::add_admin_bar_menu_items()
*/
public function test_add_admin_bar_menu_items() {
$this->accept_sanitization_by_default( false );
// No admin bar item when user lacks capability.
$this->go_to( home_url( '/' ) );
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php';
$this->assertFalse( is_admin() );
$this->assertFalse( AMP_Validation_Manager::has_cap() );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$this->assertNull( $admin_bar->get_node( 'amp' ) );
// No admin bar item when no template available.
$this->go_to( home_url() );
add_theme_support( AMP_Theme_Support::SLUG );
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->assertTrue( AMP_Validation_Manager::has_cap() );
add_filter( 'amp_supportable_templates', '__return_empty_array' );
AMP_Options_Manager::update_option( 'all_templates_supported', false );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$this->assertNull( $admin_bar->get_node( 'amp' ) );
remove_filter( 'amp_supportable_templates', '__return_empty_array' );
AMP_Options_Manager::update_option( 'all_templates_supported', true );
// Admin bar item available in AMP-first mode.
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => false ] );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $node );
$this->assertStringContains( 'action=amp_validate', $node->href );
$this->assertNull( $admin_bar->get_node( 'amp-view' ) );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-validity' ) );
// Admin bar item available in paired mode.
add_filter( 'amp_dev_mode_enabled', '__return_true' );
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $node );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-paired-browsing' ) );
/*
* Admin bar item available in transitional mode.
* Transitional mode is available once template_dir is supplied.
*/
add_theme_support( AMP_Theme_Support::SLUG, [ 'template_dir' => 'amp' ] );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $node );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-paired-browsing' ) );
// Admin bar item available in paired mode.
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $node );
$this->assertStringEndsWith( '?amp', $node->href );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-view' ) );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-validity' ) );
// Admin bar item available in paired mode with validation errors.
$_GET[ AMP_Validation_Manager::VALIDATION_ERRORS_QUERY_VAR ] = 3;
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$admin_bar = new WP_Admin_Bar();
AMP_Validation_Manager::add_admin_bar_menu_items( $admin_bar );
$node = $admin_bar->get_node( 'amp' );
$this->assertInternalType( 'object', $node );
$this->assertStringContains( 'action=amp_validate', $node->href );
$this->assertNull( $admin_bar->get_node( 'amp-view' ) );
$this->assertInternalType( 'object', $admin_bar->get_node( 'amp-validity' ) );
}
/**
* Test overrides.
*
* @covers AMP_Validation_Manager::override_validation_error_statuses()
*/
public function test_override_validation_error_statuses_with_good_nonce() {
$this->assertEmpty( AMP_Validation_Manager::$validation_error_status_overrides );
$validation_error_term_1 = AMP_Validation_Error_Taxonomy::prepare_validation_error_taxonomy_term( [ 'test' => 1 ] );
$validation_error_term_2 = AMP_Validation_Error_Taxonomy::prepare_validation_error_taxonomy_term( [ 'test' => 2 ] );
$_REQUEST['preview'] = '1';
$_REQUEST['_wpnonce'] = wp_create_nonce( AMP_Validation_Manager::MARKUP_STATUS_PREVIEW_ACTION );
$_REQUEST[ AMP_Validation_Manager::VALIDATION_ERROR_TERM_STATUS_QUERY_VAR ] = [
$validation_error_term_1['slug'] => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS,
$validation_error_term_2['slug'] => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS,
];
AMP_Validation_Manager::override_validation_error_statuses();
$this->assertCount( 2, AMP_Validation_Manager::$validation_error_status_overrides );
}
/**
* Test validation error overrides for when bad nonce is supplied.
*
* @covers AMP_Validation_Manager::override_validation_error_statuses()
* @expectedException WPDieException
*/
public function test_override_validation_error_statuses_with_bad_nonce() {
$validation_error_term_1 = AMP_Validation_Error_Taxonomy::prepare_validation_error_taxonomy_term( [ 'test' => 1 ] );
$_REQUEST['preview'] = '1';
$_REQUEST['_wpnonce'] = 'bad';
$_REQUEST[ AMP_Validation_Manager::VALIDATION_ERROR_TERM_STATUS_QUERY_VAR ] = [
$validation_error_term_1['slug'] => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS,
];
AMP_Validation_Manager::override_validation_error_statuses();
}
/**
* Test validation error overrides for when no nonce is supplied.
*
* @covers AMP_Validation_Manager::override_validation_error_statuses()
* @expectedException WPDieException
*/
public function test_override_validation_error_statuses_with_no_nonce() {
$validation_error_term_1 = AMP_Validation_Error_Taxonomy::prepare_validation_error_taxonomy_term( [ 'test' => 1 ] );
$_REQUEST['preview'] = '1';
$_REQUEST[ AMP_Validation_Manager::VALIDATION_ERROR_TERM_STATUS_QUERY_VAR ] = [
$validation_error_term_1['slug'] => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS,
];
AMP_Validation_Manager::override_validation_error_statuses();
}
/**
* Test add_validation_error_sourcing.
*
* @covers AMP_Validation_Manager::add_validation_error_sourcing()
*/
public function test_add_validation_error_sourcing() {
AMP_Validation_Manager::add_validation_error_sourcing();
$this->assertEquals( ~PHP_INT_MAX, has_filter( 'setup_theme', [ self::TESTED_CLASS, 'set_theme_variables' ] ) );
$this->assertEquals( 10, has_action( 'wp', [ self::TESTED_CLASS, 'wrap_widget_callbacks' ] ) );
$this->assertEquals( 10, has_action( 'all', [ self::TESTED_CLASS, 'wrap_hook_callbacks' ] ) );
$this->assertEquals( PHP_INT_MAX, has_filter( 'the_content', [ self::TESTED_CLASS, 'decorate_filter_source' ] ) );
$this->assertEquals( PHP_INT_MAX, has_filter( 'the_excerpt', [ self::TESTED_CLASS, 'decorate_filter_source' ] ) );
$this->assertEquals( PHP_INT_MAX, has_action( 'do_shortcode_tag', [ self::TESTED_CLASS, 'decorate_shortcode_source' ] ) );
$this->assertEquals( 8, has_action( 'the_content', [ self::TESTED_CLASS, 'add_block_source_comments' ] ) );
}
/**
* Test add_validation_error_sourcing with Gutenberg active.
*
* @covers AMP_Validation_Manager::add_validation_error_sourcing()
*/
public function test_add_validation_error_sourcing_gutenberg() {
if ( ! function_exists( 'do_blocks' ) ) {
$this->markTestSkipped( 'Gutenberg not active.' );
}
if ( version_compare( get_bloginfo( 'version' ), '4.9', '<' ) ) {
$this->markTestSkipped( 'The WP version is less than 4.9, so Gutenberg did not init.' );
}
$priority = has_filter( 'the_content', 'do_blocks' );
$this->assertNotFalse( $priority );
AMP_Validation_Manager::add_validation_error_sourcing();
$this->assertEquals( $priority - 1, has_filter( 'the_content', [ self::TESTED_CLASS, 'add_block_source_comments' ] ) );
}
/**
* Tests handle_save_post_prompting_validation.
*
* @covers AMP_Validation_Manager::handle_save_post_prompting_validation()
* @covers AMP_Validation_Manager::validate_queued_posts_on_frontend()
*/
public function test_handle_save_post_prompting_validation_and_validate_queued_posts_on_frontend() {
add_theme_support( AMP_Theme_Support::SLUG );
$_SERVER['REQUEST_METHOD'] = 'POST';
$GLOBALS['pagenow'] = 'post.php';
register_post_type( 'secret', [ 'public' => false ] );
$secret = self::factory()->post->create_and_get( [ 'post_type' => 'secret' ] );
$_POST['post_ID'] = $secret->ID;
AMP_Validation_Manager::handle_save_post_prompting_validation( $secret->ID );
$this->assertFalse( has_action( 'shutdown', [ 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ] ) );
$this->assertEmpty( AMP_Validation_Manager::validate_queued_posts_on_frontend() );
$auto_draft = self::factory()->post->create_and_get( [ 'post_status' => 'auto-draft' ] );
$_POST['post_ID'] = $auto_draft->ID;
AMP_Validation_Manager::handle_save_post_prompting_validation( $auto_draft->ID );
$this->assertFalse( has_action( 'shutdown', [ 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ] ) );
$this->assertEmpty( AMP_Validation_Manager::validate_queued_posts_on_frontend() );
// Testing without $_POST context.
$post = self::factory()->post->create_and_get( [ 'post_type' => 'post' ] );
AMP_Validation_Manager::handle_save_post_prompting_validation( $post->ID );
$this->assertFalse( has_action( 'shutdown', [ 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ] ) );
// Test success.
$post = self::factory()->post->create_and_get( [ 'post_type' => 'post' ] );
$_POST['post_ID'] = $post->ID;
AMP_Validation_Manager::handle_save_post_prompting_validation( $post->ID );
$this->assertEquals( 10, has_action( 'shutdown', [ 'AMP_Validation_Manager', 'validate_queued_posts_on_frontend' ] ) );
add_filter(
'pre_http_request',
static function() {
return new WP_Error( 'http_request_made' );
}
);
$results = AMP_Validation_Manager::validate_queued_posts_on_frontend();
$this->assertArrayHasKey( $post->ID, $results );
$this->assertInstanceOf( 'WP_Error', $results[ $post->ID ] );
unset( $GLOBALS['pagenow'] );
}
/**
* Test add_rest_api_fields.
*
* @covers AMP_Validation_Manager::add_rest_api_fields()
*/
public function test_add_rest_api_fields() {
// Test in a transitional context.
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
AMP_Theme_Support::read_theme_support();
AMP_Validation_Manager::add_rest_api_fields();
$post_types_non_canonical = array_intersect(
get_post_types_by_support( 'amp' ),
get_post_types(
[
'show_in_rest' => true,
]
)
);
$this->assert_rest_api_field_present( $post_types_non_canonical );
// Test in a AMP-first (canonical) context.
add_theme_support( AMP_Theme_Support::SLUG );
AMP_Theme_Support::read_theme_support();
AMP_Validation_Manager::add_rest_api_fields();
$post_types_canonical = get_post_types_by_support( 'editor' );
$this->assert_rest_api_field_present( $post_types_canonical );
}
/**
* Asserts that the post types have the additional REST field.
*
* @param array $post_types The post types that should have the REST field.
*/
protected function assert_rest_api_field_present( $post_types ) {
foreach ( $post_types as $post_type ) {
$field = $GLOBALS['wp_rest_additional_fields'][ $post_type ][ AMP_Validation_Manager::VALIDITY_REST_FIELD_NAME ];
$this->assertEquals(
$field['schema'],
[
'description' => 'AMP validity status',
'type' => 'object',
]
);
$this->assertEquals( $field['get_callback'], [ self::TESTED_CLASS, 'get_amp_validity_rest_field' ] );
}
}
/**
* Test get_amp_validity_rest_field.
*
* @covers AMP_Validation_Manager::get_amp_validity_rest_field()
* @covers AMP_Validation_Manager::validate_url()
*/
public function test_get_amp_validity_rest_field() {
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$this->accept_sanitization_by_default( false );
AMP_Validated_URL_Post_Type::register();
AMP_Validation_Error_Taxonomy::register();
$id = self::factory()->post->create();
$this->assertNull(
AMP_Validation_Manager::get_amp_validity_rest_field(
compact( 'id' ),
'',
new WP_REST_Request( 'GET' )
)
);
// Create an error custom post for the ID, so this will return the errors in the field.
$errors = [
[
'code' => 'test',
],
];
$this->create_custom_post(
$errors,
amp_get_permalink( $id )
);
// Make sure capability check is honored.
$this->assertNull(
AMP_Validation_Manager::get_amp_validity_rest_field(
compact( 'id' ),
'',
new WP_REST_Request( 'GET' )
)
);
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
// GET request.
$field = AMP_Validation_Manager::get_amp_validity_rest_field(
compact( 'id' ),
'',
new WP_REST_Request( 'GET' )
);
$this->assertArrayHasKey( 'results', $field );
$this->assertArrayHasKey( 'review_link', $field );
$this->assertEquals(
$field['results'],
array_map(
static function ( $error ) {
return [
'sanitized' => false,
'title' => 'Unknown error (test)',
'error' => $error,
'status' => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS,
'term_status' => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS,
'forced' => false,
];
},
$errors
)
);
// PUT request.
add_filter(
'pre_http_request',
static function() {
return [
'body' => wp_json_encode( [ 'results' => [] ] ),
'response' => [
'code' => 200,
'message' => 'ok',
],
];
}
);
$field = AMP_Validation_Manager::get_amp_validity_rest_field(
compact( 'id' ),
'',
new WP_REST_Request( 'PUT' )
);
$this->assertArrayHasKey( 'results', $field );
$this->assertArrayHasKey( 'review_link', $field );
$this->assertEmpty( $field['results'] );
}
/**
* Test has_cap.
*
* @covers AMP_Validation_Manager::has_cap()
*/
public function test_has_cap() {
wp_set_current_user(
self::factory()->user->create(
[
'role' => 'subscriber',
]
)
);
$this->assertFalse( AMP_Validation_Manager::has_cap() );
$this->set_capability();
$this->assertTrue( AMP_Validation_Manager::has_cap() );
}
/**
* Test add_validation_error.
*
* @covers AMP_Validation_Manager::add_validation_error()
*/
public function test_add_validation_error_track_removed() {
AMP_Validation_Manager::$is_validate_request = true;
$this->assertEmpty( AMP_Validation_Manager::$validation_results );
$that = $this;
$node = $this->node;
add_filter(
'amp_validation_error',
static function( $error, $context ) use ( $node, $that ) {
$error['filtered'] = true;
$that->assertEquals( AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_TAG, $error['code'] );
$that->assertSame( $node, $context['node'] );
return $error;
},
10,
2
);
AMP_Validation_Manager::add_validation_error(
[
'node_name' => $this->node->nodeName,
'code' => AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_TAG,
'node_attributes' => [],
],
[
'node' => $this->node,
]
);
$this->assertCount( 1, AMP_Validation_Manager::$validation_results );
$this->assertEquals(
[
'node_name' => 'img',
'sources' => [],
'code' => AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_TAG,
'node_attributes' => [],
'filtered' => true,
],
AMP_Validation_Manager::$validation_results[0]['error']
);
}
/**
* Test was_node_removed.
*
* @covers AMP_Validation_Manager::add_validation_error()
*/
public function test_add_validation_error_was_node_removed() {
$this->assertEmpty( AMP_Validation_Manager::$validation_results );
AMP_Validation_Manager::add_validation_error(
[
'node' => $this->node,
]
);
$this->assertNotEmpty( AMP_Validation_Manager::$validation_results );
}
/**
* Test reset_validation_results.
*
* @covers AMP_Validation_Manager::reset_validation_results()
*/
public function test_reset_validation_results() {
AMP_Validation_Manager::add_validation_error(
[
'code' => 'test',
]
);
AMP_Validation_Manager::reset_validation_results();
$this->assertEquals( [], AMP_Validation_Manager::$validation_results );
}
/**
* Test print_edit_form_validation_status
*
* @covers AMP_Validation_Manager::print_edit_form_validation_status()
*/
public function test_print_edit_form_validation_status() {
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
$this->accept_sanitization_by_default( false );
AMP_Validated_URL_Post_Type::register();
AMP_Validation_Error_Taxonomy::register();
$this->set_capability();
$post = self::factory()->post->create_and_get();
$output = get_echo( [ 'AMP_Validation_Manager', 'print_edit_form_validation_status' ], [ $post ] );
$this->assertStringNotContains( 'notice notice-warning', $output );
$validation_errors = [
[
'code' => AMP_Tag_And_Attribute_Sanitizer::DISALLOWED_TAG,
'node_name' => $this->disallowed_tag_name,
'parent_name' => 'div',
'node_attributes' => [],
'sources' => [
[
'type' => 'plugin',
'name' => $this->plugin_name,
],
],
],
];
AMP_Validated_URL_Post_Type::store_validation_errors( $validation_errors, get_permalink( $post->ID ) );
$output = get_echo( [ 'AMP_Validation_Manager', 'print_edit_form_validation_status' ], [ $post ] );
// When sanitization is accepted by default.
$this->accept_sanitization_by_default( true );
$expected_notice_non_accepted_errors = 'There is content which fails AMP validation. You will have to remove the invalid markup (or allow the plugin to remove it) to serve AMP.';
$this->assertStringContains( 'notice notice-warning', $output );
$this->assertStringContains( '<code>script</code>', $output );
$this->assertStringContains( $expected_notice_non_accepted_errors, $output );
// When auto-accepting validation errors, if there are unaccepted validation errors, there should be a notice because this will block serving an AMP document.
add_theme_support( AMP_Theme_Support::SLUG );
$output = get_echo( [ 'AMP_Validation_Manager', 'print_edit_form_validation_status' ], [ $post ] );
$this->assertStringContains( 'There is content which fails AMP validation. You will have to remove the invalid markup (or allow the plugin to remove it) to serve AMP.', $output );
/*
* When there are 'Rejected' or 'New Rejected' errors, there should be a message that explains that this will serve a non-AMP URL.
* This simulates sanitization being accepted by default, but it having been false when the validation errors were stored,
* as there are errors with 'New Rejected' status.
*/
$this->accept_sanitization_by_default( true );
add_theme_support( AMP_Theme_Support::SLUG, [ AMP_Theme_Support::PAIRED_FLAG => true ] );
AMP_Validated_URL_Post_Type::store_validation_errors( $validation_errors, get_permalink( $post->ID ) );
$this->accept_sanitization_by_default( false );
$output = get_echo( [ 'AMP_Validation_Manager', 'print_edit_form_validation_status' ], [ $post ] );
$this->assertStringContains( $expected_notice_non_accepted_errors, $output );
}
/**
* Test source comments.
*
* @covers AMP_Validation_Manager::locate_sources()
* @covers AMP_Validation_Manager::parse_source_comment()
* @covers AMP_Validation_Manager::get_source_comment()
*/
public function test_source_comments() {
$source1 = [
'type' => 'plugin',
'name' => 'foo',
'shortcode' => 'test',
'function' => __FUNCTION__,
];
$source2 = [
'type' => 'theme',
'name' => 'bar',
'function' => __FUNCTION__,
'hook' => 'something',
];
$dom = AMP_DOM_Utils::get_dom_from_content(
implode(
'',
[
AMP_Validation_Manager::get_source_comment( $source1, true ),
AMP_Validation_Manager::get_source_comment( $source2, true ),
'<b id="test">Test</b>',
AMP_Validation_Manager::get_source_comment( $source2, false ),
AMP_Validation_Manager::get_source_comment( $source1, false ),
]
)
);
/**
* Comments.
*
* @var DOMComment[] $comments
*/
$comments = [];
foreach ( $dom->xpath->query( '//comment()' ) as $comment ) {
$comments[] = $comment;
}
$this->assertCount( 4, $comments );
$sources = AMP_Validation_Manager::locate_sources( $dom->getElementById( 'test' ) );
$this->assertInternalType( 'array', $sources );
$this->assertCount( 2, $sources );
$this->assertEquals( $source1, $sources[0] );
$parsed_comment = AMP_Validation_Manager::parse_source_comment( $comments[0] );
$this->assertEquals( $source1, $parsed_comment['source'] );
$this->assertFalse( $parsed_comment['closing'] );
$parsed_comment = AMP_Validation_Manager::parse_source_comment( $comments[3] );
$this->assertEquals( $source1, $parsed_comment['source'] );
$this->assertTrue( $parsed_comment['closing'] );
$this->assertEquals( $source2, $sources[1] );
$parsed_comment = AMP_Validation_Manager::parse_source_comment( $comments[1] );
$this->assertEquals( $source2, $parsed_comment['source'] );
$this->assertFalse( $parsed_comment['closing'] );
$parsed_comment = AMP_Validation_Manager::parse_source_comment( $comments[2] );
$this->assertEquals( $source2, $parsed_comment['source'] );
$this->assertTrue( $parsed_comment['closing'] );
}
/**
* Get data for testing locate_sources.
*
* @return array
*/
public function get_locate_sources_data() {
return [
'directly_enqueued_link' => [
static function () {
add_action(
'wp_enqueue_scripts',
static function () {
wp_enqueue_style(
'foo',
'https://example.com/foo.css',
[],
'0.1'
);
}
);
},
'//link[ contains( @href, "foo.css" ) ]',
function ( $sources ) {
$this->assertArraySubset(
[
'type' => 'plugin',
'name' => 'amp',
'function' => '{closure}',
'hook' => 'wp_enqueue_scripts',
'priority' => 10,
'dependency_type' => 'style',
'handle' => 'foo',
],
$sources[0]
);
},
],
'stylesheet_added_as_dependency' => [
static function () {
add_action(
'wp_enqueue_scripts',
static function () {
wp_enqueue_style(
'bar',
'https://example.com/bar.css',
[ 'wp-codemirror' ],
'0.1'
);
}
);
},
'//link[ contains( @href, "codemirror" ) ]',
function ( $sources ) {
$this->assertArraySubset(
[
'type' => 'plugin',
'name' => 'amp',
'function' => '{closure}',
'hook' => 'wp_enqueue_scripts',
'priority' => 10,
'dependency_type' => 'style',
'handle' => 'bar',
'dependency_handle' => 'wp-codemirror',
],
$sources[0]
);
},
],
'inline_style_for_directly_enqueued_stylesheet' => [
static function () {
add_action(
'wp_enqueue_scripts',
static function () {
wp_enqueue_style(
'baz',
'https://example.com/baz.css',
[],
'0.1'
);
wp_add_inline_style( 'baz', '/*Hello Baz!*/' );
}
);
},
'//style[ contains( text(), "Hello Baz" ) ]',
function ( $sources ) {
$this->assertArraySubset(
[
'type' => 'plugin',
'name' => 'amp',
'function' => '{closure}',
'hook' => 'wp_enqueue_scripts',
'priority' => 10,
'dependency_type' => 'style',
'extra_key' => 'after',
'text' => '/*Hello Baz!*/',
'handle' => 'baz',
],
$sources[1]
);
},
],
'external_script_directly_enqueued' => [
static function () {
add_action(
'wp_enqueue_scripts',
static function () {
wp_enqueue_script(
'foo',
'https://example.com/foo.js',
[],
'0.1',
true
);
}
);
},
'//script[ contains( @src, "foo.js" ) ]',
function ( $sources ) {
$this->assertArraySubset(
[
'type' => 'plugin',
'name' => 'amp',
'function' => '{closure}',
'hook' => 'wp_enqueue_scripts',
'priority' => 10,
'dependency_type' => 'script',
'handle' => 'foo',
],
$sources[0]
);
},
],