Ticket #8508: 8508-2.diff
| File 8508-2.diff, 25.8 KB (added by , 4 years ago) |
|---|
-
src/bp-messages/bp-messages-notifications.php
diff --git src/bp-messages/bp-messages-notifications.php src/bp-messages/bp-messages-notifications.php index e2855d746..176a950d6 100644
function bp_messages_message_delete_notifications( $thread_id, $message_ids ) { 284 284 $thread = new BP_Messages_Thread( $thread_id ); 285 285 foreach ( $thread->get_recipients() as $recipient ) { 286 286 foreach ( $message_ids as $message_id ) { 287 bp_notifications_delete_notifications_by_item_id( $recipient->user_id, (int) $message_id, buddypress()->messages->id, 'new_message' ); 287 if ( ! empty( $recipient->user_id ) ) { 288 bp_notifications_delete_notifications_by_item_id( 289 $recipient->user_id, 290 (int) $message_id, 291 buddypress()->messages->id, 292 'new_message' 293 ); 294 } 288 295 } 289 296 } 290 297 } -
src/bp-messages/bp-messages-template.php
diff --git src/bp-messages/bp-messages-template.php src/bp-messages/bp-messages-template.php index d95da454c..5647c4426 100644
function bp_message_get_recipient_usernames() { 1625 1625 * 1626 1626 * @param array|string $args { 1627 1627 * Array of arguments. All are optional. 1628 * @type int $thread_id ID of the thread whose messages you are displaying. 1629 * Default: if viewing a thread, the thread ID will be parsed from 1630 * the URL (bp_action_variable( 0 )). 1631 * @type string $order 'ASC' or 'DESC'. Default: 'ASC'. 1632 * @type bool $update_meta_cache Whether to pre-fetch metadata for 1633 * queried message items. Default: true. 1628 * @type int $thread_id ID of the thread whose messages you are displaying. 1629 * Default: if viewing a thread, the thread ID will be parsed from 1630 * the URL (bp_action_variable( 0 )). 1631 * @type string $order 'ASC' or 'DESC'. Default: 'ASC'. 1632 * @type bool $update_meta_cache Whether to pre-fetch metadata for 1633 * queried message items. Default: true. 1634 * @type int|null $page Page of messages being requested. Default to null, meaning all. 1635 * @type int|null $per_page Messages to return per page. Default to null, meaning all. 1634 1636 * } 1637 * 1635 1638 * @return bool True if there are messages to display, otherwise false. 1636 1639 */ 1637 1640 function bp_thread_has_messages( $args = '' ) { 1638 1641 global $thread_template; 1639 1642 1640 $r = bp_parse_args( $args, array( 1641 'thread_id' => false, 1642 'order' => 'ASC', 1643 'update_meta_cache' => true, 1644 ), 'thread_has_messages' ); 1643 $r = bp_parse_args( 1644 $args, 1645 array( 1646 'thread_id' => false, 1647 'order' => 'ASC', 1648 'update_meta_cache' => true, 1649 'page' => null, 1650 'per_page' => null, 1651 ), 1652 'thread_has_messages' 1653 ); 1645 1654 1646 1655 if ( empty( $r['thread_id'] ) && bp_is_messages_component() && bp_is_current_action( 'view' ) ) { 1647 1656 $r['thread_id'] = (int) bp_action_variable( 0 ); -
src/bp-messages/classes/class-bp-messages-thread.php
diff --git src/bp-messages/classes/class-bp-messages-thread.php src/bp-messages/classes/class-bp-messages-thread.php index bc5f93a5e..4c2d543a4 100644
class BP_Messages_Thread { 109 109 * Constructor. 110 110 * 111 111 * @since 1.0.0 112 * @since 10.0.0 Updated the `$args` with new paremeters. 112 113 * 113 * @see BP_Messages_Thread::populate() for full description of parameters. 114 * 115 * @param bool $thread_id ID for the message thread. 116 * @param string $order Order to display the messages in. 117 * @param array $args Array of arguments for thread querying. 114 * @param int $thread_id The message thread ID. 115 * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'. 116 * Defaults to 'ASC'. 117 * @param array $args { 118 * Array of arguments. 119 * @type int $user_id ID of the user to get the unread count. 120 * @type bool $update_meta_cache Whether to pre-fetch metadata for 121 * queried message items. Default: true. 122 * @type int|null $page Page of messages being requested. Default to null, meaning all. 123 * @type int|null $per_page Messages to return per page. Default to null, meaning all. 124 * @type string $order The order to sort the messages. Either 'ASC' or 'DESC'. 125 * Defaults to 'ASC'. 126 * @type int|null $recipients_page Page of recipients being requested. Default to null, meaning all. 127 * @type int|null $recipients_per_page Recipients to return per page. Defaults to null, meaning all. 128 * } 118 129 */ 119 public function __construct( $thread_id = false, $order = 'ASC', $args = array() ) {120 if ( $thread_id) {130 public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) { 131 if ( ! empty( $thread_id ) ) { 121 132 $this->populate( $thread_id, $order, $args ); 122 133 } 123 134 } … … class BP_Messages_Thread { 125 136 /** 126 137 * Populate method. 127 138 * 128 * Used in constructor.139 * Used in the constructor. 129 140 * 130 141 * @since 1.0.0 142 * @since 10.0.0 Updated the `$args` with new paremeters. 131 143 * 132 * @param int $thread_id The message thread ID. 133 * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'. 134 * @param array $args { 144 * @param int $thread_id The message thread ID. 145 * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'. 146 * Defaults to 'ASC'. 147 * @param array $args { 135 148 * Array of arguments. 136 * @type bool $update_meta_cache Whether to pre-fetch metadata for 137 * queried message items. Default: true. 149 * @type int $user_id ID of the user to get the unread count. 150 * @type bool $update_meta_cache Whether to pre-fetch metadata for 151 * queried message items. Default: true. 152 * @type int|null $page Page of messages being requested. Default to null, meaning all. 153 * @type int|null $per_page Messages to return per page. Default to null, meaning all. 154 * @type string $order The order to sort the messages. Either 'ASC' or 'DESC'. 155 * Defaults to 'ASC'. 156 * @type int|null $recipients_page Page of recipients being requested. Default to null, meaning all. 157 * @type int|null $recipients_per_page Recipients to return per page. Defaults to null, meaning all. 138 158 * } 139 * @return bool False on failure.159 * @return bool False if there are no messages. 140 160 */ 141 161 public function populate( $thread_id = 0, $order = 'ASC', $args = array() ) { 142 162 143 if ( 'ASC' !== $order && 'DESC' !== $order) {163 if ( ! in_array( strtoupper( $order ), array( 'ASC', 'DESC' ), true ) ) { 144 164 $order = 'ASC'; 145 165 } 146 166 … … class BP_Messages_Thread { 150 170 bp_loggedin_user_id(); 151 171 152 172 // Merge $args with our defaults. 153 $r = wp_parse_args( $args, array( 154 'user_id' => $user_id, 155 'update_meta_cache' => true 156 ) ); 173 $r = wp_parse_args( 174 $args, 175 array( 176 'user_id' => $user_id, 177 'update_meta_cache' => true, 178 'page' => null, 179 'per_page' => null, 180 'order' => $order, 181 'recipients_page' => null, 182 'recipients_per_page' => null, 183 ) 184 ); 157 185 158 186 $this->messages_order = $order; 159 187 $this->thread_id = (int) $thread_id; 160 188 161 189 // Get messages for thread. 162 $this->messages = self::get_messages( $this->thread_id );190 $this->messages = self::get_messages( $this->thread_id, $r ); 163 191 164 if ( empty( $this->messages ) || is_wp_error( $this->messages )) {192 if ( empty( $this->messages ) ) { 165 193 return false; 166 194 } 167 195 168 // Flip if order is DESC.169 if ( 'DESC' === $order ) {170 $this->messages = array_reverse( $this->messages );171 }172 173 196 $last_message_index = count( $this->messages ) - 1; 174 197 $this->last_message_id = $this->messages[ $last_message_index ]->id; 175 198 $this->last_message_date = $this->messages[ $last_message_index ]->date_sent; … … class BP_Messages_Thread { 182 205 } 183 206 184 207 // Fetch the recipients. 185 $this->recipients = $this->get_recipients( );208 $this->recipients = $this->get_recipients( $thread_id, $r ); 186 209 187 // Get the unread count for the logged inuser.210 // Get the unread count for the user. 188 211 if ( isset( $this->recipients[ $r['user_id'] ] ) ) { 189 212 $this->unread_count = $this->recipients[ $r['user_id'] ]->unread_count; 190 213 } … … class BP_Messages_Thread { 198 221 * Fires after a BP_Messages_Thread object has been populated. 199 222 * 200 223 * @since 2.2.0 224 * @since 10.0.0 Added `$r` as a parameter. 201 225 * 202 226 * @param BP_Messages_Thread $this Message thread object. 227 * @param array $r Array of paremeters. 203 228 */ 204 do_action( 'bp_messages_thread_post_populate', $this );229 do_action( 'bp_messages_thread_post_populate', $this, $r ); 205 230 } 206 231 207 232 /** … … class BP_Messages_Thread { 230 255 * Returns recipients for a message thread. 231 256 * 232 257 * @since 1.0.0 233 * @since 2.3.0 Added $thread_id as a parameter. 258 * @since 2.3.0 Added `$thread_id` as a parameter. 259 * @since 10.0.0 Added `$args` as a parameter. 234 260 * 235 261 * @global BuddyPress $bp The one true BuddyPress instance. 236 262 * @global wpdb $wpdb WordPress database object. 237 263 * 238 * @param int $thread_id The thread ID. 264 * @param int $thread_id Message thread ID. 265 * @param array $args { 266 * Array of arguments. 267 * @type int|null $recipients_page Page of recipients being requested. Default to all. 268 * @type int|null $recipients_per_page Recipients to return per page. Defaults to all. 269 * } 239 270 * @return array 240 271 */ 241 public function get_recipients( $thread_id = 0 ) {272 public function get_recipients( $thread_id = 0, $args = array() ) { 242 273 global $wpdb; 243 274 244 275 if ( empty( $thread_id ) ) { … … class BP_Messages_Thread { 247 278 248 279 $thread_id = (int) $thread_id; 249 280 281 if ( empty( $thread_id ) ) { 282 return array(); 283 } 284 285 $bp = buddypress(); 286 $r = wp_parse_args( 287 $args, 288 array( 289 'recipients_page' => null, 290 'recipients_per_page' => null, 291 ) 292 ); 293 294 // Get recipients from cache if available. 250 295 $recipients = wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ); 251 if ( false === $recipients ) {252 $bp = buddypress();253 296 254 $recipients = array(); 255 $sql = $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ); 256 $results = $wpdb->get_results( $sql ); 297 // Get recipients and cache it. 298 if ( empty( $recipients ) ) { 299 300 // Query recipients. 301 $results = $wpdb->get_results( 302 $wpdb->prepare( 303 "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", 304 $thread_id 305 ) 306 ); 257 307 308 $recipients = array(); 258 309 foreach ( (array) $results as $recipient ) { 259 310 $recipients[ $recipient->user_id ] = $recipient; 260 311 } 261 312 313 // Cast all items from the messages DB table as integers. 314 foreach ( (array) $recipients as $key => $data ) { 315 $recipients[ $key ] = (object) array_map( 'intval', (array) $data ); 316 } 317 318 // Cache recipients. 262 319 wp_cache_set( 'thread_recipients_' . $thread_id, $recipients, 'bp_messages' ); 263 320 } 264 321 265 // Cast all items from the messages DB table as integers. 266 foreach ( (array) $recipients as $key => $data ) { 267 $recipients[ $key ] = (object) array_map( 'intval', (array) $data ); 322 // Paginate the results. 323 if ( ! empty( $recipients ) && $r['recipients_per_page'] && $r['recipients_page'] ) { 324 $start = ( $r['recipients_page'] - 1 ) * ( $r['recipients_per_page'] ); 325 $recipients = array_slice( $recipients, $start, $r['recipients_per_page'] ); 268 326 } 269 327 270 328 /** 271 329 * Filters the recipients of a message thread. 272 330 * 273 331 * @since 2.2.0 332 * @since 10.0.0 Added `$r` as a parameter. 274 333 * 275 334 * @param array $recipients Array of recipient objects. 276 * @param int $thread_id ID of the current thread. 335 * @param int $thread_id ID of the thread. 336 * @param array $r An array of parameters. 277 337 */ 278 return apply_filters( 'bp_messages_thread_get_recipients', $recipients, $thread_id);338 return apply_filters( 'bp_messages_thread_get_recipients', (array) $recipients, (int) $thread_id, (array) $r ); 279 339 } 280 340 281 341 /** Static Functions ******************************************************/ 282 342 283 343 /** 284 * Get allmessages associated with a thread.344 * Get messages associated with a thread. 285 345 * 286 346 * @since 2.3.0 347 * @since 10.0.0 Added `$args` as a parameter. 287 348 * 288 349 * @global BuddyPress $bp The one true BuddyPress instance. 289 350 * @global wpdb $wpdb WordPress database object. 290 351 * 291 * @param int $thread_id The message thread ID. 292 * 293 * @return array List of messages associated with a thread. 352 * @param int $thread_id The message thread ID. 353 * @param array $args { 354 * Array of arguments. 355 * @type int|null $page Page of messages being requested. Default to all. 356 * @type int|null $per_page Messages to return per page. Default to all. 357 * @type string $order The order to sort the messages. Either 'ASC' or 'DESC'. 358 * Defaults to 'ASC'. 359 * } 360 * @return array 294 361 */ 295 public static function get_messages( $thread_id = 0 ) { 362 public static function get_messages( $thread_id = 0, $args = array() ) { 363 global $wpdb; 364 296 365 $thread_id = (int) $thread_id; 297 $messages = wp_cache_get( $thread_id, 'bp_messages_threads' ); 366 if ( empty( $thread_id ) ) { 367 return array(); 368 } 298 369 299 if ( false === $messages ) { 300 global $wpdb; 370 $bp = buddypress(); 371 $r = wp_parse_args( 372 $args, 373 array( 374 'page' => null, 375 'per_page' => null, 376 'order' => 'ASC', 377 ) 378 ); 301 379 302 $bp = buddypress(); 380 // Fallback. 381 if ( ! in_array( strtoupper( $r['order'] ), array( 'ASC', 'DESC' ), true ) ) { 382 $r['order'] = 'ASC'; 383 } 303 384 304 // Always sort by ASC by default.305 $messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", $thread_id ));385 // Get messages from cache if available. 386 $messages = wp_cache_get( $thread_id, 'bp_messages_threads' ); 306 387 388 // Get messages and cache it. 389 if ( empty( $messages ) ) { 390 391 $messages = $wpdb->get_results( 392 $wpdb->prepare( 393 "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC", 394 $thread_id 395 ) 396 ); 397 398 // Integer casting. 399 foreach ( $messages as $key => $data ) { 400 $messages[ $key ]->id = (int) $messages[ $key ]->id; 401 $messages[ $key ]->thread_id = (int) $messages[ $key ]->thread_id; 402 $messages[ $key ]->sender_id = (int) $messages[ $key ]->sender_id; 403 } 404 405 // Cache request. 307 406 wp_cache_set( $thread_id, (array) $messages, 'bp_messages_threads' ); 308 407 } 309 408 310 // Integer casting. 311 foreach ( $messages as $key => $data ) { 312 $messages[ $key ]->id = (int) $messages[ $key ]->id; 313 $messages[ $key ]->thread_id = (int) $messages[ $key ]->thread_id; 314 $messages[ $key ]->sender_id = (int) $messages[ $key ]->sender_id; 409 // Flip if order is DESC. 410 if ( 'DESC' === strtoupper( $r['order'] ) ) { 411 $messages = array_reverse( $messages ); 315 412 } 316 413 317 return $messages; 414 // Paginate the results. 415 if ( ! empty( $messages ) && $r['per_page'] && $r['page'] ) { 416 $start = ( $r['page'] - 1 ) * ( $r['per_page'] ); 417 $messages = array_slice( $messages, $start, $r['per_page'] ); 418 } 419 420 /** 421 * Filters the messages associated with a thread. 422 * 423 * @since 10.0.0 424 * 425 * @param array $messages Array of message objects. 426 * @param int $thread_id ID of the thread. 427 * @param array $r An array of parameters. 428 */ 429 return apply_filters( 'bp_messages_thread_get_messages', (array) $messages, (int) $thread_id, (array) $r ); 318 430 } 319 431 320 432 /** … … class BP_Messages_Thread { 845 957 * @since 1.0.0 846 958 * 847 959 * @param int $thread_id The message thread ID. 848 * @param int $user_id The user ID. 960 * @param int $user_id The user ID. Default: ID of the logged-in user. 849 961 * @return int|null The recorded recipient ID on success, null on failure. 850 962 */ 851 963 public static function check_access( $thread_id, $user_id = 0 ) { … … class BP_Messages_Thread { 856 968 857 969 $recipients = self::get_recipients_for_thread( $thread_id ); 858 970 859 if ( isset( $recipients[ $user_id ] ) && 0 == $recipients[ $user_id ]->is_deleted ) {971 if ( isset( $recipients[ $user_id ] ) && 0 === $recipients[ $user_id ]->is_deleted ) { 860 972 return $recipients[ $user_id ]->id; 861 } else {862 return null;863 973 } 974 975 return null; 864 976 } 865 977 866 978 /** -
tests/phpunit/testcases/messages/class.bp-messages-thread.php
diff --git tests/phpunit/testcases/messages/class.bp-messages-thread.php tests/phpunit/testcases/messages/class.bp-messages-thread.php index f9de1d1cd..946a87edb 100644
class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 9 9 /** 10 10 * @group cache 11 11 */ 12 public function test_construct_cache() {12 public function construct_cache() { 13 13 $u1 = self::factory()->user->create(); 14 14 $u2 = self::factory()->user->create(); 15 15 … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 30 30 ); 31 31 } 32 32 33 public function test_get_messages_with_invalid_thread_id() { 34 $this->assertTrue( empty( BP_Messages_Thread::get_messages( 0 ) ) ); 35 } 36 37 public function test_get_messages_using_arguments() { 38 $u1 = self::factory()->user->create(); 39 $u2 = self::factory()->user->create(); 40 $m1 = self::factory()->message->create_and_get( array( 41 'sender_id' => $u1, 42 'recipients' => array( $u2 ), 43 'subject' => 'Foo', 44 ) ); 45 46 self::factory()->message->create_many( 47 98, 48 array( 49 'thread_id' => $m1->thread_id, 50 'sender_id' => $u2, 51 'recipients' => array( $u1 ), 52 'subject' => 'Bar', 53 ) 54 ); 55 56 // Last message 57 self::factory()->message->create( 58 array( 59 'thread_id' => $m1->thread_id, 60 'sender_id' => $u1, 61 'recipients' => array( $u2 ), 62 'subject' => 'Last Message', 63 ) 64 ); 65 66 // Default results. 67 $messages = BP_Messages_Thread::get_messages( $m1->thread_id ); 68 $this->assertTrue( 100 === count( $messages ) ); 69 70 // Get first 10 messages. 71 $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 1, 'per_page' => 10 ) ); 72 $this->assertTrue( 10 === count( $messages ) ); 73 74 // Get first 10 messages differently. 75 $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => 1, 'per_page' => 10 ) ); 76 $this->assertTrue( 10 === count( $thread->messages ) ); 77 78 // Get all messages. 79 $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => null, 'per_page' => null ) ); 80 $this->assertTrue( 100 === count( $messages ) ); 81 82 // Get all mesages differently. 83 $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => null, 'per_page' => null ) ); 84 $this->assertTrue( 100 === count( $thread->messages ) ); 85 86 // Get last message. 87 $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 100, 'per_page' => 1 ) ); 88 $this->assertTrue( 1 === count( $messages ) ); 89 $this->assertEquals( $u1, $messages[0]->sender_id ); 90 $this->assertEquals( 'Last Message', $messages[0]->subject ); 91 } 92 33 93 /** 34 94 * @group order 35 95 */ … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 39 99 40 100 // create thread 41 101 $message_1 = self::factory()->message->create_and_get( array( 42 'sender_id' => $u1,102 'sender_id' => $u1, 43 103 'recipients' => array( $u2 ), 44 'subject' => 'Foo',104 'subject' => 'Foo', 45 105 ) ); 46 $m1 = $message_1->id;47 106 48 107 // create reply 49 108 $message_2 = self::factory()->message->create_and_get( array( 50 'thread_id' => $message_1->thread_id,51 'sender_id' => $u1,109 'thread_id' => $message_1->thread_id, 110 'sender_id' => $u1, 52 111 'recipients' => array( $u2 ), 53 'content' => 'Bar'112 'content' => 'Bar' 54 113 ) ); 55 $m2 = $message_2->id;56 114 57 // now get thread by DESC 115 // Default sort from constructor. 116 $thread = new BP_Messages_Thread( $message_1->thread_id ); 117 $this->assertEquals( 118 array( $message_1->id, $message_2->id ), 119 wp_list_pluck( $thread->messages, 'id' ) 120 ); 121 122 // Default via the helper method. 123 $messages = BP_Messages_Thread::get_messages( $message_1->thread_id ); 124 $this->assertEquals( 125 array( $message_1->id, $message_2->id ), 126 wp_list_pluck( $messages, 'id' ) 127 ); 128 129 // Now get thread by DESC via the constructor. 58 130 $thread = new BP_Messages_Thread( $message_1->thread_id, 'DESC' ); 131 $this->assertEquals( 132 array( $message_2->id, $message_1->id ), 133 wp_list_pluck( $thread->messages, 'id' ) 134 ); 59 135 60 // assert! 136 // Testing sort with lowercase. 137 $thread = new BP_Messages_Thread( $message_1->thread_id, 'desc' ); 61 138 $this->assertEquals( 62 array( $m 2, $m1),139 array( $message_2->id, $message_1->id ), 63 140 wp_list_pluck( $thread->messages, 'id' ) 64 141 ); 142 143 // Now sorting via the helper method. 144 $messages = BP_Messages_Thread::get_messages( $message_1->thread_id, array( 'order' => 'desc' ) ); 145 $this->assertEquals( 146 array( $message_2->id, $message_1->id ), 147 wp_list_pluck( $messages, 'id' ) 148 ); 65 149 } 66 150 67 151 /** … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 71 155 $u1 = self::factory()->user->create(); 72 156 $u2 = self::factory()->user->create(); 73 157 74 $message_1 =self::factory()->message->create_and_get( array(158 self::factory()->message->create_and_get( array( 75 159 'sender_id' => $u1, 76 160 'recipients' => array( $u2 ), 77 161 'subject' => 'Foo', … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 101 185 $u1 = self::factory()->user->create(); 102 186 $u2 = self::factory()->user->create(); 103 187 104 $message_1 =self::factory()->message->create_and_get( array(188 self::factory()->message->create_and_get( array( 105 189 'sender_id' => $u1, 106 190 'recipients' => array( $u2 ), 107 191 'subject' => 'Foo', … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 133 217 $u1 = self::factory()->user->create(); 134 218 $u2 = self::factory()->user->create(); 135 219 136 $message_1 =self::factory()->message->create_and_get( array(220 self::factory()->message->create_and_get( array( 137 221 'sender_id' => $u1, 138 222 'recipients' => array( $u2 ), 139 223 'subject' => 'Foo', … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 153 237 $this->assertSame( $expected, $found ); 154 238 } 155 239 240 /** 241 * @group get_recipients 242 */ 243 public function test_get_recipients_paginated() { 244 $u1 = self::factory()->user->create(); 245 $user_ids = self::factory()->user->create_many( 9 ); 246 $m = self::factory()->message->create_and_get( array( 247 'sender_id' => $u1, 248 'recipients' => $user_ids, 249 'subject' => 'Foo', 250 ) ); 251 252 $thread_1 = new BP_Messages_Thread( $m->thread_id ); 253 $this->assertTrue( 10 === count( $thread_1->get_recipients() ) ); 254 255 $thread_2 = new BP_Messages_Thread( $m->thread_id, 'ASC', array( 'recipients_page' => 1, 'recipients_per_page' => 5 ) ); 256 $this->assertTrue( 5 === count( $thread_2->recipients ) ); 257 258 $thread_3 = new BP_Messages_Thread( $m->thread_id ); 259 $this->assertTrue( 8 === count( $thread_3->get_recipients( $m->thread_id, array( 'recipients_page' => 1, 'recipients_per_page' => 8 ) ) ) ); 260 } 261 156 262 /** 157 263 * @group get_recipients 158 264 * @group cache … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 196 302 ) ); 197 303 198 304 $thread = new BP_Messages_Thread( $message->thread_id ); 199 $ recipients = $thread->get_recipients();305 $thread->get_recipients(); 200 306 201 307 // Verify that the cache is populated. 202 308 $num_queries = $wpdb->num_queries; 203 $ recipients_cached = $thread->get_recipients();309 $thread->get_recipients(); 204 310 $this->assertEquals( $num_queries, $wpdb->num_queries ); 205 311 206 312 messages_new_message( array( … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 213 319 214 320 // Cache should be empty. 215 321 $num_queries = $wpdb->num_queries; 216 $ recipients_uncached = $thread->get_recipients();322 $thread->get_recipients(); 217 323 $this->assertEquals( $num_queries + 1, $wpdb->num_queries ); 218 324 } 219 325 … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 236 342 $t1 = $message->thread_id; 237 343 238 344 $thread = new BP_Messages_Thread( $t1 ); 239 $ recipients = $thread->get_recipients();345 $thread->get_recipients(); 240 346 241 347 // Verify that the cache is populated. 242 348 $num_queries = $wpdb->num_queries; 243 $ recipients_cached = $thread->get_recipients();349 $thread->get_recipients(); 244 350 $this->assertEquals( $num_queries, $wpdb->num_queries ); 245 351 246 352 messages_delete_thread( $t1 ); … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 268 374 $t1 = $message->thread_id; 269 375 270 376 $thread = new BP_Messages_Thread( $t1 ); 271 $ recipients = $thread->get_recipients();377 $thread->get_recipients(); 272 378 273 379 // Verify that the cache is populated. 274 380 $num_queries = $wpdb->num_queries; 275 $ recipients_cached = $thread->get_recipients();381 $thread->get_recipients(); 276 382 $this->assertEquals( $num_queries, $wpdb->num_queries ); 277 383 278 384 messages_delete_thread( array( $t1 ) ); … … class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 396 502 $t1 = $message->thread_id; 397 503 398 504 $thread = new BP_Messages_Thread( $t1 ); 399 $ recipients = $thread->get_recipients();505 $thread->get_recipients(); 400 506 401 507 // Verify that the cache is populated. 402 508 $num_queries = $wpdb->num_queries; 403 $ recipients_cached = $thread->get_recipients();509 $thread->get_recipients(); 404 510 $this->assertEquals( $num_queries, $wpdb->num_queries ); 405 511 406 512 // Mark thread as unread