/tests/phpunit/testcases/activity/notifications.php

https://github.com/dxw/buddypress · PHP · 263 lines · 140 code · 56 blank · 67 comment · 0 complexity · a9b76d5b7d37d501c4f1955f7837d8a9 MD5 · raw file

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