Skip to content

Commit 5373233

Browse files
committed
Fix handling of invalid external BG image and add tests
1 parent b7b1a47 commit 5373233

File tree

2 files changed

+170
-5
lines changed

2 files changed

+170
-5
lines changed

plugins/image-prioritizer/helper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ function image_prioritizer_filter_store_url_metric_validity( $validity, OD_Stric
261261

262262
$data = $url_metric->get( 'lcpElementExternalBackgroundImage' );
263263
if ( is_array( $data ) && isset( $data['url'] ) && is_string( $data['url'] ) ) { // Note: The isset() and is_string() checks aren't necessary since the JSON Schema enforces them to be set.
264-
$validity = image_prioritizer_validate_background_image_url( $data['url'] );
265-
if ( is_wp_error( $validity ) ) {
264+
$image_validity = image_prioritizer_validate_background_image_url( $data['url'] );
265+
if ( is_wp_error( $image_validity ) ) {
266266
/**
267267
* No WP_Exception is thrown by wp_trigger_error() since E_USER_ERROR is not passed as the error level.
268268
*
269269
* @noinspection PhpUnhandledExceptionInspection
270270
*/
271-
wp_trigger_error( __FUNCTION__, $validity->get_error_message() . ' Background image URL: ' . $data['url'] );
271+
wp_trigger_error( __FUNCTION__, $image_validity->get_error_message() . ' Background image URL: ' . $data['url'] );
272272
$url_metric->unset( 'lcpElementExternalBackgroundImage' );
273273
}
274274
}

plugins/image-prioritizer/tests/test-helper.php

Lines changed: 167 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,37 @@ static function ( $pre, $parsed_args, $url ) use ( $redirect_url ) {
659659
},
660660
'expect_error' => 'http_request_failed',
661661
),
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+
),
662693
);
663694
}
664695

@@ -680,13 +711,147 @@ public function test_image_prioritizer_validate_background_image_url( Closure $s
680711
}
681712
}
682713

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+
683841
/**
684842
* Tests image_prioritizer_filter_store_url_metric_validity().
685843
*
844+
* @dataProvider data_provider_to_test_image_prioritizer_filter_store_url_metric_validity
845+
*
686846
* @covers ::image_prioritizer_filter_store_url_metric_validity
847+
* @covers ::image_prioritizer_validate_background_image_url
687848
*/
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 );
690855
}
691856

692857
/**

0 commit comments

Comments
 (0)