[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/notifications/ -> class-bp-notifications-notification.php (source)

   1  <?php
   2  
   3  /**
   4   * @group notifications
   5   */
   6  class BP_Tests_BP_Notifications_Notification_TestCases extends BP_UnitTestCase {
   7      /**
   8       * @group get
   9       */
  10  	public function test_get_null_component_name() {
  11          $u = self::factory()->user->create();
  12          $n1 = self::factory()->notification->create( array(
  13              'component_name' => 'groups',
  14              'user_id' => $u,
  15          ) );
  16          $n2 = self::factory()->notification->create( array(
  17              'component_name' => 'messages',
  18              'user_id' => $u,
  19          ) );
  20  
  21          // temporarily turn on groups, shut off messages
  22          $groups_toggle = isset( buddypress()->active_components['groups'] );
  23          $messages_toggle = isset( buddypress()->active_components['messages'] );
  24          buddypress()->active_components['groups'] = 1;
  25          unset( buddypress()->active_components['messages'] );
  26  
  27          $n = BP_Notifications_Notification::get( array(
  28              'user_id' => $u,
  29          ) );
  30  
  31          // Check that the correct items are pulled up
  32          $expected = array( $n1 );
  33          $actual = wp_list_pluck( $n, 'id' );
  34          $this->assertEquals( $expected, $actual );
  35  
  36          // reset component toggles.
  37          if ( $groups_toggle ) {
  38              buddypress()->active_components['groups'] = 1;
  39          } else {
  40              unset( buddypress()->active_components['groups'] );
  41          }
  42  
  43          if ( $messages_toggle ) {
  44              buddypress()->active_components['messages'] = 1;
  45          } else {
  46              unset( buddypress()->active_components['messages'] );
  47          }
  48      }
  49  
  50      /**
  51       * @group get_total_count
  52       * @ticket BP5300
  53       */
  54  	public function test_get_total_count_null_component_name() {
  55          $u = self::factory()->user->create();
  56          $n1 = self::factory()->notification->create( array(
  57              'component_name' => 'groups',
  58              'user_id' => $u,
  59          ) );
  60          $n2 = self::factory()->notification->create( array(
  61              'component_name' => 'messages',
  62              'user_id' => $u,
  63          ) );
  64  
  65          // temporarily turn on groups, shut off messages
  66          $groups_toggle = isset( buddypress()->active_components['groups'] );
  67          $messages_toggle = isset( buddypress()->active_components['messages'] );
  68          buddypress()->active_components['groups'] = 1;
  69          unset( buddypress()->active_components['messages'] );
  70  
  71          $n = BP_Notifications_Notification::get_total_count( array(
  72              'user_id' => $u,
  73          ) );
  74  
  75          // Check that the correct items are pulled up
  76          $this->assertEquals( 1, $n );
  77  
  78          // reset component toggles.
  79          if ( $groups_toggle ) {
  80              buddypress()->active_components['groups'] = 1;
  81          } else {
  82              unset( buddypress()->active_components['groups'] );
  83          }
  84  
  85          if ( $messages_toggle ) {
  86              buddypress()->active_components['messages'] = 1;
  87          } else {
  88              unset( buddypress()->active_components['messages'] );
  89          }
  90      }
  91  
  92      /**
  93       * @group get_total_count
  94       * @ticket BP5300
  95       */
  96  	public function test_get_total_count_with_component_name() {
  97          $u = self::factory()->user->create();
  98          $n1 = self::factory()->notification->create( array(
  99              'component_name' => 'groups',
 100              'user_id' => $u,
 101          ) );
 102          $n2 = self::factory()->notification->create( array(
 103              'component_name' => 'groups',
 104              'user_id' => $u,
 105          ) );
 106          $n3 = self::factory()->notification->create( array(
 107              'component_name' => 'messages',
 108              'user_id' => $u,
 109          ) );
 110  
 111          $n = BP_Notifications_Notification::get_total_count( array(
 112              'user_id' => $u,
 113              'component_name' => array( 'messages' ),
 114          ) );
 115  
 116          $this->assertEquals( 1, $n );
 117      }
 118  
 119      /**
 120       * @group order_by
 121       * @group sort_order
 122       */
 123  	public function test_order_by_date() {
 124          $now = time();
 125          $u = self::factory()->user->create();
 126          $n1 = self::factory()->notification->create( array(
 127              'component_name' => 'friends',
 128              'user_id' => $u,
 129              'date_notified' => date( 'Y-m-d H:i:s', $now - 500 ),
 130          ) );
 131          $n2 = self::factory()->notification->create( array(
 132              'component_name' => 'groups',
 133              'user_id' => $u,
 134              'date_notified' => date( 'Y-m-d H:i:s', $now - 100 ),
 135          ) );
 136          $n3 = self::factory()->notification->create( array(
 137              'component_name' => 'messages',
 138              'user_id' => $u,
 139              'date_notified' => date( 'Y-m-d H:i:s', $now - 1000 ),
 140          ) );
 141  
 142          $n = BP_Notifications_Notification::get( array(
 143              'user_id' => $u,
 144              'order_by' => 'date_notified',
 145              'sort_order' => 'DESC',
 146          ) );
 147  
 148          // Check that the correct items are pulled up
 149          $expected = array( $n2, $n1, $n3 );
 150          $actual = wp_list_pluck( $n, 'id' );
 151          $this->assertEquals( $expected, $actual );
 152      }
 153  
 154      /**
 155       * @group is_new
 156       */
 157  	public function test_is_new_true() {
 158          $u = self::factory()->user->create();
 159          $n1 = self::factory()->notification->create( array(
 160              'component_name' => 'friends',
 161              'user_id' => $u,
 162              'is_new' => false,
 163          ) );
 164          $n2 = self::factory()->notification->create( array(
 165              'component_name' => 'groups',
 166              'user_id' => $u,
 167              'is_new' => true,
 168          ) );
 169          $n3 = self::factory()->notification->create( array(
 170              'component_name' => 'messages',
 171              'user_id' => $u,
 172              'is_new' => true,
 173          ) );
 174  
 175          $n = BP_Notifications_Notification::get( array(
 176              'user_id' => $u,
 177              'is_new' => true,
 178          ) );
 179  
 180          // Check that the correct items are pulled up
 181          $expected = array( $n2, $n3 );
 182          $actual = wp_list_pluck( $n, 'id' );
 183          $this->assertEquals( $expected, $actual );
 184      }
 185  
 186      /**
 187       * @group is_new
 188       */
 189  	public function test_is_new_false() {
 190          $u = self::factory()->user->create();
 191          $n1 = self::factory()->notification->create( array(
 192              'component_name' => 'friends',
 193              'user_id' => $u,
 194              'is_new' => false,
 195          ) );
 196          $n2 = self::factory()->notification->create( array(
 197              'component_name' => 'groups',
 198              'user_id' => $u,
 199              'is_new' => true,
 200          ) );
 201          $n3 = self::factory()->notification->create( array(
 202              'component_name' => 'messages',
 203              'user_id' => $u,
 204              'is_new' => true,
 205          ) );
 206  
 207          $n = BP_Notifications_Notification::get( array(
 208              'user_id' => $u,
 209              'is_new' => false,
 210          ) );
 211  
 212          // Check that the correct items are pulled up
 213          $expected = array( $n1 );
 214          $actual = wp_list_pluck( $n, 'id' );
 215          $this->assertEquals( $expected, $actual );
 216      }
 217  
 218      /**
 219       * @group is_new
 220       */
 221  	public function test_is_new_both() {
 222          $u = self::factory()->user->create();
 223          $n1 = self::factory()->notification->create( array(
 224              'component_name' => 'friends',
 225              'user_id' => $u,
 226              'is_new' => false,
 227          ) );
 228          $n2 = self::factory()->notification->create( array(
 229              'component_name' => 'groups',
 230              'user_id' => $u,
 231              'is_new' => true,
 232          ) );
 233          $n3 = self::factory()->notification->create( array(
 234              'component_name' => 'messages',
 235              'user_id' => $u,
 236              'is_new' => true,
 237          ) );
 238  
 239          $n = BP_Notifications_Notification::get( array(
 240              'user_id' => $u,
 241              'is_new' => 'both',
 242          ) );
 243  
 244          // Check that the correct items are pulled up
 245          $expected = array( $n1, $n2, $n3 );
 246          $actual = wp_list_pluck( $n, 'id' );
 247          $this->assertEquals( $expected, $actual );
 248      }
 249  
 250      /**
 251       * @group get
 252       * @group search_terms
 253       */
 254  	public function test_get_with_search_terms() {
 255          $u = self::factory()->user->create();
 256          $n1 = self::factory()->notification->create( array(
 257              'component_name' => 'friends',
 258              'user_id' => $u,
 259              'is_new' => false,
 260          ) );
 261          $n2 = self::factory()->notification->create( array(
 262              'component_name' => 'groups',
 263              'user_id' => $u,
 264              'is_new' => true,
 265          ) );
 266          $n3 = self::factory()->notification->create( array(
 267              'component_name' => 'messages',
 268              'user_id' => $u,
 269              'is_new' => true,
 270          ) );
 271  
 272          $n = BP_Notifications_Notification::get( array(
 273              'user_id' => $u,
 274              'search_terms' => 'roup',
 275          ) );
 276  
 277          // Check that the correct items are pulled up
 278          $expected = array( $n2 );
 279          $actual = wp_list_pluck( $n, 'id' );
 280          $this->assertEquals( $expected, $actual );
 281      }
 282  
 283      /**
 284       * @group pagination
 285       * @group BP6229
 286       */
 287  	public function test_get_paged_sql() {
 288          $u = self::factory()->user->create();
 289  
 290          $notifications = array();
 291          for ( $i = 1; $i <= 6; $i++ ) {
 292              $notifications[] = self::factory()->notification->create( array(
 293                  'component_name' => 'activity',
 294                  'secondary_item_id' => $i,
 295                  'user_id' => $u,
 296                  'is_new' => true,
 297              ) );
 298          }
 299  
 300          $found = BP_Notifications_Notification::get( array(
 301              'user_id' => $u,
 302              'is_new' => true,
 303              'page' => 2,
 304              'per_page' => 2,
 305              'order_by' => 'id',
 306          ) );
 307  
 308          // Check that the correct number of items are pulled up
 309          $expected = array( $notifications[2], $notifications[3] );
 310          $this->assertEquals( $expected, wp_list_pluck( $found, 'id' ) );
 311      }
 312  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1