Make WordPress Core


Ignore:
Timestamp:
10/07/2022 12:40:07 AM (2 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.