Changeset 54085 for trunk/tests/phpunit/tests/media.php
- Timestamp:
- 09/06/2022 09:11:41 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/media.php
r53920 r54085 2256 2256 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2257 2257 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2258 add_filter( 'wp_content_image_mimes', '__return_empty_array' );2259 2258 2260 2259 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 2262 2261 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2263 2262 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2264 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );2265 2266 2263 } 2267 2264 … … 2297 2294 $img = wp_img_tag_add_decoding_attr( $img, 'the_content' ); 2298 2295 $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img ); 2299 add_filter( 'wp_content_image_mimes', '__return_empty_array' );2300 2296 2301 2297 // The content filter should return the image unchanged. 2302 2298 $this->assertSame( $img, wp_filter_content_tags( $img ) ); 2303 2304 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );2305 2299 } 2306 2300 … … 2372 2366 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2373 2367 add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); 2374 add_filter( 'wp_content_image_mimes', '__return_empty_array' );2375 2368 2376 2369 add_filter( … … 2435 2428 */ 2436 2429 public function test_wp_filter_content_tags_schemes() { 2437 add_filter( 'wp_content_image_mimes', '__return_empty_array' );2438 2430 $image_meta = wp_get_attachment_metadata( self::$large_id ); 2439 2431 $size_array = $this->get_image_size_array_from_meta( $image_meta, 'medium' ); … … 2481 2473 2482 2474 $this->assertSame( $expected, $actual ); 2483 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );2484 2475 } 2485 2476 … … 2975 2966 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2976 2967 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2977 add_filter( 'wp_content_image_mimes', '__return_empty_array' );2978 2968 2979 2969 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 2981 2971 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2982 2972 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2983 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );2984 2973 } 2985 2974 … … 3057 3046 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 3058 3047 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3059 add_filter( 'wp_content_image_mimes', '__return_empty_array' );3060 3048 3061 3049 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); … … 3063 3051 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 3064 3052 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3065 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );3066 3053 } 3067 3054 … … 3092 3079 add_filter( 'wp_lazy_loading_enabled', '__return_true' ); 3093 3080 3094 add_filter( 'wp_content_image_mimes', '__return_empty_array' );3095 3096 3081 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 3097 3082 remove_filter( 'wp_lazy_loading_enabled', '__return_true' ); 3098 3083 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3099 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );3100 3101 3084 } 3102 3085 … … 3123 3106 add_filter( 'wp_lazy_loading_enabled', '__return_false' ); 3124 3107 3125 add_filter( 'wp_content_image_mimes', '__return_empty_array' );3126 3127 3108 $this->assertSame( $content, wp_filter_content_tags( $content ) ); 3128 3109 remove_filter( 'wp_lazy_loading_enabled', '__return_false' ); 3129 3110 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3130 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );3131 3111 } 3132 3112 … … 3554 3534 function test_wp_filter_content_tags_with_wp_get_loading_attr_default() { 3555 3535 global $wp_query, $wp_the_query; 3556 add_filter( 'wp_content_image_mimes', '__return_empty_array' );3557 3536 3558 3537 $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); … … 3590 3569 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3591 3570 } 3592 remove_filter( 'wp_content_image_mimes', '__return_empty_array' );3593 3571 3594 3572 // After filtering, the first image should not be lazy-loaded while the other ones should be. … … 3640 3618 remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 ); 3641 3619 } 3642 3643 /**3644 * @ticket 554433645 */3646 public function test_wp_image_use_alternate_mime_types_replaces_jpg_with_webp_where_available() {3647 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {3648 $this->markTestSkipped( 'This test requires WebP support.' );3649 }3650 3651 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default.3652 $tag = wp_get_attachment_image( self::$large_id, 'full' );3653 $expected_tag = $tag;3654 3655 $metadata = wp_get_attachment_metadata( self::$large_id );3656 foreach ( $metadata['sizes'] as $size => $properties ) {3657 // Some sizes may not have WebP if the WebP file is larger than the JPEG for the size.3658 if ( ! isset( $properties['sources']['image/webp'] ) ) {3659 continue;3660 }3661 $expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag );3662 }3663 // Same applies to the full size.3664 if ( isset( $metadata['sources']['image/webp'] ) ) {3665 $expected_tag = str_replace( $metadata['sources']['image/jpeg']['file'], $metadata['sources']['image/webp']['file'], $expected_tag );3666 }3667 3668 $this->assertNotSame( $tag, $expected_tag );3669 $this->assertSame( $expected_tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );3670 }3671 3672 /**3673 * @ticket 554433674 */3675 public function test_wp_image_use_alternate_mime_types_does_not_replace_jpg_when_webp_is_not_available() {3676 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {3677 $this->markTestSkipped( 'This test requires WebP support.' );3678 }3679 3680 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default.3681 $tag = wp_get_attachment_image( self::$large_id, 'full' );3682 3683 // Update attachment metadata as if the image had no WebP available for any sub-sizes and the full size.3684 $metadata = wp_get_attachment_metadata( self::$large_id );3685 foreach ( $metadata['sizes'] as $size => $properties ) {3686 unset( $metadata['sizes'][ $size ]['sources']['image/webp'] );3687 }3688 unset( $metadata['sources']['image/webp'] );3689 wp_update_attachment_metadata( self::$large_id, $metadata );3690 3691 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );3692 }3693 3694 /**3695 * @ticket 554433696 */3697 public function test_wp_image_use_alternate_mime_types_still_replaces_jpg_subsizes_when_webp_is_not_available_for_full_size() {3698 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {3699 $this->markTestSkipped( 'This test requires WebP support.' );3700 }3701 3702 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default.3703 $tag = wp_get_attachment_image( self::$large_id, 'full' );3704 $expected_tag = $tag;3705 3706 // Update attachment metadata as if the image had no WebP available for the full size.3707 $metadata = wp_get_attachment_metadata( self::$large_id );3708 unset( $metadata['sources']['image/webp'] );3709 wp_update_attachment_metadata( self::$large_id, $metadata );3710 3711 foreach ( $metadata['sizes'] as $size => $properties ) {3712 // Some sizes may not have WebP if the WebP file is larger than the JPEG for the size.3713 if ( ! isset( $properties['sources']['image/webp'] ) ) {3714 continue;3715 }3716 $expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag );3717 }3718 3719 $this->assertNotSame( $tag, $expected_tag );3720 $this->assertSame( $expected_tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );3721 }3722 3723 /**3724 * @ticket 554433725 */3726 public function test_wp_image_use_alternate_mime_types_respects_wp_content_image_mimes_filter() {3727 if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {3728 $this->markTestSkipped( 'This test requires WebP support.' );3729 }3730 3731 // The attachment $large_id is a JPEG image, so it gets WebP files generated by default.3732 $tag = wp_get_attachment_image( self::$large_id, 'full' );3733 3734 // Invalid filter value results in no changes to content.3735 add_filter( 'wp_content_image_mimes', '__return_false' );3736 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );3737 3738 // Empty array results in no changes to content.3739 add_filter( 'wp_content_image_mimes', '__return_empty_array' );3740 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );3741 3742 // Preferring JPEG over WebP results in no changes to content.3743 add_filter(3744 'wp_content_image_mimes',3745 function() {3746 return array( 'image/jpeg', 'image/webp' );3747 }3748 );3749 $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', self::$large_id ) );3750 }3751 3752 /**3753 * @ticket 554433754 */3755 public function test__wp_in_front_end_context_without_wp_query() {3756 unset( $GLOBALS['wp_query'] );3757 3758 $this->assertFalse( _wp_in_front_end_context() );3759 }3760 3761 /**3762 * @ticket 554433763 */3764 public function test__wp_in_front_end_context_with_feed() {3765 remove_all_actions( 'template_redirect' );3766 do_action( 'template_redirect' );3767 $GLOBALS['wp_query']->is_feed = true;3768 3769 $this->assertFalse( _wp_in_front_end_context() );3770 }3771 3772 /**3773 * @ticket 554433774 */3775 public function test__wp_in_front_end_context_before_and_after_template_redirect() {3776 $result = _wp_in_front_end_context();3777 3778 remove_all_actions( 'template_redirect' );3779 do_action( 'template_redirect' );3780 3781 $this->assertFalse( $result );3782 $this->assertTrue( _wp_in_front_end_context() );3783 }3784 3785 /**3786 * @ticket 554433787 */3788 public function test__wp_in_front_end_context_within_wp_head() {3789 remove_all_actions( 'template_redirect' );3790 do_action( 'template_redirect' );3791 3792 // Call function within a 'wp_head' callback.3793 remove_all_actions( 'wp_head' );3794 $result = null;3795 add_action(3796 'wp_head',3797 function() use ( &$result ) {3798 $result = _wp_in_front_end_context();3799 }3800 );3801 do_action( 'wp_head' );3802 3803 $this->assertFalse( $result );3804 }3805 3620 } 3806 3621
Note: See TracChangeset
for help on using the changeset viewer.