Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/03/2024 05:55:47 PM (8 months ago)
Author:
espellcaste
Message:

Messages: filter threads by recipient IDs.

bp_has_message_threads (and the BP REST API) can filter thread(s) messages by recipient IDs. Very useful when trying to identify if a member already messaged another.

Props imath and slaFFik.

See https://github.com/buddypress/BP-REST/issues/455
Closes https://github.com/buddypress/buddypress/pull/394
See #9229 and #9145
Fixes #9157

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/messages/test-controller.php

    r14037 r14069  
    7272        $this->assertCount( 1, $data[0]['messages'] );
    7373
    74         // Check the thread data for the requested user id => `$u1`.
    7574        $this->check_thread_data( $this->endpoint->get_thread_object( $data[0]['id'], $u1 ), $data[0] );
    7675    }
     
    143142        $this->assertCount( 1, $all_data[0]['recipients'] );
    144143        $this->assertCount( 1, $all_data[1]['recipients'] );
     144    }
     145
     146    /**
     147     * @BP9157
     148     * @group get_items
     149     */
     150    public function test_filter_threads_by_recipients() {
     151        $u1 = static::factory()->user->create();
     152        $u2 = static::factory()->user->create();
     153        $u3 = static::factory()->user->create();
     154
     155        $thread = $this->bp::factory()->message->create_and_get(
     156            array(
     157                'sender_id'  => $u1,
     158                'recipients' => array( $u2 ),
     159                'subject'    => 'First Thread',
     160            )
     161        );
     162
     163        $this->bp::factory()->message->create(
     164            array(
     165                'thread_id'  => $thread->thread_id,
     166                'sender_id'  => $u2,
     167                'recipients' => array( $u1 ),
     168            )
     169        );
     170
     171        $thread2 = $this->bp::factory()->message->create_and_get(
     172            array(
     173                'sender_id'  => $u1,
     174                'recipients' => array( $u3 ),
     175                'subject'    => 'Second Thread',
     176            )
     177        );
     178
     179        $this->bp::factory()->message->create(
     180            array(
     181                'thread_id'  => $thread2->thread_id,
     182                'sender_id'  => $u3,
     183                'recipients' => array( $u1 ),
     184            )
     185        );
     186
     187        $this->bp::set_current_user( $this->user );
     188
     189        $request = new WP_REST_Request( 'GET', $this->endpoint_url );
     190        $request->set_param( 'context', 'view' );
     191        $request->set_param( 'user_id', $u1 );
     192        $request->set_param( 'includes', array( $u3 ) );
     193        $response = $this->server->dispatch( $request );
     194
     195        $this->assertEquals( 200, $response->get_status() );
     196
     197        $data = $response->get_data();
     198
     199        $this->assertNotEmpty( $data );
     200
     201        $a_ids = wp_list_pluck( $data, 'id' );
     202
     203        $this->assertCount( 1, $a_ids );
     204        $this->assertSame( $thread2->thread_id, $a_ids[0] );
    145205    }
    146206
Note: See TracChangeset for help on using the changeset viewer.