[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group notifications
   5   * @group messages
   6   */
   7  class BP_Tests_Messages_Notifications extends BP_UnitTestCase {
   8  
   9      protected $filter_fired;
  10  
  11  	public function setUp() {
  12          parent::setUp();
  13  
  14          $this->reset_user_id = get_current_user_id();
  15  
  16          $this->filter_fired = '';
  17      }
  18  
  19  	public function tearDown() {
  20          parent::tearDown();
  21  
  22          $this->set_current_user( $this->reset_user_id );
  23      }
  24  
  25      /**
  26       * @group messages_format_notifications
  27       */
  28      public function test_friends_format_notifications_bp_messages_multiple_new_message_notification_nonstring_filter() {
  29          // Dummy thread ID
  30          $t = 12;
  31          $u = self::factory()->user->create();
  32          $this->set_current_user( $u );
  33  
  34          // Admin
  35          $n = self::factory()->notification->create( array(
  36              'component_name' => 'messages',
  37              'user_id' => $u,
  38              'item_id' => $t,
  39              'component_action' => 'new_message',
  40          ) );
  41  
  42          add_filter( 'bp_messages_multiple_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
  43          $n = messages_format_notifications( 'new_message', $n, '', 2, 'array' );
  44          remove_filter( 'bp_messages_multiple_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
  45  
  46          $this->assertSame( 'bp_messages_multiple_new_message_array_notification', $this->filter_fired );
  47      }
  48  
  49      /**
  50       * @group messages_format_notifications
  51       */
  52      public function test_friends_format_notifications_bp_messages_single_new_message_notification_nonstring_filter() {
  53          // Dummy thread ID
  54          $t = 12;
  55          $u = self::factory()->user->create();
  56          $this->set_current_user( $u );
  57  
  58          // Admin
  59          $n = self::factory()->notification->create( array(
  60              'component_name' => 'messages',
  61              'user_id' => $u,
  62              'item_id' => $t,
  63              'component_action' => 'new_message',
  64          ) );
  65  
  66          add_filter( 'bp_messages_single_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
  67          $n = messages_format_notifications( 'new_message', $n, '', 1, 'array' );
  68          remove_filter( 'bp_messages_single_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
  69  
  70          $this->assertSame( 'bp_messages_single_new_message_array_notification', $this->filter_fired );
  71      }
  72  
  73      /**
  74       * @group messages_format_notifications
  75       */
  76      public function test_friends_format_notifications_bp_messages_multiple_new_message_notification_string_filter() {
  77          // Dummy thread ID
  78          $t = 12;
  79          $u = self::factory()->user->create();
  80          $this->set_current_user( $u );
  81  
  82          // Admin
  83          $n = self::factory()->notification->create( array(
  84              'component_name' => 'messages',
  85              'user_id' => $u,
  86              'item_id' => $t,
  87              'component_action' => 'new_message',
  88          ) );
  89  
  90          add_filter( 'bp_messages_multiple_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
  91          $n = messages_format_notifications( 'new_message', $n, '', 2 );
  92          remove_filter( 'bp_messages_multiple_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
  93  
  94          $this->assertSame( 'bp_messages_multiple_new_message_string_notification', $this->filter_fired );
  95      }
  96  
  97      /**
  98       * @group messages_format_notifications
  99       */
 100      public function test_friends_format_notifications_bp_messages_single_new_message_notification_string_filter() {
 101          // Dummy thread ID
 102          $t = 12;
 103          $u = self::factory()->user->create();
 104          $this->set_current_user( $u );
 105  
 106          // Admin
 107          $n = self::factory()->notification->create( array(
 108              'component_name' => 'messages',
 109              'user_id' => $u,
 110              'item_id' => $t,
 111              'component_action' => 'new_message',
 112          ) );
 113  
 114          add_filter( 'bp_messages_single_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
 115          $n = messages_format_notifications( 'new_message', $n, '', 1 );
 116          remove_filter( 'bp_messages_single_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
 117  
 118          $this->assertSame( 'bp_messages_single_new_message_string_notification', $this->filter_fired );
 119      }
 120  
 121      /**
 122       * @ticket BP6329
 123       */
 124      public function test_messages_notifications_should_be_deleted_when_corresponding_message_is_deleted() {
 125          if ( ! bp_is_active( 'messages' ) ) {
 126              $this->markTestSkipped( __METHOD__ . ' requires the Messages component.' );
 127          }
 128  
 129          $u1 = self::factory()->user->create();
 130          $u2 = self::factory()->user->create();
 131  
 132          $t1 = messages_new_message( array(
 133              'sender_id'  => $u1,
 134              'recipients' => array( $u2 ),
 135              'subject'    => 'A new message',
 136              'content'    => 'Hey there!',
 137          ) );
 138  
 139          // Verify that a notification has been created for the message.
 140          $n1 = BP_Notifications_Notification::get( array(
 141              'component' => 'messages',
 142              'user_id' => $u2,
 143          ) );
 144          $this->assertNotEmpty( $n1 );
 145  
 146          $this->assertTrue( messages_delete_thread( $t1 ) );
 147  
 148          $n2 = BP_Notifications_Notification::get( array(
 149              'component' => 'messages',
 150              'user_id' => $u2,
 151          ) );
 152          $this->assertSame( array(), $n2 );
 153      }
 154  
 155      /**
 156       * @ticket BP8426
 157       */
 158      public function test_bp_messages_mark_notification_on_mark_thread() {
 159          $u1 = self::factory()->user->create();
 160          $u2 = self::factory()->user->create();
 161          $m1 = self::factory()->message->create_and_get( array(
 162              'sender_id'  => $u1,
 163              'recipients' => array( $u2 ),
 164              'subject'    => 'Foo',
 165          ) );
 166  
 167          self::factory()->message->create_many(
 168              9,
 169              array(
 170                  'thread_id' => $m1->thread_id,
 171                  'sender_id' => $u2,
 172                  'recipients' => array( $u1 ),
 173                  'subject' => 'Bar',
 174              )
 175          );
 176  
 177          $unreadn = wp_list_pluck(
 178              BP_Notifications_Notification::get(
 179                  array(
 180                      'user_id'           => $u1,
 181                      'component_name'    => buddypress()->messages->id,
 182                      'component_action'  => 'new_message',
 183                      'is_new'            => 1,
 184                  )
 185              ),
 186              'user_id',
 187              'id'
 188          );
 189  
 190          $this->set_current_user( $u1 );
 191  
 192          // Mark a thread read.
 193          bp_messages_mark_notification_on_mark_thread( $m1->thread_id, $u1, count( $unreadn ) );
 194  
 195          $readn = wp_list_pluck(
 196              BP_Notifications_Notification::get(
 197                  array(
 198                      'user_id'           => $u1,
 199                      'component_name'    => buddypress()->messages->id,
 200                      'component_action'  => 'new_message',
 201                      'is_new'            => 0,
 202                  )
 203              ),
 204              'user_id',
 205              'id'
 206          );
 207  
 208          $this->assertSame( $unreadn, $readn );
 209      }
 210  
 211      /**
 212       * @ticket BP8426
 213       * @group message_delete_notifications
 214       */
 215  	public function test_bp_messages_message_delete_notifications() {
 216          $u1 = self::factory()->user->create();
 217          $u2 = self::factory()->user->create();
 218          $m1 = self::factory()->message->create_and_get( array(
 219              'sender_id'  => $u1,
 220              'recipients' => array( $u2 ),
 221              'subject'    => 'Foo',
 222          ) );
 223  
 224          $message_ids = self::factory()->message->create_many(
 225              9,
 226              array(
 227                  'thread_id' => $m1->thread_id,
 228                  'sender_id' => $u2,
 229                  'recipients' => array( $u1 ),
 230                  'subject' => 'Bar',
 231              )
 232          );
 233  
 234          $message_ids = wp_list_pluck(
 235              BP_Notifications_Notification::get(
 236                  array(
 237                      'user_id'           => $u1,
 238                      'component_name'    => buddypress()->messages->id,
 239                      'component_action'  => 'new_message',
 240                      'is_new'            => 1,
 241                  )
 242              ),
 243              'item_id'
 244          );
 245  
 246          $test = bp_messages_message_delete_notifications( $m1->thread_id, $message_ids );
 247  
 248          $deleted = BP_Notifications_Notification::get(
 249              array(
 250                  'user_id'           => $u1,
 251                  'component_name'    => buddypress()->messages->id,
 252                  'component_action'  => 'new_message',
 253                  'is_new'            => 1,
 254              )
 255          );
 256  
 257          $this->assertEmpty( $deleted );
 258      }
 259  
 260  	public function notification_filter_callback( $value ) {
 261          $this->filter_fired = current_filter();
 262          return $value;
 263      }
 264  }


Generated: Fri Apr 19 01:01:08 2024 Cross-referenced by PHPXref 0.7.1