<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Tests][1061] trunk: Unit tests for #WP6821</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://unit-tests.trac.wordpress.org/changeset/1061">1061</a></dd>
<dt>Author</dt> <dd>kurtpayne</dd>
<dt>Date</dt> <dd>2012-10-01 21:08:41 +0000 (Mon, 01 Oct 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Unit tests for #WP6821</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsimagefunctionsphp">trunk/tests/image/functions.php</a></li>
<li><a href="#trunktestsimageresizephp">trunk/tests/image/resize.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkincludesmockimageeditorphp">trunk/includes/mock-image-editor.php</a></li>
<li><a href="#trunktestsfunctionsdeprecatedphp">trunk/tests/functions/deprecated.php</a></li>
<li><a href="#trunktestsimageeditorphp">trunk/tests/image/editor.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkincludesmockimageeditorphp"></a>
<div class="addfile"><h4>Added: trunk/includes/mock-image-editor.php (0 => 1061)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/includes/mock-image-editor.php                                (rev 0)
+++ trunk/includes/mock-image-editor.php        2012-10-01 21:08:41 UTC (rev 1061)
</span><span class="lines">@@ -0,0 +1,42 @@
</span><ins>+&lt;?php
+
+if (class_exists( 'WP_Image_Editor' ) ) :
+
+        class WP_Image_Editor_Mock extends WP_Image_Editor {
+
+                public static $load_return = true;
+                public static $test_return = true;
+
+                protected function load() {
+                        return self::$load_return;
+                }
+                public static function test() {
+                        return self::$test_return;
+                }
+                public static function supports_mime_type( $mime_type) {
+                        return true;
+                }
+                public function resize( $max_w, $max_h, $crop = false ) {
+
+                }
+                public function multi_resize( $sizes ) {
+
+                }
+                public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
+
+                }
+                public function rotate( $angle ) {
+
+                }
+                public function flip( $horz, $vert ) {
+
+                }
+                public function save( $destfilename = null, $mime_type = null ) {
+
+                }
+                public function stream( $mime_type = null ) {
+
+                }
+        }
+
+endif;
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunktestsfunctionsdeprecatedphp"></a>
<div class="addfile"><h4>Added: trunk/tests/functions/deprecated.php (0 => 1061)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/functions/deprecated.php                                (rev 0)
+++ trunk/tests/functions/deprecated.php        2012-10-01 21:08:41 UTC (rev 1061)
</span><span class="lines">@@ -0,0 +1,173 @@
</span><ins>+&lt;?php
+
+/**
+ * Test cases for deprecated functions, arguments, and files
+ *
+ * @package    WordPress
+ * @subpackage Unit Tests
+ * @since      3.5
+ * @group      deprecated
+ */
+class Test_Functions_Deprecated extends WP_UnitTestCase {
+
+        /**
+         * List of functions that have been passed through _deprecated_function()
+         * @var string[]
+         */
+        protected $_deprecated_functions = array();
+
+        /**
+         * List of arguments that have been passed through _deprecated_argument()
+         * @var string[]
+         */
+        protected $_deprecated_arguments = array();
+
+        /**
+         * List of files that have been passed through _deprecated_file()
+         * @var string[]
+         */
+        protected $_deprecated_files = array();
+        
+        /**
+         * Set up the test fixture
+         * @return void
+         */
+        public function setUp() {
+                parent::setUp();
+                $this-&gt;_deprecated_functions = array();
+                $this-&gt;_deprecated_arguments = array();
+                $this-&gt;_deprecated_files = array();
+                add_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
+                add_action( 'deprecated_function_trigger_error', '__return_false' );
+                add_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
+                add_action( 'deprecated_argument_trigger_error', '__return_false' );
+                add_action( 'deprecated_file_included' , array( $this, 'deprecated_file' ), 10, 4 );
+                add_action( 'deprecated_file_trigger_error', '__return_false' );
+        }
+
+        /**
+         * Tear down the test fixture
+         * @return void
+         */
+        public function teardown() {
+                remove_action( 'deprecated_function_run' , array( $this, 'deprecated_function' ), 10, 3 );
+                remove_action( 'deprecated_function_trigger_error', '__return_false' );
+                remove_action( 'deprecated_argument_run' , array( $this, 'deprecated_argument' ), 10, 3 );
+                remove_action( 'deprecated_argument_trigger_error', '__return_false' );
+                remove_action( 'deprecated_file_included' , array( $this, 'deprecated_argument' ), 10, 4 );
+                remove_action( 'deprecated_file_trigger_error', '__return_false' );
+                parent::tearDown();
+        }
+
+        /**
+         * Catch functions that have passed through _deprecated_function
+         * @param string $function
+         * @param string $replacement
+         * @param float $version
+         * @return void
+         */
+        public function deprecated_function( $function, $replacement, $version ) {
+                $this-&gt;_deprecated_functions[] = array(
+                        'function'    =&gt; $function,
+                        'replacement' =&gt; $replacement,
+                        'version'     =&gt; $version
+                );
+        }
+
+        /**
+         * Catch arguments that have passed through _deprecated_argument
+         * @param string $argument
+         * @param string $message
+         * @param float $version
+         * @return void
+         */
+        public function deprecated_argument( $argument, $message, $version ) {
+                $this-&gt;_deprecated_arguments[] = array(
+                        'argument' =&gt; $argument,
+                        'message'  =&gt; $message,
+                        'version'  =&gt; $version
+                );
+        }
+
+        /**
+         * Catch arguments that have passed through _deprecated_argument
+         * @param string $argument
+         * @param string $message
+         * @param float $version
+         * @return void
+         */
+        public function deprecated_file( $file, $version, $replacement, $message ) {
+                $this-&gt;_deprecated_files[] = array(
+                        'file'        =&gt; $file,
+                        'version'     =&gt; $version,
+                        'replacement' =&gt; $replacement,
+                        'message'     =&gt; $message
+                );
+        }
+        
+        /**
+         * Check if something was deprecated
+         * @param string $type argument|function|file
+         * @param string $name
+         * @return array|false
+         */
+        protected function was_deprecated( $type, $name ) {
+                switch ( $type ) {
+                        case 'argument' :
+                                $search = $this-&gt;_deprecated_arguments;
+                                $key    = 'argument';
+                                break;
+                        case 'function' :
+                                $search = $this-&gt;_deprecated_functions;
+                                $key    = 'function';
+                                break;
+                        default :
+                                $search = $this-&gt;_deprecated_files;
+                                $key    = 'file';
+                }
+                foreach ( $search as $v ) {
+                        if ( $name == $v[$key] ) {
+                                return $v;
+                        }
+                }
+                return false;
+        }
+        
+        /**
+         * Test that wp_save_image_file has a deprecated argument when passed a GD resource
+         * @ticket 6821
+         */
+        public function test_wp_save_image_file_deprecated_with_gd_resource() {
+
+                // Call wp_save_image_file
+                include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
+                $file = wp_tempnam();
+                $img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' );
+                wp_save_image_file( $file, $img, 'image/jpeg', 1 );
+                imagedestroy( $img );
+                @unlink($file);
+
+                // Check if the arg was deprecated
+                $check = $this-&gt;was_deprecated( 'argument', 'wp_save_image_file' );
+                $this-&gt;assertNotEmpty( $check );                
+        }
+        
+        /**
+         * Test that wp_save_image_file doesn't have a deprecated argument when passed a WP_Image_Editor
+         * @ticket 6821
+         */
+        public function test_wp_save_image_file_not_deprecated_with_wp_image_editor() {
+
+                // Call wp_save_image_file
+                include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
+                $file = wp_tempnam();
+                $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+                wp_save_image_file( $file, $img, 'image/jpeg', 1 );
+                unset( $img );
+                @unlink($file);
+
+                // Check if the arg was deprecated
+                $check = $this-&gt;was_deprecated( 'argument', 'wp_save_image_file' );
+                $this-&gt;assertFalse( $check );
+        }
+}
</ins></span></pre></div>
<a id="trunktestsimageeditorphp"></a>
<div class="addfile"><h4>Added: trunk/tests/image/editor.php (0 => 1061)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/image/editor.php                                (rev 0)
+++ trunk/tests/image/editor.php        2012-10-01 21:08:41 UTC (rev 1061)
</span><span class="lines">@@ -0,0 +1,275 @@
</span><ins>+&lt;?php
+
+/**
+ * Test the WP_Image_Editor base class
+ * @group image
+ * @group media
+ */
+class Tests_Image_Editor extends WP_UnitTestCase {
+
+        /**
+         * Image editor
+         * @var WP_Image_Editor
+         */
+        protected $editor = null;
+        
+        /**
+         * Setup test fixture
+         */
+        public function setup() {
+                if ( !class_exists( 'WP_Image_Editor' ) )
+                        $this-&gt;markTestSkipped();
+
+                // Include our custom mock
+                include_once( DIR_TESTDATA . '/../includes/mock-image-editor.php' );
+
+                // Mock up an abstract image editor based on WP_Image_Editor
+                // note: this *HAS* to start with 'WP_Image_Editor_'
+                $className = 'WP_Image_Editor_' . substr( md5( uniqid() ), -12 );
+                $this-&gt;editor = $this-&gt;getMockForAbstractClass( 'WP_Image_Editor', array(
+                        'get_size',
+                        'get_suffix'
+                ), $className, false );
+                
+                // Override the filters to set our own image editor
+                add_filter( 'image_editor_class', array( $this, 'image_editor_class' ) );        
+                add_filter( 'wp_editors', array( $this, 'wp_editors' ) );
+                
+                // Un-cache the chosen image implementation
+                $this-&gt;_uncache_implementation();
+        }
+
+        /**
+         * Tear down test fixture
+         */
+        public function tearDown() {
+                remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) );
+                remove_filter( 'wp_editors', array( $this, 'wp_editors' ) );
+        }
+
+        /**
+         * Unset the static implementation cache
+         */
+        protected function _uncache_implementation() {
+                $class = new ReflectionClass( 'WP_Image_Editor' );
+                $var = $class-&gt;getProperty( 'implementation' );
+                $var-&gt;setAccessible( true );
+                $var-&gt;setValue( $class, null );
+        }
+        
+        /**
+         * Override the wp_editors filter
+         * @return array
+         */
+        public function wp_editors() {                
+                return array( preg_replace('/^WP_Image_Editor_/', '', get_class( $this-&gt;editor ) ) );
+        }
+
+        /**
+         * Override the image_editor_class filter
+         * @return mixed
+         */
+        public function image_editor_class() {
+                return get_class( $this-&gt;editor );
+        }
+        
+        /**
+         * Test get_instance where load returns true
+         * @ticket 6821
+         */
+        public function test_get_instance_load_returns_true() {
+
+                // Swap out the PHPUnit mock with our custom mock
+                $func = create_function( '', 'return &quot;WP_Image_Editor_Mock&quot;;');
+                remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) );
+                add_filter( 'image_editor_class', $func );
+
+                // Set load() to return true
+                WP_Image_Editor_Mock::$load_return = true;
+
+                // Load an image
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+
+                // Everything should work
+                $this-&gt;assertInstanceOf( 'WP_Image_Editor_Mock', $editor );
+
+                // Remove our custom Mock
+                remove_filter( 'image_editor_class', $func );
+        }
+
+        /**
+         * Test get_instance where load returns false
+         * @ticket 6821
+         */
+        public function test_get_instance_load_returns_false() {
+
+                // Swap out the PHPUnit mock with our custom mock
+                $func = create_function( '', 'return &quot;WP_Image_Editor_Mock&quot;;');
+                remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) );
+                add_filter( 'image_editor_class', $func );
+
+                // Set load() to return true
+                WP_Image_Editor_Mock::$load_return = new WP_Error();
+
+                // Load an image
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+
+                // Everything should work
+                $this-&gt;assertInstanceOf( 'WP_Error', $editor );
+
+                // Remove our custom Mock
+                remove_filter( 'image_editor_class', $func );
+        }
+        
+        /**
+         * Test the &quot;test&quot; method
+         * @ticket 6821
+         */
+        public function test_test_returns_true() {
+
+                // $editor::test() returns true
+                $this-&gt;editor-&gt;staticExpects( $this-&gt;once() )
+                                     -&gt;method( 'test' )
+                                     -&gt;will( $this-&gt;returnValue( true ) );
+
+                // Load an image
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+
+                // Everything should work
+                $this-&gt;assertInstanceOf( get_class( $this-&gt;editor ), $editor );        
+        }
+        
+        /**
+         * Test the &quot;test&quot; method returns false and the fallback editor is chosen
+         * @ticket 6821
+         */
+        public function test_test_returns_false() {
+
+                // $editor::test() returns true
+                $this-&gt;editor-&gt;staticExpects( $this-&gt;once() )
+                                     -&gt;method( 'test' )
+                                     -&gt;will( $this-&gt;returnValue( false ) );
+
+                // Set a fallback editor
+                $className = preg_replace('/^WP_Image_Editor_/', '', get_class( $this-&gt;editor ) );
+                $func = create_function( '', &quot;return array('$className', 'Mock');&quot; );
+                remove_filter( 'wp_editors', array( $this, 'wp_editors' ) );
+                remove_filter( 'image_editor_class', array( $this, 'image_editor_class' ) );
+                add_filter( 'wp_editors', $func );
+
+                // Load an image
+                WP_Image_Editor_Mock::$load_return = true;
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+
+                // Everything should work
+                $this-&gt;assertInstanceOf( 'WP_Image_Editor_Mock', $editor );        
+
+                // Unhook
+                remove_filter( 'image_editor_class', '__return_null' );
+                remove_filter( 'wp_editors', $func );
+        }
+
+        /**
+         * Test test_quality
+         * @ticket 6821
+         */
+        public function test_set_quality() {
+
+                // Get an editor
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+
+                // Make quality readable
+                $property = new ReflectionProperty( $editor, 'quality' );
+                $property-&gt;setAccessible( true );
+                
+                // Ensure set_quality works
+                $this-&gt;assertTrue( $editor-&gt;set_quality( 75 ) );
+                $this-&gt;assertEquals( 75, $property-&gt;getValue( $editor ) );
+
+                // Ensure the quality filter works        
+                $func = create_function( '', &quot;return 100;&quot;);
+                add_filter( 'wp_editor_set_quality', $func );
+                $this-&gt;assertTrue( $editor-&gt;set_quality( 75 ) );
+                $this-&gt;assertEquals( 100, $property-&gt;getValue( $editor ) );
+
+                // Clean up
+                remove_filter( 'wp_editor_set_quality', $func );
+        }
+
+        /**
+         * Test generate_filename
+         * @ticket 6821
+         */
+        public function test_generate_filename() {
+                
+                // Get an editor
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+                $property = new ReflectionProperty( $editor, 'size' );
+                $property-&gt;setAccessible( true );
+                $property-&gt;setValue( $editor, array(
+                        'height' =&gt; 50,
+                        'width'  =&gt; 100
+                ));
+
+                // Test with no parameters
+                $this-&gt;assertEquals( 'canola-100x50.jpg', basename( $editor-&gt;generate_filename() ) );
+
+                // Test with a suffix only
+                $this-&gt;assertEquals( 'canola-new.jpg', basename( $editor-&gt;generate_filename( 'new' ) ) );
+
+                // Test with a destination dir only
+                $this-&gt;assertEquals(trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor-&gt;generate_filename( null, get_temp_dir() ) ) ) ) );
+
+                // Test with a suffix only
+                $this-&gt;assertEquals( 'canola-100x50.png', basename( $editor-&gt;generate_filename( null, null, 'png' ) ) );
+        
+                // Combo!
+                $this-&gt;assertEquals( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor-&gt;generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
+        }
+
+        /**
+         * Test get_size
+         * @ticket 6821
+         */
+        public function test_get_size() {
+
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+                
+                // Size should be false by default
+                $this-&gt;assertNull( $editor-&gt;get_size() );
+                
+                // Set a size
+                $size = array(
+                        'height' =&gt; 50,
+                        'width'  =&gt; 100
+                );
+                $property = new ReflectionProperty( $editor, 'size' );
+                $property-&gt;setAccessible( true );
+                $property-&gt;setValue( $editor, $size );
+
+                $this-&gt;assertEquals( $size, $editor-&gt;get_size() );
+        }
+        
+        /**
+         * Test get_suffix
+         * @ticket 6821
+         */
+        public function test_get_suffix() {
+
+                $editor = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+                
+                // Size should be false by default
+                $this-&gt;assertFalse( $editor-&gt;get_suffix() );
+
+                // Set a size
+                $size = array(
+                        'height' =&gt; 50,
+                        'width'  =&gt; 100
+                );
+                $property = new ReflectionProperty( $editor, 'size' );
+                $property-&gt;setAccessible( true );
+                $property-&gt;setValue( $editor, $size );
+
+                $this-&gt;assertEquals( '100x50', $editor-&gt;get_suffix() );
+        }
+}
</ins></span></pre></div>
<a id="trunktestsimagefunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/image/functions.php (1060 => 1061)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/image/functions.php        2012-10-01 18:03:48 UTC (rev 1060)
+++ trunk/tests/image/functions.php        2012-10-01 21:08:41 UTC (rev 1061)
</span><span class="lines">@@ -6,6 +6,26 @@
</span><span class="cx">  * @group upload
</span><span class="cx">  */
</span><span class="cx"> class Tests_Image_Functions extends WP_UnitTestCase {
</span><ins>+
+        /**
+         * Get the MIME type of a file
+         * @param string $filename
+         * @return string
+         */
+        protected function get_mime_type( $filename ) {
+                $mime_type = ''; 
+                if ( extension_loaded( 'fileinfo' ) ) { 
+                        $finfo = new finfo();
+                        $mime_type = $finfo-&gt;file( $filename, FILEINFO_MIME );
+                } elseif ( function_exists('mime_content_type') ) { 
+                        $mime_type = mime_content_type( $filename );
+                }
+                if ( false !== strpos( $mime_type, ';' ) ) {
+                        list( $mime_type, $charset ) = explode( ';', $mime_type, 2 );
+                }
+                return $mime_type;
+        }
+        
</ins><span class="cx">         function test_is_image_positive() {
</span><span class="cx">                 // these are all image files recognized by php
</span><span class="cx">                 $files = array(
</span><span class="lines">@@ -72,6 +92,131 @@
</span><span class="cx">                 foreach ($files as $file) {
</span><span class="cx">                         $this-&gt;assertFalse( file_is_displayable_image( DIR_TESTDATA.'/images/'.$file ), &quot;file_is_valid_image($file) should return false&quot; );
</span><span class="cx">                 }
</span><ins>+        }        
+        
+        /**
+         * Test save image file and mime_types
+         * @ticket 6821
+         */
+        public function test_wp_save_image_file() {
+                include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
+
+                // Mime types
+                $mime_types = array(
+                        'image/jpeg',
+                        'image/gif',
+                        'image/png'
+                );
+                
+                // Test each image editor engine
+                $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
+                foreach ( $classes as $class ) {
+                        
+                        // If the image editor isn't available, skip it
+                        if ( !$class::test() ) {
+                                continue;
+                        }
+                        $filter = create_function( '', &quot;return '$class';&quot; );
+                        add_filter( 'image_editor_class', $filter );
+
+                        // Call wp_save_image_file
+                        $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+                        
+                        // Save a file as each mime type, assert it works
+                        foreach ( $mime_types as $mime_type ) {
+                                $file = wp_tempnam();                                
+                                $ret = wp_save_image_file( $file, $img, $mime_type, 1 );
+                                $this-&gt;assertNotEmpty( $ret );
+                                $this-&gt;assertNotInstanceOf( 'WP_Error', $ret );
+                                $this-&gt;assertEquals( $mime_type, $this-&gt;get_mime_type( $ret['path'] ) );
+                                
+                                // Clean up
+                                @unlink( $file );
+                                @unlink( $ret['path'] );
+                        }
+
+                        // Clean up
+                        unset( $img );
+                }
</ins><span class="cx">         }
</span><ins>+        
+        /**
+         * Test that a passed mime type overrides the extension in the filename
+         * @ticket 6821
+         */
+        public function test_mime_overrides_filename() {
</ins><span class="cx"> 
</span><ins>+                // Test each image editor engine
+                $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
+                foreach ( $classes as $class ) {
+
+                        // If the image editor isn't available, skip it
+                        if ( !$class::test() ) {
+                                continue;
+                        }
+                        $filter = create_function( '', &quot;return '$class';&quot; );
+                        add_filter( 'image_editor_class', $filter );
+
+                        // Save the file
+                        $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+                        $mime_type = 'image/gif';
+                        $file = wp_tempnam( 'tmp.jpg' );
+                        $ret = $img-&gt;save( $file, $mime_type );
+                        
+                        // Make assertions
+                        $this-&gt;assertNotEmpty( $ret );
+                        $this-&gt;assertNotInstanceOf( 'WP_Error', $ret );
+                        $this-&gt;assertEquals( $mime_type, $this-&gt;get_mime_type( $ret['path'] ) );
+                        
+                        // Clean up
+                        @unlink( $file );
+                        @unlink( $ret['path'] );
+                        unset( $img );
+                }
+        }
+
+        /**
+         * Test that mime types are correctly inferred from file extensions
+         * @ticket 6821
+         */
+        public function test_inferred_mime_types() {
+
+                // Mime types
+                $mime_types = array(
+                        'jpg'  =&gt; 'image/jpeg',
+                        'jpeg' =&gt; 'image/jpeg',
+                        'jpe'  =&gt; 'image/jpeg',
+                        'gif'  =&gt; 'image/gif',
+                        'png'  =&gt; 'image/png',
+                        'unk'  =&gt; 'image/jpeg' // Default, unknown
+                );
+
+                // Test each image editor engine
+                $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
+                foreach ( $classes as $class ) {
+
+                        // If the image editor isn't available, skip it
+                        if ( !$class::test() ) {
+                                continue;
+                        }
+                        $filter = create_function( '', &quot;return '$class';&quot; );
+                        add_filter( 'image_editor_class', $filter );
+                        
+                        // Save the image as each file extension, check the mime type
+                        $img = WP_Image_Editor::get_instance( DIR_TESTDATA . '/images/canola.jpg' );
+                        $temp = get_temp_dir();
+                        foreach ( $mime_types as $ext =&gt; $mime_type ) {
+                                $file = wp_unique_filename( $temp, uniqid() . &quot;.$ext&quot; );
+                                $ret = $img-&gt;save( trailingslashit( $temp ) . $file );
+                                $this-&gt;assertNotEmpty( $ret );
+                                $this-&gt;assertNotInstanceOf( 'WP_Error', $ret );
+                                $this-&gt;assertEquals( $mime_type, $this-&gt;get_mime_type( $ret['path'] ) );
+                                @unlink( $file );
+                                @unlink( $ret['path'] );
+                        }
+
+                        // Clean up
+                        unset( $img );
+                }
+        }
</ins><span class="cx"> }
</span></span></pre></div>
<a id="trunktestsimageresizephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/image/resize.php (1060 => 1061)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/image/resize.php        2012-10-01 18:03:48 UTC (rev 1060)
+++ trunk/tests/image/resize.php        2012-10-01 21:08:41 UTC (rev 1061)
</span><span class="lines">@@ -123,4 +123,39 @@
</span><span class="cx">                 unlink($image);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        /**
+         * Try resizing a non-existent image
+         * @ticket 6821
+         */
+        public function test_resize_non_existent_image() {
+                $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
+                foreach ( $classes as $class ) {
+                        if ( !$class::test() ) {
+                                continue;
+                        }
+                        $filter = create_function( '', &quot;return $class;&quot; );
+                        add_filter( 'image_editor_class', $filter );
+                        $image = image_resize( DIR_TESTDATA.'/images/test-non-existent-image.jpg', 25, 25 );
+                        $this-&gt;assertInstanceOf( 'WP_Error', $image );
+                        $this-&gt;assertEquals( 'error_loading_image', $image-&gt;get_error_code() );
+                }
+        }
+        
+        /**
+         * Try resizing a php file (bad image)
+         * @ticket 6821
+         */
+        public function test_resize_bad_image() {
+                $classes = array('WP_Image_Editor_GD', 'WP_Image_Editor_Imagick');
+                foreach ( $classes as $class ) {
+                        if ( !$class::test() ) {
+                                continue;
+                        }
+                        $filter = create_function( '', &quot;return $class;&quot; );
+                        add_filter( 'image_editor_class', $filter );
+                        $image = image_resize( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 );
+                        $this-&gt;assertInstanceOf( 'WP_Error', $image );
+                        $this-&gt;assertEquals( 'invalid_image', $image-&gt;get_error_code() );
+                }
+        }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>