Make WordPress Core

Changeset 161 in tests


Ignore:
Timestamp:
02/26/2008 01:02:16 PM (17 years ago)
Author:
tellyworth
Message:

image resize tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_image.php

    r158 r161  
    224224    }
    225225   
     226    function test_constrain_size_for_editor_thumb() {
     227        $out = image_constrain_size_for_editor(600, 400, 'thumb');
     228        $this->assertEquals(array(128, 85), $out);
     229       
     230        $out = image_constrain_size_for_editor(64, 64, 'thumb');
     231        $this->assertEquals(array(64, 64), $out);
     232    }
     233
     234    function test_constrain_size_for_editor_medium() {
     235        // default max width is 500, no constraint on height
     236        global $content_width;
     237        $content_width = 0;
     238       
     239        $out = image_constrain_size_for_editor(600, 400, 'medium');
     240        $this->assertEquals(array(500, 333), $out);
     241
     242        $out = image_constrain_size_for_editor(400, 600, 'medium');
     243        $this->assertEquals(array(400, 600), $out);
     244       
     245        $out = image_constrain_size_for_editor(64, 64, 'medium');
     246        $this->assertEquals(array(64, 64), $out);
     247       
     248        $content_width = 350;
     249        $out = image_constrain_size_for_editor(600, 400, 'medium');
     250        $this->assertEquals(array(350, 233), $out);
     251
     252    }
     253
     254    function test_constrain_size_for_editor_full() {
     255        global $content_width;
     256        $content_width = 500;
     257        $out = image_constrain_size_for_editor(600, 400, 'full');
     258        $this->assertEquals(array(600, 400), $out);
     259       
     260        $out = image_constrain_size_for_editor(64, 64, 'full');
     261        $this->assertEquals(array(64, 64), $out);
     262    }
     263
     264   
    226265}
    227266
     267class TestIsImageFunctions extends WPTestCase {
     268    function test_is_image_positive() {
     269    }
     270}
     271
     272class TestImageResizeDimensions extends WPTestCase {
     273    function test_400x400_no_crop() {
     274        // landscape: resize 640x480 to fit 400x400: 400x300
     275        $out = image_resize_dimensions(640, 480, 400, 400, false);
     276        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     277        $this->assertEquals( array(0, 0, 0, 0, 400, 300, 640, 480), $out );
     278
     279        // portrait: resize 480x640 to fit 400x400: 300x400
     280        $out = image_resize_dimensions(480, 640, 400, 400, false);
     281        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     282        $this->assertEquals( array(0, 0, 0, 0, 300, 400, 480, 640), $out );
     283    }
     284   
     285    function test_400x0_no_crop() {
     286        // landscape: resize 640x480 to fit 400w: 400x300
     287        $out = image_resize_dimensions(640, 480, 400, 0, false);
     288        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     289        $this->assertEquals( array(0, 0, 0, 0, 400, 300, 640, 480), $out );
     290
     291        // portrait: resize 480x640 to fit 400w: 400x533
     292        $out = image_resize_dimensions(480, 640, 400, 0, false);
     293        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     294        $this->assertEquals( array(0, 0, 0, 0, 400, 533, 480, 640), $out );
     295    }
     296
     297    function test_0x400_no_crop() {
     298        // landscape: resize 640x480 to fit 400h: 533x400
     299        $out = image_resize_dimensions(640, 480, 0, 400, false);
     300        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     301        $this->assertEquals( array(0, 0, 0, 0, 533, 400, 640, 480), $out );
     302
     303        // portrait: resize 480x640 to fit 400h: 300x400
     304        $out = image_resize_dimensions(480, 640, 0, 400, false);
     305        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     306        $this->assertEquals( array(0, 0, 0, 0, 300, 400, 480, 640), $out );
     307    }
     308
     309    function test_800x800_no_crop() {
     310        // landscape: resize 640x480 to fit 800x800
     311        $out = image_resize_dimensions(640, 480, 800, 800, false);
     312        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     313        $this->assertEquals( false, $out );
     314
     315        // portrait: resize 480x640 to fit 800x800
     316        $out = image_resize_dimensions(480, 640, 800, 800, false);
     317        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     318        $this->assertEquals( false, $out );
     319    }
     320
     321    function test_800x0_no_crop() {
     322        // landscape: resize 640x480 to fit 800w
     323        $out = image_resize_dimensions(640, 480, 800, 0, false);
     324        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     325        $this->assertEquals( false, $out );
     326
     327        // portrait: resize 480x640 to fit 800w
     328        $out = image_resize_dimensions(480, 640, 800, 0, false);
     329        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     330        $this->assertEquals( false, $out );
     331    }
     332
     333    function test_0x800_no_crop() {
     334        // landscape: resize 640x480 to fit 800h
     335        $out = image_resize_dimensions(640, 480, 0, 800, false);
     336        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     337        $this->assertEquals( false, $out );
     338
     339        // portrait: resize 480x640 to fit 800h
     340        $out = image_resize_dimensions(480, 640, 0, 800, false);
     341        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     342        $this->assertEquals( false, $out );
     343    }
     344
     345    // cropped versions
     346
     347    function test_400x400_crop() {
     348        // landscape: crop 640x480 to fit 400x400: 400x400 taken from a 480x480 crop at (80. 0)
     349        $out = image_resize_dimensions(640, 480, 400, 400, true);
     350        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     351        $this->assertEquals( array(0, 0, 80, 0, 400, 400, 480, 480), $out );
     352
     353        // portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop at (0. 80)
     354        $out = image_resize_dimensions(480, 640, 400, 400, true);
     355        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     356        $this->assertEquals( array(0, 0, 0, 80, 400, 400, 480, 480), $out );
     357    }
     358   
     359    function test_400x0_crop() {
     360        // landscape: resize 640x480 to fit 400w: 400x300
     361        $out = image_resize_dimensions(640, 480, 400, 0, true);
     362        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     363        $this->assertEquals( array(0, 0, 0, 0, 400, 300, 640, 480), $out );
     364
     365        // portrait: resize 480x640 to fit 400w: 400x533
     366        $out = image_resize_dimensions(480, 640, 400, 0, true);
     367        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     368        $this->assertEquals( array(0, 0, 0, 0, 400, 533, 480, 640), $out );
     369    }
     370
     371    function test_0x400_crop() {
     372        // landscape: resize 640x480 to fit 400h: 533x400
     373        $out = image_resize_dimensions(640, 480, 0, 400, true);
     374        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     375        $this->assertEquals( array(0, 0, 0, 0, 533, 400, 640, 480), $out );
     376
     377        // portrait: resize 480x640 to fit 400h: 300x400
     378        $out = image_resize_dimensions(480, 640, 0, 400, true);
     379        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     380        $this->assertEquals( array(0, 0, 0, 0, 300, 400, 480, 640), $out );
     381    }
     382   
     383    function test_400x500_crop() {
     384        // landscape: crop 640x480 to fit 400x500: 400x400 taken from a 480x480 crop at (80. 0)
     385        $out = image_resize_dimensions(640, 480, 400, 500, true);
     386        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     387        $this->assertEquals( array(0, 0, 120, 0, 400, 480, 400, 480), $out );
     388
     389        // portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop at (0. 80)
     390        $out = image_resize_dimensions(480, 640, 400, 500, true);
     391        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     392        $this->assertEquals( array(0, 0, 0, 20, 400, 500, 480, 600), $out );
     393    }
     394   
     395}
     396
     397class TestImageResize extends WPTestCase {
     398    // image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=75)
     399
     400    function test_resize_jpg() {
     401        $image = image_resize( DIR_TESTDATA.'/images/test-image.jpg', 25, 25 );
     402        $this->assertEquals( 'test-image-25x25.jpg', basename($image) );
     403        list($w, $h, $type) = getimagesize($image);
     404        $this->assertEquals( 25, $w );
     405        $this->assertEquals( 25, $h );
     406        $this->assertEquals( IMAGETYPE_JPEG, $type );
     407        unlink($image);
     408    }
     409
     410    function test_resize_png() {
     411        $image = image_resize( DIR_TESTDATA.'/images/test-image.png', 25, 25 );
     412        $this->assertEquals( 'test-image-25x25.png', basename($image) );
     413        list($w, $h, $type) = getimagesize($image);
     414        $this->assertEquals( 25, $w );
     415        $this->assertEquals( 25, $h );
     416        $this->assertEquals( IMAGETYPE_PNG, $type );
     417        unlink($image);
     418    }
     419
     420    function test_resize_gif() {
     421        $image = image_resize( DIR_TESTDATA.'/images/test-image.gif', 25, 25 );
     422        $this->assertEquals( 'test-image-25x25.gif', basename($image) );
     423        list($w, $h, $type) = getimagesize($image);
     424        $this->assertEquals( 25, $w );
     425        $this->assertEquals( 25, $h );
     426        $this->assertEquals( IMAGETYPE_GIF, $type );
     427        unlink($image);
     428    }
     429
     430    function test_resize_larger() {
     431        // image_resize() should refuse to make an image larger
     432        $image = image_resize( DIR_TESTDATA.'/images/test-image.jpg', 100, 100 );
     433        $this->assertEquals( false, $image );
     434    }
     435   
     436    function test_resize_thumb_128x96() {
     437        $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 96 );
     438        $this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
     439        list($w, $h, $type) = getimagesize($image);
     440        $this->assertEquals( 63, $w );
     441        $this->assertEquals( 96, $h );
     442        $this->assertEquals( IMAGETYPE_JPEG, $type );
     443        // is this a valid test? do different systems always generate the same file?
     444        $this->assertEquals( 'be8f7aaa7b939970a3ded069a6e3619b', md5_file($image) );
     445        unlink($image);
     446    }
     447
     448    function test_resize_thumb_128x0() {
     449        $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 128, 0 );
     450        $this->assertEquals( '2007-06-17DSC_4173-128x192.jpg', basename($image) );
     451        list($w, $h, $type) = getimagesize($image);
     452        $this->assertEquals( 128, $w );
     453        $this->assertEquals( 192, $h );
     454        $this->assertEquals( IMAGETYPE_JPEG, $type );
     455        // is this a valid test? do different systems always generate the same file?
     456        $this->assertEquals( '7b1c0d5e7d4f6a18ae7541c8abf1fd09', md5_file($image) );
     457        unlink($image);
     458    }
     459
     460    function test_resize_thumb_0x96() {
     461        $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 0, 96 );
     462        $this->assertEquals( '2007-06-17DSC_4173-63x96.jpg', basename($image) );
     463        list($w, $h, $type) = getimagesize($image);
     464        $this->assertEquals( 63, $w );
     465        $this->assertEquals( 96, $h );
     466        $this->assertEquals( IMAGETYPE_JPEG, $type );
     467        // is this a valid test? do different systems always generate the same file?
     468        $this->assertEquals( 'be8f7aaa7b939970a3ded069a6e3619b', md5_file($image) );
     469        unlink($image);
     470    }
     471
     472    function test_resize_thumb_150x150_crop() {
     473        $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 150, true );
     474        $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename($image) );
     475        list($w, $h, $type) = getimagesize($image);
     476        $this->assertEquals( 150, $w );
     477        $this->assertEquals( 150, $h );
     478        $this->assertEquals( IMAGETYPE_JPEG, $type );
     479        // is this a valid test? do different systems always generate the same file?
     480        $this->assertEquals( '9fdcf728e1d43da89edf866d009ca7e8', md5_file($image) );
     481        unlink($image);
     482    }
     483
     484    function test_resize_thumb_150x100_crop() {
     485        $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 150, 100, true );
     486        $this->assertEquals( '2007-06-17DSC_4173-150x100.jpg', basename($image) );
     487        list($w, $h, $type) = getimagesize($image);
     488        $this->assertEquals( 150, $w );
     489        $this->assertEquals( 100, $h );
     490        $this->assertEquals( IMAGETYPE_JPEG, $type );
     491        // is this a valid test? do different systems always generate the same file?
     492        $this->assertEquals( '2eeb18856505ab946074d90babc46452', md5_file($image) );
     493        unlink($image);
     494    }
     495
     496    function test_resize_thumb_50x150_crop() {
     497        $image = image_resize( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG', 50, 150, true );
     498        $this->assertEquals( '2007-06-17DSC_4173-50x150.jpg', basename($image) );
     499        list($w, $h, $type) = getimagesize($image);
     500        $this->assertEquals( 50, $w );
     501        $this->assertEquals( 150, $h );
     502        $this->assertEquals( IMAGETYPE_JPEG, $type );
     503        // is this a valid test? do different systems always generate the same file?
     504        $this->assertEquals( '94ed8e8463475774a5c5ff66118d3127', md5_file($image) );
     505        unlink($image);
     506    }
     507
     508}
     509
    228510?>
Note: See TracChangeset for help on using the changeset viewer.