Skip to:
Content

BuddyPress.org

Ticket #6336: 6336.04.patch

File 6336.04.patch, 6.2 KB (added by imath, 8 years ago)

Use mystery-man.jpg and humans.txt instead of adding files in tests assets

  • src/bp-core/bp-core-avatars.php

    diff --git src/bp-core/bp-core-avatars.php src/bp-core/bp-core-avatars.php
    index 0a8c51c..9408bd0 100644
    function bp_core_check_avatar_size( $file ) { 
    962962}
    963963
    964964/**
     965 * Get allowed avatar types
     966 *
     967 * @since  BuddyPress (2.3.0)
     968 */
     969function bp_core_get_allowed_avatar_types() {
     970        $allowed_types = array( 'jpeg', 'gif', 'png' );
     971
     972        /**
     973         * Use this filter to restrict image types
     974         *
     975         * @since BuddyPress (2.3.0)
     976         *
     977         * @param array list of image types
     978         */
     979        $avatar_types = (array) apply_filters( 'bp_core_get_allowed_avatar_types', $allowed_types );
     980
     981        if ( empty( $avatar_types ) ) {
     982                $avatar_types = $allowed_types;
     983        } else {
     984                $avatar_types = array_intersect( $allowed_types, $avatar_types );
     985        }
     986
     987        return array_values( $avatar_types );
     988}
     989
     990/**
     991 * Get allowed avatar mime types
     992 *
     993 * @since  BuddyPress (2.3.0)
     994 */
     995function bp_core_get_allowed_avatar_mimes() {
     996        $allowed_types  = bp_core_get_allowed_avatar_types();
     997        $validate_mimes = wp_match_mime_types( join( ',', $allowed_types ), wp_get_mime_types() );
     998        $allowed_mimes  = array_map( 'implode', $validate_mimes );
     999
     1000        /**
     1001         * Include jpg type if needed so that bp_core_check_avatar_type()
     1002         * will check for jpeg and jpg extensions.
     1003         */
     1004        if ( isset( $allowed_mimes['jpeg'] ) ) {
     1005                $allowed_mimes['jpg'] = $allowed_mimes['jpeg'];
     1006        }
     1007
     1008        return $allowed_mimes;
     1009}
     1010
     1011/**
    9651012 * Does the current avatar upload have an allowed file type?
    9661013 *
    9671014 * Permitted file types are JPG, GIF and PNG.
    function bp_core_check_avatar_size( $file ) { 
    9691016 * @param array $file The $_FILES array.
    9701017 * @return bool True if the file extension is permitted, otherwise false.
    9711018 */
    972 function bp_core_check_avatar_type($file) {
    973         if ( ( !empty( $file['file']['type'] ) && !preg_match('/(jpe?g|gif|png)$/i', $file['file']['type'] ) ) || !preg_match( '/(jpe?g|gif|png)$/i', $file['file']['name'] ) )
    974                 return false;
     1019function bp_core_check_avatar_type( $file ) {
     1020        $avatar_filetype = wp_check_filetype_and_ext( $file['file']['tmp_name'], $file['file']['name'], bp_core_get_allowed_avatar_mimes() );
    9751021
    976         return true;
     1022        if ( ! empty( $avatar_filetype['ext'] ) && ! empty( $avatar_filetype['type'] ) ) {
     1023                return true;
     1024        }
     1025
     1026        return false;
    9771027}
    9781028
    9791029/**
  • tests/phpunit/testcases/core/avatars.php

    diff --git tests/phpunit/testcases/core/avatars.php tests/phpunit/testcases/core/avatars.php
    index d37fe1a..9fab304 100644
    class BP_Tests_Avatars extends BP_UnitTestCase { 
    204204                $classes = explode( ' ', $matches[1] );
    205205                $this->assertSame( $expected, array_intersect_key( $expected, $classes ) );
    206206        }
     207
     208        /**
     209         * @group bp_core_check_avatar_type
     210         */
     211        public function test_bp_core_check_avatar_type() {
     212                $plugin_dir = trailingslashit( buddypress()->plugin_dir );
     213
     214                $file = array(
     215                        'file' => array(
     216                                'name' => 'humans.txt',
     217                                'type' => 'text/plain',
     218                                'tmp_name' => $plugin_dir . 'humans.txt',
     219                        )
     220                );
     221
     222                $this->assertFalse( bp_core_check_avatar_type( $file ) );
     223
     224                $file = array(
     225                        'file' => array(
     226                                'name' => 'mystery-man.jpg',
     227                                'type' => 'image/jpeg',
     228                                'tmp_name' => $plugin_dir . 'bp-core/images/mystery-man.jpg',
     229                        )
     230                );
     231
     232                $this->assertTrue( bp_core_check_avatar_type( $file ) );
     233
     234                $file = array(
     235                        'file' => array(
     236                                'name' => 'mystery-man.jpg',
     237                                'type' => 'application/octet-stream',
     238                                'tmp_name' => $plugin_dir . 'bp-core/images/mystery-man.jpg',
     239                        )
     240                );
     241
     242                $this->assertTrue( bp_core_check_avatar_type( $file ), 'flash is using application/octet-stream for image uploads' );
     243        }
     244
     245        /**
     246         * @group bp_core_check_avatar_type
     247         * @group bp_core_get_allowed_avatar_types
     248         */
     249        public function test_bp_core_get_allowed_avatar_types_filter() {
     250                add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
     251
     252                $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() );
     253
     254                remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
     255
     256                add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
     257
     258                $this->assertEquals( array( 'gif', 'png' ), bp_core_get_allowed_avatar_types() );
     259
     260                remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
     261
     262                add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
     263
     264                $this->assertEquals( array( 'jpeg', 'gif', 'png' ), bp_core_get_allowed_avatar_types() );
     265
     266                remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
     267        }
     268
     269        /**
     270         * @group bp_core_check_avatar_type
     271         * @group bp_core_get_allowed_avatar_mimes
     272         */
     273        public function test_bp_core_get_allowed_avatar_mimes() {
     274                $mimes = bp_core_get_allowed_avatar_mimes();
     275
     276                $this->assertEquals( array( 'jpeg', 'gif', 'png', 'jpg' ), array_keys( $mimes ) );
     277                $this->assertEquals( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( $mimes ) );
     278
     279                add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
     280
     281                $this->assertEquals( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) );
     282
     283                remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_add_type' ) );
     284
     285                add_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
     286
     287                $this->assertEquals( array( 'image/gif', 'image/png' ), array_values( bp_core_get_allowed_avatar_mimes() ) );
     288
     289                remove_filter( 'bp_core_get_allowed_avatar_types', array( $this, 'avatar_types_filter_remove_type' ) );
     290
     291                add_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
     292
     293                $this->assertEquals( array( 'image/jpeg', 'image/gif', 'image/png', 'image/jpeg' ), array_values( bp_core_get_allowed_avatar_mimes() ) );
     294
     295                remove_filter( 'bp_core_get_allowed_avatar_types', '__return_empty_array' );
     296        }
     297
     298        public function avatar_types_filter_add_type( $types ) {
     299                $types[] = 'bmp';
     300
     301                return $types;
     302        }
     303
     304        public function avatar_types_filter_remove_type( $types ) {
     305                $jpeg = array_shift( $types );
     306
     307                return $types;
     308        }
    207309}