Skip to:
Content

BuddyPress.org

Changeset 13102


Ignore:
Timestamp:
09/09/2021 04:10:21 AM (5 years ago)
Author:
espellcaste
Message:

Adding support to the offset pagination to Messages and Recipients in a thread.

Previously, the BP_Messages_Thread query class would grab ALL messages created for a thread and ALL its recipients. This commit introduces support to the offset pagination
allowing one to paginate both messages and recipients using parameters with a specific limit and offset.

Props imath
Fixes #8508

Location:
trunk
Files:
4 edited

Legend:

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

    r13096 r13102  
    285285        foreach ( $thread->get_recipients() as $recipient ) {
    286286                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                        }
    288295                }
    289296        }
  • trunk/src/bp-messages/bp-messages-template.php

    r13096 r13102  
    16261626 * @param array|string $args {
    16271627 *     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.
    16341636 * }
     1637 *
    16351638 * @return bool True if there are messages to display, otherwise false.
    16361639 */
     
    16381641        global $thread_template;
    16391642
    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        );
    16451654
    16461655        if ( empty( $r['thread_id'] ) && bp_is_messages_component() && bp_is_current_action( 'view' ) ) {
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r13096 r13102  
    110110         *
    111111         * @since 1.0.0
    112          *
    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.
    118          */
    119         public function __construct( $thread_id = false, $order = 'ASC', $args = array() ) {
    120                 if ( $thread_id ) {
     112         * @since 10.0.0 Updated the `$args` with new paremeters.
     113         *
     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         * }
     129         */
     130        public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) {
     131                if ( ! empty( $thread_id ) ) {
    121132                        $this->populate( $thread_id, $order, $args );
    122133                }
     
    126137         * Populate method.
    127138         *
    128          * Used in constructor.
    129          *
    130          * @since 1.0.0
    131          *
    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 {
     139         * Used in the constructor.
     140         *
     141         * @since 1.0.0
     142         * @since 10.0.0 Updated the `$args` with new paremeters.
     143         *
     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                        {
    135148         *     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.
    138158         * }
    139          * @return bool False on failure.
     159         * @return bool False if there are no messages.
    140160         */
    141161        public function populate( $thread_id = 0, $order = 'ASC', $args = array() ) {
    142162
    143                 if ( 'ASC' !== $order && 'DESC' !== $order ) {
     163                if ( ! in_array( strtoupper( $order ), array( 'ASC', 'DESC' ), true ) ) {
    144164                        $order = 'ASC';
    145165                }
     
    151171
    152172                // 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                );
    157185
    158186                $this->messages_order = $order;
     
    160188
    161189                // Get messages for thread.
    162                 $this->messages = self::get_messages( $this->thread_id );
    163 
    164                 if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) {
     190                $this->messages = self::get_messages( $this->thread_id, $r );
     191
     192                if ( empty( $this->messages ) ) {
    165193                        return false;
    166                 }
    167 
    168                 // Flip if order is DESC.
    169                 if ( 'DESC' === $order ) {
    170                         $this->messages = array_reverse( $this->messages );
    171194                }
    172195
     
    183206
    184207                // Fetch the recipients.
    185                 $this->recipients = $this->get_recipients();
    186 
    187                 // Get the unread count for the logged in user.
     208                $this->recipients = $this->get_recipients( $thread_id, $r );
     209
     210                // Get the unread count for the user.
    188211                if ( isset( $this->recipients[ $r['user_id'] ] ) ) {
    189212                        $this->unread_count = $this->recipients[ $r['user_id'] ]->unread_count;
     
    199222                 *
    200223                 * @since 2.2.0
     224                 * @since 10.0.0 Added `$r` as a parameter.
    201225                 *
    202226                 * @param BP_Messages_Thread $this Message thread object.
     227                 * @param array              $r    Array of paremeters.
    203228                 */
    204                 do_action( 'bp_messages_thread_post_populate', $this );
     229                do_action( 'bp_messages_thread_post_populate', $this, $r );
    205230        }
    206231
     
    231256         *
    232257         * @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.
    234260         *
    235261         * @global BuddyPress $bp The one true BuddyPress instance.
    236262         * @global wpdb $wpdb WordPress database object.
    237263         *
    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         * }
    239270         * @return array
    240271         */
    241         public function get_recipients( $thread_id = 0 ) {
     272        public function get_recipients( $thread_id = 0, $args = array() ) {
    242273                global $wpdb;
    243274
     
    248279                $thread_id = (int) $thread_id;
    249280
     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.
    250295                $recipients = wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' );
    251                 if ( false === $recipients ) {
    252                         $bp = buddypress();
     296
     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                        );
    253307
    254308                        $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 );
    257 
    258309                        foreach ( (array) $results as $recipient ) {
    259                                 $recipients[ $recipient->user_id ] = $recipient;
     310                                $recipient_properties              = get_object_vars( $recipient );
     311                                $recipients[ $recipient->user_id ] = (object) array_map( 'intval', $recipient_properties );
    260312                        }
    261313
    262                         wp_cache_set( 'thread_recipients_' . $thread_id, $recipients, 'bp_messages' );
    263                 }
    264 
    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 );
     314                        // Cache recipients.
     315                        wp_cache_set( 'thread_recipients_' . $thread_id, (array) $recipients, 'bp_messages' );
     316                }
     317
     318                // Paginate the results.
     319                if ( ! empty( $recipients ) && $r['recipients_per_page'] && $r['recipients_page'] ) {
     320                        $start      = ( $r['recipients_page'] - 1 ) * ( $r['recipients_per_page'] );
     321                        $recipients = array_slice( $recipients, $start, $r['recipients_per_page'] );
    268322                }
    269323
     
    272326                 *
    273327                 * @since 2.2.0
     328                 * @since 10.0.0 Added `$r` as a parameter.
    274329                 *
    275330                 * @param array $recipients Array of recipient objects.
    276                  * @param int   $thread_id  ID of the current thread.
     331                 * @param int   $thread_id  ID of the thread.
     332                 * @param array $r          An array of parameters.
    277333                 */
    278                 return apply_filters( 'bp_messages_thread_get_recipients', $recipients, $thread_id );
     334                return apply_filters( 'bp_messages_thread_get_recipients', (array) $recipients, (int) $thread_id, (array) $r );
    279335        }
    280336
     
    282338
    283339        /**
    284          * Get all messages associated with a thread.
     340         * Get messages associated with a thread.
    285341         *
    286342         * @since 2.3.0
     343         * @since 10.0.0 Added `$args` as a parameter.
    287344         *
    288345         * @global BuddyPress $bp The one true BuddyPress instance.
    289346         * @global wpdb $wpdb WordPress database object.
    290347         *
    291          * @param int $thread_id The message thread ID.
    292          *
    293          * @return array List of messages associated with a thread.
    294          */
    295         public static function get_messages( $thread_id = 0 ) {
     348         * @param int   $thread_id The message thread ID.
     349         * @param array $args      {
     350         *     Array of arguments.
     351         *     @type int|null    $page     Page of messages being requested. Default to all.
     352         *     @type int|null    $per_page Messages to return per page. Default to all.
     353         *     @type string      $order    The order to sort the messages. Either 'ASC' or 'DESC'.
     354         *                                 Defaults to 'ASC'.
     355         * }
     356         * @return array
     357         */
     358        public static function get_messages( $thread_id = 0, $args = array() ) {
     359                global $wpdb;
     360
    296361                $thread_id = (int) $thread_id;
    297                 $messages  = wp_cache_get( $thread_id, 'bp_messages_threads' );
    298 
    299                 if ( false === $messages ) {
    300                         global $wpdb;
    301 
    302                         $bp = buddypress();
    303 
    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 ) );
    306 
     362                if ( empty( $thread_id ) ) {
     363                        return array();
     364                }
     365
     366                $bp = buddypress();
     367                $r  = wp_parse_args(
     368                        $args,
     369                        array(
     370                                'page'     => null,
     371                                'per_page' => null,
     372                                'order'    => 'ASC',
     373                        )
     374                );
     375
     376                // Fallback.
     377                if ( ! in_array( strtoupper( $r['order'] ), array( 'ASC', 'DESC' ), true ) ) {
     378                        $r['order'] = 'ASC';
     379                }
     380
     381                // Get messages from cache if available.
     382                $messages = wp_cache_get( $thread_id, 'bp_messages_threads' );
     383
     384                // Get messages and cache it.
     385                if ( empty( $messages ) ) {
     386
     387                        // Query messages.
     388                        $messages = $wpdb->get_results(
     389                                $wpdb->prepare(
     390                                        "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent ASC",
     391                                        $thread_id
     392                                )
     393                        );
     394
     395                        foreach ( $messages as $key => $data ) {
     396                                $messages[ $key ]->id        = (int) $messages[ $key ]->id;
     397                                $messages[ $key ]->thread_id = (int) $messages[ $key ]->thread_id;
     398                                $messages[ $key ]->sender_id = (int) $messages[ $key ]->sender_id;
     399                        }
     400
     401                        // Cache messages.
    307402                        wp_cache_set( $thread_id, (array) $messages, 'bp_messages_threads' );
    308403                }
    309404
    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;
    315                 }
    316 
    317                 return $messages;
     405                // Flip if order is DESC.
     406                if ( 'DESC' === strtoupper( $r['order'] ) ) {
     407                        $messages = array_reverse( $messages );
     408                }
     409
     410                // Paginate the results.
     411                if ( ! empty( $messages ) && $r['per_page'] && $r['page'] ) {
     412                        $start    = ( $r['page'] - 1 ) * ( $r['per_page'] );
     413                        $messages = array_slice( $messages, $start, $r['per_page'] );
     414                }
     415
     416                /**
     417                 * Filters the messages associated with a thread.
     418                 *
     419                 * @since 10.0.0
     420                 *
     421                 * @param array $messages   Array of message objects.
     422                 * @param int   $thread_id  ID of the thread.
     423                 * @param array $r          An array of parameters.
     424                 */
     425                return apply_filters( 'bp_messages_thread_get_messages', (array) $messages, (int) $thread_id, (array) $r );
    318426        }
    319427
     
    846954         *
    847955         * @param int $thread_id The message thread ID.
    848          * @param int $user_id   The user ID.
     956         * @param int $user_id   The user ID. Default: ID of the logged-in user.
    849957         * @return int|null The recorded recipient ID on success, null on failure.
    850958         */
     
    857965                $recipients = self::get_recipients_for_thread( $thread_id );
    858966
    859                 if ( isset( $recipients[ $user_id ] ) && 0 == $recipients[ $user_id ]->is_deleted ) {
     967                if ( isset( $recipients[ $user_id ] ) && 0 === $recipients[ $user_id ]->is_deleted ) {
    860968                        return $recipients[ $user_id ]->id;
    861                 } else {
    862                         return null;
    863                 }
     969                }
     970
     971                return null;
    864972        }
    865973
  • trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php

    r12984 r13102  
    1010         * @group cache
    1111         */
    12         public function test_construct_cache() {
     12        public function construct_cache() {
    1313                $u1 = self::factory()->user->create();
    1414                $u2 = self::factory()->user->create();
     
    3131        }
    3232
     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
    3393        /**
    3494         * @group order
     
    40100                // create thread
    41101                $message_1 = self::factory()->message->create_and_get( array(
    42                         'sender_id' => $u1,
    43                         'recipients' => array( $u2 ),
    44                         'subject' => 'Foo',
    45                 ) );
    46                 $m1 = $message_1->id;
     102                        'sender_id'  => $u1,
     103                        'recipients' => array( $u2 ),
     104                        'subject'    => 'Foo',
     105                ) );
    47106
    48107                // create reply
    49108                $message_2 = self::factory()->message->create_and_get( array(
    50                         'thread_id' => $message_1->thread_id,
    51                         'sender_id' => $u1,
    52                         'recipients' => array( $u2 ),
    53                         'content' => 'Bar'
    54                 ) );
    55                 $m2 = $message_2->id;
    56 
    57                 // now get thread by DESC
     109                        'thread_id'  => $message_1->thread_id,
     110                        'sender_id'  => $u1,
     111                        'recipients' => array( $u2 ),
     112                        'content'    => 'Bar'
     113                ) );
     114
     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.
    58130                $thread = new BP_Messages_Thread( $message_1->thread_id, 'DESC' );
    59 
    60                 // assert!
    61131                $this->assertEquals(
    62                         array( $m2, $m1 ),
     132                        array( $message_2->id, $message_1->id ),
    63133                        wp_list_pluck( $thread->messages, 'id' )
    64134                );
     135
     136                // Testing sort with lowercase.
     137                $thread = new BP_Messages_Thread( $message_1->thread_id, 'desc' );
     138                $this->assertEquals(
     139                        array( $message_2->id, $message_1->id ),
     140                        wp_list_pluck( $thread->messages, 'id' )
     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                );
    65149        }
    66150
     
    72156                $u2 = self::factory()->user->create();
    73157
    74                 $message_1 = self::factory()->message->create_and_get( array(
     158                self::factory()->message->create_and_get( array(
    75159                        'sender_id' => $u1,
    76160                        'recipients' => array( $u2 ),
     
    102186                $u2 = self::factory()->user->create();
    103187
    104                 $message_1 = self::factory()->message->create_and_get( array(
     188                self::factory()->message->create_and_get( array(
    105189                        'sender_id' => $u1,
    106190                        'recipients' => array( $u2 ),
     
    134218                $u2 = self::factory()->user->create();
    135219
    136                 $message_1 = self::factory()->message->create_and_get( array(
     220                self::factory()->message->create_and_get( array(
    137221                        'sender_id' => $u1,
    138222                        'recipients' => array( $u2 ),
     
    156240        /**
    157241         * @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
     262        /**
     263         * @group get_recipients
    158264         * @group cache
    159265         */
     
    197303
    198304                $thread = new BP_Messages_Thread( $message->thread_id );
    199                 $recipients = $thread->get_recipients();
     305                $thread->get_recipients();
    200306
    201307                // Verify that the cache is populated.
    202308                $num_queries = $wpdb->num_queries;
    203                 $recipients_cached = $thread->get_recipients();
     309                $thread->get_recipients();
    204310                $this->assertEquals( $num_queries, $wpdb->num_queries );
    205311
     
    214320                // Cache should be empty.
    215321                $num_queries = $wpdb->num_queries;
    216                 $recipients_uncached = $thread->get_recipients();
     322                $thread->get_recipients();
    217323                $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
    218324        }
     
    223329         */
    224330        public function test_get_recipients_cache_should_be_busted_when_single_thread_is_deleted() {
     331                global $wpdb;
     332
     333                $u1 = self::factory()->user->create();
     334                $u2 = self::factory()->user->create();
     335
     336                $message = self::factory()->message->create_and_get( array(
     337                        'sender_id' => $u1,
     338                        'recipients' => array( $u2 ),
     339                        'subject' => 'Foo',
     340                ) );
     341
     342                $t1 = $message->thread_id;
     343
     344                $thread = new BP_Messages_Thread( $t1 );
     345                $thread->get_recipients();
     346
     347                // Verify that the cache is populated.
     348                $num_queries = $wpdb->num_queries;
     349                $thread->get_recipients();
     350                $this->assertEquals( $num_queries, $wpdb->num_queries );
     351
     352                messages_delete_thread( $t1 );
     353
     354                // Cache should be empty.
     355                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
     356        }
     357
     358        /**
     359         * @group get_recipients
     360         * @group cache
     361         */
     362        public function test_get_recipients_cache_should_be_busted_when_array_of_threads_is_deleted() {
     363                global $wpdb;
     364
     365                $u1 = self::factory()->user->create();
     366                $u2 = self::factory()->user->create();
     367
     368                $message = self::factory()->message->create_and_get( array(
     369                        'sender_id' => $u1,
     370                        'recipients' => array( $u2 ),
     371                        'subject' => 'Foo',
     372                ) );
     373
     374                $t1 = $message->thread_id;
     375
     376                $thread = new BP_Messages_Thread( $t1 );
     377                $thread->get_recipients();
     378
     379                // Verify that the cache is populated.
     380                $num_queries = $wpdb->num_queries;
     381                $thread->get_recipients();
     382                $this->assertEquals( $num_queries, $wpdb->num_queries );
     383
     384                messages_delete_thread( array( $t1 ) );
     385
     386                // Cache should be empty.
     387                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
     388        }
     389
     390        /**
     391         * @group get_recipients
     392         * @group cache
     393         */
     394        public function test_get_recipients_cache_should_be_busted_when_thread_is_read() {
    225395                global $wpdb;
    226396
     
    244414                $this->assertEquals( $num_queries, $wpdb->num_queries );
    245415
    246                 messages_delete_thread( $t1 );
    247 
    248                 // Cache should be empty.
    249                 $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
    250         }
    251 
    252         /**
    253          * @group get_recipients
    254          * @group cache
    255          */
    256         public function test_get_recipients_cache_should_be_busted_when_array_of_threads_is_deleted() {
    257                 global $wpdb;
    258 
    259                 $u1 = self::factory()->user->create();
    260                 $u2 = self::factory()->user->create();
    261 
    262                 $message = self::factory()->message->create_and_get( array(
    263                         'sender_id' => $u1,
    264                         'recipients' => array( $u2 ),
    265                         'subject' => 'Foo',
    266                 ) );
    267 
    268                 $t1 = $message->thread_id;
    269 
    270                 $thread = new BP_Messages_Thread( $t1 );
    271                 $recipients = $thread->get_recipients();
    272 
    273                 // Verify that the cache is populated.
    274                 $num_queries = $wpdb->num_queries;
    275                 $recipients_cached = $thread->get_recipients();
    276                 $this->assertEquals( $num_queries, $wpdb->num_queries );
    277 
    278                 messages_delete_thread( array( $t1 ) );
    279 
    280                 // Cache should be empty.
    281                 $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
    282         }
    283 
    284         /**
    285          * @group get_recipients
    286          * @group cache
    287          */
    288         public function test_get_recipients_cache_should_be_busted_when_thread_is_read() {
    289                 global $wpdb;
    290 
    291                 $u1 = self::factory()->user->create();
    292                 $u2 = self::factory()->user->create();
    293 
    294                 $message = self::factory()->message->create_and_get( array(
    295                         'sender_id' => $u1,
    296                         'recipients' => array( $u2 ),
    297                         'subject' => 'Foo',
    298                 ) );
    299 
    300                 $t1 = $message->thread_id;
    301 
    302                 $thread = new BP_Messages_Thread( $t1 );
    303                 $recipients = $thread->get_recipients();
    304 
    305                 // Verify that the cache is populated.
    306                 $num_queries = $wpdb->num_queries;
    307                 $recipients_cached = $thread->get_recipients();
    308                 $this->assertEquals( $num_queries, $wpdb->num_queries );
    309 
    310416                // Mark thread as read
    311417                $current_user = get_current_user_id();
     
    397503
    398504                $thread = new BP_Messages_Thread( $t1 );
    399                 $recipients = $thread->get_recipients();
     505                $thread->get_recipients();
    400506
    401507                // Verify that the cache is populated.
    402508                $num_queries = $wpdb->num_queries;
    403                 $recipients_cached = $thread->get_recipients();
     509                $thread->get_recipients();
    404510                $this->assertEquals( $num_queries, $wpdb->num_queries );
    405511
Note: See TracChangeset for help on using the changeset viewer.