@@ -474,26 +474,191 @@ public function test_image_prioritizer_add_element_item_schema_properties_inputs
474
474
*/
475
475
public function data_provider_to_test_image_prioritizer_validate_background_image_url (): array {
476
476
return array (
477
- 'url_parse_error ' => array (
477
+ 'bad_url_parse_error ' => array (
478
478
'set_up ' => static function (): string {
479
479
return 'https:///www.example.com ' ;
480
480
},
481
481
'expect_error ' => 'background_image_url_lacks_host ' ,
482
482
),
483
- 'url_no_host ' => array (
483
+ 'bad_url_no_host ' => array (
484
484
'set_up ' => static function (): string {
485
485
return '/foo/bar?baz=1 ' ;
486
486
},
487
487
'expect_error ' => 'background_image_url_lacks_host ' ,
488
488
),
489
- 'url_disallowed_origin ' => array (
489
+
490
+ 'bad_url_disallowed_origin ' => array (
490
491
'set_up ' => static function (): string {
491
492
return 'https://bad.example.com/foo.jpg ' ;
492
493
},
493
494
'expect_error ' => 'disallowed_background_image_url_host ' ,
494
495
),
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
+ ),
497
662
);
498
663
}
499
664
0 commit comments