[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group groups
   5   * @group notifications
   6   */
   7  class BP_Tests_Groups_Notifications extends BP_UnitTestCase {
   8      protected $filter_fired;
   9      protected $current_user;
  10      protected $requesting_user_id;
  11      protected $group;
  12  
  13  	public function setUp() {
  14          parent::setUp();
  15          $this->current_user = get_current_user_id();
  16          $this->set_current_user( self::factory()->user->create() );
  17  
  18          $this->requesting_user_id = self::factory()->user->create();
  19          $this->group = self::factory()->group->create( array( 'status' =>  'private' ) );
  20          $this->filter_fired = '';
  21      }
  22  
  23  	public function tearDown() {
  24          parent::tearDown();
  25          $this->set_current_user( $this->current_user );
  26      }
  27  
  28      /**
  29       * @group groups_format_notifications
  30       */
  31      public function test_groups_format_notifications_bp_groups_multiple_new_membership_requests_notification() {
  32          add_filter( 'bp_groups_multiple_new_membership_requests_notification', array( $this, 'notification_filter_callback' ) );
  33          $n = groups_format_notifications( 'new_membership_request', $this->group, $this->requesting_user_id, 5 );
  34          remove_filter( 'bp_groups_multiple_new_membership_requests_notification', array( $this, 'notification_filter_callback' ) );
  35  
  36          $this->assertSame( 'bp_groups_multiple_new_membership_requests_notification', $this->filter_fired );
  37      }
  38  
  39      /**
  40       * @group groups_format_notifications
  41       */
  42      public function test_groups_format_notifications_bp_groups_single_new_membership_request_notification() {
  43          add_filter( 'bp_groups_single_new_membership_request_notification', array( $this, 'notification_filter_callback' ) );
  44          $n = groups_format_notifications( 'new_membership_request', $this->group, 0, 1 );
  45          remove_filter( 'bp_groups_single_new_membership_request_notification', array( $this, 'notification_filter_callback' ) );
  46  
  47          $this->assertSame( 'bp_groups_single_new_membership_request_notification', $this->filter_fired );
  48      }
  49  
  50      /**
  51       * @group groups_format_notifications
  52       */
  53      public function test_groups_format_notifications_bp_groups_multiple_membership_request_accepted_notification() {
  54          add_filter( 'bp_groups_multiple_membership_request_accepted_notification', array( $this, 'notification_filter_callback' ) );
  55          $n = groups_format_notifications( 'membership_request_accepted', $this->group, 0, 5 );
  56          remove_filter( 'bp_groups_multiple_membership_request_accepted_notification', array( $this, 'notification_filter_callback' ) );
  57  
  58          $this->assertSame( 'bp_groups_multiple_membership_request_accepted_notification', $this->filter_fired );
  59      }
  60  
  61      /**
  62       * @group groups_format_notifications
  63       */
  64      public function test_groups_format_notifications_bp_groups_single_membership_request_accepted_notification() {
  65          add_filter( 'bp_groups_single_membership_request_accepted_notification', array( $this, 'notification_filter_callback' ) );
  66          $n = groups_format_notifications( 'membership_request_accepted', $this->group, 0, 1 );
  67          remove_filter( 'bp_groups_single_membership_request_accepted_notification', array( $this, 'notification_filter_callback' ) );
  68  
  69          $this->assertSame( 'bp_groups_single_membership_request_accepted_notification', $this->filter_fired );
  70      }
  71  
  72      /**
  73       * @group groups_format_notifications
  74       */
  75      public function test_groups_format_notifications_bp_groups_multiple_membership_request_rejected_notification() {
  76          add_filter( 'bp_groups_multiple_membership_request_rejected_notification', array( $this, 'notification_filter_callback' ) );
  77          $n = groups_format_notifications( 'membership_request_rejected', $this->group, 0, 5 );
  78          remove_filter( 'bp_groups_multiple_membership_request_rejected_notification', array( $this, 'notification_filter_callback' ) );
  79  
  80          $this->assertSame( 'bp_groups_multiple_membership_request_rejected_notification', $this->filter_fired );
  81      }
  82  
  83      /**
  84       * @group groups_format_notifications
  85       */
  86      public function test_groups_format_notifications_bp_groups_single_membership_request_rejected_notification() {
  87          add_filter( 'bp_groups_single_membership_request_rejected_notification', array( $this, 'notification_filter_callback' ) );
  88          $n = groups_format_notifications( 'membership_request_rejected', $this->group, 0, 1 );
  89          remove_filter( 'bp_groups_single_membership_request_rejected_notification', array( $this, 'notification_filter_callback' ) );
  90  
  91          $this->assertSame( 'bp_groups_single_membership_request_rejected_notification', $this->filter_fired );
  92      }
  93  
  94      /**
  95       * @group groups_format_notifications
  96       */
  97      public function test_groups_format_notifications_bp_groups_multiple_member_promoted_to_admin_notification() {
  98          add_filter( 'bp_groups_multiple_member_promoted_to_admin_notification', array( $this, 'notification_filter_callback' ) );
  99          $n = groups_format_notifications( 'member_promoted_to_admin', $this->group, 0, 5 );
 100          remove_filter( 'bp_groups_multiple_member_promoted_to_admin_notification', array( $this, 'notification_filter_callback' ) );
 101  
 102          $this->assertSame( 'bp_groups_multiple_member_promoted_to_admin_notification', $this->filter_fired );
 103      }
 104  
 105      /**
 106       * @group groups_format_notifications
 107       */
 108      public function test_groups_format_notifications_bp_groups_single_member_promoted_to_admin_notification() {
 109          add_filter( 'bp_groups_single_member_promoted_to_admin_notification', array( $this, 'notification_filter_callback' ) );
 110          $n = groups_format_notifications( 'member_promoted_to_admin', $this->group, 0, 1 );
 111          remove_filter( 'bp_groups_single_member_promoted_to_admin_notification', array( $this, 'notification_filter_callback' ) );
 112  
 113          $this->assertSame( 'bp_groups_single_member_promoted_to_admin_notification', $this->filter_fired );
 114      }
 115  
 116      /**
 117       * @group groups_format_notifications
 118       */
 119      public function test_groups_format_notifications_bp_groups_multiple_member_promoted_to_mod_notification() {
 120          add_filter( 'bp_groups_multiple_member_promoted_to_mod_notification', array( $this, 'notification_filter_callback' ) );
 121          $n = groups_format_notifications( 'member_promoted_to_mod', $this->group, 0, 5 );
 122          remove_filter( 'bp_groups_multiple_member_promoted_to_mod_notification', array( $this, 'notification_filter_callback' ) );
 123  
 124          $this->assertSame( 'bp_groups_multiple_member_promoted_to_mod_notification', $this->filter_fired );
 125      }
 126  
 127      /**
 128       * @group groups_format_notifications
 129       */
 130      public function test_groups_format_notifications_bp_groups_single_member_promoted_to_mod_notification() {
 131          add_filter( 'bp_groups_single_member_promoted_to_mod_notification', array( $this, 'notification_filter_callback' ) );
 132          $n = groups_format_notifications( 'member_promoted_to_mod', $this->group, 0, 1 );
 133          remove_filter( 'bp_groups_single_member_promoted_to_mod_notification', array( $this, 'notification_filter_callback' ) );
 134  
 135          $this->assertSame( 'bp_groups_single_member_promoted_to_mod_notification', $this->filter_fired );
 136      }
 137  
 138      /**
 139       * @group groups_format_notifications
 140       */
 141      public function test_groups_format_notifications_bp_groups_multiple_group_invite_notification() {
 142          add_filter( 'bp_groups_multiple_group_invite_notification', array( $this, 'notification_filter_callback' ) );
 143          $n = groups_format_notifications( 'group_invite', $this->group, 0, 5 );
 144          remove_filter( 'bp_groups_multiple_group_invite_notification', array( $this, 'notification_filter_callback' ) );
 145  
 146          $this->assertSame( 'bp_groups_multiple_group_invite_notification', $this->filter_fired );
 147      }
 148  
 149      /**
 150       * @group groups_format_notifications
 151       */
 152      public function test_groups_format_notifications_bp_groups_single_group_invite_notification() {
 153          add_filter( 'bp_groups_single_group_invite_notification', array( $this, 'notification_filter_callback' ) );
 154          $n = groups_format_notifications( 'group_invite', $this->group, 0, 1 );
 155          remove_filter( 'bp_groups_single_group_invite_notification', array( $this, 'notification_filter_callback' ) );
 156  
 157          $this->assertSame( 'bp_groups_single_group_invite_notification', $this->filter_fired );
 158      }
 159  
 160      /**
 161       * @group bp_groups_delete_promotion_notifications
 162       */
 163  	public function test_bp_groups_delete_promotion_notifications() {
 164          // Dummy group and user IDs
 165          $u = 5;
 166          $g = 12;
 167  
 168          // Admin
 169          $n = self::factory()->notification->create( array(
 170              'component_name' => 'groups',
 171              'user_id' => $u,
 172              'item_id' => $g,
 173              'component_action' => 'member_promoted_to_admin',
 174          ) );
 175  
 176          $notifications = BP_Notifications_Notification::get( array(
 177              'user_id' => $u,
 178          ) );
 179  
 180          // Double check it's there
 181          $this->assertEquals( array( $n ), wp_list_pluck( $notifications, 'id' ) );
 182  
 183          // fire the hook
 184          do_action( 'groups_demoted_member', $u, $g );
 185  
 186          $notifications = BP_Notifications_Notification::get( array(
 187              'user_id' => $u,
 188          ) );
 189  
 190          $this->assertEmpty( $notifications );
 191  
 192          // Mod
 193          $n = self::factory()->notification->create( array(
 194              'component_name' => 'groups',
 195              'user_id' => $u,
 196              'item_id' => $g,
 197              'component_action' => 'member_promoted_to_mod',
 198          ) );
 199  
 200          $notifications = BP_Notifications_Notification::get( array(
 201              'user_id' => $u,
 202          ) );
 203  
 204          // Double check it's there
 205          $this->assertEquals( array( $n ), wp_list_pluck( $notifications, 'id' ) );
 206  
 207          // fire the hook
 208          do_action( 'groups_demoted_member', $u, $g );
 209  
 210          $notifications = BP_Notifications_Notification::get( array(
 211              'user_id' => $u,
 212          ) );
 213  
 214          $this->assertEmpty( $notifications );
 215      }
 216  
 217  	public function notification_filter_callback( $value ) {
 218          $this->filter_fired = current_filter();
 219          return $value;
 220      }
 221  
 222      /**
 223       * @group BP7375
 224       */
 225      public function test_membership_request_notifications_should_be_cleared_when_request_is_accepted() {
 226          $users = self::factory()->user->create_many( 3 );
 227  
 228          $this->add_user_to_group( $users[0], $this->group, array(
 229              'is_admin' => 1,
 230          ) );
 231          $this->add_user_to_group( $users[1], $this->group, array(
 232              'is_admin' => 1,
 233          ) );
 234  
 235          groups_send_membership_request( array(
 236              'user_id' => $users[2],
 237              'group_id' => $this->group
 238          ) );
 239  
 240          // Both admins should get a notification.
 241          $get_args = array(
 242              'user_id' => $users[0],
 243              'item_id' => $this->group,
 244              'secondary_item_id' => $users[2],
 245              'component_action' => 'new_membership_request',
 246              'is_new' => true,
 247          );
 248          $u0_notifications = BP_Notifications_Notification::get( $get_args );
 249          $u1_notifications = BP_Notifications_Notification::get( $get_args );
 250          $this->assertNotEmpty( $u0_notifications );
 251          $this->assertNotEmpty( $u1_notifications );
 252  
 253          groups_accept_membership_request( false, $users[2], $this->group );
 254  
 255          $u0_notifications = BP_Notifications_Notification::get( $get_args );
 256          $u1_notifications = BP_Notifications_Notification::get( $get_args );
 257          $this->assertEmpty( $u0_notifications );
 258          $this->assertEmpty( $u1_notifications );
 259      }
 260  
 261      public function test_membership_request_notifications_should_be_cleared_when_request_is_accepted_via_invite() {
 262          $users = self::factory()->user->create_many( 3 );
 263  
 264          $this->add_user_to_group( $users[0], $this->group, array(
 265              'is_admin' => 1,
 266          ) );
 267          $this->add_user_to_group( $users[1], $this->group, array(
 268              'is_admin' => 1,
 269          ) );
 270  
 271          groups_send_membership_request( array(
 272              'user_id' => $users[2],
 273              'group_id' => $this->group
 274          ) );
 275  
 276          // Both admins should get a notification.
 277          $get_args = array(
 278              'user_id' => $users[0],
 279              'item_id' => $this->group,
 280              'secondary_item_id' => $users[2],
 281              'component_action' => 'new_membership_request',
 282              'is_new' => true,
 283          );
 284          $u0_notifications = BP_Notifications_Notification::get( $get_args );
 285          $u1_notifications = BP_Notifications_Notification::get( $get_args );
 286          $this->assertNotEmpty( $u0_notifications );
 287          $this->assertNotEmpty( $u1_notifications );
 288  
 289          // 'Accept' the request by sending an invite.
 290          groups_invite_user( array(
 291              'user_id' => $users[2],
 292              'group_id' => $this->group,
 293              'send_invite' => true
 294          ) );
 295  
 296          $u0_notifications = BP_Notifications_Notification::get( $get_args );
 297          $u1_notifications = BP_Notifications_Notification::get( $get_args );
 298          $this->assertEmpty( $u0_notifications );
 299          $this->assertEmpty( $u1_notifications );
 300      }
 301  
 302  }


Generated: Sun Nov 24 01:00:53 2024 Cross-referenced by PHPXref 0.7.1