| 45 | $expected = sprintf( __( '%1$s requested %2$s to be their friend', 'buddypress' ), bp_core_get_userlink( $u1 ), bp_core_get_userlink( $u2 ) ); |
| 46 | |
| 47 | $a_obj = new BP_Activity_Activity( $a ); |
| 48 | |
| 49 | $this->assertSame( $expected, $a_obj->action ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @group activity_action |
| 54 | * @group bp_friends_format_activity_action_friendship_accepted |
| 55 | * @ticket BP6040 |
| 56 | */ |
| 57 | public function test_bp_friends_format_activity_action_friendship_accepted_is_user() { |
| 58 | $bp = buddypress(); |
| 59 | $displayed_user = $bp->displayed_user; |
| 60 | |
| 61 | $u1 = $this->factory->user->create(); |
| 62 | $u2 = $this->factory->user->create(); |
| 63 | |
| 64 | $a = $this->factory->activity->create( array( |
| 65 | 'component' => buddypress()->friends->id, |
| 66 | 'type' => 'friendship_accepted', |
| 67 | 'user_id' => $u1, |
| 68 | 'secondary_item_id' => $u2, |
| 69 | ) ); |
| 70 | |
| 71 | $bp->displayed_user->id = $u1; |
| 72 | |
| 73 | // Use directly the class to avoid another activity to be created |
| 74 | // Maybe a new BP_UnitTest_Factory_For_Friends class would be better |
| 75 | $friendship = new BP_Friends_Friendship; |
| 76 | $friendship->initiator_user_id = $u2; |
| 77 | $friendship->friend_user_id = $u1; |
| 78 | $friendship->is_confirmed = 1; |
| 79 | $friendship->date_created = bp_core_current_time(); |
| 80 | $friendship->save(); |
| 81 | |
| 82 | $expected = sprintf( __( '%1$s and %2$s are now friends', 'buddypress' ), bp_core_get_userlink( $u1 ), bp_core_get_userlink( $u2 ) ); |
| 83 | |
| 84 | $a_obj = new BP_Activity_Activity( $a ); |
| 85 | |
| 86 | $this->assertSame( $expected, $a_obj->action ); |
| 87 | |
| 88 | // Reset the friendship |
| 89 | $friendship->delete(); |
| 90 | |
| 91 | // Reset displayed user |
| 92 | $bp->displayed_user = $displayed_user; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @group activity_action |
| 97 | * @group bp_friends_format_activity_action_friendship_created |
| 98 | * @ticket BP6040 |
| 99 | */ |
| 100 | public function test_bp_friends_format_activity_action_friendship_created_is_user() { |
| 101 | $bp = buddypress(); |
| 102 | $displayed_user = $bp->displayed_user; |
| 103 | |
| 104 | $u1 = $this->factory->user->create(); |
| 105 | $u2 = $this->factory->user->create(); |
| 106 | |
| 107 | $a = $this->factory->activity->create( array( |
| 108 | 'component' => buddypress()->friends->id, |
| 109 | 'type' => 'friendship_created', |
| 110 | 'user_id' => $u1, |
| 111 | 'secondary_item_id' => $u2, |
| 112 | ) ); |
| 113 | |
| 114 | $bp->displayed_user->id = $u1; |
| 115 | |
| 116 | // Use directly the class to avoid another activity to be created |
| 117 | // Maybe a new BP_UnitTest_Factory_For_Friends class would be better |
| 118 | $friendship = new BP_Friends_Friendship; |
| 119 | $friendship->initiator_user_id = $u1; |
| 120 | $friendship->friend_user_id = $u2; |
| 121 | $friendship->is_confirmed = 1; |
| 122 | $friendship->date_created = bp_core_current_time(); |
| 123 | $friendship->save(); |
| 124 | |
| 130 | |
| 131 | // Reset the friendship |
| 132 | $friendship->delete(); |
| 133 | |
| 134 | // Reset displayed user |
| 135 | $bp->displayed_user = $displayed_user; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @group bp_friends_friendship_requested_activity |
| 140 | * @ticket BP6040 |
| 141 | */ |
| 142 | public function test_bp_friends_friendship_requested_activity() { |
| 143 | $u1 = $this->factory->user->create(); |
| 144 | $u2 = $this->factory->user->create(); |
| 145 | |
| 146 | friends_add_friend( $u2, $u1 ); |
| 147 | $friendship_id = friends_get_friendship_id( $u2, $u1 ); |
| 148 | |
| 149 | $fr_act = bp_activity_get( array( |
| 150 | 'component' => buddypress()->friends->id, |
| 151 | 'item_id' => $friendship_id, |
| 152 | 'filter' => array( 'action' => array( 'friendship_created' ) ), |
| 153 | ) ); |
| 154 | |
| 155 | $this->assertTrue( count( $fr_act['activities'] ) == 1, 'An activity should be created when a friendship is requested' ); |
| 156 | |
| 157 | $friendship = new BP_Friends_Friendship( $friendship_id ); |
| 158 | // Reset the friendship |
| 159 | $friendship->delete(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @group bp_friends_friendship_accepted_activity |
| 164 | * @ticket BP6040 |
| 165 | */ |
| 166 | public function test_bp_friends_friendship_accepted_activity() { |
| 167 | $u1 = $this->factory->user->create(); |
| 168 | $u2 = $this->factory->user->create(); |
| 169 | |
| 170 | // Force the friendship to be accepted |
| 171 | friends_add_friend( $u2, $u1, true ); |
| 172 | $friendship_id = friends_get_friendship_id( $u2, $u1 ); |
| 173 | |
| 174 | $fa_act = bp_activity_get( array( |
| 175 | 'component' => buddypress()->friends->id, |
| 176 | 'item_id' => $friendship_id, |
| 177 | 'filter' => array( 'action' => array( 'friendship_accepted' ) ), |
| 178 | ) ); |
| 179 | |
| 180 | $this->assertTrue( count( $fa_act['activities'] ) == 1, 'An activity should be created when a friendship is accepted' ); |
| 181 | |
| 182 | $friendship = new BP_Friends_Friendship( $friendship_id ); |
| 183 | // Reset the friendship |
| 184 | $friendship->delete(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @group bp_friends_friendship_withdrawn_activity |
| 189 | * @ticket BP6040 |
| 190 | */ |
| 191 | public function test_bp_friends_friendship_withdrawn_activity() { |
| 192 | $old_user = get_current_user_id(); |
| 193 | |
| 194 | $u1 = $this->factory->user->create(); |
| 195 | $u2 = $this->factory->user->create(); |
| 196 | |
| 197 | friends_add_friend( $u2, $u1 ); |
| 198 | $friendship_id = friends_get_friendship_id( $u2, $u1 ); |
| 199 | |
| 200 | $fr_act = bp_activity_get( array( |
| 201 | 'component' => buddypress()->friends->id, |
| 202 | 'item_id' => $friendship_id, |
| 203 | 'filter' => array( 'action' => array( 'friendship_created' ) ), |
| 204 | 'user_id' => $u2, |
| 205 | ) ); |
| 206 | |
| 207 | $this->assertTrue( count( $fr_act['activities'] ) == 1, 'An activity should be created when a friendship is requested' ); |
| 208 | |
| 209 | // Set current user to u2 to make sure the friendship can be withdrawn |
| 210 | $this->set_current_user( $u2 ); |
| 211 | |
| 212 | // Withdraw the friendship |
| 213 | friends_withdraw_friendship( $u2, $u1 ); |
| 214 | |
| 215 | // Reset the current user |
| 216 | $this->set_current_user( $old_user ); |
| 217 | |
| 218 | $fw_act = bp_activity_get( array( |
| 219 | 'component' => buddypress()->friends->id, |
| 220 | 'item_id' => $friendship_id, |
| 221 | 'filter' => array( 'action' => array( 'friendship_created' ) ), |
| 222 | 'user_id' => $u2, |
| 223 | ) ); |
| 224 | |
| 225 | $this->assertTrue( count( $fw_act['activities'] ) == 0, 'The friendship_created activity should be deleted when a friendship is withdrawn' ); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @group bp_friends_friendship_rejected_activity |
| 230 | * @ticket BP6040 |
| 231 | */ |
| 232 | public function test_bp_friends_friendship_rejected_activity() { |
| 233 | $old_user = get_current_user_id(); |
| 234 | |
| 235 | $u1 = $this->factory->user->create(); |
| 236 | $u2 = $this->factory->user->create(); |
| 237 | |
| 238 | friends_add_friend( $u2, $u1 ); |
| 239 | $friendship_id = friends_get_friendship_id( $u2, $u1 ); |
| 240 | |
| 241 | // Set current user to u1 to make sure the friendship can be rejected |
| 242 | $this->set_current_user( $u1 ); |
| 243 | |
| 244 | // Withdraw the friendship |
| 245 | friends_reject_friendship( $friendship_id ); |
| 246 | |
| 247 | // Reset the current user |
| 248 | $this->set_current_user( $friendship_id ); |
| 249 | |
| 250 | $fj_act = bp_activity_get( array( |
| 251 | 'component' => buddypress()->friends->id, |
| 252 | 'item_id' => $friendship_id, |
| 253 | ) ); |
| 254 | |
| 255 | $this->assertTrue( count( $fj_act['activities'] ) == 0, 'Activities about a friendship should be deleted when a friendship is rejected' ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @group friends_delete_activity |
| 260 | * @ticket BP6040 |
| 261 | */ |
| 262 | public function test_friends_delete_activity() { |
| 263 | $old_user = get_current_user_id(); |
| 264 | |
| 265 | $u1 = $this->factory->user->create(); |
| 266 | $u2 = $this->factory->user->create(); |
| 267 | |
| 268 | friends_add_friend( $u2, $u1 ); |
| 269 | $friendship_id = friends_get_friendship_id( $u2, $u1 ); |
| 270 | |
| 271 | // Set current user to u1 to accepte the friendship |
| 272 | $this->set_current_user( $u1 ); |
| 273 | friends_accept_friendship( $friendship_id ); |
| 274 | |
| 275 | // Reset the current user |
| 276 | $this->set_current_user( $old_user ); |
| 277 | |
| 278 | // Random activities |
| 279 | $au1 = $this->factory->activity->create( array( 'user_id' => $u1 ) ); |
| 280 | $au2 = $this->factory->activity->create( array( 'user_id' => $u2 ) ); |
| 281 | |
| 282 | $fc_act = bp_activity_get( array( |
| 283 | 'component' => buddypress()->friends->id, |
| 284 | 'item_id' => $friendship_id, |
| 285 | 'filter' => array( 'action' => array( 'friendship_accepted', 'friendship_created' ) ), // friendship_accepted is no more used in BuddyPress since 1.1 |
| 286 | 'show_hidden' => true |
| 287 | ) ); |
| 288 | |
| 289 | $this->assertTrue( count( $fc_act['activities'] ) == 2, '2 activities should be created when a friendship is confirmed' ); |
| 290 | |
| 291 | // Remove the friendship |
| 292 | friends_remove_friend( $u2, $u1 ); |
| 293 | |
| 294 | $this->assertFalse( friends_check_friendship( $u2, $u1 ), '2 users should not be friend once the friendship is removed' ); |
| 295 | |
| 296 | $fd_act = bp_activity_get( array( |
| 297 | 'component' => buddypress()->friends->id, |
| 298 | 'item_id' => $friendship_id, |
| 299 | 'filter' => array( 'action' => array( 'friendship_accepted', 'friendship_created' ) ), // friendship_accepted is no more used in BuddyPress since 1.1 |
| 300 | 'show_hidden' => true |
| 301 | ) ); |
| 302 | |
| 303 | $this->assertTrue( count( $fd_act['activities'] ) == 0, 'friends_delete_activity() should remove activities about a deleted friendship' ); |