@@ -659,6 +659,37 @@ static function ( $pre, $parsed_args, $url ) use ( $redirect_url ) {
659
659
},
660
660
'expect_error ' => 'http_request_failed ' ,
661
661
),
662
+
663
+ 'good_same_origin ' => array (
664
+ 'set_up ' => static function (): string {
665
+ $ image_url = home_url ( '/good.jpg ' );
666
+
667
+ add_filter (
668
+ 'pre_http_request ' ,
669
+ static function ( $ pre , $ parsed_args , $ url ) use ( $ image_url ) {
670
+ if ( 'HEAD ' !== $ parsed_args ['method ' ] || $ image_url !== $ url ) {
671
+ return $ pre ;
672
+ }
673
+ return array (
674
+ 'headers ' => array (
675
+ 'content-type ' => 'image/jpeg ' ,
676
+ 'content-length ' => '288449 ' ,
677
+ ),
678
+ 'body ' => '' ,
679
+ 'response ' => array (
680
+ 'code ' => 200 ,
681
+ 'message ' => 'OK ' ,
682
+ ),
683
+ );
684
+ },
685
+ 10 ,
686
+ 3
687
+ );
688
+
689
+ return $ image_url ;
690
+ },
691
+ 'expect_error ' => null ,
692
+ ),
662
693
);
663
694
}
664
695
@@ -680,13 +711,147 @@ public function test_image_prioritizer_validate_background_image_url( Closure $s
680
711
}
681
712
}
682
713
714
+ /**
715
+ * Data provider.
716
+ *
717
+ * @return array<string, mixed>
718
+ */
719
+ public function data_provider_to_test_image_prioritizer_filter_store_url_metric_validity (): array {
720
+ return array (
721
+ 'pass_through_true ' => array (
722
+ 'set_up ' => static function ( array $ sample_url_metric_data ): array {
723
+ $ url_metric = new OD_Strict_URL_Metric ( $ sample_url_metric_data );
724
+ return array ( true , $ url_metric );
725
+ },
726
+ 'assert ' => function ( $ value ): void {
727
+ $ this ->assertTrue ( $ value );
728
+ },
729
+ ),
730
+
731
+ 'pass_through_false ' => array (
732
+ 'set_up ' => static function ( array $ sample_url_metric_data ): array {
733
+ $ url_metric = new OD_Strict_URL_Metric ( $ sample_url_metric_data );
734
+ return array ( false , $ url_metric );
735
+ },
736
+ 'assert ' => function ( $ value ): void {
737
+ $ this ->assertFalse ( $ value );
738
+ },
739
+ ),
740
+
741
+ 'pass_through_truthy_string ' => array (
742
+ 'set_up ' => static function ( array $ sample_url_metric_data ): array {
743
+ $ url_metric = new OD_Strict_URL_Metric ( $ sample_url_metric_data );
744
+ return array ( 'so true ' , $ url_metric );
745
+ },
746
+ 'assert ' => function ( $ value ): void {
747
+ $ this ->assertTrue ( $ value );
748
+ },
749
+ ),
750
+
751
+ 'pass_through_falsy_string ' => array (
752
+ 'set_up ' => static function ( array $ sample_url_metric_data ): array {
753
+ $ url_metric = new OD_Strict_URL_Metric ( $ sample_url_metric_data );
754
+ return array ( '' , $ url_metric );
755
+ },
756
+ 'assert ' => function ( $ value ): void {
757
+ $ this ->assertFalse ( $ value );
758
+ },
759
+ ),
760
+
761
+ 'pass_through_wp_error ' => array (
762
+ 'set_up ' => static function ( array $ sample_url_metric_data ): array {
763
+ $ url_metric = new OD_Strict_URL_Metric ( $ sample_url_metric_data );
764
+ return array ( new WP_Error ( 'bad ' , 'Evil ' ), $ url_metric );
765
+ },
766
+ 'assert ' => function ( $ value ): void {
767
+ $ this ->assertInstanceOf ( WP_Error::class, $ value );
768
+ $ this ->assertSame ( 'bad ' , $ value ->get_error_code () );
769
+ },
770
+ ),
771
+
772
+ 'invalid_external_bg_image ' => array (
773
+ 'set_up ' => static function ( array $ sample_url_metric_data ): array {
774
+ $ sample_url_metric_data ['lcpElementExternalBackgroundImage ' ] = array (
775
+ 'url ' => 'https://bad-origin.example.com/image.jpg ' ,
776
+ 'tag ' => 'DIV ' ,
777
+ 'id ' => null ,
778
+ 'class ' => null ,
779
+ );
780
+ $ url_metric = new OD_Strict_URL_Metric ( $ sample_url_metric_data );
781
+ return array ( true , $ url_metric );
782
+ },
783
+ 'assert ' => function ( $ value , OD_Strict_URL_Metric $ url_metric ): void {
784
+ $ this ->assertTrue ( $ value );
785
+ $ this ->assertNull ( $ url_metric ->get ( 'lcpElementExternalBackgroundImage ' ) );
786
+ },
787
+ ),
788
+
789
+ 'valid_external_bg_image ' => array (
790
+ 'set_up ' => static function ( array $ sample_url_metric_data ): array {
791
+ $ image_url = home_url ( '/good.jpg ' );
792
+
793
+ add_filter (
794
+ 'pre_http_request ' ,
795
+ static function ( $ pre , $ parsed_args , $ url ) use ( $ image_url ) {
796
+ if ( 'HEAD ' !== $ parsed_args ['method ' ] || $ image_url !== $ url ) {
797
+ return $ pre ;
798
+ }
799
+ return array (
800
+ 'headers ' => array (
801
+ 'content-type ' => 'image/jpeg ' ,
802
+ 'content-length ' => '288449 ' ,
803
+ ),
804
+ 'body ' => '' ,
805
+ 'response ' => array (
806
+ 'code ' => 200 ,
807
+ 'message ' => 'OK ' ,
808
+ ),
809
+ );
810
+ },
811
+ 10 ,
812
+ 3
813
+ );
814
+
815
+ $ sample_url_metric_data ['lcpElementExternalBackgroundImage ' ] = array (
816
+ 'url ' => $ image_url ,
817
+ 'tag ' => 'DIV ' ,
818
+ 'id ' => null ,
819
+ 'class ' => null ,
820
+ );
821
+ $ url_metric = new OD_Strict_URL_Metric ( $ sample_url_metric_data );
822
+ return array ( true , $ url_metric );
823
+ },
824
+ 'assert ' => function ( $ value , OD_Strict_URL_Metric $ url_metric ): void {
825
+ $ this ->assertTrue ( $ value );
826
+ $ this ->assertIsArray ( $ url_metric ->get ( 'lcpElementExternalBackgroundImage ' ) );
827
+ $ this ->assertSame (
828
+ array (
829
+ 'url ' => home_url ( '/good.jpg ' ),
830
+ 'tag ' => 'DIV ' ,
831
+ 'id ' => null ,
832
+ 'class ' => null ,
833
+ ),
834
+ $ url_metric ->get ( 'lcpElementExternalBackgroundImage ' )
835
+ );
836
+ },
837
+ ),
838
+ );
839
+ }
840
+
683
841
/**
684
842
* Tests image_prioritizer_filter_store_url_metric_validity().
685
843
*
844
+ * @dataProvider data_provider_to_test_image_prioritizer_filter_store_url_metric_validity
845
+ *
686
846
* @covers ::image_prioritizer_filter_store_url_metric_validity
847
+ * @covers ::image_prioritizer_validate_background_image_url
687
848
*/
688
- public function test_image_prioritizer_filter_store_url_metric_validity (): void {
689
- $ this ->markTestIncomplete ();
849
+ public function test_image_prioritizer_filter_store_url_metric_validity ( Closure $ set_up , Closure $ assert ): void {
850
+ $ sample_url_metric_data = $ this ->get_sample_url_metric ( array () )->jsonSerialize ();
851
+ list ( $ validity , $ url_metric ) = $ set_up ( $ sample_url_metric_data );
852
+
853
+ $ validity = image_prioritizer_filter_store_url_metric_validity ( $ validity , $ url_metric );
854
+ $ assert ( $ validity , $ url_metric );
690
855
}
691
856
692
857
/**
0 commit comments