| 225 | * @ticket 53668 |
| 226 | */ |
| 227 | function test__wp_check_alternate_output_format_uniqueness() { |
| 228 | $testdir = DIR_TESTDATA . '/images/'; |
| 229 | |
| 230 | add_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); |
| 231 | |
| 232 | // Standard test that wp_unique_filename allows usage if file does not exist yet. |
| 233 | $this->assertSame( 'abcdef.png', wp_unique_filename( $testdir, 'abcdef.png' ), 'non-existent file should not have name changed' ); |
| 234 | // Difference in extension does not affect wp_unique_filename by default (canola.jpg exists). |
| 235 | $this->assertSame( 'canola.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should not have name changed' ); |
| 236 | // Run again to prove no memory. |
| 237 | $this->assertSame( 'canola.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should not have name changed' ); |
| 238 | // Actual clash recognized. |
| 239 | $this->assertSame( 'canola-1.jpg', wp_unique_filename( $testdir, 'canola.jpg' ), 'existing file should have name changed' ); |
| 240 | // Future clash by regenerated thumbnails not applicable. |
| 241 | $this->assertSame( 'codeispoetry.jpg', wp_unique_filename( $testdir, 'codeispoetry.jpg' ), 'non-existent file by extension should not have name changed' ); |
| 242 | |
| 243 | // Now output jpg thumbnails for png files. |
| 244 | add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); |
| 245 | |
| 246 | // Standard test that wp_unique_filename allows usage if file does not exist yet. |
| 247 | $this->assertSame( 'abcdef.png', wp_unique_filename( $testdir, 'abcdef.png' ), 'non-existent file should not have name changed' ); |
| 248 | // Difference in extension does affect wp_unique_filename when thumbnails use existing file's type. |
| 249 | $this->assertSame( 'canola-1.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should have name changed when thumbnails use existing file\'s type' ); |
| 250 | // Run again to prove no memory. |
| 251 | $this->assertSame( 'canola-1.png', wp_unique_filename( $testdir, 'canola.png' ), 'clashing base filename but not extension should have name changed when thumbnails use existing file\'s type' ); |
| 252 | // Actual clash recognized. |
| 253 | $this->assertSame( 'canola-1.jpg', wp_unique_filename( $testdir, 'canola.jpg' ), 'existing file should have name changed' ); |
| 254 | // Future clash by regenerated thumbnails recognized. |
| 255 | $this->assertSame( 'codeispoetry-1.jpg', wp_unique_filename( $testdir, 'codeispoetry.jpg' ), 'non-existent thumbnails that could be created on regenerate thumbnails recognized' ); |
| 256 | |
| 257 | remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_format_handler' ) ); |
| 258 | remove_filter( 'upload_dir', array( $this, 'upload_dir_patch_basedir' ) ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Changes the output format for a png file's generated thumbnails to be jpg. |
| 263 | * |
| 264 | * @param array $formats |
| 265 | * |
| 266 | * @return array |
| 267 | */ |
| 268 | public function image_editor_output_format_handler( $formats ) { |
| 269 | $formats['image/png'] = 'image/jpeg'; |
| 270 | |
| 271 | return $formats; |
| 272 | } |
| 273 | |
| 274 | /** |
| 1999 | |
| 2000 | /** |
| 2001 | * @ticket 53668 |
| 2002 | */ |
| 2003 | public function test_wp_get_default_extension_for_mime_type() { |
| 2004 | $this->assertEquals( 'jpg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpg not returned as default extension for "image/jpeg"' ); |
| 2005 | $this->assertNotEquals( 'jpeg', wp_get_default_extension_for_mime_type( 'image/jpeg' ), 'jpeg should not be returned as default extension for "image/jpeg"' ); |
| 2006 | $this->assertEquals( 'png', wp_get_default_extension_for_mime_type( 'image/png' ), 'png not returned as default extension for "image/png"' ); |
| 2007 | $this->assertFalse( wp_get_default_extension_for_mime_type( 'wibble/wobble' ), 'false not returned for unrecognized mime type' ); |
| 2008 | $this->assertFalse( wp_get_default_extension_for_mime_type(), 'false not returned when no mime type supplied' ); |
| 2009 | $this->assertFalse( wp_get_default_extension_for_mime_type( '' ), 'false not returned when empty string as mime type supplied' ); |
| 2010 | $this->assertFalse( wp_get_default_extension_for_mime_type( ' ' ), 'false not returned when empty string as mime type supplied' ); |
| 2011 | $this->assertFalse( wp_get_default_extension_for_mime_type( 123 ), 'false not returned when int as mime type supplied' ); |
| 2012 | $this->assertFalse( wp_get_default_extension_for_mime_type( null ), 'false not returned when null as mime type supplied' ); |
| 2013 | } |