Make WordPress Core

Changeset 53541


Ignore:
Timestamp:
06/20/2022 10:04:24 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Further improve Tests_Image_Functions::test_wp_crop_image*() tests.

Includes:

  • Making the test method names more specific.
  • Converting one-off helper methods to static closures.
  • Adding a failure message to each assertion when multiple assertions are used in the test.

Follow-up to [1126/tests], [1201/tests], [53292], [53495], [53522], [53538].

See #55652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/image/functions.php

    r53538 r53541  
    484484
    485485        /**
    486          * @ticket 55403
    487          * @covers ::wp_crop_image
    488          */
    489         public function test_wp_crop_image_with_filtered_extension() {
    490                 add_filter( 'image_editor_output_format', array( $this, 'filter_image_editor_output_format' ) );
    491                 $file = wp_crop_image(
    492                         DIR_TESTDATA . '/images/canola.jpg',
    493                         0,
    494                         0,
    495                         100,
    496                         100,
    497                         100,
    498                         100
    499                 );
    500 
    501                 $this->assertNotWPError( $file );
    502                 $this->assertFileExists( $file );
    503 
    504                 unlink( $file );
    505         }
    506 
    507         public function filter_image_editor_output_format() {
    508                 return array_fill_keys( array( 'image/jpg', 'image/jpeg', 'image/png' ), 'image/webp' );
    509         }
    510 
    511         /**
    512486         * @covers ::wp_crop_image
    513487         * @requires function imagejpeg
    514488         */
    515         public function test_wp_crop_image_file() {
     489        public function test_wp_crop_image_with_file() {
    516490                $file = wp_crop_image(
    517491                        DIR_TESTDATA . '/images/canola.jpg',
    … …  
    540514         * @requires extension openssl
    541515         */
    542         public function test_wp_crop_image_url() {
     516        public function test_wp_crop_image_with_url() {
    543517                $file = wp_crop_image(
    544518                        'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg',
    … …  
    572546         * @covers ::wp_crop_image
    573547         */
    574         public function test_wp_crop_image_file_not_exists() {
     548        public function test_wp_crop_image_should_fail_with_wp_error_object_if_file_does_not_exists() {
    575549                $file = wp_crop_image(
    576550                        DIR_TESTDATA . '/images/canoladoesnotexist.jpg',
    … …  
    589563         * @requires extension openssl
    590564         */
    591         public function test_wp_crop_image_url_not_exists() {
     565        public function test_wp_crop_image_should_fail_with_wp_error_object_if_url_does_not_exist() {
    592566                $file = wp_crop_image(
    593567                        'https://asdftestblog1.files.wordpress.com/2008/04/canoladoesnotexist.jpg',
    … …  
    606580         * @covers ::wp_crop_image
    607581         */
    608         public function test_wp_crop_image_error_on_saving() {
     582        public function test_wp_crop_image_should_fail_with_wp_error_object_if_there_was_an_error_on_saving() {
    609583                WP_Image_Editor_Mock::$save_return = new WP_Error();
    610                 add_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) );
     584
     585                add_filter(
     586                        'wp_image_editors',
     587                        static function( $editors ) {
     588                                return array( 'WP_Image_Editor_Mock' );
     589                        }
     590                );
    611591
    612592                $file = wp_crop_image(
    … …  
    621601                $this->assertInstanceOf( 'WP_Error', $file );
    622602
    623                 remove_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) );
    624603                WP_Image_Editor_Mock::$save_return = array();
    625604        }
    626605
    627606        /**
    628          * @see test_wp_crop_image_error_on_saving()
    629          */
    630         public function mock_image_editor( $editors ) {
    631                 return array( 'WP_Image_Editor_Mock' );
     607         * @ticket 55403
     608         * @covers ::wp_crop_image
     609         */
     610        public function test_wp_crop_image_should_return_correct_file_extension_if_output_format_was_modified() {
     611                add_filter(
     612                        'image_editor_output_format',
     613                        static function() {
     614                                return array_fill_keys( array( 'image/jpg', 'image/jpeg', 'image/png' ), 'image/webp' );
     615                        }
     616                );
     617
     618                $file = wp_crop_image(
     619                        DIR_TESTDATA . '/images/canola.jpg',
     620                        0,
     621                        0,
     622                        100,
     623                        100,
     624                        100,
     625                        100
     626                );
     627
     628                $this->assertNotWPError( $file, 'Cropping the image resulted in a WP_Error.' );
     629                $this->assertFileExists( $file, "The file $file does not exist." );
     630
     631                unlink( $file );
    632632        }
    633633
Note: See TracChangeset for help on using the changeset viewer.