Make WordPress Core


Ignore:
Timestamp:
03/27/2014 08:39:08 PM (11 years ago)
Author:
wonderboymusic
Message:

In multi_resize() image editor methods, assert that null can only be passed for one of the arguments, not both. Add a lot more unit test assertions to ensure this.

Props pbearne, DH-Shredder.
Fixes #26823.

File:
1 edited

Legend:

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

    r25002 r27794  
    1212    public $editor_engine = 'WP_Image_Editor_Imagick';
    1313
    14     public function setup() {
     14    public function setUp() {
    1515        require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );
    1616        require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );
    1717
    18         $editor = new WP_Image_Editor_Imagick( null );
    19 
    20         if ( ! $editor->test() )
    21             $this->markTestSkipped( 'Image Magick not available' );
    22 
    2318        parent::setUp();
    2419    }
    2520
    26     /**
    27      * Check support for Image Magick compatible mime types.
    28      *
     21    public function shutDown() {
     22        $folder = DIR_TESTDATA . '/images/waffles-*.jpg';
     23
     24        foreach ( glob( $folder ) as $file ) {
     25            unlink( $file );
     26        }
     27
     28        parent::shutDown();
     29    }
     30
     31    /**
     32     * Check support for ImageMagick compatible mime types.
    2933     */
    3034    public function test_supports_mime_type() {
    31 
    3235        $imagick_image_editor = new WP_Image_Editor_Imagick( null );
    3336
     
    3942    /**
    4043     * Test resizing an image, not using crop
    41      *
    4244     */
    4345    public function test_resize() {
    44 
     46        $file = DIR_TESTDATA . '/images/waffles.jpg';
     47
     48        $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     49        $imagick_image_editor->load();
     50
     51        $imagick_image_editor->resize( 100, 50 );
     52
     53        $this->assertEquals(
     54            array(
     55                'width'  => 75,
     56                'height' => 50,
     57            ),
     58            $imagick_image_editor->get_size()
     59        );
     60    }
     61
     62    /**
     63     * Test multi_resize with single image resize and no crop
     64     */
     65    public function test_single_multi_resize() {
     66        $file = DIR_TESTDATA . '/images/waffles.jpg';
     67
     68        $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     69        $imagick_image_editor->load();
     70
     71        $sizes_array =  array(
     72            array(
     73                'width'  => 50,
     74                'height' => 50,
     75            ),
     76        );
     77
     78        $resized = $imagick_image_editor->multi_resize( $sizes_array );
     79
     80        # First, check to see if returned array is as expected
     81        $expected_array = array(
     82            array(
     83                'file'      => 'waffles-50x33.jpg',
     84                'width'     => 50,
     85                'height'    => 33,
     86                'mime-type' => 'image/jpeg',
     87            ),
     88        );
     89
     90        $this->assertEquals( $expected_array, $resized );
     91
     92        // Now, verify real dimensions are as expected
     93        $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file'];
     94        $this->assertImageDimensions(
     95            $image_path,
     96            $expected_array[0]['width'],
     97            $expected_array[0]['height']
     98        );
     99    }
     100
     101    /**
     102     * Ensure multi_resize doesn't create an image when
     103     * both height and weight are missing, null, or 0.
     104     *
     105     * ticket 26823
     106     */
     107    public function test_multi_resize_does_not_create() {
     108        $file = DIR_TESTDATA . '/images/waffles.jpg';
     109
     110        $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     111        $imagick_image_editor->load();
     112
     113        $sizes_array = array(
     114            array(
     115                'width'  => 0,
     116                'height' => 0,
     117            ),
     118            array(
     119                'width'  => 0,
     120                'height' => 0,
     121                'crop'   => true,
     122            ),
     123            array(
     124                'width'  => null,
     125                'height' => null,
     126            ),
     127            array(
     128                'width'  => null,
     129                'height' => null,
     130                'crop'   => true,
     131            ),
     132            array(
     133                'width'  => '',
     134                'height' => '',
     135            ),
     136            array(
     137                'width'  => '',
     138                'height' => '',
     139                'crop'   => true,
     140            ),
     141            array(
     142                'width'  => 0,
     143            ),
     144            array(
     145                'width'  => 0,
     146                'crop'   => true,
     147            ),
     148            array(
     149                'width'  => null,
     150            ),
     151            array(
     152                'width'  => null,
     153                'crop'   => true,
     154            ),
     155            array(
     156                'width'  => '',
     157            ),
     158            array(
     159                'width'  => '',
     160                'crop'   => true,
     161            ),
     162        );
     163
     164        $resized = $imagick_image_editor->multi_resize( $sizes_array );
     165
     166        // If no images are generated, the returned array is empty.
     167        $this->assertEmpty( $resized );
     168    }
     169
     170    /**
     171     * Test multi_resize with multiple sizes
     172     *
     173     * ticket 26823
     174     */
     175    public function test_multi_resize() {
     176        $file = DIR_TESTDATA . '/images/waffles.jpg';
     177
     178        $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     179        $imagick_image_editor->load();
     180
     181        $sizes_array = array(
     182
     183            /**
     184             * #0 - 10x10 resize, no cropping.
     185             * By aspect, should be 10x6 output.
     186             */
     187            array(
     188                'width'  => 10,
     189                'height' => 10,
     190                'crop'   => false,
     191            ),
     192
     193            /**
     194             * #1 - 75x50 resize, with cropping.
     195             * Output dimensions should be 75x50
     196             */
     197            array(
     198                'width'  => 75,
     199                'height' => 50,
     200                'crop'   => true,
     201            ),
     202
     203            /**
     204             * #2 - 20 pixel max height, no cropping.
     205             * By aspect, should be 30x20 output.
     206             */
     207            array(
     208                'width'  => 9999, # Arbitrary High Value
     209                'height' => 20,
     210                'crop'   => false,
     211            ),
     212
     213            /**
     214             * #3 - 45 pixel max height, with cropping.
     215             * By aspect, should be 45x400 output.
     216             */
     217            array(
     218                'width'  => 45,
     219                'height' => 9999, # Arbitrary High Value
     220                'crop'   => true,
     221            ),
     222
     223            /**
     224             * #4 - 50 pixel max width, no cropping.
     225             * By aspect, should be 50x33 output.
     226             */
     227            array(
     228                'width' => 50,
     229            ),
     230
     231            /**
     232             * #5 - 55 pixel max width, no cropping, null height
     233             * By aspect, should be 55x36 output.
     234             */
     235            array(
     236                'width'  => 55,
     237                'height' => null,
     238            ),
     239
     240            /**
     241             * #6 - 55 pixel max height, no cropping, no width specified.
     242             * By aspect, should be 82x55 output.
     243             */
     244            array(
     245                'height' => 55,
     246            ),
     247
     248            /**
     249             * #7 - 60 pixel max height, no cropping, null width.
     250             * By aspect, should be 90x60 output.
     251             */
     252            array(
     253                'width'  => null,
     254                'height' => 60,
     255            ),
     256
     257            /**
     258             * #8 - 70 pixel max height, no cropping, negative width.
     259             * By aspect, should be 105x70 output.
     260             */
     261            array(
     262                'width'  => -9999, # Arbitrary Negative Value
     263                'height' => 70,
     264            ),
     265
     266            /**
     267             * #9 - 200 pixel max width, no cropping, negative height.
     268             * By aspect, should be 200x133 output.
     269             */
     270            array(
     271                'width'  => 200,
     272                'height' => -9999, # Arbitrary Negative Value
     273            ),
     274        );
     275
     276        $resized = $imagick_image_editor->multi_resize( $sizes_array );
     277
     278        $expected_array = array(
     279
     280            // #0
     281            array(
     282                'file'      => 'waffles-10x6.jpg',
     283                'width'     => 10,
     284                'height'    => 6,
     285                'mime-type' => 'image/jpeg',
     286            ),
     287
     288            // #1
     289            array(
     290                'file'      => 'waffles-75x50.jpg',
     291                'width'     => 75,
     292                'height'    => 50,
     293                'mime-type' => 'image/jpeg',
     294            ),
     295
     296            // #2
     297            array(
     298                'file'      => 'waffles-30x20.jpg',
     299                'width'     => 30,
     300                'height'    => 20,
     301                'mime-type' => 'image/jpeg',
     302            ),
     303
     304            // #3
     305            array(
     306                'file'      => 'waffles-45x400.jpg',
     307                'width'     => 45,
     308                'height'    => 400,
     309                'mime-type' => 'image/jpeg',
     310            ),
     311
     312            // #4
     313            array(
     314                'file'      => 'waffles-50x33.jpg',
     315                'width'     => 50,
     316                'height'    => 33,
     317                'mime-type' => 'image/jpeg',
     318            ),
     319
     320            // #5
     321            array(
     322                'file'      => 'waffles-55x36.jpg',
     323                'width'     => 55,
     324                'height'    => 36,
     325                'mime-type' => 'image/jpeg',
     326            ),
     327
     328            // #6
     329            array(
     330                'file'      => 'waffles-82x55.jpg',
     331                'width'     => 82,
     332                'height'    => 55,
     333                'mime-type' => 'image/jpeg',
     334            ),
     335
     336            // #7
     337            array(
     338                'file'      => 'waffles-90x60.jpg',
     339                'width'     => 90,
     340                'height'    => 60,
     341                'mime-type' => 'image/jpeg',
     342            ),
     343
     344            // #8
     345            array(
     346                'file'      => 'waffles-105x70.jpg',
     347                'width'     => 105,
     348                'height'    => 70,
     349                'mime-type' => 'image/jpeg',
     350            ),
     351
     352            // #9
     353            array(
     354                'file'      => 'waffles-200x133.jpg',
     355                'width'     => 200,
     356                'height'    => 133,
     357                'mime-type' => 'image/jpeg',
     358            ),
     359        );
     360
     361        $this->assertNotNull( $resized );
     362        $this->assertEquals( $expected_array, $resized );
     363
     364        foreach( $resized as $key => $image_data ){
     365            $image_path = DIR_TESTDATA . '/images/' . $image_data['file'];
     366
     367            // Now, verify real dimensions are as expected
     368            $this->assertImageDimensions(
     369                $image_path,
     370                $expected_array[$key]['width'],
     371                $expected_array[$key]['height']
     372            );
     373        }
     374    }
     375
     376    /**
     377     * Test resizing an image with cropping
     378     */
     379    public function test_resize_and_crop() {
     380        $file = DIR_TESTDATA . '/images/waffles.jpg';
     381
     382        $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     383        $imagick_image_editor->load();
     384
     385        $imagick_image_editor->resize( 100, 50, true );
     386
     387        $this->assertEquals(
     388            array(
     389                'width'  => 100,
     390                'height' => 50,
     391            ),
     392            $imagick_image_editor->get_size()
     393        );
     394    }
     395
     396    /**
     397     * Test cropping an image
     398     */
     399    public function test_crop() {
    45400        $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    46401
     
    48403        $imagick_image_editor->load();
    49404
    50         $imagick_image_editor->resize( 100, 50 );
    51 
    52         $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() );
    53     }
    54 
    55     /**
    56      * Test resizing an image including cropping
    57      *
    58      */
    59     public function test_resize_and_crop() {
    60 
    61         $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    62 
    63         $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
    64         $imagick_image_editor->load();
    65 
    66         $imagick_image_editor->resize( 100, 50, true );
    67 
    68         $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $imagick_image_editor->get_size() );
    69     }
    70 
    71     /**
    72      * Test cropping an image
    73      */
    74     public function test_crop() {
    75 
    76         $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    77 
    78         $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
    79         $imagick_image_editor->load();
    80 
    81405        $imagick_image_editor->crop( 0, 0, 50, 50 );
    82406
    83         $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $imagick_image_editor->get_size() );
    84 
     407        $this->assertEquals(
     408            array(
     409                'width' => 50,
     410                'height' => 50,
     411            ),
     412            $imagick_image_editor->get_size()
     413        );
    85414    }
    86415
     
    89418     */
    90419    public function test_rotate() {
    91 
    92420        $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    93421
     
    109437     */
    110438    public function test_flip() {
    111 
    112439        $file = DIR_TESTDATA . '/images/gradient-square.jpg';
    113440
     
    126453
    127454    /**
    128      * Test the image created with WP_Image_Edior_Imagick preserves alpha when resizing
     455     * Test the image created with WP_Image_Editor_Imagick preserves alpha when resizing
    129456     *
    130457     * @ticket 24871
    131458     */
    132459    public function test_image_preserves_alpha_on_resize() {
    133 
    134460        $file = DIR_TESTDATA . '/images/transparent.png';
    135461
    136462        $editor = wp_get_image_editor( $file );
    137463        $editor->load();
    138         $editor->resize(5,5);
     464        $editor->resize( 5, 5 );
    139465        $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    140466       
     
    142468
    143469        $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 );
    144 
    145     }
    146    
    147     /**
    148      * Test the image created with WP_Image_Edior_Imagick preserves alpha with no resizing etc
     470    }
     471
     472    /**
     473     * Test the image created with WP_Image_Editor_Imagick preserves alpha with no resizing etc
    149474     *
    150475     * @ticket 24871
    151476     */
    152477    public function test_image_preserves_alpha() {
    153 
    154478        $file = DIR_TESTDATA . '/images/transparent.png';
    155479
     
    158482
    159483        $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';
    160        
     484
    161485        $editor->save( $save_to_file );
    162486
Note: See TracChangeset for help on using the changeset viewer.