Ticket #6821: 6821-unit-tests-4.2.patch
File 6821-unit-tests-4.2.patch, 20.5 KB (added by , 9 years ago) |
---|
-
includes/mock-image-editor.php
1 <?php 2 3 if (class_exists( 'WP_Image_Editor' ) ) : 4 5 class WP_Image_Editor_Mock extends WP_Image_Editor { 6 7 public static $load_return = true; 8 public static $test_return = true; 9 10 protected function load() { 11 return self::$load_return; 12 } 13 public static function test() { 14 return self::$test_return; 15 } 16 public static function supports_mime_type( $mime_type) { 17 return true; 18 } 19 public function resize( $max_w, $max_h, $crop = false ) { 20 21 } 22 public function multi_resize( $sizes ) { 23 24 } 25 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 26 27 } 28 public function rotate( $angle ) { 29 30 } 31 public function flip( $horz, $vert ) { 32 33 } 34 public function save( $destfilename = null, $mime_type = null ) { 35 36 } 37 public function stream( $mime_type = null ) { 38 39 } 40 } 41 42 endif; 43 No newline at end of file -
tests/functions/deprecated.php
1 <?php 2 3 /** 4 * Test cases for deprecated functions, arguments, and files 5 * 6 * @package WordPress 7 * @subpackage Unit Tests 8 * @since 3.5 9 * @group deprecated 10 */ 11 class Test_Functions_Deprecated extends WP_UnitTestCase { 12 13 /** 14 * List of functions that have been passed through _deprecated_function() 15 * @var string[] 16 */ 17 protected $_deprecated_functions = array(); 18 19 /** 20 * List of arguments that have been passed through _deprecated_argument() 21 * @var string[] 22 */ 23 protected $_deprecated_arguments = array(); 24 25 /** 26 * List of files that have been passed through _deprecated_file() 27 * @var string[] 28 */ 29 protected $_deprecated_files = array(); 30 31 /** 32 * Set up the test fixture 33 * @return void 34 */ 35 public function setUp() { 36 parent::setUp(); 37 $this->_deprecated_functions = array(); 38 $this->_deprecated_arguments = array(); 39 $this->_deprecated_files = array(); 40 add_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 ); 41 add_action( 'deprecated_function_trigger_error', '__return_false' ); 42 add_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 ); 43 add_action( 'deprecated_argument_trigger_error', '__return_false' ); 44 add_action( 'deprecated_file_included' , array( $this, 'deprecated_file' ), 10, 4 ); 45 add_action( 'deprecated_file_trigger_error', '__return_false' ); 46 } 47 48 /** 49 * Tear down the test fixture 50 * @return void 51 */ 52 public function teardown() { 53 remove_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 ); 54 remove_action( 'deprecated_function_trigger_error', '__return_false' ); 55 remove_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 ); 56 remove_action( 'deprecated_argument_trigger_error', '__return_false' ); 57 remove_action( 'deprecated_file_included' , array( $this, 'deprecated_argument' ), 10, 4 ); 58 remove_action( 'deprecated_file_trigger_error', '__return_false' ); 59 parent::tearDown(); 60 } 61 62 /** 63 * Catch functions that have passed through _deprecated_function 64 * @param string $function 65 * @param string $replacement 66 * @param float $version 67 * @return void 68 */ 69 public function deprecated_function( $function, $replacement, $version ) { 70 $this->_deprecated_functions[] = array( 71 'function' => $function, 72 'replacement' => $replacement, 73 'version' => $version 74 ); 75 } 76 77 /** 78 * Catch arguments that have passed through _deprecated_argument 79 * @param string $argument 80 * @param string $message 81 * @param float $version 82 * @return void 83 */ 84 public function deprecated_argument( $argument, $message, $version ) { 85 $this->_deprecated_arguments[] = array( 86 'argument' => $argument, 87 'message' => $message, 88 'version' => $version 89 ); 90 } 91 92 /** 93 * Catch arguments that have passed through _deprecated_argument 94 * @param string $argument 95 * @param string $message 96 * @param float $version 97 * @return void 98 */ 99 public function deprecated_file( $file, $version, $replacement, $message ) { 100 $this->_deprecated_files[] = array( 101 'file' => $file, 102 'version' => $version, 103 'replacement' => $replacement, 104 'message' => $message 105 ); 106 } 107 108 /** 109 * Check if something was deprecated 110 * @param string $type argument|function|file 111 * @param string $name 112 * @return array|false 113 */ 114 protected function was_deprecated( $type, $name ) { 115 switch ( $type ) { 116 case 'argument' : 117 $search = $this->_deprecated_arguments; 118 $key = 'argument'; 119 break; 120 case 'function' : 121 $search = $this->_deprecated_functions; 122 $key = 'function'; 123 break; 124 default : 125 $search = $this->_deprecated_files; 126 $key = 'file'; 127 } 128 foreach ( $search as $v ) { 129 if ( $name == $v[$key] ) { 130 return $v; 131 } 132 } 133 return false; 134 } 135 136 /** 137 * Test that wp_save_image_file has a deprecated argument when passed a GD resource 138 * @ticket 6821 139 */ 140 public function test_wp_save_image_file_deprecated_with_gd_resource() { 141 142 // Call wp_save_image_file 143 include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); 144 $file = wp_tempnam(); 145 $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' ); 146 wp_save_image_file( $file, $img, 'image/jpeg', 1 ); 147 imagedestroy( $img ); 148 @unlink($file); 149 150 // Check if the arg was deprecated 151 $check = $this->was_deprecated( 'argument', 'wp_save_image_file' ); 152 $this->assertNotEmpty( $check ); 153 } 154 155 /** 156 * Test that wp_save_image_file doesn't have a deprecated argument when passed a WP_Image_Editor 157 * @ticket 6821 158 */ 159 public function test_wp_save_image_file_not_deprecated_with_wp_image_editor() { 160 161 // Call wp_save_image_file 162 include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); 163 $file = wp_tempnam(); 164 $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 165 wp_save_image_file( $file, $img, 'image/jpeg', 1 ); 166 unset( $img ); 167 @unlink($file); 168 169 // Check if the arg was deprecated 170 $check = $this->was_deprecated( 'argument', 'wp_save_image_file' ); 171 $this->assertFalse( $check ); 172 } 173 } -
tests/image/editor.php
1 <?php 2 3 /** 4 * Test the WP_Image_Editor base class 5 * @group image 6 * @group media 7 */ 8 class Tests_Image_Editor extends WP_UnitTestCase { 9 10 /** 11 * Image editor 12 * @var WP_Image_Editor 13 */ 14 protected $editor = null; 15 16 /** 17 * Setup test fixture 18 */ 19 public function setup() { 20 if ( !class_exists( 'WP_Image_Editor' ) ) 21 $this->markTestSkipped(); 22 23 // Include our custom mock 24 include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' ); 25 26 // Mock up an abstract image editor based on WP_Image_Editor 27 // note: this *HAS* to start with 'WP_Image_Editor_' 28 $className = 'WP_Image_Editor_' . substr( md5( uniqid() ), -12 ); 29 $this->editor = $this->getMockForAbstractClass( 'WP_Image_Editor', array( 30 'get_size', 31 'get_suffix' 32 ), $className, false ); 33 34 // Override the filters to set our own image editor 35 add_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); 36 add_filter( 'wp_editors', array( $this, 'wp_editors' ) ); 37 38 // Un-cache the chosen image implementation 39 $this->_uncache_implementation(); 40 } 41 42 /** 43 * Tear down test fixture 44 */ 45 public function tearDown() { 46 remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); 47 remove_filter( 'wp_editors', array( $this, 'wp_editors' ) ); 48 } 49 50 /** 51 * Unset the static implementation cache 52 */ 53 protected function _uncache_implementation() { 54 $class = new ReflectionClass( 'WP_Image_Editor' ); 55 $var = $class->getProperty( 'implementation' ); 56 $var->setAccessible( true ); 57 $var->setValue( $class, null ); 58 } 59 60 /** 61 * Override the wp_editors filter 62 * @return array 63 */ 64 public function wp_editors() { 65 return array( preg_replace('/^WP_Image_Editor_/', '', get_class( $this->editor ) ) ); 66 } 67 68 /** 69 * Override the image_editor_class filter 70 * @return mixed 71 */ 72 public function image_editor_class() { 73 return get_class( $this->editor ); 74 } 75 76 /** 77 * Test get_instance where load returns true 78 * @ticket 6821 79 */ 80 public function test_get_instance_load_returns_true() { 81 82 // Swap out the PHPUnit mock with our custom mock 83 $func = create_function( '', 'return "WP_Image_Editor_Mock";'); 84 remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); 85 add_filter( 'image_editor_class', $func ); 86 87 // Set load() to return true 88 WP_Image_Editor_Mock::$load_return = true; 89 90 // Load an image 91 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 92 93 // Everything should work 94 $this->assertInstanceOf( 'WP_Image_Editor_Mock', $editor ); 95 96 // Remove our custom Mock 97 remove_filter( 'image_editor_class', $func ); 98 } 99 100 /** 101 * Test get_instance where load returns false 102 * @ticket 6821 103 */ 104 public function test_get_instance_load_returns_false() { 105 106 // Swap out the PHPUnit mock with our custom mock 107 $func = create_function( '', 'return "WP_Image_Editor_Mock";'); 108 remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); 109 add_filter( 'image_editor_class', $func ); 110 111 // Set load() to return true 112 WP_Image_Editor_Mock::$load_return = new WP_Error(); 113 114 // Load an image 115 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 116 117 // Everything should work 118 $this->assertInstanceOf( 'WP_Error', $editor ); 119 120 // Remove our custom Mock 121 remove_filter( 'image_editor_class', $func ); 122 } 123 124 /** 125 * Test the "test" method 126 * @ticket 6821 127 */ 128 public function test_test_returns_true() { 129 130 // $editor::test() returns true 131 $this->editor->staticExpects( $this->once() ) 132 ->method( 'test' ) 133 ->will( $this->returnValue( true ) ); 134 135 // Load an image 136 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 137 138 // Everything should work 139 $this->assertInstanceOf( get_class( $this->editor ), $editor ); 140 } 141 142 /** 143 * Test the "test" method returns false and the fallback editor is chosen 144 * @ticket 6821 145 */ 146 public function test_test_returns_false() { 147 148 // $editor::test() returns true 149 $this->editor->staticExpects( $this->once() ) 150 ->method( 'test' ) 151 ->will( $this->returnValue( false ) ); 152 153 // Set a fallback editor 154 $className = preg_replace('/^WP_Image_Editor_/', '', get_class( $this->editor ) ); 155 $func = create_function( '', "return array('$className', 'Mock');" ); 156 remove_filter( 'wp_editors', array( $this, 'wp_editors' ) ); 157 remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) ); 158 add_filter( 'wp_editors', $func ); 159 160 // Load an image 161 WP_Image_Editor_Mock::$load_return = true; 162 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 163 164 // Everything should work 165 $this->assertInstanceOf( 'WP_Image_Editor_Mock', $editor ); 166 167 // Unhook 168 remove_filter( 'image_editor_class', '__return_null' ); 169 remove_filter( 'wp_editors', $func ); 170 } 171 172 /** 173 * Test test_quality 174 * @ticket 6821 175 */ 176 public function test_set_quality() { 177 178 // Get an editor 179 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 180 181 // Make quality readable 182 $property = new ReflectionProperty( $editor, 'quality' ); 183 $property->setAccessible( true ); 184 185 // Ensure set_quality works 186 $this->assertTrue( $editor->set_quality( 75 ) ); 187 $this->assertEquals( 75, $property->getValue( $editor ) ); 188 189 // Ensure the quality filter works 190 $func = create_function( '', "return 100;"); 191 add_filter( 'wp_editor_set_quality', $func ); 192 $this->assertTrue( $editor->set_quality( 75 ) ); 193 $this->assertEquals( 100, $property->getValue( $editor ) ); 194 195 // Clean up 196 remove_filter( 'wp_editor_set_quality', $func ); 197 } 198 199 /** 200 * Test generate_filename 201 * @ticket 6821 202 */ 203 public function test_generate_filename() { 204 205 // Get an editor 206 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 207 $property = new ReflectionProperty( $editor, 'size' ); 208 $property->setAccessible( true ); 209 $property->setValue( $editor, array( 210 'height' => 50, 211 'width' => 100 212 )); 213 214 // Test with no parameters 215 $this->assertEquals( 'canola-100x50.jpg', basename( $editor->generate_filename() ) ); 216 217 // Test with a suffix only 218 $this->assertEquals( 'canola-new.jpg', basename( $editor->generate_filename( 'new' ) ) ); 219 220 // Test with a destination dir only 221 $this->assertEquals(trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) ); 222 223 // Test with a suffix only 224 $this->assertEquals( 'canola-100x50.png', basename( $editor->generate_filename( null, null, 'png' ) ) ); 225 226 // Combo! 227 $this->assertEquals( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) ); 228 } 229 230 /** 231 * Test get_size 232 * @ticket 6821 233 */ 234 public function test_get_size() { 235 236 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 237 238 // Size should be false by default 239 $this->assertNull( $editor->get_size() ); 240 241 // Set a size 242 $size = array( 243 'height' => 50, 244 'width' => 100 245 ); 246 $property = new ReflectionProperty( $editor, 'size' ); 247 $property->setAccessible( true ); 248 $property->setValue( $editor, $size ); 249 250 $this->assertEquals( $size, $editor->get_size() ); 251 } 252 253 /** 254 * Test get_suffix 255 * @ticket 6821 256 */ 257 public function test_get_suffix() { 258 259 $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 260 261 // Size should be false by default 262 $this->assertFalse( $editor->get_suffix() ); 263 264 // Set a size 265 $size = array( 266 'height' => 50, 267 'width' => 100 268 ); 269 $property = new ReflectionProperty( $editor, 'size' ); 270 $property->setAccessible( true ); 271 $property->setValue( $editor, $size ); 272 273 $this->assertEquals( '100x50', $editor->get_suffix() ); 274 } 275 } -
tests/image/resize.php
123 123 unlink($image); 124 124 } 125 125 126 /** 127 * Try resizing a non-existent image 128 * @ticket 6821 129 */ 130 public function test_resize_non_existent_image() { 131 $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'); 132 foreach ( $classes as $class ) { 133 if ( !$class::test() ) { 134 continue; 135 } 136 $filter = create_function( '', "return $class;" ); 137 add_filter( 'image_editor_class', $filter ); 138 $image = image_resize( DIR_TESTDATA.'/images/test-non-existent-image.jpg', 25, 25 ); 139 $this->assertInstanceOf( 'WP_Error', $image ); 140 $this->assertEquals( 'error_loading_image', $image->get_error_code() ); 141 } 142 } 143 144 /** 145 * Try resizing a php file (bad image) 146 * @ticket 6821 147 */ 148 public function test_resize_bad_image() { 149 $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'); 150 foreach ( $classes as $class ) { 151 if ( !$class::test() ) { 152 continue; 153 } 154 $filter = create_function( '', "return $class;" ); 155 add_filter( 'image_editor_class', $filter ); 156 $image = image_resize( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 ); 157 $this->assertInstanceOf( 'WP_Error', $image ); 158 $this->assertEquals( 'invalid_image', $image->get_error_code() ); 159 } 160 } 126 161 } -
tests/image/functions.php
6 6 * @group upload 7 7 */ 8 8 class Tests_Image_Functions extends WP_UnitTestCase { 9 10 /** 11 * Get the MIME type of a file 12 * @param string $filename 13 * @return string 14 */ 15 protected function get_mime_type( $filename ) { 16 $mime_type = ''; 17 if ( extension_loaded( 'fileinfo' ) ) { 18 $finfo = new finfo(); 19 $mime_type = $finfo->file( $filename, FILEINFO_MIME ); 20 } elseif ( function_exists('mime_content_type') ) { 21 $mime_type = mime_content_type( $filename ); 22 } 23 if ( false !== strpos( $mime_type, ';' ) ) { 24 list( $mime_type, $charset ) = explode( ';', $mime_type, 2 ); 25 } 26 return $mime_type; 27 } 28 9 29 function test_is_image_positive() { 10 30 // these are all image files recognized by php 11 31 $files = array( … … 72 92 foreach ($files as $file) { 73 93 $this->assertFalse( file_is_displayable_image( DIR_TESTDATA.'/images/'.$file ), "file_is_valid_image($file) should return false" ); 74 94 } 95 } 96 97 /** 98 * Test save image file and mime_types 99 * @ticket 6821 100 */ 101 public function test_wp_save_image_file() { 102 include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); 103 104 // Mime types 105 $mime_types = array( 106 'image/jpeg', 107 'image/gif', 108 'image/png' 109 ); 110 111 // Test each image editor engine 112 $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'); 113 foreach ( $classes as $class ) { 114 115 // If the image editor isn't available, skip it 116 if ( !$class::test() ) { 117 continue; 118 } 119 $filter = create_function( '', "return '$class';" ); 120 add_filter( 'image_editor_class', $filter ); 121 122 // Call wp_save_image_file 123 $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 124 125 // Save a file as each mime type, assert it works 126 foreach ( $mime_types as $mime_type ) { 127 $file = wp_tempnam(); 128 $ret = wp_save_image_file( $file, $img, $mime_type, 1 ); 129 $this->assertNotEmpty( $ret ); 130 $this->assertNotInstanceOf( 'WP_Error', $ret ); 131 $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) ); 132 133 // Clean up 134 @unlink( $file ); 135 @unlink( $ret['path'] ); 136 } 137 138 // Clean up 139 unset( $img ); 140 } 75 141 } 142 143 /** 144 * Test that a passed mime type overrides the extension in the filename 145 * @ticket 6821 146 */ 147 public function test_mime_overrides_filename() { 76 148 149 // Test each image editor engine 150 $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'); 151 foreach ( $classes as $class ) { 152 153 // If the image editor isn't available, skip it 154 if ( !$class::test() ) { 155 continue; 156 } 157 $filter = create_function( '', "return '$class';" ); 158 add_filter( 'image_editor_class', $filter ); 159 160 // Save the file 161 $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 162 $mime_type = 'image/gif'; 163 $file = wp_tempnam( 'tmp.jpg' ); 164 $ret = $img->save( $file, $mime_type ); 165 166 // Make assertions 167 $this->assertNotEmpty( $ret ); 168 $this->assertNotInstanceOf( 'WP_Error', $ret ); 169 $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) ); 170 171 // Clean up 172 @unlink( $file ); 173 @unlink( $ret['path'] ); 174 unset( $img ); 175 } 176 } 177 178 /** 179 * Test that mime types are correctly inferred from file extensions 180 * @ticket 6821 181 */ 182 public function test_inferred_mime_types() { 183 184 // Mime types 185 $mime_types = array( 186 'jpg' => 'image/jpeg', 187 'jpeg' => 'image/jpeg', 188 'jpe' => 'image/jpeg', 189 'gif' => 'image/gif', 190 'png' => 'image/png', 191 'unk' => 'image/jpeg' // Default, unknown 192 ); 193 194 // Test each image editor engine 195 $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick'); 196 foreach ( $classes as $class ) { 197 198 // If the image editor isn't available, skip it 199 if ( !$class::test() ) { 200 continue; 201 } 202 $filter = create_function( '', "return '$class';" ); 203 add_filter( 'image_editor_class', $filter ); 204 205 // Save the image as each file extension, check the mime type 206 $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' ); 207 $temp = get_temp_dir(); 208 foreach ( $mime_types as $ext => $mime_type ) { 209 $file = wp_unique_filename( $temp, uniqid() . ".$ext" ); 210 $ret = $img->save( trailingslashit( $temp ) . $file ); 211 $this->assertNotEmpty( $ret ); 212 $this->assertNotInstanceOf( 'WP_Error', $ret ); 213 $this->assertEquals( $mime_type, $this->get_mime_type( $ret['path'] ) ); 214 @unlink( $file ); 215 @unlink( $ret['path'] ); 216 } 217 218 // Clean up 219 unset( $img ); 220 } 221 } 77 222 }