Skip to:
Content

BuddyPress.org

Changeset 9744


Ignore:
Timestamp:
04/12/2015 07:42:38 PM (9 years ago)
Author:
boonebgorges
Message:

Ensure that corresponding notifications are deleted when a message thread is deleted.

This means deleting the notifications for each message in the thread, for each
recipient on the thread. Previously, only a subset of these were being deleted.

This changeset removes a test that used a dummy thread_id, a testing
technique that doesn't work with the new looping technique used for
notification deletion. The new unit test is a proper replacement.

Props pareshradadiya for an initial patch.
Fixes #6329.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-notifications.php

    r9351 r9744  
    298298 * @since BuddyPress (2.0.0)
    299299 *
    300  * @param int $message_id ID of the message.
    301  */
    302 function bp_messages_message_delete_notifications( $message_id = 0 ) {
    303     if ( bp_is_active( 'notifications' ) && ! empty( $message_id ) ) {
    304         bp_notifications_delete_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' );
    305     }
    306 }
    307 add_action( 'messages_thread_deleted_thread', 'bp_messages_message_delete_notifications', 10, 1 );
     300 * @param int   $message_id  ID of the thread.
     301 * @param array $message_ids IDs of the messages.
     302 */
     303function bp_messages_message_delete_notifications( $thread_id, $message_ids ) {
     304    if ( ! bp_is_active( 'notifications' ) ) {
     305        return;
     306    }
     307
     308    // For each recipient, delete notifications corresponding to each message.
     309    $thread = new BP_Messages_Thread( $thread_id );
     310    foreach ( $thread->get_recipients() as $recipient ) {
     311        foreach ( $message_ids as $message_id ) {
     312            bp_notifications_delete_notifications_by_item_id( $recipient->user_id, (int) $message_id, buddypress()->messages->id, 'new_message' );
     313        }
     314    }
     315}
     316add_action( 'bp_messages_thread_after_delete', 'bp_messages_message_delete_notifications', 10, 2 );
  • trunk/tests/phpunit/testcases/messages/notifications.php

    r9208 r9744  
    1313
    1414        $this->filter_fired = '';
    15     }
    16     /**
    17      * @group bp_messages_message_delete_notifications
    18      */
    19     public function test_bp_messages_message_delete_notifications() {
    20         $current_user = get_current_user_id();
    21         $u = $this->factory->user->create();
    22         $this->set_current_user( $u );
    23 
    24         // Dummy thread ID
    25         $t = 12;
    26 
    27         // Admin
    28         $n = $this->factory->notification->create( array(
    29             'component_name' => 'messages',
    30             'user_id' => $u,
    31             'item_id' => $t,
    32             'component_action' => 'new_message',
    33         ) );
    34 
    35         $notifications = BP_Notifications_Notification::get( array(
    36             'user_id' => $u,
    37         ) );
    38 
    39         // Double check it's there
    40         $this->assertEquals( array( $n ), wp_list_pluck( $notifications, 'id' ) );
    41 
    42         // fire the hook
    43         do_action( 'messages_thread_deleted_thread', $t );
    44 
    45         $notifications = BP_Notifications_Notification::get( array(
    46             'user_id' => $u,
    47         ) );
    48 
    49         $this->assertEmpty( $notifications );
    50 
    51         $this->set_current_user( $current_user );
    5215    }
    5316
     
    148111    }
    149112
     113    /**
     114     * @ticket BP6329
     115     */
     116    public function test_messages_notifications_should_be_deleted_when_corresponding_message_is_deleted() {
     117        if ( ! bp_is_active( 'messages' ) ) {
     118            $this->markTestSkipped( __METHOD__ . ' requires the Messages component.' );
     119        }
     120
     121        $u1 = $this->factory->user->create();
     122        $u2 = $this->factory->user->create();
     123
     124        $t1 = messages_new_message( array(
     125            'sender_id'  => $u1,
     126            'recipients' => array( $u2 ),
     127            'subject'    => 'A new message',
     128            'content'    => 'Hey there!',
     129        ) );
     130
     131        // Verify that a notification has been created for the message.
     132        $n1 = BP_Notifications_Notification::get( array(
     133            'component' => 'messages',
     134            'user_id' => $u2,
     135        ) );
     136        $this->assertNotEmpty( $n1 );
     137
     138        $this->assertTrue( messages_delete_thread( $t1 ) );
     139
     140        $n2 = BP_Notifications_Notification::get( array(
     141            'component' => 'messages',
     142            'user_id' => $u2,
     143        ) );
     144        $this->assertSame( array(), $n2 );
     145    }
     146
    150147    public function notification_filter_callback( $value ) {
    151148        $this->filter_fired = current_filter();
Note: See TracChangeset for help on using the changeset viewer.