Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/11/2021 12:00:16 PM (22 months ago)
Author:
imath
Message:

Introduce a new method to the BP Attachment API to add file revisions

The BP_Attachment::add_revision() method is primarily used by the BP_Attachment_Avatar class to keep the history of previously uploaded user avatars (and potentially other objects avatar).

When a user uploads a new avatar, the previously uploaded one is moved inside an history subdirectory of the user's avatar directory. This avatars history is then available for the new_avatar activities to display the avatar the user had at the time these were published.

Props vapvarun, oztaser

See #8581

File:
1 edited

Legend:

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

    r12840 r13175  
    407407    }
    408408
     409    /**
     410     * @group add_revision
     411     */
     412    public function test_bp_attachment_add_revision() {
     413        if ( false === _wp_image_editor_choose() || version_compare( phpversion(), '7.0' , '<' ) ) {
     414            $this->markTestSkipped( 'This test requires PHP >= 7.0 and to have a valid image editor that is compatible with WordPress.' );
     415        }
     416
     417        $image = BP_TESTS_DIR . 'assets/upside-down.jpg';
     418
     419        $attachment = new BPTest_Attachment_Extension(
     420            array(
     421                'base_dir'   => 'add_revision',
     422                'action'     => 'attachment_action',
     423                'file_input' => 'attachment_file_input',
     424            )
     425        );
     426
     427        $abs_path_copy = $attachment->upload_path . '/upside-down.jpg';
     428        copy( $image, $abs_path_copy );
     429
     430        $revision = $attachment->add_revision(
     431            'media',
     432            array(
     433                'file_abspath' => $abs_path_copy,
     434                'file_id'      => 'media',
     435            )
     436        );
     437
     438        $this->assertFalse( file_exists( $abs_path_copy ) );
     439        $this->assertTrue( file_exists( $revision->path ) );
     440
     441        // Cleanup
     442        @unlink( $revision->path );
     443        @rmdir( dirname( $revision->path ) );
     444        $this->clean_files( 'add_revision' );
     445    }
     446
     447    /**
     448     * @group add_revision
     449     * @group avatars
     450     */
     451    public function test_bp_attachment_add_avatar_history() {
     452        if ( false === _wp_image_editor_choose() || version_compare( phpversion(), '7.0' , '<' ) ) {
     453            $this->markTestSkipped( 'This test requires PHP >= 7.0 and to have a valid image editor that is compatible with WordPress.' );
     454        }
     455
     456        $image = BP_TESTS_DIR . 'assets/upside-down.jpg';
     457
     458        $attachment = new BPTest_Attachment_Extension(
     459            array(
     460                'base_dir'   => 'add_history',
     461                'action'     => 'attachment_action',
     462                'file_input' => 'attachment_file_input',
     463            )
     464        );
     465
     466        $abs_path_copy = $attachment->upload_path . '/upside-down.jpg';
     467        copy( $image, $abs_path_copy );
     468
     469        $revision = $attachment->add_revision(
     470            'avatar',
     471            array(
     472                'file_abspath' => $abs_path_copy,
     473                'file_id'      => 'avatar',
     474            )
     475        );
     476
     477        $this->assertFalse( file_exists( $abs_path_copy ) );
     478        $this->assertTrue( file_exists( $revision->path ) );
     479        $this->assertSame( $attachment->url . '/history/upside-down.jpg', $revision->url );
     480
     481        // Cleanup
     482        @unlink( $revision->path );
     483        @rmdir( dirname( $revision->path ) );
     484        $this->clean_files( 'add_history' );
     485    }
     486
    409487    public function limit_to_50px( $max_width ) {
    410488        return 50;
Note: See TracChangeset for help on using the changeset viewer.