Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/06/2015 11:53:33 PM (9 years ago)
Author:
imath
Message:

Allow class extending the BP_Attachment class to get the original WordPress upload dir within their upload_dir_filter() method.

To do so, you simply need to add the argument $upload_dir_filter_args to the array you use inside your constructor and set it to 1.

Props rittesh.patel

Fixes #6591

File:
1 edited

Legend:

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

    r10178 r10194  
    1313    public function setUp() {
    1414        parent::setUp();
    15         add_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    16         add_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
     15        add_filter( 'bp_attachment_upload_overrides',        array( $this, 'filter_overrides' ),       10, 1 );
     16        add_filter( 'upload_dir',                            array( $this, 'filter_upload_dir' ),      20, 1 );
     17        add_filter( 'bp_attachments_cover_image_upload_dir', array( $this, 'filter_cover_image_dir' ), 10, 2 );
    1718        $this->upload_results = array();
    1819        $this->image_file = trailingslashit( buddypress()->plugin_dir ) . 'bp-core/images/mystery-man.jpg';
     20        $this->original_upload_dir = array();
    1921    }
    2022
    2123    public function tearDown() {
    2224        parent::tearDown();
    23         remove_filter( 'bp_attachment_upload_overrides', array( $this, 'filter_overrides' ),  10, 1 );
    24         remove_filter( 'upload_dir',                     array( $this, 'filter_upload_dir' ), 20, 1 );
     25        remove_filter( 'bp_attachment_upload_overrides',     array( $this, 'filter_overrides' ),       10, 1 );
     26        remove_filter( 'upload_dir',                         array( $this, 'filter_upload_dir' ),      20, 1 );
     27        add_filter( 'bp_attachments_cover_image_upload_dir', array( $this, 'filter_cover_image_dir' ), 10, 2 );
    2528        $this->upload_results = array();
    2629        $this->image_file = '';
     30        $this->original_upload_dir = array();
    2731    }
    2832
     
    4448
    4549        return $upload_dir;
     50    }
     51
     52    public function filter_cover_image_dir( $cover_dir, $upload_dir ) {
     53        $this->original_upload_dir = $upload_dir;
     54
     55        return $cover_dir;
    4656    }
    4757
     
    454464        $this->assertTrue( 3 === $image_data['meta']['orientation'] );
    455465    }
     466
     467    /**
     468     * @group upload
     469     * @group cover_images
     470     */
     471    public function test_bp_attachment_upload_dir_filter_arg() {
     472        $reset_files = $_FILES;
     473        $reset_post = $_POST;
     474
     475        $attachment_class = new BPTest_Attachment_Extension( array(
     476            'action'                 => 'attachment_action',
     477            'file_input'             => 'attachment_file_input',
     478            'base_dir'               => 'attachment_base_dir',
     479            'upload_dir_filter_args' => 1,
     480        ) );
     481
     482        $_POST['action'] = $attachment_class->action;
     483        $_FILES[ $attachment_class->file_input ] = array(
     484            'tmp_name' => $this->image_file,
     485            'name'     => 'mystery-man.jpg',
     486            'type'     => 'image/jpeg',
     487            'error'    => 0,
     488            'size'     => filesize( $this->image_file ),
     489        );
     490
     491        // Simulate an upload
     492        $attachment_class->upload( $_FILES );
     493
     494        // Remove the filter used to fake uploads
     495        remove_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20, 1 );
     496
     497        $this->assertSame( $attachment_class->original_upload_dir, wp_upload_dir() );
     498
     499        // Restore the filter used to fake uploads
     500        add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ), 20, 1 );
     501
     502        $this->assertTrue( 1 === $attachment_class->upload_dir_filter_args );
     503
     504        $cover_image_class = new BP_Attachment_Cover_Image();
     505
     506        // Simulate an upload
     507        $cover_image_class->upload( $_FILES );
     508
     509        // Should be empty
     510        $this->assertEmpty( $this->original_upload_dir );
     511
     512        $this->assertTrue( 0 === $cover_image_class->upload_dir_filter_args );
     513
     514        $_FILES = $reset_files;
     515        $_POST = $reset_post;
     516    }
    456517}
Note: See TracChangeset for help on using the changeset viewer.