Skip to content

Commit b7b1a47

Browse files
committed
Add tests for various validity conditions for external BG images
1 parent 18553ee commit b7b1a47

File tree

2 files changed

+172
-7
lines changed

2 files changed

+172
-7
lines changed

plugins/image-prioritizer/helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static function ( $host ) {
194194
sprintf(
195195
/* translators: %s is the list of allowed hosts */
196196
__( 'Background image URL host is not among allowed: %s.', 'image-prioritizer' ),
197-
join( ', ', $allowed_hosts )
197+
join( ', ', array_unique( $allowed_hosts ) )
198198
)
199199
);
200200
}
@@ -222,7 +222,7 @@ static function ( $host ) {
222222
}
223223

224224
// Validate that the Content-Type is an image.
225-
$content_type = (array) wp_remote_retrieve_header( $r, 'Content-Type' );
225+
$content_type = (array) wp_remote_retrieve_header( $r, 'content-type' );
226226
if ( ! is_string( $content_type[0] ) || ! str_starts_with( $content_type[0], 'image/' ) ) {
227227
return new WP_Error(
228228
'background_image_response_not_image',

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

Lines changed: 170 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,26 +474,191 @@ public function test_image_prioritizer_add_element_item_schema_properties_inputs
474474
*/
475475
public function data_provider_to_test_image_prioritizer_validate_background_image_url(): array {
476476
return array(
477-
'url_parse_error' => array(
477+
'bad_url_parse_error' => array(
478478
'set_up' => static function (): string {
479479
return 'https:///www.example.com';
480480
},
481481
'expect_error' => 'background_image_url_lacks_host',
482482
),
483-
'url_no_host' => array(
483+
'bad_url_no_host' => array(
484484
'set_up' => static function (): string {
485485
return '/foo/bar?baz=1';
486486
},
487487
'expect_error' => 'background_image_url_lacks_host',
488488
),
489-
'url_disallowed_origin' => array(
489+
490+
'bad_url_disallowed_origin' => array(
490491
'set_up' => static function (): string {
491492
return 'https://bad.example.com/foo.jpg';
492493
},
493494
'expect_error' => 'disallowed_background_image_url_host',
494495
),
495-
// TODO: Try uploading image attachment and have it point to a CDN.
496-
// TODO: Try a URL that returns a non image Content-Type.
496+
497+
'good_other_origin_via_allowed_http_origins_filter' => array(
498+
'set_up' => static function (): string {
499+
$image_url = 'https://other-origin.example.com/foo.jpg';
500+
501+
add_filter(
502+
'allowed_http_origins',
503+
static function ( array $allowed_origins ): array {
504+
$allowed_origins[] = 'https://other-origin.example.com';
505+
return $allowed_origins;
506+
}
507+
);
508+
509+
add_filter(
510+
'pre_http_request',
511+
static function ( $pre, $parsed_args, $url ) use ( $image_url ) {
512+
if ( 'HEAD' !== $parsed_args['method'] || $image_url !== $url ) {
513+
return $pre;
514+
}
515+
return array(
516+
'headers' => array(
517+
'content-type' => 'image/jpeg',
518+
'content-length' => '288449',
519+
),
520+
'body' => '',
521+
'response' => array(
522+
'code' => 200,
523+
'message' => 'OK',
524+
),
525+
);
526+
},
527+
10,
528+
3
529+
);
530+
531+
return $image_url;
532+
},
533+
'expect_error' => null,
534+
),
535+
536+
'good_url_allowed_cdn_origin' => array(
537+
'set_up' => function (): string {
538+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
539+
$this->assertIsInt( $attachment_id );
540+
541+
add_filter(
542+
'wp_get_attachment_image_src',
543+
static function ( $src ): array {
544+
$src[0] = preg_replace( '#^https?://#i', 'https://my-image-cdn.example.com/', $src[0] );
545+
return $src;
546+
}
547+
);
548+
549+
$src = wp_get_attachment_image_src( $attachment_id, 'large' );
550+
$this->assertIsArray( $src );
551+
$this->assertStringStartsWith( 'https://my-image-cdn.example.com/', $src[0] );
552+
553+
add_filter(
554+
'pre_http_request',
555+
static function ( $pre, $parsed_args, $url ) use ( $src ) {
556+
if ( 'HEAD' !== $parsed_args['method'] || $src[0] !== $url ) {
557+
return $pre;
558+
}
559+
return array(
560+
'headers' => array(
561+
'content-type' => 'image/jpeg',
562+
'content-length' => '288449',
563+
),
564+
'body' => '',
565+
'response' => array(
566+
'code' => 200,
567+
'message' => 'OK',
568+
),
569+
);
570+
},
571+
10,
572+
3
573+
);
574+
575+
return $src[0];
576+
},
577+
'expect_error' => null,
578+
),
579+
580+
'bad_not_found' => array(
581+
'set_up' => static function (): string {
582+
$image_url = home_url( '/bad.jpg' );
583+
584+
add_filter(
585+
'pre_http_request',
586+
static function ( $pre, $parsed_args, $url ) use ( $image_url ) {
587+
if ( 'HEAD' !== $parsed_args['method'] || $image_url !== $url ) {
588+
return $pre;
589+
}
590+
return array(
591+
'headers' => array(
592+
'content-type' => 'text/html',
593+
'content-length' => 1000,
594+
),
595+
'body' => '',
596+
'response' => array(
597+
'code' => 404,
598+
'message' => 'Not Found',
599+
),
600+
);
601+
},
602+
10,
603+
3
604+
);
605+
606+
return $image_url;
607+
},
608+
'expect_error' => 'background_image_response_not_ok',
609+
),
610+
611+
'bad_content_type' => array(
612+
'set_up' => static function (): string {
613+
$video_url = home_url( '/bad.mp4' );
614+
615+
add_filter(
616+
'pre_http_request',
617+
static function ( $pre, $parsed_args, $url ) use ( $video_url ) {
618+
if ( 'HEAD' !== $parsed_args['method'] || $video_url !== $url ) {
619+
return $pre;
620+
}
621+
return array(
622+
'headers' => array(
623+
'content-type' => 'video/mp4',
624+
'content-length' => '288449000',
625+
),
626+
'body' => '',
627+
'response' => array(
628+
'code' => 200,
629+
'message' => 'OK',
630+
),
631+
);
632+
},
633+
10,
634+
3
635+
);
636+
637+
return $video_url;
638+
},
639+
'expect_error' => 'background_image_response_not_image',
640+
),
641+
642+
'bad_redirect' => array(
643+
'set_up' => static function (): string {
644+
$redirect_url = home_url( '/redirect.jpg' );
645+
646+
add_filter(
647+
'pre_http_request',
648+
static function ( $pre, $parsed_args, $url ) use ( $redirect_url ) {
649+
if ( $redirect_url === $url ) {
650+
return new WP_Error( 'http_request_failed', 'Too many redirects.' );
651+
}
652+
return $pre;
653+
},
654+
10,
655+
3
656+
);
657+
658+
return $redirect_url;
659+
},
660+
'expect_error' => 'http_request_failed',
661+
),
497662
);
498663
}
499664

0 commit comments

Comments
 (0)