[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group notifications
   5   */
   6  class BP_Tests_Notifications_Functions extends BP_UnitTestCase {
   7  
   8      /**
   9       * @group bp_notifications_add_notification
  10       */
  11      public function test_bp_notifications_add_notification_no_dupes() {
  12          $args = array(
  13              'user_id'           => 5,
  14              'item_id'           => 10,
  15              'secondary_item_id' => 25,
  16              'component_name'    => 'messages',
  17              'component_action'  => 'new_message'
  18          );
  19  
  20          self::factory()->notification->create( $args );
  21  
  22          $this->assertFalse( bp_notifications_add_notification( $args ) );
  23      }
  24  
  25      /**
  26       * @group bp_notifications_add_notification
  27       */
  28      public function test_bp_notifications_add_notification_allow_duplicate() {
  29          $args = array(
  30              'user_id'           => 5,
  31              'item_id'           => 10,
  32              'secondary_item_id' => 25,
  33              'component_name'    => 'messages',
  34              'component_action'  => 'new_message'
  35          );
  36  
  37          self::factory()->notification->create( $args );
  38  
  39          $args['allow_duplicate'] = true;
  40  
  41          $this->assertNotEmpty( bp_notifications_add_notification( $args ) );
  42      }
  43  
  44      /**
  45       * @group bp_notifications_get_unread_notification_count
  46       * @group cache
  47       */
  48      public function test_bp_notifications_get_unread_notification_count_cache() {
  49          $u1 = self::factory()->user->create();
  50          $u2 = self::factory()->user->create();
  51  
  52          self::factory()->notification->create( array(
  53              'component_name'    => 'messages',
  54              'component_action'  => 'new_message',
  55              'item_id'           => 99,
  56              'user_id'           => $u2,
  57              'secondary_item_id' => $u1,
  58              'is_new'            => true
  59          ) );
  60  
  61          // prime cache
  62          bp_notifications_get_unread_notification_count( $u2 );
  63  
  64          // mark the created notification as read
  65          bp_notifications_mark_notifications_by_item_id( $u2, 99, 'messages', 'new_message', $u1 );
  66  
  67          // now grab the updated notification count
  68          $n = bp_notifications_get_unread_notification_count( $u2 );
  69  
  70          // assert
  71          $this->assertEquals( 0, $n );
  72      }
  73  
  74      /**
  75       * @group bp_has_notifications
  76       */
  77  	public function test_bp_has_notifications_filtering() {
  78          $u1 = self::factory()->user->create();
  79          $u2 = self::factory()->user->create();
  80  
  81          // create a mixture of different notifications
  82          self::factory()->notification->create( array(
  83              'component_name'    => 'messages',
  84              'component_action'  => 'new_message',
  85              'item_id'           => 99,
  86              'user_id'           => $u2,
  87              'secondary_item_id' => $u1,
  88              'is_new'            => true
  89          ) );
  90  
  91          self::factory()->notification->create( array(
  92              'component_name'    => 'activity',
  93              'component_action'  => 'new_at_mention',
  94              'item_id'           => 99,
  95              'user_id'           => $u2,
  96              'secondary_item_id' => $u1,
  97              'is_new'            => true
  98          ) );
  99  
 100          self::factory()->notification->create( array(
 101              'component_name'    => 'activity',
 102              'component_action'  => 'new_at_mention',
 103              'item_id'           => 100,
 104              'user_id'           => $u2,
 105              'secondary_item_id' => $u1,
 106              'is_new'            => true
 107          ) );
 108  
 109          // now fetch only activity notifications
 110          bp_has_notifications( array(
 111              'component_name' => 'activity',
 112              'user_id'        => $u2
 113          ) );
 114  
 115          // assert
 116          $this->assertEquals( 2, buddypress()->notifications->query_loop->total_notification_count );
 117      }
 118  
 119      /**
 120       * @group bp_notifications_delete_notifications_on_user_delete
 121       * @ticket BP6681
 122       */
 123      public function test_bp_notifications_delete_notifications_on_user_delete_should_delete_all_notifications() {
 124          $u = self::factory()->user->create();
 125  
 126          // Create notifications
 127          $n1 = self::factory()->notification->create( array(
 128              'component_name'    => 'messages',
 129              'component_action'  => 'new_message',
 130              'item_id'           => 99,
 131              'user_id'           => $u,
 132          ) );
 133  
 134          $n2 = self::factory()->notification->create( array(
 135              'component_name'    => 'activity',
 136              'component_action'  => 'new_at_mention',
 137              'item_id'           => 99,
 138              'user_id'           => $u,
 139          ) );
 140  
 141          $n3 = self::factory()->notification->create( array(
 142              'component_name' => 'groups',
 143              'user_id'        => $u,
 144          ) );
 145  
 146          $n4 = self::factory()->notification->create( array(
 147              'component_name'   => 'friends',
 148              'component_action' => 'friendship_request',
 149              'user_id'          => $u,
 150          ) );
 151  
 152          // Create notification for non-core component
 153          $n5 = self::factory()->notification->create( array(
 154              'component_name'    => 'foo',
 155              'component_action'  => 'bar',
 156              'item_id'           => 99,
 157              'user_id'           => $u,
 158          ) );
 159  
 160          global $wpdb, $bp;
 161  
 162          /**
 163           * Can't use BP_Notifications_Notification::get(), because class::parse_args,
 164           * checks against bp_notifications_get_registered_components()
 165           * and if component is disabled it will be ignored.
 166           */
 167          $query = $wpdb->prepare( "SELECT id FROM {$bp->notifications->table_name} WHERE user_id = %d and is_new = 1", $u );
 168  
 169          // Make sure notifications have been added.
 170          $found1 = $wpdb->get_col( $query );
 171          $this->assertEqualSets( array( $n1, $n2, $n3, $n4, $n5 ), $found1 );
 172  
 173          $this->delete_user( $u );
 174  
 175          // Check if notifications are deleted.
 176          $found2 = $wpdb->get_col( $query );
 177          $this->assertEmpty( $found2 );
 178      }
 179  
 180      /**
 181       * @group  notification_callback
 182       * @ticket BP7141
 183       */
 184      public function test_notification_callback_parameter_integrity() {
 185          $u = self::factory()->user->create();
 186  
 187          $n = self::factory()->notification->create( array(
 188              'component_name'    => 'activity',
 189              'component_action'  => 'new_at_mention',
 190              'item_id'           => 99,
 191              'user_id'           => $u,
 192          ) );
 193  
 194          // Override activity notification callback so we can test integrity.
 195          buddypress()->activity->notification_callback = array( $this, 'dummy_notification_callback' );
 196  
 197          // Fetch notifications with string format.
 198          bp_notifications_get_notifications_for_user( $u, 'string' );
 199  
 200          // Assert!
 201          // @todo When we cast all numeric strings as integers, this needs to be changed.
 202          $expected = array(
 203              'action'            => 'new_at_mention',
 204              'item_id'           => '99',
 205              'secondary_item_id' => '0',
 206              'total_items'       => 1,
 207              'id'                => (string) $n,
 208              'format'            => 'string'
 209          );
 210          $this->assertEquals( $expected, $this->n_args );
 211  
 212          // Fetch notifications with object format this time.
 213          bp_notifications_get_notifications_for_user( $u, 'object' );
 214  
 215          // Assert!
 216          $expected['format'] = 'array';
 217          $this->assertEquals( $expected, $this->n_args );
 218  
 219          // Reset!
 220          buddypress()->activity->notification_callback = 'bp_activity_format_notifications';
 221          unset( $this->n_args );
 222      }
 223  
 224      /**
 225       * Used in test_notification_callback_parameter_integrity() test.
 226       */
 227  	public function dummy_notification_callback( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $id = 0 ) {
 228          $this->n_args = compact( 'action', 'item_id', 'secondary_item_id', 'total_items', 'id', 'format' );
 229      }
 230  
 231      /**
 232       * @group cache
 233       * @ticket BP7130
 234       */
 235      public function test_get_grouped_notifications_for_user_cache_invalidation() {
 236          $u = self::factory()->user->create();
 237  
 238          $n1 = self::factory()->notification->create( array(
 239              'component_name'    => 'activity',
 240              'component_action'  => 'new_at_mention',
 241              'item_id'           => 99,
 242              'user_id'           => $u,
 243          ) );
 244  
 245          // Prime cache.
 246          $found = bp_notifications_get_grouped_notifications_for_user( $u );
 247          $this->assertEquals( 1, $found[0]->total_count );
 248  
 249          $n2 = self::factory()->notification->create( array(
 250              'component_name'    => 'activity',
 251              'component_action'  => 'new_at_mention',
 252              'item_id'           => 100,
 253              'user_id'           => $u,
 254          ) );
 255  
 256          $found = bp_notifications_get_grouped_notifications_for_user( $u );
 257          $this->assertEquals( 2, $found[0]->total_count );
 258      }
 259  
 260      /**
 261       * @ticket BP7827
 262       */
 263  	public function test_bp_notifications_personal_data_exporter() {
 264          $u = self::factory()->user->create();
 265  
 266          // Create notifications
 267          $n1 = self::factory()->notification->create( array(
 268              'component_name'    => 'messages',
 269              'component_action'  => 'new_message',
 270              'item_id'           => 99,
 271              'user_id'           => $u,
 272          ) );
 273  
 274          $n2 = self::factory()->notification->create( array(
 275              'component_name'    => 'activity',
 276              'component_action'  => 'new_at_mention',
 277              'item_id'           => 99,
 278              'user_id'           => $u,
 279          ) );
 280  
 281          $test_user = new WP_User( $u );
 282  
 283          $actual = bp_notifications_personal_data_exporter( $test_user->user_email, 1 );
 284  
 285          $this->assertTrue( $actual['done'] );
 286  
 287          // Number of exported notification items.
 288          $this->assertSame( 2, count( $actual['data'] ) );
 289      }
 290  
 291      /**
 292       * @ticket BP8175
 293       */
 294      public function test_notifications_data_should_be_deleted_on_user_delete_non_multisite() {
 295          if ( is_multisite() ) {
 296              $this->markTestSkipped( __METHOD__ . ' requires non-multisite.' );
 297          }
 298  
 299          $u = self::factory()->user->create();
 300  
 301          $n1 = self::factory()->notification->create( array(
 302              'component_name'    => 'messages',
 303              'component_action'  => 'new_message',
 304              'item_id'           => 99,
 305              'user_id'           => $u,
 306          ) );
 307  
 308          $found = bp_notifications_get_notifications_for_user( $u, 'object' );
 309          $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) );
 310  
 311          wp_delete_user( $u );
 312  
 313          $found = bp_notifications_get_notifications_for_user( $u, 'object' );
 314          $this->assertEmpty( '', $found );
 315      }
 316  
 317      /**
 318       * @ticket BP8175
 319       */
 320      public function test_notifications_data_should_be_deleted_on_user_delete_multisite() {
 321          if ( ! is_multisite() ) {
 322              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
 323          }
 324  
 325          $u = self::factory()->user->create();
 326  
 327          $n1 = self::factory()->notification->create( array(
 328              'component_name'    => 'messages',
 329              'component_action'  => 'new_message',
 330              'item_id'           => 99,
 331              'user_id'           => $u,
 332          ) );
 333  
 334          $found = bp_notifications_get_notifications_for_user( $u, 'object' );
 335          $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) );
 336  
 337          wpmu_delete_user( $u );
 338  
 339          $found = bp_notifications_get_notifications_for_user( $u, 'object' );
 340          $this->assertEmpty( '', $found );
 341      }
 342  
 343      /**
 344       * @ticket BP8175
 345       */
 346      public function test_notifications_data_should_not_be_deleted_on_wp_delete_user_multisite() {
 347          if ( ! is_multisite() ) {
 348              $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
 349          }
 350  
 351          $u = self::factory()->user->create();
 352  
 353          $n1 = self::factory()->notification->create( array(
 354              'component_name'    => 'messages',
 355              'component_action'  => 'new_message',
 356              'item_id'           => 99,
 357              'user_id'           => $u,
 358          ) );
 359  
 360          $found = bp_notifications_get_notifications_for_user( $u, 'object' );
 361          $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) );
 362  
 363          wp_delete_user( $u );
 364  
 365          $found = bp_notifications_get_notifications_for_user( $u, 'object' );
 366          $this->assertEqualSets( [ $n1 ], wp_list_pluck( $found, 'id' ) );
 367      }
 368  
 369      /**
 370       * @ticket BP8426
 371       */
 372      public function test_bp_notifications_mark_notifications_by_ids() {
 373          $u = self::factory()->user->create();
 374  
 375          $n = self::factory()->notification->create(
 376              array(
 377                  'component_name'    => 'barfoo',
 378                  'component_action'  => 'new_bar',
 379                  'item_id'           => 98,
 380                  'user_id'           => $u,
 381              )
 382          );
 383  
 384          for ( $i = 101; $i < 111; ++$i ) {
 385              self::factory()->notification->create(
 386                  array(
 387                      'component_name'    => 'foobar',
 388                      'component_action'  => 'new_foo',
 389                      'item_id'           => $i,
 390                      'user_id'           => $u,
 391                  )
 392              );
 393          }
 394  
 395          $unread = wp_list_pluck(
 396              BP_Notifications_Notification::get(
 397                  array(
 398                      'user_id'           => $u,
 399                      'component_name'    => 'foobar',
 400                      'component_action'  => 'new_foo',
 401                      'is_new'            => 1,
 402                  )
 403              ),
 404              'id'
 405          );
 406  
 407          bp_notifications_mark_notifications_by_ids( $unread );
 408  
 409          $read = wp_list_pluck(
 410              BP_Notifications_Notification::get(
 411                  array(
 412                      'user_id'           => $u,
 413                      'component_name'    => 'foobar',
 414                      'component_action'  => 'new_foo',
 415                      'is_new'            => 0,
 416                  )
 417              ),
 418              'id'
 419          );
 420  
 421          $n_get = BP_Notifications_Notification::get(
 422              array(
 423                  'id' => $n,
 424                  'component_name'    => 'barfoo',
 425                  'component_action'  => 'new_bar',
 426              )
 427          );
 428  
 429          $n_obj = reset( $n_get );
 430  
 431          $this->assertEquals( $unread, $read );
 432          $this->assertEquals( $n, $n_obj->id );
 433          $this->assertTrue( 1 === (int) $n_obj->is_new );
 434      }
 435  
 436      /**
 437       * @ticket BP8426
 438       * @group delete_notifications_by_ids
 439       */
 440      public function test_bp_notifications_delete_notifications_by_ids() {
 441          $u = self::factory()->user->create();
 442  
 443          $n = self::factory()->notification->create(
 444              array(
 445                  'component_name'    => 'barfoo',
 446                  'component_action'  => 'new_bar',
 447                  'item_id'           => 98,
 448                  'user_id'           => $u,
 449              )
 450          );
 451  
 452          for ( $i = 101; $i < 111; ++$i ) {
 453              self::factory()->notification->create(
 454                  array(
 455                      'component_name'    => 'foobar',
 456                      'component_action'  => 'new_foo',
 457                      'item_id'           => $i,
 458                      'user_id'           => $u,
 459                  )
 460              );
 461          }
 462  
 463          $unread = wp_list_pluck(
 464              BP_Notifications_Notification::get(
 465                  array(
 466                      'user_id'           => $u,
 467                      'component_name'    => 'foobar',
 468                      'component_action'  => 'new_foo',
 469                      'is_new'            => 1,
 470                  )
 471              ),
 472              'id'
 473          );
 474  
 475          bp_notifications_delete_notifications_by_ids( $unread );
 476  
 477          $deleted = wp_list_pluck(
 478              BP_Notifications_Notification::get(
 479                  array(
 480                      'user_id'           => $u,
 481                      'component_name'    => 'foobar',
 482                      'component_action'  => 'new_foo',
 483                      'is_new'            => 1,
 484                  )
 485              ),
 486              'id'
 487          );
 488  
 489          $n_get = BP_Notifications_Notification::get(
 490              array(
 491                  'id' => $n,
 492                  'component_name'    => 'barfoo',
 493                  'component_action'  => 'new_bar',
 494              )
 495          );
 496  
 497          $n_obj = reset( $n_get );
 498  
 499          $this->assertEmpty( $deleted );
 500          $this->assertEquals( $n, $n_obj->id );
 501          $this->assertTrue( 1 === (int) $n_obj->is_new );
 502      }
 503  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1