| 71 | |
| 72 | /** |
| 73 | * @group bp_has_message_threads |
| 74 | * @group meta_query |
| 75 | */ |
| 76 | public function test_thread_has_messages_meta_query() { |
| 77 | $u1 = $this->factory->user->create(); |
| 78 | $u2 = $this->factory->user->create(); |
| 79 | |
| 80 | // create some threads |
| 81 | $t1 = $this->factory->message->create( array( |
| 82 | 'sender_id' => $u1, |
| 83 | 'recipients' => array( $u2 ), |
| 84 | 'subject' => 'This is a knive', |
| 85 | ) ); |
| 86 | $t2 = $this->factory->message->create( array( |
| 87 | 'sender_id' => $u1, |
| 88 | 'recipients' => array( $u2 ), |
| 89 | 'subject' => 'Oy', |
| 90 | ) ); |
| 91 | |
| 92 | // misc threads |
| 93 | $this->factory->message->create_many( 3, array( |
| 94 | 'sender_id' => $u1, |
| 95 | 'recipients' => array( $u2 ), |
| 96 | ) ); |
| 97 | |
| 98 | // create some replies for thread 1 |
| 99 | $this->factory->message->create( array( |
| 100 | 'thread_id' => $t1, |
| 101 | 'sender_id' => $u2, |
| 102 | 'recipients' => array( $u1 ), |
| 103 | 'content' => "That's a spoon", |
| 104 | ) ); |
| 105 | $this->factory->message->create( array( |
| 106 | 'thread_id' => $t1, |
| 107 | 'sender_id' => $u1, |
| 108 | 'recipients' => array( $u2 ), |
| 109 | 'content' => "I see you've played knivey-spooney before.", |
| 110 | ) ); |
| 111 | $this->factory->message->create( array( |
| 112 | 'thread_id' => $t1, |
| 113 | 'sender_id' => $u2, |
| 114 | 'recipients' => array( $u1 ), |
| 115 | 'content' => '*crickets*', |
| 116 | ) ); |
| 117 | |
| 118 | // create some replies for thread 2 |
| 119 | $this->factory->message->create( array( |
| 120 | 'thread_id' => $t2, |
| 121 | 'sender_id' => $u2, |
| 122 | 'recipients' => array( $u1 ), |
| 123 | 'content' => "Oy yourself.", |
| 124 | ) ); |
| 125 | |
| 126 | // grab the message ids as individual variables for thread 1 |
| 127 | $thread = new BP_Messages_Thread( $t1 ); |
| 128 | $mids = wp_list_pluck( $thread->messages, 'id' ); |
| 129 | list( $m1, $m2, $m3, $m4 ) = $mids; |
| 130 | |
| 131 | // grab the message ids as individual variables for thread 2 |
| 132 | $thread = new BP_Messages_Thread( $t2 ); |
| 133 | $mids = wp_list_pluck( $thread->messages, 'id' ); |
| 134 | list( $m5, $m6 ) = $mids; |
| 135 | |
| 136 | // add meta for some of the messages |
| 137 | bp_messages_update_meta( $m1, 'utensil', 'knive' ); |
| 138 | bp_messages_update_meta( $m1, 'is_knive', 'yes' ); |
| 139 | bp_messages_update_meta( $m1, "starred_by_user_{$u2}", true ); |
| 140 | |
| 141 | bp_messages_update_meta( $m2, 'utensil', 'spoon' ); |
| 142 | bp_messages_update_meta( $m2, 'is_knive', 'no' ); |
| 143 | bp_messages_update_meta( $m2, 'is_spoon', 'yes' ); |
| 144 | |
| 145 | bp_messages_update_meta( $m3, "starred_by_user_{$u2}", true ); |
| 146 | |
| 147 | bp_messages_update_meta( $m5, "starred_by_user_{$u2}", true ); |
| 148 | |
| 149 | // now, do the message thread loop query |
| 150 | global $messages_template; |
| 151 | bp_has_message_threads( array( |
| 152 | 'user_id' => $u2, |
| 153 | 'meta_query' => array( |
| 154 | array( |
| 155 | 'key' => "starred_by_user_{$u2}" |
| 156 | ), |
| 157 | ) |
| 158 | ) ); |
| 159 | |
| 160 | $this->assertEquals( 2, $messages_template->thread_count ); |
| 161 | $this->assertEqualSets( array( $t1, $t2 ), wp_list_pluck( $messages_template->threads, 'thread_id' ) ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @group bp_has_message_threads |
| 166 | * @group meta_query |
| 167 | */ |
| 168 | public function test_thread_has_messages_meta_query_multiple_clauses_relation_and() { |
| 169 | $u1 = $this->factory->user->create(); |
| 170 | $u2 = $this->factory->user->create(); |
| 171 | |
| 172 | // create some threads |
| 173 | $t1 = $this->factory->message->create( array( |
| 174 | 'sender_id' => $u1, |
| 175 | 'recipients' => array( $u2 ), |
| 176 | 'subject' => 'This is a knive', |
| 177 | ) ); |
| 178 | $t2 = $this->factory->message->create( array( |
| 179 | 'sender_id' => $u1, |
| 180 | 'recipients' => array( $u2 ), |
| 181 | 'subject' => 'Oy', |
| 182 | ) ); |
| 183 | |
| 184 | // misc threads |
| 185 | $this->factory->message->create_many( 3, array( |
| 186 | 'sender_id' => $u1, |
| 187 | 'recipients' => array( $u2 ), |
| 188 | ) ); |
| 189 | |
| 190 | // create some replies for thread 1 |
| 191 | $this->factory->message->create( array( |
| 192 | 'thread_id' => $t1, |
| 193 | 'sender_id' => $u2, |
| 194 | 'recipients' => array( $u1 ), |
| 195 | 'content' => "That's a spoon", |
| 196 | ) ); |
| 197 | $this->factory->message->create( array( |
| 198 | 'thread_id' => $t1, |
| 199 | 'sender_id' => $u1, |
| 200 | 'recipients' => array( $u2 ), |
| 201 | 'content' => "I see you've played knivey-spooney before.", |
| 202 | ) ); |
| 203 | $this->factory->message->create( array( |
| 204 | 'thread_id' => $t1, |
| 205 | 'sender_id' => $u2, |
| 206 | 'recipients' => array( $u1 ), |
| 207 | 'content' => '*crickets*', |
| 208 | ) ); |
| 209 | |
| 210 | // create some replies for thread 2 |
| 211 | $this->factory->message->create( array( |
| 212 | 'thread_id' => $t2, |
| 213 | 'sender_id' => $u2, |
| 214 | 'recipients' => array( $u1 ), |
| 215 | 'content' => "Oy yourself.", |
| 216 | ) ); |
| 217 | |
| 218 | // grab the message ids as individual variables for thread 1 |
| 219 | $thread = new BP_Messages_Thread( $t1 ); |
| 220 | $mids = wp_list_pluck( $thread->messages, 'id' ); |
| 221 | list( $m1, $m2, $m3, $m4 ) = $mids; |
| 222 | |
| 223 | // grab the message ids as individual variables for thread 2 |
| 224 | $thread = new BP_Messages_Thread( $t2 ); |
| 225 | $mids = wp_list_pluck( $thread->messages, 'id' ); |
| 226 | list( $m5, $m6 ) = $mids; |
| 227 | |
| 228 | // add meta for some of the messages |
| 229 | bp_messages_update_meta( $m1, 'utensil', 'knive' ); |
| 230 | bp_messages_update_meta( $m1, 'is_knive', 'yes' ); |
| 231 | bp_messages_update_meta( $m1, "starred_by_user_{$u2}", true ); |
| 232 | |
| 233 | bp_messages_update_meta( $m2, 'utensil', 'spoon' ); |
| 234 | bp_messages_update_meta( $m2, 'is_knive', 'no' ); |
| 235 | bp_messages_update_meta( $m2, 'is_spoon', 'yes' ); |
| 236 | |
| 237 | bp_messages_update_meta( $m3, "starred_by_user_{$u2}", true ); |
| 238 | |
| 239 | bp_messages_update_meta( $m5, "starred_by_user_{$u2}", true ); |
| 240 | |
| 241 | // now, do the message thread loop query |
| 242 | global $messages_template; |
| 243 | bp_has_message_threads( array( |
| 244 | 'user_id' => $u2, |
| 245 | 'meta_query' => array( |
| 246 | 'relation' => 'AND', |
| 247 | array( |
| 248 | 'key' => "starred_by_user_{$u2}" |
| 249 | ), |
| 250 | array( |
| 251 | 'key' => 'utensil', |
| 252 | 'value' => 'knive', |
| 253 | ), |
| 254 | ) |
| 255 | ) ); |
| 256 | |
| 257 | $this->assertEquals( 1, $messages_template->thread_count ); |
| 258 | $this->assertEqualSets( array( $t1 ), wp_list_pluck( $messages_template->threads, 'thread_id' ) ); |
| 259 | } |