From 26a5e78d947a2d2da55ad38073c1952c7f91a110 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 13 Apr 2016 03:11:25 +0000 Subject: [PATCH] Improve testability and coverage of `wp_ext2type()`. * Following pattern of `wp_get_mime_types()`, introduce `wp_get_ext_types()` function. New function returns a filtered list of file types with their extensions. * Use this function in new tests for `wp_ext2type()`. Props borgesbruno. Fixes #35987. git-svn-id: https://develop.svn.wordpress.org/trunk@37189 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 56 +++++++++++++++++++------------ tests/phpunit/tests/functions.php | 35 +++++++++++++++++++ 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 0e720ee168..4de9880db0 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2185,28 +2185,7 @@ function wp_upload_bits( $name, $deprecated, $bits, $time = null ) { function wp_ext2type( $ext ) { $ext = strtolower( $ext ); - /** - * Filter file type based on the extension name. - * - * @since 2.5.0 - * - * @see wp_ext2type() - * - * @param array $ext2type Multi-dimensional array with extensions for a default set - * of file types. - */ - $ext2type = apply_filters( 'ext2type', array( - 'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ), - 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), - 'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), - 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ), - 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ), - 'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ), - 'text' => array( 'asc', 'csv', 'tsv', 'txt' ), - 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ), - 'code' => array( 'css', 'htm', 'html', 'php', 'js' ), - ) ); - + $ext2type = wp_get_ext_types(); foreach ( $ext2type as $type => $exts ) if ( in_array( $ext, $exts ) ) return $type; @@ -2451,6 +2430,39 @@ function wp_get_mime_types() { 'pages' => 'application/vnd.apple.pages', ) ); } + +/** + * Retrieve list of common file extensions and their types. + * + * @since 4.6.0 + * + * @return array Array of file extensions types keyed by the type of file. + */ +function wp_get_ext_types() { + + /** + * Filter file type based on the extension name. + * + * @since 2.5.0 + * + * @see wp_ext2type() + * + * @param array $ext2type Multi-dimensional array with extensions for a default set + * of file types. + */ + return apply_filters( 'ext2type', array( + 'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ), + 'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), + 'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), + 'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ), + 'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ), + 'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ), + 'text' => array( 'asc', 'csv', 'tsv', 'txt' ), + 'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ), + 'code' => array( 'css', 'htm', 'html', 'php', 'js' ), + ) ); +} + /** * Retrieve list of allowed mime types and file extensions. * diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 6d6e1779cb..7d3beb3d3c 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -813,4 +813,39 @@ class Tests_Functions extends WP_UnitTestCase { array( '2016-03-02T19:13:00', '16-03-02 19:13' ) ); } + + /** + * @ticket 35987 + */ + public function test_wp_get_ext_types() { + $extensions = wp_get_ext_types(); + + $this->assertInternalType( 'array', $extensions ); + $this->assertNotEmpty( $extensions ); + + add_filter( 'ext2type', '__return_empty_array' ); + $extensions = wp_get_ext_types(); + $this->assertSame( array(), $extensions ); + + remove_filter( 'ext2type', '__return_empty_array' ); + $extensions = wp_get_ext_types(); + $this->assertInternalType( 'array', $extensions ); + $this->assertNotEmpty( $extensions ); + } + + /** + * @ticket 35987 + */ + public function test_wp_ext2type() { + $extensions = wp_get_ext_types(); + + foreach ( $extensions as $type => $extensionList ) { + foreach ( $extensionList as $extension ) { + $this->assertEquals( $type, wp_ext2type( $extension ) ); + $this->assertEquals( $type, wp_ext2type( strtoupper( $extension ) ) ); + } + } + + $this->assertNull( wp_ext2type( 'unknown_format' ) ); + } }