[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group messages
   5   * @group template
   6   */
   7  class BP_Tests_Messages_Template extends BP_UnitTestCase {
   8      /**
   9       * @group bp_has_message_threads
  10       */
  11  	public function test_has_message_threads() {
  12          $u1 = self::factory()->user->create();
  13          $u2 = self::factory()->user->create();
  14  
  15          // create initial thread
  16          $message_1 = self::factory()->message->create_and_get( array(
  17              'sender_id'  => $u1,
  18              'recipients' => array( $u2 ),
  19          ) );
  20  
  21          // create some replies to thread
  22          $message_2 = self::factory()->message->create_and_get( array(
  23              'thread_id' => $message_1->thread_id,
  24              'sender_id' => $u2,
  25              'recipients' => array( $u1 ),
  26          ) );
  27          $message_3 = self::factory()->message->create_and_get( array(
  28              'thread_id' => $message_1->thread_id,
  29              'sender_id' => $u2,
  30              'recipients' => array( $u1 ),
  31          ) );
  32  
  33          $messages_template = new BP_Messages_Box_Template( array( 'user_id' => $u1 ) );
  34  
  35          $this->assertEquals( 1, $messages_template->thread_count );
  36          $this->assertSame( array( $message_1->thread_id ), wp_list_pluck( $messages_template->threads, 'thread_id' ) );
  37      }
  38  
  39      /**
  40       * @group bp_has_message_threads
  41       *
  42       * @expectedDeprecated BP_Messages_Box_Template::__construct
  43       */
  44  	public function test_has_message_threads_old_args() {
  45          $u1 = self::factory()->user->create();
  46          $u2 = self::factory()->user->create();
  47  
  48          // create initial thread
  49          $message_1 = self::factory()->message->create_and_get( array(
  50              'sender_id'  => $u1,
  51              'recipients' => array( $u2 ),
  52          ) );
  53  
  54          // create some replies to thread
  55          $message_2 = self::factory()->message->create_and_get( array(
  56              'thread_id' => $message_1->thread_id,
  57              'sender_id' => $u2,
  58              'recipients' => array( $u1 ),
  59          ) );
  60          $message_3 = self::factory()->message->create_and_get( array(
  61              'thread_id' => $message_1->thread_id,
  62              'sender_id' => $u2,
  63              'recipients' => array( $u1 ),
  64          ) );
  65  
  66          $messages_template = new BP_Messages_Box_Template( $u1 );
  67  
  68          $this->assertEquals( 1, $messages_template->thread_count );
  69          $this->assertSame( array( $message_1->thread_id ), wp_list_pluck( $messages_template->threads, 'thread_id' ) );
  70      }
  71  
  72      /**
  73       * @group bp_has_message_threads
  74       * @group meta_query
  75       */
  76  	public function test_thread_has_messages_meta_query() {
  77          $u1 = self::factory()->user->create();
  78          $u2 = self::factory()->user->create();
  79  
  80          // create some threads
  81          $message_1 = self::factory()->message->create_and_get( array(
  82              'sender_id'  => $u1,
  83              'recipients' => array( $u2 ),
  84              'subject'    => 'This is a knive',
  85          ) );
  86          $message_2 = self::factory()->message->create_and_get( array(
  87              'sender_id'  => $u1,
  88              'recipients' => array( $u2 ),
  89              'subject'    => 'Oy',
  90          ) );
  91  
  92          // misc threads
  93          self::factory()->message->create_many( 3, array(
  94              'sender_id'  => $u1,
  95              'recipients' => array( $u2 ),
  96          ) );
  97  
  98          $t1 = $message_1->thread_id;
  99          $t2 = $message_2->thread_id;
 100  
 101          // create some replies for thread 1
 102          $message_3 = self::factory()->message->create_and_get( array(
 103              'thread_id'  => $t1,
 104              'sender_id'  => $u2,
 105              'recipients' => array( $u1 ),
 106              'content'    => "That's a spoon",
 107          ) );
 108          $message_4 = self::factory()->message->create_and_get( array(
 109              'thread_id'  => $t1,
 110              'sender_id'  => $u1,
 111              'recipients' => array( $u2 ),
 112              'content'    => "I see you've played knivey-spooney before.",
 113          ) );
 114          $message_5 = self::factory()->message->create_and_get( array(
 115              'thread_id'  => $t1,
 116              'sender_id'  => $u2,
 117              'recipients' => array( $u1 ),
 118              'content'    => '*crickets*',
 119          ) );
 120  
 121          // create some replies for thread 2
 122          $message_6 = self::factory()->message->create_and_get( array(
 123              'thread_id'  => $t2,
 124              'sender_id'  => $u2,
 125              'recipients' => array( $u1 ),
 126              'content'    => "Oy yourself.",
 127          ) );
 128  
 129          // belong to $t1
 130          $m1 = $message_1->id;
 131          $m3 = $message_3->id;
 132          $m4 = $message_4->id;
 133          $m5 = $message_5->id;
 134  
 135          // belong to $t2
 136          $m2 = $message_2->id;
 137          $m6 = $message_6->id;
 138  
 139          // add meta for some of the messages
 140          bp_messages_update_meta( $m1, 'utensil',  'knive' );
 141          bp_messages_update_meta( $m1, 'is_knive', 'yes' );
 142          bp_messages_update_meta( $m1, "starred_by_user_{$u2}", true );
 143  
 144          bp_messages_update_meta( $m2, 'utensil',  'spoon' );
 145          bp_messages_update_meta( $m2, 'is_knive', 'no' );
 146          bp_messages_update_meta( $m2, 'is_spoon', 'yes' );
 147  
 148          bp_messages_update_meta( $m3, "starred_by_user_{$u2}", true );
 149  
 150          bp_messages_update_meta( $m6, "starred_by_user_{$u2}", true );
 151  
 152          // now, do the message thread loop query
 153          global $messages_template;
 154          bp_has_message_threads( array(
 155              'user_id' => $u2,
 156              'meta_query' => array(
 157                  array(
 158                      'key' => "starred_by_user_{$u2}"
 159                  ),
 160              )
 161          ) );
 162  
 163          $this->assertEquals( 2, $messages_template->thread_count );
 164          $this->assertEqualSets( array( $t1, $t2 ), wp_list_pluck( $messages_template->threads, 'thread_id' ) );
 165      }
 166  
 167      /**
 168       * @group bp_has_message_threads
 169       * @group meta_query
 170       */
 171      public function test_thread_has_messages_meta_query_multiple_clauses_relation_and() {
 172          $u1 = self::factory()->user->create();
 173          $u2 = self::factory()->user->create();
 174  
 175          // create some threads
 176          $message_1 = self::factory()->message->create_and_get( array(
 177              'sender_id'  => $u1,
 178              'recipients' => array( $u2 ),
 179              'subject'    => 'This is a knive',
 180          ) );
 181          $message_2 = self::factory()->message->create_and_get( array(
 182              'sender_id'  => $u1,
 183              'recipients' => array( $u2 ),
 184              'subject'    => 'Oy',
 185          ) );
 186  
 187          // misc threads
 188          self::factory()->message->create_many( 3, array(
 189              'sender_id'  => $u1,
 190              'recipients' => array( $u2 ),
 191          ) );
 192  
 193          $t1 = $message_1->thread_id;
 194          $t2 = $message_2->thread_id;
 195  
 196          // create some replies for thread 1
 197          $message_3 = self::factory()->message->create_and_get( array(
 198              'thread_id'  => $t1,
 199              'sender_id'  => $u2,
 200              'recipients' => array( $u1 ),
 201              'content'    => "That's a spoon",
 202          ) );
 203          $message_4 = self::factory()->message->create_and_get( array(
 204              'thread_id'  => $t1,
 205              'sender_id'  => $u1,
 206              'recipients' => array( $u2 ),
 207              'content'    => "I see you've played knivey-spooney before.",
 208          ) );
 209          $message_5 = self::factory()->message->create_and_get( array(
 210              'thread_id'  => $t1,
 211              'sender_id'  => $u2,
 212              'recipients' => array( $u1 ),
 213              'content'    => '*crickets*',
 214          ) );
 215  
 216          // create some replies for thread 2
 217          $message_6 = self::factory()->message->create_and_get( array(
 218              'thread_id'  => $t2,
 219              'sender_id'  => $u2,
 220              'recipients' => array( $u1 ),
 221              'content'    => "Oy yourself.",
 222          ) );
 223  
 224          // belong to $t1
 225          $m1 = $message_1->id;
 226          $m3 = $message_3->id;
 227          $m4 = $message_4->id;
 228          $m5 = $message_5->id;
 229  
 230          // belong to $t2
 231          $m2 = $message_2->id;
 232          $m6 = $message_6->id;
 233  
 234          // add meta for some of the messages
 235          bp_messages_update_meta( $m1, 'utensil',  'knive' );
 236          bp_messages_update_meta( $m1, 'is_knive', 'yes' );
 237          bp_messages_update_meta( $m1, "starred_by_user_{$u2}", true );
 238  
 239          bp_messages_update_meta( $m2, 'utensil',  'spoon' );
 240          bp_messages_update_meta( $m2, 'is_knive', 'no' );
 241          bp_messages_update_meta( $m2, 'is_spoon', 'yes' );
 242  
 243          bp_messages_update_meta( $m3, "starred_by_user_{$u2}", true );
 244  
 245          bp_messages_update_meta( $m6, "starred_by_user_{$u2}", true );
 246  
 247          // now, do the message thread loop query
 248          global $messages_template;
 249          bp_has_message_threads( array(
 250              'user_id' => $u2,
 251              'meta_query' => array(
 252                  'relation' => 'AND',
 253                  array(
 254                      'key' => "starred_by_user_{$u2}"
 255                  ),
 256                  array(
 257                      'key' => 'utensil',
 258                      'value' => 'knive',
 259                  ),
 260              )
 261          ) );
 262  
 263          $this->assertEquals( 1, $messages_template->thread_count );
 264          $this->assertEqualSets( array( $t1 ), wp_list_pluck( $messages_template->threads, 'thread_id' ) );
 265      }
 266  
 267      /**
 268       * @group bp_has_message_threads
 269       */
 270      public function test_has_message_threads_anonymous_user_should_not_see_threads() {
 271          $u1 = self::factory()->user->create();
 272          $u2 = self::factory()->user->create();
 273  
 274          // create initial thread
 275          self::factory()->message->create( array(
 276              'sender_id'  => $u1,
 277              'recipients' => array( $u2 ),
 278          ) );
 279  
 280          // set user to anonymous
 281          $old_current_user = get_current_user_id();
 282          $this->set_current_user( 0 );
 283  
 284          // now, do the message thread query
 285          global $messages_template;
 286          bp_has_message_threads();
 287  
 288          // assert!
 289          $this->assertEquals( 0, $messages_template->thread_count );
 290          $this->assertEmpty( $messages_template->threads );
 291  
 292          $this->set_current_user( $old_current_user );
 293      }
 294  
 295      /**
 296       * @group pagination
 297       * @group BP_Messages_Box_Template
 298       */
 299      public function test_bp_messages_template_should_give_precedence_to_mpage_URL_param() {
 300          $request = $_REQUEST;
 301          $_REQUEST['mpage'] = '5';
 302  
 303          $at = new BP_Messages_Box_Template( array(
 304              'page' => 8,
 305          ) );
 306  
 307          $this->assertEquals( 5, $at->pag_page );
 308  
 309          $_REQUEST = $request;
 310      }
 311  
 312      /**
 313       * @group pagination
 314       * @group BP_Messages_Box_Template
 315       */
 316      public function test_bp_messages_template_should_reset_0_pag_page_URL_param_to_default_pag_page_value() {
 317          $request = $_REQUEST;
 318          $_REQUEST['mpage'] = '0';
 319  
 320          $at = new BP_Messages_Box_Template( array(
 321              'page' => 8,
 322          ) );
 323  
 324          $this->assertEquals( 8, $at->pag_page );
 325  
 326          $_REQUEST = $request;
 327      }
 328  
 329      /**
 330       * @group pagination
 331       * @group BP_Messages_Box_Template
 332       */
 333      public function test_bp_messages_template_should_give_precedence_to_num_URL_param() {
 334          $request = $_REQUEST;
 335          $_REQUEST['num'] = '14';
 336  
 337          $at = new BP_Messages_Box_Template( array(
 338              'per_page' => 13,
 339          ) );
 340  
 341          $this->assertEquals( 14, $at->pag_num );
 342  
 343          $_REQUEST = $request;
 344      }
 345  
 346      /**
 347       * @group pagination
 348       * @group BP_Messages_Box_Template
 349       */
 350      public function test_bp_messages_template_should_reset_0_pag_num_URL_param_to_default_pag_num_value() {
 351          $request = $_REQUEST;
 352          $_REQUEST['num'] = '0';
 353  
 354          $at = new BP_Messages_Box_Template( array(
 355              'per_page' => 13,
 356          ) );
 357  
 358          $this->assertEquals( 13, $at->pag_num );
 359  
 360          $_REQUEST = $request;
 361      }
 362  }


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1