WordPress.org

Make WordPress Core

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

File wp-image-editor-gd-tests.diff, 2.3 KB (added by joehoyle, 6 months ago)
  • tests/image/base.php

     
    3030        public function setEngine( $editors ) { 
    3131                return array( $this->editor_engine ); 
    3232        } 
     33 
     34        /** 
     35         * Helper assertion for testing alpha on images 
     36         *  
     37         * @param  string $image_path 
     38         * @param  array $point      array(x,y) 
     39         * @param  int $alpha 
     40         */ 
     41        protected function assertImageAlphaAtPoint( $image_path, $point, $alpha ) { 
     42 
     43                $im = imagecreatefrompng( $image_path ); 
     44                $rgb = imagecolorat($im, $point[0], $point[1]); 
     45 
     46                $colors = imagecolorsforindex($im, $rgb); 
     47 
     48                $this->assertEquals( $alpha, $colors['alpha'] ); 
     49        } 
    3350} 
  • tests/image/editor_gd.php

     
     1<?php 
     2 
     3/** 
     4 * Test the WP_Image_Editor_GD class 
     5 * @group image 
     6 * @group media 
     7 */ 
     8 
     9/** 
     10 * @todo how does one run this test on it's own without including the parent class? 
     11 */ 
     12require_once( 'base.php' ); 
     13 
     14class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
     15        public $editor_engine = 'WP_Image_Editor_GD'; 
     16 
     17        public function setup() { 
     18                require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' ); 
     19                require_once( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' ); 
     20                parent::setUp(); 
     21        } 
     22 
     23        public function test_image_preserves_alpha_on_resize() { 
     24 
     25                $file = DIR_TESTDATA . '/images/transparent.png'; 
     26 
     27                $editor = wp_get_image_editor( $file ); 
     28                $editor->load(); 
     29                $editor->resize(5,5); 
     30                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; 
     31                 
     32                $editor->save( $save_to_file ); 
     33 
     34                $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); 
     35 
     36        } 
     37         
     38        /** 
     39         * Test the image created with WP_Image_Edior_GD preserves alpha with no resizing etc 
     40         *  
     41         *  
     42         */ 
     43        public function test_image_preserves_alpha() { 
     44 
     45                $file = DIR_TESTDATA . '/images/transparent.png'; 
     46 
     47                $editor = wp_get_image_editor( $file ); 
     48                $editor->load(); 
     49 
     50                $save_to_file = tempnam( get_temp_dir(), '' ) . '.png'; 
     51                 
     52                $editor->save( $save_to_file ); 
     53 
     54                $this->assertImageAlphaAtPoint( $save_to_file, array( 0,0 ), 127 ); 
     55        } 
     56 
     57}