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


Generated: Mon Nov 29 01:01:02 2021 Cross-referenced by PHPXref 0.7.1