Changeset 53751 for trunk/tests/phpunit/tests/media.php
- Timestamp:
- 07/21/2022 06:01:01 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/media.php
r53558 r53751 2252 2252 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2253 2253 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2254 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2254 2255 2255 2256 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 2257 2258 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2258 2259 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2260 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2261 2259 2262 } 2260 2263 … … 2290 2293 $img = wp_img_tag_add_decoding_attr( $img, 'the_content' ); 2291 2294 $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img ); 2295 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2292 2296 2293 2297 // The content filter should return the image unchanged. 2294 2298 $this->assertSame( $img, wp_filter_content_tags( $img ) ); 2299 2300 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2295 2301 } 2296 2302 … … 2362 2368 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2363 2369 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 2370 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2364 2371 2365 2372 add_filter( … … 2424 2431 */ 2425 2432 public function test_wp_filter_content_tags_schemes() { 2433 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2426 2434 $image_meta = wp_get_attachment_metadata( self::$large_id ); 2427 2435 $size_array = $this->get_image_size_array_from_meta( $image_meta, 'medium' ); … … 2469 2477 2470 2478 $this->assertSame( $expected, $actual ); 2479 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2471 2480 } 2472 2481 … … 2962 2971 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2963 2972 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2973 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2964 2974 2965 2975 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 2967 2977 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2968 2978 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2979 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2969 2980 } 2970 2981 … … 3042 3053 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 3043 3054 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3055 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3044 3056 3045 3057 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 3047 3059 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 3048 3060 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3061 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3049 3062 } 3050 3063 … … 3075 3088 add_filter( 'wp_lazy_loading_enabled', '__return_true' ); 3076 3089 3090 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3091 3077 3092 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 3078 3093 remove_filter( 'wp_lazy_loading_enabled', '__return_true' ); 3079 3094 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3095 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3096 3080 3097 } 3081 3098 … … 3102 3119 add_filter( 'wp_lazy_loading_enabled', '__return_false' ); 3103 3120 3121 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3122 3104 3123 $this->assertSame( $content, wp_filter_content_tags( $content ) ); 3105 3124 remove_filter( 'wp_lazy_loading_enabled', '__return_false' ); 3106 3125 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3126 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3107 3127 } 3108 3128 … … 3530 3550 function test_wp_filter_content_tags_with_wp_get_loading_attr_default() { 3531 3551 global $wp_query, $wp_the_query; 3552 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3532 3553 3533 3554 $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); … … 3565 3586 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3566 3587 } 3588 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3567 3589 3568 3590 // After filtering, the first image should not be lazy-loaded while the other ones should be. … … 3614 3636 remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 ); 3615 3637 } 3638 3639 /** 3640 * @ticket 55443 3641 */ 3642 public function test_wp_image_use_alternate_mime_types_replaces_jpg_with_webp_where_available() { 3643 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { 3644 $this->markTestSkipped( 'This test requires WebP support.' ); 3645 } 3646 3647 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default. 3648 $tag = wp_get_attachment_image( self::$large_id, 'full' ); 3649 $expected_tag = $tag; 3650 3651 $metadata = wp_get_attachment_metadata( self::$large_id ); 3652 foreach ( $metadata['sizes'] as $size => $properties ) { 3653 // Some sizes may not have WebP if the WebP file is larger than the JPEG for the size. 3654 if ( ! isset( $properties['sources']['image/webp'] ) ) { 3655 continue; 3656 } 3657 $expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag ); 3658 } 3659 // Same applies to the full size. 3660 if ( isset( $metadata['sources']['image/webp'] ) ) { 3661 $expected_tag = str_replace( $metadata['sources']['image/jpeg']['file'], $metadata['sources']['image/webp']['file'], $expected_tag ); 3662 } 3663 3664 $this->assertNotSame( $tag, $expected_tag ); 3665 $this->assertSame( $expected_tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) ); 3666 } 3667 3668 /** 3669 * @ticket 55443 3670 */ 3671 public function test_wp_image_use_alternate_mime_types_does_not_replace_jpg_when_webp_is_not_available() { 3672 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { 3673 $this->markTestSkipped( 'This test requires WebP support.' ); 3674 } 3675 3676 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default. 3677 $tag = wp_get_attachment_image( self::$large_id, 'full' ); 3678 3679 // Update attachment metadata as if the image had no WebP available for any sub-sizes and the full size. 3680 $metadata = wp_get_attachment_metadata( self::$large_id ); 3681 foreach ( $metadata['sizes'] as $size => $properties ) { 3682 unset( $metadata['sizes'][ $size ]['sources']['image/webp'] ); 3683 } 3684 unset( $metadata['sources']['image/webp'] ); 3685 wp_update_attachment_metadata( self::$large_id, $metadata ); 3686 3687 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) ); 3688 } 3689 3690 /** 3691 * @ticket 55443 3692 */ 3693 public function test_wp_image_use_alternate_mime_types_still_replaces_jpg_subsizes_when_webp_is_not_available_for_full_size() { 3694 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { 3695 $this->markTestSkipped( 'This test requires WebP support.' ); 3696 } 3697 3698 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default. 3699 $tag = wp_get_attachment_image( self::$large_id, 'full' ); 3700 $expected_tag = $tag; 3701 3702 // Update attachment metadata as if the image had no WebP available for the full size. 3703 $metadata = wp_get_attachment_metadata( self::$large_id ); 3704 unset( $metadata['sources']['image/webp'] ); 3705 wp_update_attachment_metadata( self::$large_id, $metadata ); 3706 3707 foreach ( $metadata['sizes'] as $size => $properties ) { 3708 // Some sizes may not have WebP if the WebP file is larger than the JPEG for the size. 3709 if ( ! isset( $properties['sources']['image/webp'] ) ) { 3710 continue; 3711 } 3712 $expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag ); 3713 } 3714 3715 $this->assertNotSame( $tag, $expected_tag ); 3716 $this->assertSame( $expected_tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) ); 3717 } 3718 3719 /** 3720 * @ticket 55443 3721 */ 3722 public function test_wp_image_use_alternate_mime_types_respects_wp_content_image_mimes_filter() { 3723 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { 3724 $this->markTestSkipped( 'This test requires WebP support.' ); 3725 } 3726 3727 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default. 3728 $tag = wp_get_attachment_image( self::$large_id, 'full' ); 3729 3730 // Invalid filter value results in no changes to content. 3731 add_filter( 'wp_content_image_mimes', '__return_false' ); 3732 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) ); 3733 3734 // Empty array results in no changes to content. 3735 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3736 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) ); 3737 3738 // Preferring JPEG over WebP results in no changes to content. 3739 add_filter( 3740 'wp_content_image_mimes', 3741 function() { 3742 return array( 'image/jpeg', 'image/webp' ); 3743 } 3744 ); 3745 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) ); 3746 } 3747 3748 /** 3749 * @ticket 55443 3750 */ 3751 public function test__wp_in_front_end_context_without_wp_query() { 3752 unset( $GLOBALS['wp_query'] ); 3753 3754 $this->assertFalse( _wp_in_front_end_context() ); 3755 } 3756 3757 /** 3758 * @ticket 55443 3759 */ 3760 public function test__wp_in_front_end_context_with_feed() { 3761 remove_all_actions( 'template_redirect' ); 3762 do_action( 'template_redirect' ); 3763 $GLOBALS['wp_query']->is_feed = true; 3764 3765 $this->assertFalse( _wp_in_front_end_context() ); 3766 } 3767 3768 /** 3769 * @ticket 55443 3770 */ 3771 public function test__wp_in_front_end_context_before_and_after_template_redirect() { 3772 $result = _wp_in_front_end_context(); 3773 3774 remove_all_actions( 'template_redirect' ); 3775 do_action( 'template_redirect' ); 3776 3777 $this->assertFalse( $result ); 3778 $this->assertTrue( _wp_in_front_end_context() ); 3779 } 3780 3781 /** 3782 * @ticket 55443 3783 */ 3784 public function test__wp_in_front_end_context_within_wp_head() { 3785 remove_all_actions( 'template_redirect' ); 3786 do_action( 'template_redirect' ); 3787 3788 // Call function within a 'wp_head' callback. 3789 remove_all_actions( 'wp_head' ); 3790 $result = null; 3791 add_action( 3792 'wp_head', 3793 function() use ( &$result ) { 3794 $result = _wp_in_front_end_context(); 3795 } 3796 ); 3797 do_action( 'wp_head' ); 3798 3799 $this->assertFalse( $result ); 3800 } 3616 3801 } 3617 3802
Note: See TracChangeset
for help on using the changeset viewer.