[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * @group messages
   4   * @group star
   5   */
   6  class BP_Tests_Messages_Star_ extends BP_UnitTestCase {
   7  
   8      /**
   9       * @group bp_messages_is_message_starred
  10       * @group bp_messages_star_set_action
  11       */
  12  	public function test_is_message_starred() {
  13          $u1 = self::factory()->user->create();
  14          $u2 = self::factory()->user->create();
  15  
  16          // create the thread
  17          $message_1 = self::factory()->message->create_and_get( array(
  18              'sender_id'  => $u1,
  19              'recipients' => array( $u2 ),
  20              'subject'    => 'This is a knive',
  21          ) );
  22  
  23          // create a reply
  24          $message_2 = self::factory()->message->create_and_get( array(
  25              'thread_id'  => $message_1->thread_id,
  26              'sender_id'  => $u2,
  27              'recipients' => array( $u1 ),
  28              'content'    => "That's a spoon",
  29          ) );
  30  
  31          $m1 = $message_1->id;
  32          $m2 = $message_2->id;
  33  
  34          // star the second message
  35          $star = bp_messages_star_set_action( array(
  36              'user_id'    => $u1,
  37              'message_id' => $m2,
  38          ) );
  39  
  40          // assert that star is set
  41          $this->assertTrue( $star );
  42          $this->assertTrue( bp_messages_is_message_starred( $m2, $u1 ) );
  43  
  44          // unstar the second message
  45          $unstar = bp_messages_star_set_action( array(
  46              'user_id'    => $u1,
  47              'message_id' => $m2,
  48              'action'     => 'unstar'
  49          ) );
  50  
  51          // assert that star is removed
  52          $this->assertTrue( $unstar );
  53          $this->assertFalse( bp_messages_is_message_starred( $m2, $u1 ) );
  54      }
  55  
  56      /**
  57       * @group bp_messages_star_set_action
  58       * @group bulk
  59       */
  60  	public function test_star_set_action_bulk_unstar() {
  61          $u1 = self::factory()->user->create();
  62          $u2 = self::factory()->user->create();
  63  
  64          // create the thread
  65          $message_1 = self::factory()->message->create_and_get( array(
  66              'sender_id'  => $u1,
  67              'recipients' => array( $u2 ),
  68              'subject'    => 'This is a knive',
  69          ) );
  70  
  71          // create a reply
  72          $message_2 = self::factory()->message->create_and_get( array(
  73              'thread_id'  => $message_1->thread_id,
  74              'sender_id'  => $u2,
  75              'recipients' => array( $u1 ),
  76              'content'    => "That's a spoon",
  77          ) );
  78  
  79          $m1 = $message_1->id;
  80          $m2 = $message_2->id;
  81          $t1 = $message_1->thread_id;
  82  
  83          // star all messages
  84          bp_messages_star_set_action( array(
  85              'user_id'    => $u1,
  86              'message_id' => $m1,
  87          ) );
  88          bp_messages_star_set_action( array(
  89              'user_id'    => $u1,
  90              'message_id' => $m2,
  91          ) );
  92  
  93          // assert that stars are set
  94          $this->assertTrue( bp_messages_is_message_starred( $m1, $u1 ) );
  95          $this->assertTrue( bp_messages_is_message_starred( $m2, $u1 ) );
  96  
  97          // unstar all messages
  98          bp_messages_star_set_action( array(
  99              'user_id'    => $u1,
 100              'thread_id'  => $t1,
 101              'action'     => 'unstar',
 102              'bulk'       => true
 103          ) );
 104  
 105          // assert that star is removed
 106          $this->assertFalse( bp_messages_is_message_starred( $m1, $u1 ) );
 107          $this->assertFalse( bp_messages_is_message_starred( $m2, $u1 ) );
 108      }
 109  
 110      /**
 111       * @group bp_messages_filter_starred_message_threads
 112       */
 113      public function test_get_starred_threads_should_not_include_deleted_thread() {
 114          $old_current_user = get_current_user_id();
 115          $u1 = self::factory()->user->create();
 116          $u2 = self::factory()->user->create();
 117  
 118          // create three threads
 119          $message_1 = self::factory()->message->create_and_get( array(
 120              'sender_id'  => $u1,
 121              'recipients' => array( $u2 ),
 122              'subject'    => 'A',
 123          ) );
 124          $message_2 = self::factory()->message->create_and_get( array(
 125              'sender_id'  => $u1,
 126              'recipients' => array( $u2 ),
 127              'subject'    => 'B',
 128          ) );
 129          $message_3 = self::factory()->message->create_and_get( array(
 130              'sender_id'  => $u1,
 131              'recipients' => array( $u2 ),
 132              'subject'    => 'C',
 133          ) );
 134  
 135          // grab the message ids as individual variables
 136          $m1 = $message_1->id;
 137          $m2 = $message_2->id;
 138          $m3 = $message_3->id;
 139  
 140          $t2 = $message_2->thread_id;
 141  
 142          // star all threads
 143          bp_messages_star_set_action( array(
 144              'user_id'    => $u2,
 145              'message_id' => $m1,
 146          ) );
 147          bp_messages_star_set_action( array(
 148              'user_id'    => $u2,
 149              'message_id' => $m2,
 150          ) );
 151          bp_messages_star_set_action( array(
 152              'user_id'    => $u2,
 153              'message_id' => $m3,
 154          ) );
 155  
 156          // delete the second thread
 157          $this->set_current_user( $u2 );
 158          messages_delete_thread( $t2 );
 159  
 160          // load the starred threads loop
 161          global $messages_template;
 162          add_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
 163          bp_has_message_threads();
 164          remove_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
 165  
 166          // assert that second thread isn't in starred thread loop
 167          $thread_ids = wp_list_pluck( $messages_template->threads, 'thread_id' );
 168          $this->assertFalse( in_array( $t2, $thread_ids ) );
 169  
 170          // reset
 171          $this->set_current_user( $old_current_user );
 172      }
 173  
 174      /**
 175       * Helper method to grab the message IDs from a message thread.
 176       *
 177       * @param int $thread_id The message thread ID
 178       * @return array
 179       */
 180  	protected function get_message_ids( $thread_id = 0 ) {
 181          $thread = new BP_Messages_Thread( $thread_id );
 182          return wp_list_pluck( $thread->messages, 'id' );
 183      }
 184  }


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1