Make WordPress Core

Ticket #23039: wp-image-editor-gd-more.diff

File wp-image-editor-gd-more.diff, 3.2 KB (added by joehoyle, 13 years ago)

Added some more tests for WP_Image_Editor_GD

  • tests/image/editor_gd.php

     
    44 * Test the WP_Image_Editor_GD class
    55 * @group image
    66 * @group media
     7 * @group wp-image-editor-gd
    78 */
    89
    910class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
     
    1617        }
    1718
    1819        /**
     20         * Check support for GD compatible mime types.
     21         *
     22         */
     23        public function test_supports_mime_type() {
     24                $gd_image_editor = new WP_Image_Editor_GD( null );
     25
     26                $this->assertTrue( $gd_image_editor->supports_mime_type( 'image/jpeg' ), 'Does not support image/jpeg' );
     27                $this->assertTrue( $gd_image_editor->supports_mime_type( 'image/png' ), 'Does not support image/png' );
     28                $this->assertTrue( $gd_image_editor->supports_mime_type( 'image/gif' ), 'Does not support image/gif' );
     29        }
     30
     31        /**
     32         * Test resizing an image, not using crop
     33         *
     34         */
     35        public function test_resize() {
     36
     37                $file = DIR_TESTDATA . '/images/cropped-canola.jpg';
     38
     39                $gd_image_editor = new WP_Image_Editor_GD( $file );
     40                $gd_image_editor->load();
     41
     42                $gd_image_editor->resize( 100, 50 );
     43
     44                $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() );
     45        }
     46
     47        /**
     48         * Test resizing an image including cropping
     49         *
     50         */
     51        public function test_resize_and_crop() {
     52
     53                $file = DIR_TESTDATA . '/images/cropped-canola.jpg';
     54
     55                $gd_image_editor = new WP_Image_Editor_GD( $file );
     56                $gd_image_editor->load();
     57
     58                $gd_image_editor->resize( 100, 50, true );
     59
     60                $this->assertEquals( array( 'width' => 100, 'height' => 50 ), $gd_image_editor->get_size() );
     61        }
     62
     63        /**
     64         * Test cropping an image
     65         */
     66        public function test_crop() {
     67
     68                $file = DIR_TESTDATA . '/images/cropped-canola.jpg';
     69
     70                $gd_image_editor = new WP_Image_Editor_GD( $file );
     71                $gd_image_editor->load();
     72
     73                $gd_image_editor->crop( 0, 0, 50, 50 );
     74
     75                $this->assertEquals( array( 'width' => 50, 'height' => 50 ), $gd_image_editor->get_size() );
     76
     77        }
     78
     79        /**
     80         * Test rotating an image 180 deg
     81         */
     82        public function test_rotate() {
     83
     84                $file = DIR_TESTDATA . '/images/cropped-canola.jpg';
     85
     86                $gd_image_editor = new WP_Image_Editor_GD( $file );
     87                $gd_image_editor->load();
     88
     89                $property = new ReflectionProperty( $gd_image_editor, 'image' );
     90                $property->setAccessible( true );
     91
     92                $color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 );
     93
     94                $gd_image_editor->rotate( 180 );
     95
     96                $this->assertEquals( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 99, 99 ) );
     97        }
     98
     99        /**
     100         * Test flipping an image
     101         */
     102        public function test_flip() {
     103
     104                $file = DIR_TESTDATA . '/images/cropped-canola.jpg';
     105
     106                $gd_image_editor = new WP_Image_Editor_GD( $file );
     107                $gd_image_editor->load();
     108
     109                $property = new ReflectionProperty( $gd_image_editor, 'image' );
     110                $property->setAccessible( true );
     111
     112                $color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 );
     113
     114                $gd_image_editor->flip( true, false );
     115
     116                $this->assertEquals( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 0, 99 ) );
     117        }
     118
     119        /**
    19120         * Test the image created with WP_Image_Edior_GD preserves alpha when resizing
    20121         *
    21122         * @ticket 23039