| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group activity |
| 5 | * @group notifications |
| 6 | */ |
| 7 | class BP_Tests_Activity_Notifications extends BP_UnitTestCase { |
| 8 | protected $current_user; |
| 9 | protected $u1; |
| 10 | protected $u2; |
| 11 | protected $a1; |
| 12 | protected $a2; |
| 13 | |
| 14 | public function setUp() { |
| 15 | parent::setUp(); |
| 16 | $this->current_user = get_current_user_id(); |
| 17 | $this->u1 = $this->create_user(); |
| 18 | $this->u2 = $this->create_user(); |
| 19 | $this->set_current_user( $this->u1 ); |
| 20 | } |
| 21 | |
| 22 | public function tearDown() { |
| 23 | $this->set_current_user( $this->current_user ); |
| 24 | parent::tearDown(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @group bp_activity_remove_screen_notifications |
| 29 | * @group mentions |
| 30 | */ |
| 31 | public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink() { |
| 32 | $this->create_notifications(); |
| 33 | |
| 34 | $notifications = BP_Notifications_Notification::get( array( |
| 35 | 'user_id' => $this->u1, |
| 36 | ) ); |
| 37 | |
| 38 | // Double check it's there |
| 39 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 40 | |
| 41 | // Go to the activity permalink page |
| 42 | $this->go_to( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $this->a1 . '/' ); |
| 43 | $activity = bp_activity_get_specific( array( 'activity_ids' => $this->a1, 'show_hidden' => true, 'spam' => 'ham_only', ) ); |
| 44 | do_action( 'bp_activity_screen_single_activity_permalink', $activity['activities'][0] ); |
| 45 | |
| 46 | $notifications = BP_Notifications_Notification::get( array( |
| 47 | 'user_id' => $this->u1, |
| 48 | ) ); |
| 49 | |
| 50 | // Should be empty |
| 51 | $this->assertEquals( array(), $notifications ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @group bp_activity_remove_screen_notifications |
| 56 | * @group mentions |
| 57 | */ |
| 58 | public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink_logged_out() { |
| 59 | $this->create_notifications(); |
| 60 | |
| 61 | $notifications = BP_Notifications_Notification::get( array( |
| 62 | 'user_id' => $this->u1, |
| 63 | ) ); |
| 64 | |
| 65 | // Double check it's there |
| 66 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 67 | |
| 68 | // Log out |
| 69 | $this->set_current_user( 0 ); |
| 70 | |
| 71 | // Go to the activity permalink page |
| 72 | $this->go_to( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $this->a1 . '/' ); |
| 73 | $activity = bp_activity_get_specific( array( 'activity_ids' => $this->a1, 'show_hidden' => true, 'spam' => 'ham_only', ) ); |
| 74 | do_action( 'bp_activity_screen_single_activity_permalink', $activity['activities'][0] ); |
| 75 | |
| 76 | $notifications = BP_Notifications_Notification::get( array( |
| 77 | 'user_id' => $this->u1, |
| 78 | ) ); |
| 79 | |
| 80 | // Should be untouched |
| 81 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 82 | |
| 83 | $this->set_current_user( $this->u1 ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @group bp_activity_remove_screen_notifications |
| 88 | * @group mentions |
| 89 | */ |
| 90 | public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink_wrong_user() { |
| 91 | $this->create_notifications(); |
| 92 | |
| 93 | $notifications = BP_Notifications_Notification::get( array( |
| 94 | 'user_id' => $this->u1, |
| 95 | ) ); |
| 96 | |
| 97 | // Double check it's there |
| 98 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 99 | |
| 100 | // Switch user |
| 101 | $this->set_current_user( $this->u2 ); |
| 102 | |
| 103 | // Go to the activity permalink page |
| 104 | $this->go_to( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/p/' . $this->a1 . '/' ); |
| 105 | $activity = bp_activity_get_specific( array( 'activity_ids' => $this->a1, 'show_hidden' => true, 'spam' => 'ham_only', ) ); |
| 106 | do_action( 'bp_activity_screen_single_activity_permalink', $activity['activities'][0] ); |
| 107 | |
| 108 | $notifications = BP_Notifications_Notification::get( array( |
| 109 | 'user_id' => $this->u1, |
| 110 | ) ); |
| 111 | |
| 112 | // Should be untouched |
| 113 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 114 | |
| 115 | $this->set_current_user( $this->u1 ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @group bp_activity_remove_screen_notifications |
| 120 | * @group mentions |
| 121 | */ |
| 122 | public function test_bp_activity_remove_screen_notifications_on_mentions() { |
| 123 | $this->create_notifications(); |
| 124 | |
| 125 | $notifications = BP_Notifications_Notification::get( array( |
| 126 | 'user_id' => $this->u1, |
| 127 | ) ); |
| 128 | |
| 129 | // Double check it's there |
| 130 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 131 | |
| 132 | // Go to the My Activity page |
| 133 | $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' ); |
| 134 | do_action( 'bp_activity_screen_mentions' ); |
| 135 | |
| 136 | $notifications = BP_Notifications_Notification::get( array( |
| 137 | 'user_id' => $this->u1, |
| 138 | ) ); |
| 139 | |
| 140 | // Should be empty |
| 141 | $this->assertEquals( array(), $notifications ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @group bp_activity_remove_screen_notifications |
| 146 | * @group mentions |
| 147 | */ |
| 148 | public function test_bp_activity_remove_screen_notifications_on_mentions_logged_out() { |
| 149 | $this->create_notifications(); |
| 150 | |
| 151 | $notifications = BP_Notifications_Notification::get( array( |
| 152 | 'user_id' => $this->u1, |
| 153 | ) ); |
| 154 | |
| 155 | // Double check it's there |
| 156 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 157 | |
| 158 | // Log out |
| 159 | $this->set_current_user( 0 ); |
| 160 | |
| 161 | // Go to the My Activity page |
| 162 | $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' ); |
| 163 | do_action( 'bp_activity_screen_mentions' ); |
| 164 | |
| 165 | $notifications = BP_Notifications_Notification::get( array( |
| 166 | 'user_id' => $this->u1, |
| 167 | ) ); |
| 168 | |
| 169 | // Should be untouched |
| 170 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 171 | |
| 172 | // clean up |
| 173 | $this->set_current_user( $this->u1 ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @group bp_activity_remove_screen_notifications |
| 178 | * @group mentions |
| 179 | */ |
| 180 | public function test_bp_activity_remove_screen_notifications_on_mentions_wrong_user() { |
| 181 | $this->create_notifications(); |
| 182 | |
| 183 | $notifications = BP_Notifications_Notification::get( array( |
| 184 | 'user_id' => $this->u1, |
| 185 | ) ); |
| 186 | |
| 187 | // Double check it's there |
| 188 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 189 | |
| 190 | // Log out |
| 191 | $this->set_current_user( $this->u2 ); |
| 192 | |
| 193 | // Go to the My Activity page |
| 194 | $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' ); |
| 195 | do_action( 'bp_activity_screen_mentions' ); |
| 196 | |
| 197 | $notifications = BP_Notifications_Notification::get( array( |
| 198 | 'user_id' => $this->u1, |
| 199 | ) ); |
| 200 | |
| 201 | // Should be untouched |
| 202 | $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) ); |
| 203 | |
| 204 | // clean up |
| 205 | $this->set_current_user( $this->u1 ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Creates two notifications for $u1, one of which is for mentions |
| 210 | */ |
| 211 | protected function create_notifications() { |
| 212 | $u1_mentionname = bp_activity_get_user_mentionname( $this->u1 ); |
| 213 | $this->a1 = $this->factory->activity->create( array( |
| 214 | 'user_id' => $this->u2, |
| 215 | 'component' => buddypress()->activity->id, |
| 216 | 'type' => 'activity_update', |
| 217 | 'content' => sprintf( 'Hello! @%s', $u1_mentionname ), |
| 218 | ) ); |
| 219 | $u2_mentionname = bp_activity_get_user_mentionname( $this->u2 ); |
| 220 | $this->a2 = $this->factory->activity->create( array( |
| 221 | 'user_id' => $this->u1, |
| 222 | 'component' => buddypress()->activity->id, |
| 223 | 'type' => 'activity_update', |
| 224 | 'content' => sprintf( 'Hello! @%s', $u2_mentionname ), |
| 225 | ) ); |
| 226 | } |
| 227 | } |