[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/activity/ -> notifications.php (source)

   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 = self::factory()->user->create();
  18          $this->u2 = self::factory()->user->create();
  19          $this->set_current_user( $this->u1 );
  20  
  21          /**
  22           * Tests suite in WP < 4.0 does not include the WP_UnitTestCase->_restore_hooks() function
  23           * When updating an activity, the following filter is fired to prevent sending more than one
  24           * notification. Once we've reached this filter all at_mentions tests fails so we need to
  25           * temporarly remove it and restore it in $this->tearDown()
  26           */
  27          remove_filter( 'bp_activity_at_name_do_notifications', '__return_false' );
  28      }
  29  
  30  	public function tearDown() {
  31          $this->set_current_user( $this->current_user );
  32          parent::tearDown();
  33  
  34          // Restore the filter
  35          add_filter( 'bp_activity_at_name_do_notifications', '__return_false' );
  36      }
  37  
  38      /**
  39       * @group bp_activity_remove_screen_notifications
  40       * @group mentions
  41       */
  42      public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink() {
  43          $this->create_notifications();
  44  
  45          $notifications = BP_Notifications_Notification::get( array(
  46              'user_id' => $this->u1,
  47          ) );
  48  
  49          // Double check it's there
  50          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
  51  
  52          // Go to the activity permalink page
  53          $this->go_to( bp_core_get_user_domain( $this->u1 ) . 'activity/' . $this->a1 );
  54  
  55          $notifications = BP_Notifications_Notification::get( array(
  56              'user_id' => $this->u1,
  57          ) );
  58  
  59          // Should be empty
  60          $this->assertEquals( array(), $notifications );
  61      }
  62  
  63      /**
  64       * @group bp_activity_remove_screen_notifications
  65       * @group mentions
  66       */
  67      public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink_logged_out() {
  68          $this->create_notifications();
  69  
  70          $notifications = BP_Notifications_Notification::get( array(
  71              'user_id' => $this->u1,
  72          ) );
  73  
  74          // Double check it's there
  75          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
  76  
  77          // Log out
  78          $this->set_current_user( 0 );
  79  
  80          // Go to the activity permalink page
  81          $this->go_to( bp_core_get_user_domain( $this->u1 ) . 'activity/' . $this->a1 );
  82  
  83          $notifications = BP_Notifications_Notification::get( array(
  84              'user_id' => $this->u1,
  85          ) );
  86  
  87          // Should be untouched
  88          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
  89  
  90          $this->set_current_user( $this->u1 );
  91      }
  92  
  93      /**
  94       * @group bp_activity_remove_screen_notifications
  95       * @group mentions
  96       */
  97      public function test_bp_activity_remove_screen_notifications_on_single_activity_permalink_wrong_user() {
  98          $this->create_notifications();
  99  
 100          $notifications = BP_Notifications_Notification::get( array(
 101              'user_id' => $this->u1,
 102          ) );
 103  
 104          // Double check it's there
 105          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 106  
 107          // Switch user
 108          $this->set_current_user( $this->u2 );
 109  
 110          // Go to the activity permalink page
 111          $this->go_to( bp_core_get_user_domain( $this->u1 ) . 'activity/' . $this->a1 );
 112  
 113          $notifications = BP_Notifications_Notification::get( array(
 114              'user_id' => $this->u1,
 115          ) );
 116  
 117          // Should be untouched
 118          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 119  
 120          $this->set_current_user( $this->u1 );
 121      }
 122  
 123      /**
 124       * @group bp_activity_remove_screen_notifications
 125       * @group mentions
 126       */
 127      public function test_bp_activity_remove_screen_notifications_on_mentions() {
 128          $this->create_notifications();
 129  
 130          $notifications = BP_Notifications_Notification::get( array(
 131              'user_id' => $this->u1,
 132          ) );
 133  
 134          // Double check it's there
 135          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 136  
 137          // Go to the My Activity page
 138          $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' );
 139  
 140          $notifications = BP_Notifications_Notification::get( array(
 141              'user_id' => $this->u1,
 142          ) );
 143  
 144          // Should be empty
 145          $this->assertEquals( array(), $notifications );
 146      }
 147  
 148      /**
 149       * @group bp_activity_remove_screen_notifications
 150       * @group mentions
 151       */
 152      public function test_bp_activity_remove_screen_notifications_on_mentions_logged_out() {
 153          $this->create_notifications();
 154  
 155          $notifications = BP_Notifications_Notification::get( array(
 156              'user_id' => $this->u1,
 157          ) );
 158  
 159          // Double check it's there
 160          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 161  
 162          // Log out
 163          $this->set_current_user( 0 );
 164  
 165          // Go to the My Activity page
 166          $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' );
 167  
 168          $notifications = BP_Notifications_Notification::get( array(
 169              'user_id' => $this->u1,
 170          ) );
 171  
 172          // Should be untouched
 173          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 174  
 175          // clean up
 176          $this->set_current_user( $this->u1 );
 177      }
 178  
 179      /**
 180       * @group bp_activity_remove_screen_notifications
 181       * @group mentions
 182       */
 183      public function test_bp_activity_remove_screen_notifications_on_mentions_wrong_user() {
 184          $this->create_notifications();
 185  
 186          $notifications = BP_Notifications_Notification::get( array(
 187              'user_id' => $this->u1,
 188          ) );
 189  
 190          // Double check it's there
 191          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 192  
 193          // Log out
 194          $this->set_current_user( $this->u2 );
 195  
 196          // Go to the My Activity page
 197          $this->go_to( bp_core_get_user_domain( $this->u1 ) . bp_get_activity_slug() . '/mentions/' );
 198  
 199          $notifications = BP_Notifications_Notification::get( array(
 200              'user_id' => $this->u1,
 201          ) );
 202  
 203          // Should be untouched
 204          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 205  
 206          // clean up
 207          $this->set_current_user( $this->u1 );
 208      }
 209  
 210      /**
 211       * @group bp_notifications_delete_all_notifications_by_type
 212       * @group bp_activity_at_mention_delete_notification
 213       */
 214      public function test_bp_activity_at_mention_delete_notification() {
 215          $this->create_notifications();
 216  
 217          $notifications = BP_Notifications_Notification::get( array(
 218              'item_id' => $this->a1,
 219          ) );
 220  
 221          // Double check it's there
 222          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 223  
 224          bp_activity_delete( array(
 225              'id' => $this->a1,
 226          ) );
 227  
 228          $notifications = BP_Notifications_Notification::get( array(
 229              'item_id' => $this->a1,
 230          ) );
 231  
 232          $this->assertEmpty( $notifications );
 233      }
 234  
 235      /**
 236       * @group bp_activity_remove_screen_notifications
 237       * @group mentions
 238       * @ticket BP6687
 239       */
 240      public function test_bp_activity_remove_screen_notifications_on_new_mentions_cleared() {
 241          $this->create_notifications();
 242  
 243          $notifications = BP_Notifications_Notification::get( array(
 244              'item_id' => $this->a1,
 245          ) );
 246  
 247          // Double check it's there
 248          $this->assertEquals( array( $this->a1 ), wp_list_pluck( $notifications, 'item_id' ) );
 249          $this->assertEquals( 1, bp_get_total_mention_count_for_user( $this->u1 ) );
 250  
 251          // Clear notifications for $this->u1
 252          bp_activity_clear_new_mentions( $this->u1 );
 253  
 254          $notifications = BP_Notifications_Notification::get( array(
 255              'item_id' => $this->a1,
 256          ) );
 257  
 258          $this->assertEmpty( $notifications, 'Notifications should be cleared when new mention metas are removed' );
 259          $this->assertEmpty( bp_get_total_mention_count_for_user( $this->u1 ) );
 260      }
 261  
 262      /**
 263       * Creates two notifications for $u1, one of which is for mentions
 264       */
 265  	protected function create_notifications() {
 266          $u1_mentionname = bp_activity_get_user_mentionname( $this->u1 );
 267          $this->a1 = self::factory()->activity->create( array(
 268              'user_id' => $this->u2,
 269              'component' => buddypress()->activity->id,
 270              'type' => 'activity_update',
 271              'content' => sprintf( 'Hello! @%s', $u1_mentionname ),
 272          ) );
 273          $u2_mentionname = bp_activity_get_user_mentionname( $this->u2 );
 274          $this->a2 = self::factory()->activity->create( array(
 275              'user_id' => $this->u1,
 276              'component' => buddypress()->activity->id,
 277              'type' => 'activity_update',
 278              'content' => sprintf( 'Hello! @%s', $u2_mentionname ),
 279          ) );
 280      }
 281  
 282      /**
 283       * @group bp_activity_format_notifications
 284       */
 285      public function test_bp_activity_format_notifications_new_at_mention() {
 286          $this->test_format_filter = array();
 287  
 288          // Current user is $this->u1, so $this->u2 posted the mention
 289          $a = self::factory()->activity->create( array(
 290              'user_id' => $this->u2,
 291              'component' => buddypress()->activity->id,
 292              'type' => 'activity_update',
 293              'content' => 'fake new_at_mention activity',
 294          ) );
 295  
 296          add_filter( 'bp_activity_single_at_mentions_notification', array( $this, 'format_notification_filter' ), 10, 1 );
 297          add_filter( 'bp_activity_multiple_at_mentions_notification', array( $this, 'format_notification_filter' ), 10, 1 );
 298  
 299          $format_tests = array(
 300              'array_single'    => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 1, 'array' ),
 301              'string_single'   => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 1 ),
 302              'array_multiple'  => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 2, 'array' ),
 303              'string_multiple' => bp_activity_format_notifications( 'new_at_mention', $a, $this->u2, 2 ),
 304          );
 305  
 306          remove_filter( 'bp_activity_single_at_mentions_notification', array( $this, 'format_notification_filter' ), 10 );
 307          remove_filter( 'bp_activity_multiple_at_mentions_notification', array( $this, 'format_notification_filter' ), 10 );
 308  
 309          $single = sprintf( __( '%1$s mentioned you', 'buddypress' ), bp_core_get_user_displayname( $this->u2 ) );
 310          $multiple = 'You have 2 new mentions';
 311  
 312          $this->assertContains( $single, $format_tests['string_single'] );
 313          $this->assertContains( $single, $format_tests['array_single']['text'] );
 314          $this->assertContains( $multiple, $format_tests['string_multiple'] );
 315          $this->assertContains( $multiple, $format_tests['array_multiple']['text'] );
 316  
 317          // Check filters
 318          $this->assertTrue( 4 === count( $this->test_format_filter ) );
 319      }
 320  
 321  	public function format_notification_filter( $return ) {
 322          $this->test_format_filter[] = current_filter();
 323          return $return;
 324      }
 325  
 326      /**
 327       * @group bp_activity_update_reply_add_notification
 328       * @group bp_activity_comment_reply_add_notification
 329       */
 330  	public function test_bp_activity_comment_add_notification() {
 331          $a = self::factory()->activity->create( array(
 332              'user_id' => $this->u1,
 333              'component' => buddypress()->activity->id,
 334              'type' => 'activity_update',
 335              'content' => 'Please comment this activity.',
 336          ) );
 337  
 338          $c = bp_activity_new_comment( array(
 339              'content'     => 'this is the comment',
 340              'user_id'     => $this->u2,
 341              'activity_id' => $a, // ID of the root activity item.
 342              'parent_id'   => false  // ID of a parent comment (optional).
 343          ) );
 344  
 345          $u3 = self::factory()->user->create();
 346  
 347          $r3 = bp_activity_new_comment( array(
 348              'content'     => 'this is a reply to a comment',
 349              'user_id'     => $u3,
 350              'activity_id' => $a, // ID of the root activity item.
 351              'parent_id'   => $c  // ID of a parent comment (optional).
 352          ) );
 353  
 354          $u1_notifications = BP_Notifications_Notification::get( array(
 355              'user_id' => $this->u1,
 356          ) );
 357  
 358          $expected_commenters = array( $this->u2, $u3 );
 359          $this->assertEquals( $expected_commenters, wp_list_pluck( $u1_notifications, 'secondary_item_id' ) );
 360  
 361          $u2_notifications = BP_Notifications_Notification::get( array(
 362              'user_id' => $this->u2,
 363          ) );
 364  
 365          $expected_commenter = array( $u3 );
 366          $this->assertEquals( $expected_commenter, wp_list_pluck( $u2_notifications, 'secondary_item_id' ) );
 367  
 368          // Attempt to mark 'update_reply' notifications as read for user 1.
 369          foreach ( $u1_notifications as $i => $n ) {
 370              $n = bp_activity_format_notifications( $n->component_action, $n->item_id, $n->secondary_item_id, 1, 'array', $n->id );
 371              if ( ! empty( $n['link'] ) ) {
 372                  // Remove redirecter for unit tests.
 373                  $n['link'] = str_replace( '/p/', '/', $n['link'] );
 374  
 375                  // Attempt to clear the notification by going to the activity permalink.
 376                  $this->go_to( $n['link'] );
 377              }
 378          }
 379  
 380          // Assert that notifications for user 1 are cleared and empty.
 381          $this->assertEmpty( BP_Notifications_Notification::get( array(
 382              'user_id' => $this->u1,
 383          ) ) );
 384  
 385          // Attempt to mark 'comment_reply' notifications as read for user 2.
 386          $this->set_current_user( $this->u2 );
 387          foreach ( $u2_notifications as $i => $n ) {
 388              $n = bp_activity_format_notifications( $n->component_action, $n->item_id, $n->secondary_item_id, 1, 'array', $n->id );
 389              if ( ! empty( $n['link'] ) ) {
 390                  // Remove redirecter for unit tests.
 391                  $n['link'] = str_replace( '/p/', '/', $n['link'] );
 392  
 393                  // Attempt to clear the notification by going to the activity permalink.
 394                  $this->go_to( $n['link'] );
 395              }
 396          }
 397  
 398          // Assert that notifications for user 2 are cleared and empty.
 399          $this->assertEmpty( BP_Notifications_Notification::get( array(
 400              'user_id' => $this->u2,
 401          ) ) );
 402      }
 403  
 404      /**
 405       * @ticket BP7135
 406       */
 407      public function test_activity_reply_notifications_for_blog_comment_to_activity_comment_sync() {
 408          $old_user = get_current_user_id();
 409          $u1 = self::factory()->user->create();
 410          $u2 = self::factory()->user->create();
 411          $u3 = self::factory()->user->create();
 412  
 413          $this->set_current_user( $u1 );
 414          $userdata = get_userdata( $u1 );
 415  
 416          // let's use activity comments instead of single "new_blog_comment" activity items
 417          add_filter( 'bp_disable_blogforum_comments', '__return_false' );
 418  
 419          // Silence comment flood errors.
 420          add_filter( 'comment_flood_filter', '__return_false' );
 421  
 422          // create the blog post
 423          $post_id = self::factory()->post->create( array(
 424              'post_status' => 'publish',
 425              'post_type'   => 'post',
 426              'post_title'  => 'Test post',
 427          ) );
 428  
 429          $this->set_current_user( $u2 );
 430          $userdata = get_userdata( $u2 );
 431  
 432          $c1 = wp_new_comment( array(
 433              'comment_post_ID'      => $post_id,
 434              'comment_author'       => $userdata->user_nicename,
 435              'comment_author_url'   => 'http://buddypress.org',
 436              'comment_author_email' => $userdata->user_email,
 437              'comment_content'      => 'this is a blog comment',
 438              'comment_type'         => '',
 439              'comment_parent'       => 0,
 440              'user_id'              => $u2,
 441          ) );
 442          // Approve the comment
 443          self::factory()->comment->update_object( $c1, array( 'comment_approved' => 1 ) );
 444  
 445          $this->set_current_user( $u3 );
 446          $userdata = get_userdata( $u3 );
 447  
 448          $c2 = wp_new_comment( array(
 449              'comment_post_ID'      => $post_id,
 450              'comment_author'       => $userdata->user_nicename,
 451              'comment_author_url'   => 'http://buddypress.org',
 452              'comment_author_email' => $userdata->user_email,
 453              'comment_content'      => 'this is a blog comment',
 454              'comment_type'         => '',
 455              'comment_parent'       => $c1,
 456              'user_id'              => $u3,
 457          ) );
 458          // Approve the comment
 459          self::factory()->comment->update_object( $c2, array( 'comment_approved' => 1 ) );
 460  
 461          // Get activity IDs.
 462          $ac1 = get_comment_meta( $c1, 'bp_activity_comment_id', true );
 463          $ac2 = get_comment_meta( $c2, 'bp_activity_comment_id', true );
 464  
 465          // Check if notifications exists for user 1.
 466          $n1 = BP_Notifications_Notification::get( array(
 467              'component_name' => 'activity',
 468              'user_id'        => $u1
 469          ) );
 470          $this->assertEquals( 2, count( $n1 ) );
 471          $this->assertEquals(
 472              array( $ac1, $ac2 ),
 473              wp_list_pluck( $n1, 'item_id' )
 474          );
 475  
 476          // Check if notification exists for user 2.
 477          $n2 = BP_Notifications_Notification::get( array(
 478              'component_action' => 'comment_reply',
 479              'item_id'          => $ac2,
 480              'user_id'          => $u2
 481          ) );
 482          $this->assertNotEmpty( $n2 );
 483  
 484          // Reset.
 485          $this->set_current_user( $old_user );
 486          remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
 487          remove_filter( 'comment_flood_filter', '__return_false' );
 488      }
 489  
 490  }


Generated: Sat Dec 7 01:00:57 2024 Cross-referenced by PHPXref 0.7.1