Make WordPress Core

Changeset 37189


Ignore:
Timestamp:
04/13/2016 03:11:25 AM (9 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r36925 r37189  
    21862186    $ext = strtolower( $ext );
    21872187
    2188     /**
    2189      * Filter file type based on the extension name.
    2190      *
    2191      * @since 2.5.0
    2192      *
    2193      * @see wp_ext2type()
    2194      *
    2195      * @param array $ext2type Multi-dimensional array with extensions for a default set
    2196      *                        of file types.
    2197      */
    2198     $ext2type = apply_filters( 'ext2type', array(
    2199         'image'       => array( 'jpg', 'jpeg', 'jpe',  'gif',  'png',  'bmp',   'tif',  'tiff', 'ico' ),
    2200         'audio'       => array( 'aac', 'ac3',  'aif',  'aiff', 'm3a',  'm4a',   'm4b',  'mka',  'mp1',  'mp2',  'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
    2201         'video'       => array( '3g2',  '3gp', '3gpp', 'asf', 'avi',  'divx', 'dv',   'flv',  'm4v',   'mkv',  'mov',  'mp4',  'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt',  'rm', 'vob', 'wmv' ),
    2202         'document'    => array( 'doc', 'docx', 'docm', 'dotm', 'odt',  'pages', 'pdf',  'xps',  'oxps', 'rtf',  'wp', 'wpd', 'psd', 'xcf' ),
    2203         'spreadsheet' => array( 'numbers',     'ods',  'xls',  'xlsx', 'xlsm',  'xlsb' ),
    2204         'interactive' => array( 'swf', 'key',  'ppt',  'pptx', 'pptm', 'pps',   'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
    2205         'text'        => array( 'asc', 'csv',  'tsv',  'txt' ),
    2206         'archive'     => array( 'bz2', 'cab',  'dmg',  'gz',   'rar',  'sea',   'sit',  'sqx',  'tar',  'tgz',  'zip', '7z' ),
    2207         'code'        => array( 'css', 'htm',  'html', 'php',  'js' ),
    2208     ) );
    2209 
     2188    $ext2type = wp_get_ext_types();
    22102189    foreach ( $ext2type as $type => $exts )
    22112190        if ( in_array( $ext, $exts ) )
     
    24522431    ) );
    24532432}
     2433
     2434/**
     2435 * Retrieve list of common file extensions and their types.
     2436 *
     2437 * @since 4.6.0
     2438 *
     2439 * @return array Array of file extensions types keyed by the type of file.
     2440 */
     2441function wp_get_ext_types() {
     2442
     2443    /**
     2444     * Filter file type based on the extension name.
     2445     *
     2446     * @since 2.5.0
     2447     *
     2448     * @see wp_ext2type()
     2449     *
     2450     * @param array $ext2type Multi-dimensional array with extensions for a default set
     2451     *                        of file types.
     2452     */
     2453    return apply_filters( 'ext2type', array(
     2454        'image'       => array( 'jpg', 'jpeg', 'jpe',  'gif',  'png',  'bmp',   'tif',  'tiff', 'ico' ),
     2455        'audio'       => array( 'aac', 'ac3',  'aif',  'aiff', 'm3a',  'm4a',   'm4b',  'mka',  'mp1',  'mp2',  'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ),
     2456        'video'       => array( '3g2',  '3gp', '3gpp', 'asf', 'avi',  'divx', 'dv',   'flv',  'm4v',   'mkv',  'mov',  'mp4',  'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt',  'rm', 'vob', 'wmv' ),
     2457        'document'    => array( 'doc', 'docx', 'docm', 'dotm', 'odt',  'pages', 'pdf',  'xps',  'oxps', 'rtf',  'wp', 'wpd', 'psd', 'xcf' ),
     2458        'spreadsheet' => array( 'numbers',     'ods',  'xls',  'xlsx', 'xlsm',  'xlsb' ),
     2459        'interactive' => array( 'swf', 'key',  'ppt',  'pptx', 'pptm', 'pps',   'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ),
     2460        'text'        => array( 'asc', 'csv',  'tsv',  'txt' ),
     2461        'archive'     => array( 'bz2', 'cab',  'dmg',  'gz',   'rar',  'sea',   'sit',  'sqx',  'tar',  'tgz',  'zip', '7z' ),
     2462        'code'        => array( 'css', 'htm',  'html', 'php',  'js' ),
     2463    ) );
     2464}
     2465
    24542466/**
    24552467 * Retrieve list of allowed mime types and file extensions.
  • trunk/tests/phpunit/tests/functions.php

    r36883 r37189  
    814814        );
    815815    }
     816
     817    /**
     818     * @ticket 35987
     819     */
     820    public function test_wp_get_ext_types() {
     821        $extensions = wp_get_ext_types();
     822
     823        $this->assertInternalType( 'array', $extensions );
     824        $this->assertNotEmpty( $extensions );
     825
     826        add_filter( 'ext2type', '__return_empty_array' );
     827        $extensions = wp_get_ext_types();
     828        $this->assertSame( array(), $extensions );
     829
     830        remove_filter( 'ext2type', '__return_empty_array' );
     831        $extensions = wp_get_ext_types();
     832        $this->assertInternalType( 'array', $extensions );
     833        $this->assertNotEmpty( $extensions );
     834    }
     835
     836    /**
     837     * @ticket 35987
     838     */
     839    public function test_wp_ext2type() {
     840        $extensions = wp_get_ext_types();
     841
     842        foreach ( $extensions as $type => $extensionList ) {
     843            foreach ( $extensionList as $extension ) {
     844                $this->assertEquals( $type, wp_ext2type( $extension ) );
     845                $this->assertEquals( $type, wp_ext2type( strtoupper( $extension ) ) );
     846            }
     847        }
     848
     849        $this->assertNull( wp_ext2type( 'unknown_format' ) );
     850    }
    816851}
Note: See TracChangeset for help on using the changeset viewer.