Make WordPress Core

Ticket #55443: 55443-tests.diff

File 55443-tests.diff, 27.7 KB (added by adamsilverstein, 3 years ago)
  • tests/phpunit/tests/image/editor.php

    diff --git tests/phpunit/tests/image/editor.php tests/phpunit/tests/image/editor.php
    index c051ffec2b..1c5bb8d6fa 100644
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    363363                        ),
    364364                );
    365365        }
    366 
    367         /**
    368          * Test creating  the original image mime type when the image is uploaded.
    369          *
    370          * @ticket 55443
    371          *
    372          * @dataProvider provider_image_with_default_behaviors_during_upload
    373          */
    374         public function it_should_create_the_original_image_mime_type_when_the_image_is_uploaded( $file_location, $expected_mime, $targeted_mime ) {
    375                 $attachment_id = $this->factory->attachment->create_upload_object( $file_location );
    376 
    377                 $metadata = wp_get_attachment_metadata( $attachment_id );
    378 
    379                 $this->assertIsArray( $metadata );
    380                 foreach ( $metadata['sizes'] as $size_name => $properties ) {
    381                         $this->assertArrayHasKey( 'sources', $properties );
    382                         $this->assertIsArray( $properties['sources'] );
    383                         $this->assertArrayHasKey( $expected_mime, $properties['sources'] );
    384                         $this->assertArrayHasKey( 'filesize', $properties['sources'][ $expected_mime ] );
    385                         $this->assertArrayHasKey( 'file', $properties['sources'][ $expected_mime ] );
    386                         $this->assertArrayHasKey( $targeted_mime, $properties['sources'] );
    387                         $this->assertArrayHasKey( 'filesize', $properties['sources'][ $targeted_mime ] );
    388                         $this->assertArrayHasKey( 'file', $properties['sources'][ $targeted_mime ] );
    389                 }
    390         }
    391 
    392         /**
    393          * Data provider for it_should_create_the_original_image_mime_type_when_the_image_is_uploaded.
    394          */
    395         public function provider_image_with_default_behaviors_during_upload() {
    396                 yield 'JPEG image' => array(
    397                         DIR_TESTDATA . '/images/test-image.jpg',
    398                         'image/jpeg',
    399                         'image/webp',
    400                 );
    401 
    402                 yield 'WebP image' => array(
    403                         DIR_TESTDATA . '/images/webp-lossy.webp',
    404                         'image/webp',
    405                         'image/jpeg',
    406                 );
    407         }
    408 
    409         /**
    410          * Test Do not create the sources property if no transform is provided.
    411          *
    412          * @ticket 55443
    413          */
    414         public function it_should_not_create_the_sources_property_if_no_transform_is_provided() {
    415                 add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
    416 
    417                 $attachment_id = $this->factory->attachment->create_upload_object(
    418                         DIR_TESTDATA . '/images/test-image.jpg'
    419                 );
    420 
    421                 $metadata = wp_get_attachment_metadata( $attachment_id );
    422 
    423                 $this->assertIsArray( $metadata );
    424                 foreach ( $metadata['sizes'] as $size_name => $properties ) {
    425                         $this->assertArrayNotHasKey( 'sources', $properties );
    426                 }
    427         }
    428 
    429         /**
    430          * Test creating the sources property when no transform is available.
    431          *
    432          * @ticket 55443
    433          */
    434         public function it_should_create_the_sources_property_when_no_transform_is_available() {
    435                 add_filter(
    436                         'wp_upload_image_mime_transforms',
    437                         function () {
    438                                 return array( 'image/jpeg' => array() );
    439                         }
    440                 );
    441 
    442                 $attachment_id = $this->factory->attachment->create_upload_object(
    443                         DIR_TESTDATA . '/images/test-image.jpg'
    444                 );
    445 
    446                 $metadata = wp_get_attachment_metadata( $attachment_id );
    447 
    448                 $this->assertIsArray( $metadata );
    449                 foreach ( $metadata['sizes'] as $size_name => $properties ) {
    450                         $this->assertArrayHasKey( 'sources', $properties );
    451                         $this->assertIsArray( $properties['sources'] );
    452                         $this->assertArrayHasKey( 'image/jpeg', $properties['sources'] );
    453                         $this->assertArrayHasKey( 'filesize', $properties['sources']['image/jpeg'] );
    454                         $this->assertArrayHasKey( 'file', $properties['sources']['image/jpeg'] );
    455                         $this->assertArrayNotHasKey( 'image/webp', $properties['sources'] );
    456                 }
    457         }
    458 
    459         /**
    460          * Test not creating the sources property if the mime is not specified on the transforms images.
    461          *
    462          * @ticket 55443
    463          */
    464         public function it_should_not_create_the_sources_property_if_the_mime_is_not_specified_on_the_transforms_images() {
    465                 add_filter(
    466                         'wp_upload_image_mime_transforms',
    467                         function () {
    468                                 return array( 'image/jpeg' => array() );
    469                         }
    470                 );
    471 
    472                 $attachment_id = $this->factory->attachment->create_upload_object(
    473                         DIR_TESTDATA . '/images/webp-lossy.webp'
    474                 );
    475 
    476                 $metadata = wp_get_attachment_metadata( $attachment_id );
    477 
    478                 $this->assertIsArray( $metadata );
    479                 foreach ( $metadata['sizes'] as $size_name => $properties ) {
    480                         $this->assertArrayNotHasKey( 'sources', $properties );
    481                 }
    482         }
    483 
    484 
    485         /**
    486          * Test creating a WebP version with all the required properties.
    487          *
    488          * @ticket 55443
    489          */
    490         public function it_should_create_a_webp_version_with_all_the_required_properties() {
    491                 $attachment_id = $this->factory->attachment->create_upload_object(
    492                         DIR_TESTDATA . '/images/test-image.jpg'
    493                 );
    494 
    495                 $metadata = wp_get_attachment_metadata( $attachment_id );
    496                 $this->assertArrayHasKey( 'sources', $metadata['sizes']['thumbnail'] );
    497                 $this->assertArrayHasKey( 'image/jpeg', $metadata['sizes']['thumbnail']['sources'] );
    498                 $this->assertArrayHasKey( 'filesize', $metadata['sizes']['thumbnail']['sources']['image/jpeg'] );
    499                 $this->assertArrayHasKey( 'file', $metadata['sizes']['thumbnail']['sources']['image/jpeg'] );
    500                 $this->assertArrayHasKey( 'image/webp', $metadata['sizes']['thumbnail']['sources'] );
    501                 $this->assertArrayHasKey( 'filesize', $metadata['sizes']['thumbnail']['sources']['image/webp'] );
    502                 $this->assertArrayHasKey( 'file', $metadata['sizes']['thumbnail']['sources']['image/webp'] );
    503                 $this->assertStringEndsNotWith( '.jpeg', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] );
    504                 $this->assertStringEndsWith( '.webp', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] );
    505         }
    506 
    507         /**
    508          * Test removing `scaled` suffix from the generated filename.
    509          *
    510          * @ticket 55443
    511          */
    512         public function it_should_remove_scaled_suffix_from_the_generated_filename() {
    513                 // The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created.
    514                 add_filter(
    515                         'big_image_size_threshold',
    516                         function () {
    517                                 return 850;
    518                         }
    519                 );
    520 
    521                 $attachment_id = $this->factory->attachment->create_upload_object(
    522                         DIR_TESTDATA . '/images/test-image.jpg'
    523                 );
    524                 $metadata      = wp_get_attachment_metadata( $attachment_id );
    525                 $this->assertStringEndsWith( '-scaled.jpg', get_attached_file( $attachment_id ) );
    526                 $this->assertArrayHasKey( 'image/webp', $metadata['sizes']['medium']['sources'] );
    527                 $this->assertStringEndsNotWith( '-scaled.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
    528                 $this->assertStringEndsWith( '-300x200.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
    529         }
    530 
    531         /**
    532          * Test removing the generated webp images when the attachment is deleted.
    533          *
    534          * @ticket 55443
    535          */
    536         public function it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted() {
    537                 // Make sure no editor is available.
    538                 $attachment_id = $this->factory->attachment->create_upload_object(
    539                         DIR_TESTDATA . '/images/test-image.jpg'
    540                 );
    541 
    542                 $file    = get_attached_file( $attachment_id, true );
    543                 $dirname = pathinfo( $file, PATHINFO_DIRNAME );
    544 
    545                 $this->assertIsString( $file );
    546                 $this->assertFileExists( $file );
    547 
    548                 $metadata = wp_get_attachment_metadata( $attachment_id );
    549                 $sizes    = array( 'thumbnail', 'medium' );
    550 
    551                 foreach ( $sizes as $size_name ) {
    552                         $this->assertArrayHasKey( 'image/webp', $metadata['sizes'][ $size_name ]['sources'] );
    553                         $this->assertArrayHasKey( 'file', $metadata['sizes'][ $size_name ]['sources']['image/webp'] );
    554                         $this->assertFileExists(
    555                                 path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] )
    556                         );
    557                 }
    558 
    559                 wp_delete_attachment( $attachment_id );
    560 
    561                 foreach ( $sizes as $size_name ) {
    562                         $this->assertFileDoesNotExist(
    563                                 path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] )
    564                         );
    565                 }
    566         }
    567 
    568         /**
    569          * Test removing the attached WebP version if the attachment is force deleted but empty trash day is not defined.
    570          *
    571          * @ticket 55443
    572          */
    573         public function it_should_remove_the_attached_webp_version_if_the_attachment_is_force_deleted_but_empty_trash_day_is_not_defined() {
    574                 // Make sure no editor is available.
    575                 $attachment_id = $this->factory->attachment->create_upload_object(
    576                         DIR_TESTDATA . '/images/test-image.jpg'
    577                 );
    578 
    579                 $file    = get_attached_file( $attachment_id, true );
    580                 $dirname = pathinfo( $file, PATHINFO_DIRNAME );
    581 
    582                 $this->assertIsString( $file );
    583                 $this->assertFileExists( $file );
    584 
    585                 $metadata = wp_get_attachment_metadata( $attachment_id );
    586 
    587                 $this->assertFileExists(
    588                         path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
    589                 );
    590 
    591                 wp_delete_attachment( $attachment_id, true );
    592 
    593                 $this->assertFileDoesNotExist(
    594                         path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
    595                 );
    596         }
    597 
    598         /**
    599          * Test removing the WebP version of the image if the image is force deleted and empty trash days is set to zero.
    600          *
    601          * @ticket 55443
    602          */
    603         public function it_should_remove_the_webp_version_of_the_image_if_the_image_is_force_deleted_and_empty_trash_days_is_set_to_zero() {
    604                 // Make sure no editor is available.
    605                 $attachment_id = $this->factory->attachment->create_upload_object(
    606                         DIR_TESTDATA . '/images/test-image.jpg'
    607                 );
    608 
    609                 $file    = get_attached_file( $attachment_id, true );
    610                 $dirname = pathinfo( $file, PATHINFO_DIRNAME );
    611 
    612                 $this->assertIsString( $file );
    613                 $this->assertFileExists( $file );
    614 
    615                 $metadata = wp_get_attachment_metadata( $attachment_id );
    616 
    617                 $this->assertFileExists(
    618                         path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
    619                 );
    620 
    621                 define( 'EMPTY_TRASH_DAYS', 0 );
    622 
    623                 wp_delete_attachment( $attachment_id, true );
    624 
    625                 $this->assertFileDoesNotExist(
    626                         path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
    627                 );
    628         }
    629 
    630         /**
    631          * Test avoiding the change of URLs of images that are not part of the media library.
    632          *
    633          * @ticket 55443
    634          */
    635         public function it_should_avoid_the_change_of_urls_of_images_that_are_not_part_of_the_media_library() {
    636                 $paragraph = '<p>Donec accumsan, sapien et <img src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>';
    637 
    638                 $this->assertSame( $paragraph, webp_uploads_update_image_references( $paragraph ) );
    639         }
    640 
    641         /**
    642          * Test avoiding replacing not existing attachment IDs.
    643          *
    644          * @ticket 55443
    645          */
    646         public function it_should_avoid_replacing_not_existing_attachment_i_ds() {
    647                 $paragraph = '<p>Donec accumsan, sapien et <img class="wp-image-0" src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>';
    648 
    649                 $this->assertSame( $paragraph, webp_uploads_update_image_references( $paragraph ) );
    650         }
    651 
    652         /**
    653          * Test preventing replacing a WebP image.
    654          *
    655          * @ticket 55443
    656          */
    657         public function it_should_test_preventing_replacing_a_webp_image() {
    658                 $attachment_id = $this->factory->attachment->create_upload_object(
    659                         DIR_TESTDATA . '/images/webp-lossy.webp'
    660                 );
    661 
    662                 $tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
    663 
    664                 $this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
    665         }
    666 
    667         /**
    668          * Test preventing replacing a jpg image if the image does not have the target class name.
    669          *
    670          * @ticket 55443
    671          */
    672         public function it_should_test_preventing_replacing_a_jpg_image_if_the_image_does_not_have_the_target_class_name() {
    673                 $attachment_id = $this->factory->attachment->create_upload_object(
    674                         DIR_TESTDATA . '/images/test-image.jpg'
    675                 );
    676 
    677                 $tag = wp_get_attachment_image( $attachment_id, 'medium' );
    678 
    679                 $this->assertSame( $tag, webp_uploads_update_image_references( $tag ) );
    680         }
    681 
    682         /**
    683          * Test replacing the references to a JPG image to a WebP version.
    684          *
    685          * @dataProvider provider_replace_images_with_different_extensions
    686          *
    687          * @ticket 55443
    688          */
    689         public function it_should_replace_the_references_to_a_jpg_image_to_a_webp_version( $image_path ) {
    690                 $attachment_id = $this->factory->attachment->create_upload_object( $image_path );
    691 
    692                 $tag          = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
    693                 $expected_tag = $tag;
    694                 $metadata     = wp_get_attachment_metadata( $attachment_id );
    695                 foreach ( $metadata['sizes'] as $size => $properties ) {
    696                         $expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag );
    697                 }
    698 
    699                 $this->assertNotEmpty( $expected_tag );
    700                 $this->assertNotSame( $tag, $expected_tag );
    701                 $this->assertSame( $expected_tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
    702         }
    703 
    704         public function provider_replace_images_with_different_extensions() {
    705                 yield 'An image with a .jpg extension' => array( DIR_TESTDATA . '/images/test-image.jpg' );
    706                 yield 'An image with a .jpeg extension' => array( DIR_TESTDATA . '/images/test-image.jpeg' );
    707         }
    708 
    709         /**
    710          * Test the full image size from the original mime type.
    711          *
    712          * @ticket 55443
    713          */
    714         public function it_should_contain_the_full_image_size_from_the_original_mime() {
    715                 $attachment_id = $this->factory->attachment->create_upload_object(
    716                         DIR_TESTDATA . '/images/test-image.jpg'
    717                 );
    718 
    719                 $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
    720 
    721                 $expected = array(
    722                         'ext'  => 'jpg',
    723                         'type' => 'image/jpeg',
    724                 );
    725                 $this->assertSame( $expected, wp_check_filetype( get_attached_file( $attachment_id ) ) );
    726                 $this->assertContains( wp_basename( get_attached_file( $attachment_id ) ), webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
    727         }
    728 
    729         /**
    730          * Test preventing replacing an image with no available sources.
    731          *
    732          * @ticket 55443
    733          */
    734         public function it_should_prevent_replacing_an_image_with_no_available_sources() {
    735                 add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
    736 
    737                 $attachment_id = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.jpg' );
    738 
    739                 $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
    740                 $this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
    741         }
    742 
    743         /**
    744          * Test preventing update not supported images with no available sources.
    745          *
    746          * @dataProvider provider_it_should_prevent_update_not_supported_images_with_no_available_sources
    747          *
    748          * @ticket 55443
    749          */
    750         public function it_should_prevent_update_not_supported_images_with_no_available_sources( $image_path ) {
    751                 $attachment_id = $this->factory->attachment->create_upload_object( $image_path );
    752 
    753                 $this->assertIsNumeric( $attachment_id );
    754                 $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
    755 
    756                 $this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
    757         }
    758 
    759         /**
    760          * Data provider for it_should_prevent_update_not_supported_images_with_no_available_sources.
    761          */
    762         public function provider_it_should_prevent_update_not_supported_images_with_no_available_sources() {
    763                 yield 'PNG image' => array( DIR_TESTDATA . '/images/test-image.png' );
    764                 yield 'GIFT image' => array( DIR_TESTDATA . '/images/test-image.gif' );
    765         }
    766 
    767366}
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index daf585d548..74340f3d2a 100644
    EOF; 
    37973797
    37983798                $this->assertFalse( $result );
    37993799        }
     3800
     3801        /**
     3802         * @ticket 55443
     3803         *
     3804         * @dataProvider provider_image_with_default_behaviors_during_upload
     3805         */
     3806        public function test_it_should_create_the_original_mime_type_as_well_with_all_the_available_sources_for_the_specified_mime( $file_location, $expected_mime, $targeted_mime ) {
     3807                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3808                        $this->markTestSkipped( 'This test requires WebP support.' );
     3809                }
     3810
     3811                $attachment_id = $this->factory->attachment->create_upload_object( $file_location );
     3812
     3813                $metadata = wp_get_attachment_metadata( $attachment_id );
     3814                $this->assertIsArray( $metadata );
     3815                $this->assertStringEndsWith( $metadata['sources'][ $expected_mime ]['file'], $metadata['file'] );
     3816
     3817                // Assert some sizes that include both mime types.
     3818                $sizes = array( 'large', 'medium_large' );
     3819                foreach ( $sizes as $size_name ) {
     3820                        if ( isset( $metadata['sizes'][ $size_name ]['sources'][ $expected_mime ] ) ) {
     3821                                $this->assertArrayHasKey( $expected_mime, $metadata['sizes'][ $size_name ]['sources'] );
     3822                                $this->assertArrayHasKey( 'filesize', $metadata['sizes'][ $size_name ]['sources'][ $expected_mime ] );
     3823                                $this->assertArrayHasKey( 'file', $metadata['sizes'][ $size_name ]['sources'][ $expected_mime ] );
     3824                        }
     3825                        if ( isset( $metadata['sizes'][ $size_name ]['sources'][ $targeted_mime ] ) ) {
     3826                                $this->assertArrayHasKey( $targeted_mime, $metadata['sizes'][ $size_name ]['sources'] );
     3827                                $this->assertArrayHasKey( 'filesize', $metadata['sizes'][ $size_name ]['sources'][ $targeted_mime ] );
     3828                                $this->assertArrayHasKey( 'file', $metadata['sizes'][ $size_name ]['sources'][ $targeted_mime ] );
     3829                        }
     3830                }
     3831        }
     3832
     3833        public function provider_image_with_default_behaviors_during_upload() {
     3834                yield 'JPEG image' => array(
     3835                        DIR_TESTDATA . '/images/33772.jpg',
     3836                        'image/jpeg',
     3837                        'image/webp',
     3838                );
     3839
     3840                yield 'WebP image' => array(
     3841                        DIR_TESTDATA . '/images/webp-lossless.webp',
     3842                        'image/webp',
     3843                        'image/jpeg',
     3844                );
     3845        }
     3846
     3847        /**
     3848         * @ticket 55443
     3849         *
     3850         * @dataProvider provider_image_with_default_behaviors_during_upload
     3851         */
     3852        public function test_it_should_create_the_sources_property_when_no_transform_is_available( $file_location, $expected_mime, $targeted_mime ) {
     3853                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3854                        $this->markTestSkipped( 'This test requires WebP support.' );
     3855                }
     3856
     3857                add_filter(
     3858                        'wp_upload_image_mime_transforms',
     3859                        function () {
     3860                                return array( 'image/jpeg' => array() );
     3861                        }
     3862                );
     3863
     3864                $attachment_id = $this->factory->attachment->create_upload_object( $file_location );
     3865
     3866                $metadata = wp_get_attachment_metadata( $attachment_id );
     3867
     3868                $this->assertIsArray( $metadata );
     3869                foreach ( $metadata['sizes'] as $size_name => $properties ) {
     3870                        $this->assertArrayHasKey( 'sources', $properties );
     3871                        $this->assertIsArray( $properties['sources'] );
     3872                        $this->assertArrayHasKey( $expected_mime, $properties['sources'] );
     3873                        $this->assertArrayHasKey( 'filesize', $properties['sources'][ $expected_mime ] );
     3874                        $this->assertArrayHasKey( 'file', $properties['sources'][ $expected_mime ] );
     3875                        $this->assertArrayNotHasKey( $targeted_mime, $properties['sources'] );
     3876                }
     3877        }
     3878
     3879        /**
     3880         * @ticket 55443
     3881         */
     3882        public function test_it_should_create_a_webp_version_with_all_the_required_properties() {
     3883                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3884                        $this->markTestSkipped( 'This test requires WebP support.' );
     3885                }
     3886
     3887                $attachment_id = $this->factory->attachment->create_upload_object(
     3888                        DIR_TESTDATA . '/images/canola.jpg'
     3889                );
     3890
     3891                $metadata = wp_get_attachment_metadata( $attachment_id );
     3892
     3893                foreach ( $metadata['sizes'] as $size_name => $properties ) {
     3894                        $this->assertArrayHasKey( 'sources', $properties );
     3895                        $this->assertArrayHasKey( 'image/jpeg', $properties['sources'] );
     3896                        $this->assertArrayHasKey( 'filesize', $properties['sources']['image/jpeg'] );
     3897                        $this->assertArrayHasKey( 'file', $properties['sources']['image/jpeg'] );
     3898                        $this->assertArrayHasKey( 'image/webp', $properties['sources'] );
     3899                        $this->assertArrayHasKey( 'filesize', $properties['sources']['image/webp'] );
     3900                        $this->assertArrayHasKey( 'file', $properties['sources']['image/webp'] );
     3901                        $this->assertStringEndsNotWith( '.jpeg', $properties['sources']['image/webp']['file'] );
     3902                        $this->assertStringEndsWith( '.webp', $properties['sources']['image/webp']['file'] );
     3903                }
     3904        }
     3905
     3906        /**
     3907         * @ticket 55443
     3908         */
     3909        public function test_it_should_remove_scaled_suffix_from_the_generated_filename() {
     3910                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3911                        $this->markTestSkipped( 'This test requires WebP support.' );
     3912                }
     3913
     3914                // The 33772 image is 1910 pixels wide with this filter we ensure a -scaled version is created.
     3915                add_filter(
     3916                        'big_image_size_threshold',
     3917                        function () {
     3918                                return 850;
     3919                        }
     3920                );
     3921
     3922                $attachment_id = $this->factory->attachment->create_upload_object(
     3923                        DIR_TESTDATA . '/images/33772.jpg'
     3924                );
     3925                $metadata      = wp_get_attachment_metadata( $attachment_id );
     3926                $this->assertStringEndsWith( '-scaled.jpg', get_attached_file( $attachment_id ) );
     3927                $this->assertArrayHasKey( 'image/webp', $metadata['sizes']['thumbnail']['sources'] );
     3928                $this->assertStringEndsNotWith( '-scaled-jpg.webp', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] );
     3929                $this->assertStringEndsWith( '-150x150-jpg.webp', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] );
     3930        }
     3931
     3932        /**
     3933         * @ticket 55443
     3934         */
     3935        public function test_it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted() {
     3936                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3937                        $this->markTestSkipped( 'This test requires WebP support.' );
     3938                }
     3939
     3940                $attachment_id = $this->factory->attachment->create_upload_object(
     3941                        DIR_TESTDATA . '/images/canola.jpg'
     3942                );
     3943
     3944                $file    = get_attached_file( $attachment_id, true );
     3945                $dirname = pathinfo( $file, PATHINFO_DIRNAME );
     3946
     3947                $this->assertIsString( $file );
     3948                $this->assertFileExists( $file );
     3949
     3950                $metadata = wp_get_attachment_metadata( $attachment_id );
     3951                $sizes    = array( 'thumbnail', 'medium' );
     3952
     3953                foreach ( $sizes as $size_name ) {
     3954                        $this->assertArrayHasKey( 'image/webp', $metadata['sizes'][ $size_name ]['sources'] );
     3955                        $this->assertArrayHasKey( 'file', $metadata['sizes'][ $size_name ]['sources']['image/webp'] );
     3956                        $this->assertFileExists(
     3957                                path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] )
     3958                        );
     3959                }
     3960
     3961                wp_delete_attachment( $attachment_id );
     3962
     3963                foreach ( $sizes as $size_name ) {
     3964                        $this->assertFileDoesNotExist(
     3965                                path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] )
     3966                        );
     3967                }
     3968        }
     3969
     3970        /**
     3971         * @ticket 55443
     3972         */
     3973        public function test_it_should_remove_the_attached_webp_version_if_the_attachment_is_force_deleted() {
     3974                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     3975                        $this->markTestSkipped( 'This test requires WebP support.' );
     3976                }
     3977
     3978                // Make sure no editor is available.
     3979                $attachment_id = $this->factory->attachment->create_upload_object(
     3980                        DIR_TESTDATA . '/images/canola.jpg'
     3981                );
     3982
     3983                $file    = get_attached_file( $attachment_id, true );
     3984                $dirname = pathinfo( $file, PATHINFO_DIRNAME );
     3985
     3986                $this->assertIsString( $file );
     3987                $this->assertFileExists( $file );
     3988
     3989                $metadata = wp_get_attachment_metadata( $attachment_id );
     3990
     3991                $this->assertFileExists(
     3992                        path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
     3993                );
     3994
     3995                wp_delete_attachment( $attachment_id, true );
     3996
     3997                $this->assertFileDoesNotExist(
     3998                        path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] )
     3999                );
     4000        }
     4001
     4002        /**
     4003         * @ticket 55443
     4004         */
     4005        public function test_it_should_avoid_the_change_of_urls_of_images_that_are_not_part_of_the_media_library() {
     4006                add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' );
     4007                $paragraph = '<p>Donec accumsan, sapien et <img src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>';
     4008
     4009                $this->assertSame( $paragraph, wp_filter_content_tags( $paragraph ) );
     4010        }
     4011
     4012        /**
     4013         * @ticket 55443
     4014         */
     4015        public function test_it_should_avoid_replacing_not_existing_attachment_i_ds() {
     4016                add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' );
     4017                $paragraph = '<p>Donec accumsan, sapien et <img class="wp-image-0" src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>';
     4018
     4019                $this->assertSame( $paragraph, wp_filter_content_tags( $paragraph ) );
     4020        }
     4021
     4022        /**
     4023         * @ticket 55443
     4024         */
     4025        public function test_it_should_test_preventing_replacing_a_webp_image() {
     4026                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     4027                        $this->markTestSkipped( 'This test requires WebP support.' );
     4028                }
     4029
     4030                $attachment_id = $this->factory->attachment->create_upload_object(
     4031                        DIR_TESTDATA . '/images/webp-lossy.webp'
     4032                );
     4033
     4034                $tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
     4035
     4036                $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', $attachment_id ) );
     4037        }
     4038
     4039        /**
     4040         * @ticket 55443
     4041         */
     4042        public function test_it_should_test_preventing_replacing_a_jpg_image_if_the_image_does_not_have_the_target_class_name() {
     4043                $attachment_id = $this->factory->attachment->create_upload_object(
     4044                        DIR_TESTDATA . '/images/canola.jpg'
     4045                );
     4046
     4047                $tag = wp_get_attachment_image( $attachment_id, 'medium' );
     4048
     4049                $this->assertSame( $tag, wp_filter_content_tags( $tag ) );
     4050        }
     4051
     4052        /**
     4053         * @ticket 55443
     4054         */
     4055        public function test_it_should_contain_the_full_image_size_from_the_original_mime() {
     4056                if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     4057                        $this->markTestSkipped( 'This test requires WebP support.' );
     4058                }
     4059
     4060                $attachment_id = $this->factory->attachment->create_upload_object(
     4061                        DIR_TESTDATA . '/images/test-image.jpg'
     4062                );
     4063
     4064                $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
     4065
     4066                $expected = array(
     4067                        'ext'  => 'jpg',
     4068                        'type' => 'image/jpeg',
     4069                );
     4070                $metadata = wp_get_attachment_metadata( $attachment_id );
     4071                $this->assertSame( $expected, wp_check_filetype( get_attached_file( $attachment_id ) ) );
     4072                $this->assertStringNotContainsString( wp_basename( get_attached_file( $attachment_id ) ), wp_image_use_alternate_mime_types( $tag, 'the_content', $attachment_id ) );
     4073                $this->assertStringContainsString( $metadata['sources']['image/webp']['file'], wp_image_use_alternate_mime_types( $tag, 'the_content', $attachment_id ) );
     4074        }
     4075
     4076        /**
     4077         * @ticket 55443
     4078         */
     4079        public function test_it_should_prevent_replacing_an_image_with_no_available_sources() {
     4080                add_filter( 'wp_upload_image_mime_transforms', '__return_empty_array' );
     4081
     4082                $attachment_id = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.jpg' );
     4083
     4084                $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
     4085                $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', $attachment_id ) );
     4086        }
     4087
     4088        /**
     4089         * @dataProvider provider_it_should_prevent_update_not_supported_images_with_no_available_sources
     4090         *
     4091         * @ticket 55443
     4092         */
     4093        public function test_it_should_prevent_update_not_supported_images_with_no_available_sources( $image_path ) {
     4094                $attachment_id = $this->factory->attachment->create_upload_object( $image_path );
     4095
     4096                $this->assertIsNumeric( $attachment_id );
     4097                $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) );
     4098
     4099                $this->assertSame( $tag, wp_image_use_alternate_mime_types( $tag, 'the_content', $attachment_id ) );
     4100        }
     4101
     4102        /**
     4103         * Data provider for it_should_prevent_update_not_supported_images_with_no_available_sources.
     4104         */
     4105        public function provider_it_should_prevent_update_not_supported_images_with_no_available_sources() {
     4106                yield 'PNG image' => array( DIR_TESTDATA . '/images/test-image.png' );
     4107                yield 'GIFT image' => array( DIR_TESTDATA . '/images/test-image.gif' );
     4108        }
    38004109}
    38014110
    38024111/**