Make WordPress Core

Changeset 54401


Ignore:
Timestamp:
10/07/2022 12:40:07 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Bring some consistency to WP_Image_Editor_GD and WP_Image_Editor_Imagick tests.

Includes:

  • Adjusting test method descriptions and comments per the documentation standards.
  • Creating image editor class instances directly, instead of calling wp_get_image_editor().
  • Cleaning up temporary files before performing assertions, where possible.
  • Using more consistent variable names for image editor class instances.
  • Reordering some test methods.

Follow-up to [1182/tests], [1188/tests], [27794], [30549], [30990], [31040], [39580], [40123], [49230], [49488], [49542], [49751].

See #55652.

Location:
trunk/tests/phpunit/tests/image
Files:
2 edited

Legend:

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

    r54226 r54401  
    5353
    5454        /**
    55          * Test resizing an image, not using crop
     55         * Tests resizing an image, not using crop.
    5656         *
    5757         * @requires function imagejpeg
     
    7575
    7676        /**
    77          * Test multi_resize with single image resize and no crop
     77         * Tests multi_resize() with single image resize and no crop.
    7878         *
    7979         * @requires function imagejpeg
     
    117117
    118118        /**
    119          * Ensure multi_resize doesn't create an image when
     119         * Tests that multi_resize() does not create an image when
    120120         * both height and weight are missing, null, or 0.
    121121         *
     
    186186
    187187        /**
    188          * Test multi_resize with multiple sizes
     188         * Tests multi_resize() with multiple sizes.
    189189         *
    190190         * @ticket 26823
     
    199199                $sizes_array = array(
    200200
    201                         /**
     201                        /*
    202202                         * #0 - 10x10 resize, no cropping.
    203203                         * By aspect, should be 10x6 output.
     
    209209                        ),
    210210
    211                         /**
     211                        /*
    212212                         * #1 - 75x50 resize, with cropping.
    213213                         * Output dimensions should be 75x50
     
    219219                        ),
    220220
    221                         /**
     221                        /*
    222222                         * #2 - 20 pixel max height, no cropping.
    223223                         * By aspect, should be 30x20 output.
     
    229229                        ),
    230230
    231                         /**
     231                        /*
    232232                         * #3 - 45 pixel max height, with cropping.
    233233                         * By aspect, should be 45x400 output.
     
    239239                        ),
    240240
    241                         /**
     241                        /*
    242242                         * #4 - 50 pixel max width, no cropping.
    243243                         * By aspect, should be 50x33 output.
     
    247247                        ),
    248248
    249                         /**
     249                        /*
    250250                         * #5 - 55 pixel max width, no cropping, null height
    251251                         * By aspect, should be 55x36 output.
     
    256256                        ),
    257257
    258                         /**
     258                        /*
    259259                         * #6 - 55 pixel max height, no cropping, no width specified.
    260260                         * By aspect, should be 82x55 output.
     
    264264                        ),
    265265
    266                         /**
     266                        /*
    267267                         * #7 - 60 pixel max height, no cropping, null width.
    268268                         * By aspect, should be 90x60 output.
     
    273273                        ),
    274274
    275                         /**
     275                        /*
    276276                         * #8 - 70 pixel max height, no cropping, negative width.
    277277                         * By aspect, should be 105x70 output.
     
    282282                        ),
    283283
    284                         /**
     284                        /*
    285285                         * #9 - 200 pixel max width, no cropping, negative height.
    286286                         * By aspect, should be 200x133 output.
     
    403403
    404404        /**
    405          * Test resizing an image with cropping
     405         * Tests resizing an image with cropping.
    406406         */
    407407        public function test_resize_and_crop() {
     
    423423
    424424        /**
    425          * Test cropping an image.
     425         * Tests cropping an image.
    426426         *
    427427         * @ticket 51937
     
    480480
    481481        /**
    482          * Test should return WP_Error when dimensions are not integer or are <= 0.
     482         * Tests that crop() returns WP_Error when dimensions are not integer or are <= 0.
    483483         *
    484484         * @ticket 51937
     
    546546
    547547        /**
    548          * Test rotating an image 180 deg
     548         * Tests rotating an image 180 deg.
    549549         */
    550550        public function test_rotate() {
     
    565565
    566566        /**
    567          * Test flipping an image
     567         * Tests flipping an image.
    568568         */
    569569        public function test_flip() {
     
    584584
    585585        /**
    586          * Test the image created with WP_Image_Editor_GD preserves alpha when resizing
     586         * Tests that an image created with WP_Image_Editor_GD preserves alpha with no resizing.
     587         *
     588         * @ticket 23039
     589         */
     590        public function test_image_preserves_alpha() {
     591                if ( ! ( imagetypes() & IMG_PNG ) ) {
     592                        $this->fail( 'This test requires PHP to be compiled with PNG support.' );
     593                }
     594
     595                $file = DIR_TESTDATA . '/images/transparent.png';
     596
     597                $gd_image_editor = new WP_Image_Editor_GD( $file );
     598                $gd_image_editor->load();
     599
     600                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
     601
     602                $gd_image_editor->save( $save_to_file );
     603
     604                $this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
     605
     606                unlink( $save_to_file );
     607        }
     608
     609        /**
     610         * Tests that an image created with WP_Image_Editor_GD preserves alpha when resizing.
    587611         *
    588612         * @ticket 23039
     
    595619                $file = DIR_TESTDATA . '/images/transparent.png';
    596620
    597                 $editor = wp_get_image_editor( $file );
    598 
    599                 $this->assertNotWPError( $editor );
    600 
    601                 $editor->load();
    602                 $editor->resize( 5, 5 );
     621                $gd_image_editor = new WP_Image_Editor_GD( $file );
     622                $gd_image_editor->load();
     623
     624                $gd_image_editor->resize( 5, 5 );
    603625                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    604626
    605                 $editor->save( $save_to_file );
    606 
    607                 $this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
    608 
    609                 unlink( $save_to_file );
    610         }
    611 
    612         /**
    613          * Test the image created with WP_Image_Editor_GD preserves alpha with no resizing etc
    614          *
    615          * @ticket 23039
    616          */
    617         public function test_image_preserves_alpha() {
    618                 if ( ! ( imagetypes() & IMG_PNG ) ) {
    619                         $this->fail( 'This test requires PHP to be compiled with PNG support.' );
    620                 }
    621 
    622                 $file = DIR_TESTDATA . '/images/transparent.png';
    623 
    624                 $editor = wp_get_image_editor( $file );
    625 
    626                 $this->assertNotWPError( $editor );
    627 
    628                 $editor->load();
    629 
    630                 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    631 
    632                 $editor->save( $save_to_file );
     627                $gd_image_editor->save( $save_to_file );
    633628
    634629                $this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), 127 );
     
    651646                $expected = imagecolorsforindex( $image, $rgb );
    652647
    653                 $editor = new WP_Image_Editor_GD( $file );
    654                                 $editor->load();
    655                                 $editor->rotate( 180 );
    656                                 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    657 
    658                                 $editor->save( $save_to_file );
    659 
    660                                 $this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), $expected['alpha'] );
    661                                 unlink( $save_to_file );
    662 
    663         }
    664 
    665         /**
    666          * Test WP_Image_Editor_GD handles extension-less images
     648                $gd_image_editor = new WP_Image_Editor_GD( $file );
     649                $gd_image_editor->load();
     650
     651                $gd_image_editor->rotate( 180 );
     652                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
     653
     654                $gd_image_editor->save( $save_to_file );
     655
     656                $this->assertImageAlphaAtPointGD( $save_to_file, array( 0, 0 ), $expected['alpha'] );
     657
     658                unlink( $save_to_file );
     659        }
     660
     661        /**
     662         * Tests that WP_Image_Editor_GD handles extensionless images.
    667663         *
    668664         * @ticket 39195
    669665         */
    670666        public function test_image_non_existent_extension() {
    671                 $image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/test-image-no-extension' );
    672                 $result       = $image_editor->load();
    673 
    674                 $this->assertTrue( $result );
     667                $gd_image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/test-image-no-extension' );
     668
     669                $loaded = $gd_image_editor->load();
     670
     671                $this->assertTrue( $loaded );
    675672        }
    676673}
  • trunk/tests/phpunit/tests/image/editorImagick.php

    r54226 r54401  
    3636
    3737        /**
    38          * Check support for ImageMagick compatible mime types.
     38         * Tests support for ImageMagick compatible mime types.
    3939         */
    4040        public function test_supports_mime_type() {
     
    4747
    4848        /**
    49          * Test resizing an image, not using crop
     49         * Tests resizing an image, not using crop.
    5050         */
    5151        public function test_resize() {
     
    6767
    6868        /**
    69          * Test multi_resize with single image resize and no crop
     69         * Tests multi_resize() with single image resize and no crop.
    7070         */
    7171        public function test_single_multi_resize() {
     
    107107
    108108        /**
    109          * Ensure multi_resize doesn't create an image when
     109         * Tests that multi_resize() does not create an image when
    110110         * both height and weight are missing, null, or 0.
    111111         *
     
    176176
    177177        /**
    178          * Test multi_resize with multiple sizes
     178         * Tests multi_resize() with multiple sizes.
    179179         *
    180180         * @ticket 26823
     
    188188                $sizes_array = array(
    189189
    190                         /**
     190                        /*
    191191                         * #0 - 10x10 resize, no cropping.
    192192                         * By aspect, should be 10x6 output.
     
    198198                        ),
    199199
    200                         /**
     200                        /*
    201201                         * #1 - 75x50 resize, with cropping.
    202202                         * Output dimensions should be 75x50
     
    208208                        ),
    209209
    210                         /**
     210                        /*
    211211                         * #2 - 20 pixel max height, no cropping.
    212212                         * By aspect, should be 30x20 output.
     
    218218                        ),
    219219
    220                         /**
     220                        /*
    221221                         * #3 - 45 pixel max height, with cropping.
    222222                         * By aspect, should be 45x400 output.
     
    228228                        ),
    229229
    230                         /**
     230                        /*
    231231                         * #4 - 50 pixel max width, no cropping.
    232232                         * By aspect, should be 50x33 output.
     
    236236                        ),
    237237
    238                         /**
     238                        /*
    239239                         * #5 - 55 pixel max width, no cropping, null height
    240240                         * By aspect, should be 55x36 output.
     
    245245                        ),
    246246
    247                         /**
     247                        /*
    248248                         * #6 - 55 pixel max height, no cropping, no width specified.
    249249                         * By aspect, should be 82x55 output.
     
    253253                        ),
    254254
    255                         /**
     255                        /*
    256256                         * #7 - 60 pixel max height, no cropping, null width.
    257257                         * By aspect, should be 90x60 output.
     
    262262                        ),
    263263
    264                         /**
     264                        /*
    265265                         * #8 - 70 pixel max height, no cropping, negative width.
    266266                         * By aspect, should be 105x70 output.
     
    271271                        ),
    272272
    273                         /**
     273                        /*
    274274                         * #9 - 200 pixel max width, no cropping, negative height.
    275275                         * By aspect, should be 200x133 output.
     
    392392
    393393        /**
    394          * Test resizing an image with cropping
     394         * Tests resizing an image with cropping.
    395395         */
    396396        public function test_resize_and_crop() {
     
    412412
    413413        /**
    414          * Test cropping an image
     414         * Tests cropping an image.
    415415         */
    416416        public function test_crop() {
     
    432432
    433433        /**
    434          * Test rotating an image 180 deg
     434         * Tests rotating an image 180 deg.
    435435         */
    436436        public function test_rotate() {
     
    451451
    452452        /**
    453          * Test flipping an image
     453         * Tests flipping an image.
    454454         */
    455455        public function test_flip() {
     
    470470
    471471        /**
    472          * Test the image created with WP_Image_Editor_Imagick preserves alpha when resizing
     472         * Tests that an image created with WP_Image_Editor_Imagick preserves alpha with no resizing.
    473473         *
    474474         * @ticket 24871
    475475         */
    476         public function test_image_preserves_alpha_on_resize() {
     476        public function test_image_preserves_alpha() {
    477477                $file = DIR_TESTDATA . '/images/transparent.png';
    478478
    479                 $editor = new WP_Image_Editor_Imagick( $file );
    480                 $editor->load();
    481                 $editor->resize( 5, 5 );
     479                $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     480                $imagick_image_editor->load();
     481
    482482                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    483483
    484                 $editor->save( $save_to_file );
     484                $imagick_image_editor->save( $save_to_file );
    485485
    486486                $im       = new Imagick( $save_to_file );
     
    494494
    495495        /**
    496          * Test the image created with WP_Image_Editor_Imagick preserves alpha with no resizing etc
     496         * Tests that an image created with WP_Image_Editor_Imagick preserves alpha when resizing.
    497497         *
    498498         * @ticket 24871
    499499         */
    500         public function test_image_preserves_alpha() {
     500        public function test_image_preserves_alpha_on_resize() {
    501501                $file = DIR_TESTDATA . '/images/transparent.png';
    502502
    503                 $editor = new WP_Image_Editor_Imagick( $file );
    504                 $editor->load();
    505 
     503                $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     504                $imagick_image_editor->load();
     505
     506                $imagick_image_editor->resize( 5, 5 );
    506507                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    507508
    508                 $editor->save( $save_to_file );
     509                $imagick_image_editor->save( $save_to_file );
    509510
    510511                $im       = new Imagick( $save_to_file );
     
    526527                $pre_rotate_pixel  = $pre_rotate_editor->getImagePixelColor( 0, 0 );
    527528                $pre_rotate_alpha  = $pre_rotate_pixel->getColorValue( imagick::COLOR_ALPHA );
    528                 $save_to_file      = tempnam( get_temp_dir(), '' ) . '.png';
     529
     530                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    529531                $pre_rotate_editor->writeImage( $save_to_file );
    530532                $pre_rotate_editor->destroy();
    531533
    532                 $image_editor = new WP_Image_Editor_Imagick( $save_to_file );
    533                 $image_editor->load();
    534                 $image_editor->rotate( 180 );
    535                 $image_editor->save( $save_to_file );
     534                $imagick_image_editor = new WP_Image_Editor_Imagick( $save_to_file );
     535                $imagick_image_editor->load();
     536
     537                $imagick_image_editor->rotate( 180 );
     538                $imagick_image_editor->save( $save_to_file );
    536539
    537540                $this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $pre_rotate_alpha );
     541
    538542                unlink( $save_to_file );
    539543        }
    540544
    541545        /**
    542          * Test WP_Image_Editor_Imagick handles extension-less images
     546         * Tests that WP_Image_Editor_Imagick handles extensionless images.
    543547         *
    544548         * @ticket 39195
    545549         */
    546550        public function test_image_non_existent_extension() {
    547                 $image_editor = new WP_Image_Editor_Imagick( DIR_TESTDATA . '/images/test-image-no-extension' );
    548                 $result       = $image_editor->load();
    549 
    550                 $this->assertTrue( $result );
    551         }
    552 
    553         /**
    554          * Test resetting Exif orientation data on rotate
     551                $imagick_image_editor = new WP_Image_Editor_Imagick( DIR_TESTDATA . '/images/test-image-no-extension' );
     552
     553                $loaded = $imagick_image_editor->load();
     554
     555                $this->assertTrue( $loaded );
     556        }
     557
     558        /**
     559         * Tests resetting Exif orientation data on rotate.
    555560         *
    556561         * @ticket 37140
     
    565570
    566571                $temp_file = wp_tempnam( $file );
    567                 $image     = wp_get_image_editor( $file );
     572
     573                $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     574                $imagick_image_editor->load();
    568575
    569576                // Test a value that would not lead back to 1, as WP is resetting the value to 1 manually.
    570                 $image->rotate( 90 );
    571                 $ret = $image->save( $temp_file, 'image/jpeg' );
    572 
    573                 $data = wp_read_image_metadata( $ret['path'] );
     577                $imagick_image_editor->rotate( 90 );
     578                $saved = $imagick_image_editor->save( $temp_file, 'image/jpeg' );
     579
     580                $data = wp_read_image_metadata( $saved['path'] );
     581
     582                // Remove both the generated file ending in .tmp and tmp.jpg due to wp_tempnam().
     583                unlink( $temp_file );
     584                unlink( $saved['path'] );
    574585
    575586                // Make sure the image is no longer in The Upside Down Exif orientation.
    576587                $this->assertSame( 1, (int) $data['orientation'], 'Orientation Exif data was not updated after rotating image: ' . $file );
    577 
    578                 // Remove both the generated file ending in .tmp and tmp.jpg due to wp_tempnam().
    579                 unlink( $temp_file );
    580                 unlink( $ret['path'] );
    581         }
    582 
    583         /**
    584          * Test that images can be loaded and written over streams
     588        }
     589
     590        /**
     591         * Tests that images can be loaded and written over streams.
    585592         */
    586593        public function test_streams() {
     
    595602                $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
    596603
    597                 $ret = $imagick_image_editor->load();
    598                 $this->assertNotWPError( $ret );
     604                $loaded = $imagick_image_editor->load();
     605                $this->assertNotWPError( $loaded );
    599606
    600607                $temp_file = 'wptest://Tests_Image_Editor_Imagick/write.jpg';
    601608
    602                 $ret = $imagick_image_editor->save( $temp_file );
    603                 $this->assertNotWPError( $ret );
    604 
    605                 $this->assertSame( $temp_file, $ret['path'] );
    606 
    607                 if ( $temp_file !== $ret['path'] ) {
    608                         unlink( $ret['path'] );
     609                $saved = $imagick_image_editor->save( $temp_file );
     610
     611                if ( $temp_file !== $saved['path'] ) {
     612                        unlink( $saved['path'] );
    609613                }
    610614                unlink( $temp_file );
     615
     616                $this->assertNotWPError( $saved );
     617                $this->assertSame( $temp_file, $saved['path'] );
    611618        }
    612619
     
    617624                $file      = realpath( DIR_TESTDATA ) . '/images/a2-small.jpg';
    618625                $directory = realpath( DIR_TESTDATA ) . '/images/nonexistent-directory';
    619                 $editor    = new WP_Image_Editor_Imagick( $file );
     626
     627                $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
    620628
    621629                $this->assertFileDoesNotExist( $directory );
    622630
    623                 $loaded = $editor->load();
     631                $loaded = $imagick_image_editor->load();
    624632                $this->assertNotWPError( $loaded );
    625633
    626                 $resized = $editor->resize( 100, 100, true );
     634                $resized = $imagick_image_editor->resize( 100, 100, true );
    627635                $this->assertNotWPError( $resized );
    628636
    629                 $saved = $editor->save( $directory . '/a2-small-cropped.jpg' );
    630                 $this->assertNotWPError( $saved );
     637                $saved = $imagick_image_editor->save( $directory . '/a2-small-cropped.jpg' );
    631638
    632639                unlink( $directory . '/a2-small-cropped.jpg' );
    633640                rmdir( $directory );
     641
     642                $this->assertNotWPError( $saved );
    634643        }
    635644}
Note: See TracChangeset for help on using the changeset viewer.