Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/17/2015 11:14:39 AM (11 years ago)
Author:
imath
Message:

In BP_Attachment->upload(), only filter the WordPress upload_dir if the callback function is set.

This callback function can be passed as the second argument of BP_Attachment->upload(), or dynamically created if the specific $base_dir parameter was provided by the extending class.

About this second case, if the extending class forgets to set the $base_dir parameter or do it on purpose in order to use the WordPress default upload directory, we will not filter the WordPress upload_dir to avoid a warning.

This commit also adds a third argument to BP_Attachment->upload(), in case there is a need to pass a specific time (yyyy/mm) to set the WordPress default upload sub directory.

Fixes #6438

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/core/class-bp-attachment.php

    r9831 r9874  
    1414                parent::setUp();
    1515                add_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    16                 add_filter( 'bp_attachment_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
    17                 add_filter( 'xprofile_avatar_upload_dir',     array( $this, 'filter_upload_dir' ), 10, 1 );
    18                 add_filter( 'groups_avatar_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     16                add_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
    1917                $this->upload_results = array();
    2018                $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg';
     
    2422                parent::tearDown();
    2523                remove_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    26                 remove_filter( 'bp_attachment_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
    27                 remove_filter( 'xprofile_avatar_upload_dir',     array( $this, 'filter_upload_dir' ), 10, 1 );
    28                 remove_filter( 'groups_avatar_upload_dir',       array( $this, 'filter_upload_dir' ), 10, 1 );
     24                remove_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
    2925                $this->upload_results = array();
    3026                $this->image_file = '';
     
    176172        /**
    177173         * @group upload
     174         */
     175        public function test_bp_attachment_upload_no_base_dir_specific_time() {
     176                $reset_files = $_FILES;
     177                $reset_post = $_POST;
     178
     179                $attachment_class = new BPTest_Attachment_Extension( array(
     180                        'action'                => 'attachment_action',
     181                        'file_input'            => 'attachment_file_input',
     182                ) );
     183
     184                $_POST['action'] = $attachment_class->action;
     185                $_FILES[ $attachment_class->file_input ] = array(
     186                        'tmp_name' => $this->image_file,
     187                        'name'     => 'mystery-man.jpg',
     188                        'type'     => 'image/jpeg',
     189                        'error'    => 0,
     190                        'size'     => filesize( $this->image_file ),
     191                );
     192
     193                $time = '2015/01';
     194
     195                $upload = $attachment_class->upload( $_FILES, '', $time );
     196
     197                // If no base_dir was provided, default WordPress uploads dir should be used.
     198                $this->assertEquals( $upload['file'], $attachment_class->upload_path . '/' . $time . '/mystery-man.jpg' );
     199
     200                // clean up!
     201                $_FILES = $reset_files;
     202                $_POST = $reset_post;
     203        }
     204
     205        /**
     206         * @group upload
    178207         * @group avatar
    179208         */
Note: See TracChangeset for help on using the changeset viewer.