[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/messages/ -> class.bp-messages-thread.php (source)

   1  <?php
   2  
   3  /**
   4   * @group BP_Messages_Thread
   5   * @group messages
   6   */
   7  class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase {
   8  
   9      /**
  10       * @group cache
  11       */
  12  	public function construct_cache() {
  13          $u1 = self::factory()->user->create();
  14          $u2 = self::factory()->user->create();
  15  
  16          $message = self::factory()->message->create_and_get( array(
  17              'sender_id' => $u1,
  18              'recipients' => array( $u2 ),
  19              'subject' => 'Foo',
  20          ) );
  21  
  22          // prime cache
  23          new BP_Messages_Thread( $message->thread_id );
  24  
  25          // Cache should exist
  26          $this->assertThat(
  27              wp_cache_get( $message->thread_id, 'bp_messages_threads' ),
  28              $this->logicalNot( $this->equalTo( false ) ),
  29              'Message thread cache should exist.'
  30          );
  31      }
  32  
  33  	public function test_get_messages_with_invalid_thread_id() {
  34          $this->assertTrue( empty( BP_Messages_Thread::get_messages( 0 ) ) );
  35      }
  36  
  37  	public function test_get_messages_using_arguments() {
  38          $u1 = self::factory()->user->create();
  39          $u2 = self::factory()->user->create();
  40          $m1 = self::factory()->message->create_and_get( array(
  41              'sender_id' => $u1,
  42              'recipients' => array( $u2 ),
  43              'subject' => 'Foo',
  44          ) );
  45  
  46          self::factory()->message->create_many(
  47              98,
  48              array(
  49                  'thread_id' => $m1->thread_id,
  50                  'sender_id' => $u2,
  51                  'recipients' => array( $u1 ),
  52                  'subject' => 'Bar',
  53              )
  54          );
  55  
  56          // Last message
  57          self::factory()->message->create(
  58              array(
  59                  'thread_id' => $m1->thread_id,
  60                  'sender_id' => $u1,
  61                  'recipients' => array( $u2 ),
  62                  'subject' => 'Last Message',
  63              )
  64          );
  65  
  66          // Default results.
  67          $messages = BP_Messages_Thread::get_messages( $m1->thread_id );
  68          $this->assertTrue( 100 === count( $messages ) );
  69  
  70          // Get first 10 messages.
  71          $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 1, 'per_page' => 10 ) );
  72          $this->assertTrue( 10 === count( $messages ) );
  73  
  74          // Get first 10 messages differently.
  75          $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => 1, 'per_page' => 10 ) );
  76          $this->assertTrue( 10 === count( $thread->messages ) );
  77  
  78          // Get all messages.
  79          $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => null, 'per_page' => null ) );
  80          $this->assertTrue( 100 === count( $messages ) );
  81  
  82          // Get all mesages differently.
  83          $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => null, 'per_page' => null ) );
  84          $this->assertTrue( 100 === count( $thread->messages ) );
  85  
  86          // Get last message.
  87          $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 100, 'per_page' => 1 ) );
  88          $this->assertTrue( 1 === count( $messages ) );
  89          $this->assertEquals( $u1, $messages[0]->sender_id );
  90          $this->assertEquals( 'Last Message', $messages[0]->subject );
  91      }
  92  
  93      /**
  94       * @group order
  95       */
  96  	public function test_construct_order_desc() {
  97          $u1 = self::factory()->user->create();
  98          $u2 = self::factory()->user->create();
  99  
 100          // create thread
 101          $message_1 = self::factory()->message->create_and_get( array(
 102              'sender_id'  => $u1,
 103              'recipients' => array( $u2 ),
 104              'subject'    => 'Foo',
 105          ) );
 106  
 107          // create reply
 108          $message_2 = self::factory()->message->create_and_get( array(
 109              'thread_id'  => $message_1->thread_id,
 110              'sender_id'  => $u1,
 111              'recipients' => array( $u2 ),
 112              'content'    => 'Bar'
 113          ) );
 114  
 115          // Default sort from constructor.
 116          $thread = new BP_Messages_Thread( $message_1->thread_id );
 117          $this->assertEquals(
 118              array( $message_1->id, $message_2->id ),
 119              wp_list_pluck( $thread->messages, 'id' )
 120          );
 121  
 122          // Default via the helper method.
 123          $messages = BP_Messages_Thread::get_messages( $message_1->thread_id );
 124          $this->assertEquals(
 125              array( $message_1->id, $message_2->id ),
 126              wp_list_pluck( $messages, 'id' )
 127          );
 128  
 129          // Now get thread by DESC via the constructor.
 130          $thread = new BP_Messages_Thread( $message_1->thread_id, 'DESC' );
 131          $this->assertEquals(
 132              array( $message_2->id, $message_1->id ),
 133              wp_list_pluck( $thread->messages, 'id' )
 134          );
 135  
 136          // Testing sort with lowercase.
 137          $thread = new BP_Messages_Thread( $message_1->thread_id, 'desc' );
 138          $this->assertEquals(
 139              array( $message_2->id, $message_1->id ),
 140              wp_list_pluck( $thread->messages, 'id' )
 141          );
 142  
 143          // Now sorting via the helper method.
 144          $messages = BP_Messages_Thread::get_messages( $message_1->thread_id, array( 'order' => 'desc' ) );
 145          $this->assertEquals(
 146              array( $message_2->id, $message_1->id ),
 147              wp_list_pluck( $messages, 'id' )
 148          );
 149      }
 150  
 151      /**
 152       * @group get_current_threads_for_user
 153       */
 154      public function test_get_current_threads_for_user_with_search_terms_inbox() {
 155          $u1 = self::factory()->user->create();
 156          $u2 = self::factory()->user->create();
 157  
 158          self::factory()->message->create_and_get( array(
 159              'sender_id' => $u1,
 160              'recipients' => array( $u2 ),
 161              'subject' => 'Foo',
 162          ) );
 163  
 164          $message_2 = self::factory()->message->create_and_get( array(
 165              'sender_id' => $u1,
 166              'recipients' => array( $u2 ),
 167              'subject' => 'Bar',
 168          ) );
 169  
 170          $threads = BP_Messages_Thread::get_current_threads_for_user( array(
 171              'user_id' => $u2,
 172              'search_terms' => 'ar',
 173          ) );
 174  
 175          $expected = array( $message_2->thread_id );
 176          $found = wp_parse_id_list( wp_list_pluck( $threads['threads'], 'thread_id' ) );
 177  
 178          $this->assertSame( $expected, $found );
 179      }
 180  
 181      /**
 182       * @group get_current_threads_for_user
 183       */
 184      public function test_get_current_threads_for_user_with_search_terms_sentbox() {
 185          $u1 = self::factory()->user->create();
 186          $u2 = self::factory()->user->create();
 187  
 188          self::factory()->message->create_and_get( array(
 189              'sender_id' => $u1,
 190              'recipients' => array( $u2 ),
 191              'subject' => 'Foo',
 192          ) );
 193  
 194          $message_2 = self::factory()->message->create_and_get( array(
 195              'sender_id' => $u1,
 196              'recipients' => array( $u2 ),
 197              'subject' => 'Bar',
 198          ) );
 199  
 200          $threads = BP_Messages_Thread::get_current_threads_for_user( array(
 201              'user_id' => $u1,
 202              'box' => 'sentbox',
 203              'search_terms' => 'ar',
 204          ) );
 205  
 206          $expected = array( $message_2->thread_id );
 207          $found = wp_parse_id_list( wp_list_pluck( $threads['threads'], 'thread_id' ) );
 208  
 209          $this->assertSame( $expected, $found );
 210      }
 211  
 212      /**
 213       * @group get_current_threads_for_user
 214       * @expectedDeprecated BP_Messages_Thread::get_current_threads_for_user
 215       */
 216      public function test_get_current_threads_for_user_with_old_args() {
 217          $u1 = self::factory()->user->create();
 218          $u2 = self::factory()->user->create();
 219  
 220          self::factory()->message->create_and_get( array(
 221              'sender_id' => $u1,
 222              'recipients' => array( $u2 ),
 223              'subject' => 'Foo',
 224          ) );
 225  
 226          $message_2 = self::factory()->message->create_and_get( array(
 227              'sender_id' => $u1,
 228              'recipients' => array( $u2 ),
 229              'subject' => 'Bar',
 230          ) );
 231  
 232          $threads = BP_Messages_Thread::get_current_threads_for_user( $u1, 'sentbox', 'all', null, null, 'ar' );
 233  
 234          $expected = array( $message_2->thread_id );
 235          $found = wp_parse_id_list( wp_list_pluck( $threads['threads'], 'thread_id' ) );
 236  
 237          $this->assertSame( $expected, $found );
 238      }
 239  
 240      /**
 241       * @group get_recipients
 242       */
 243  	public function test_get_recipients_paginated() {
 244          $u1       = self::factory()->user->create();
 245          $user_ids = self::factory()->user->create_many( 9 );
 246          $m        = self::factory()->message->create_and_get( array(
 247              'sender_id'  => $u1,
 248              'recipients' => $user_ids,
 249              'subject'    => 'Foo',
 250          ) );
 251  
 252          $thread_1 = new BP_Messages_Thread( $m->thread_id );
 253          $this->assertTrue( 10 === count( $thread_1->get_recipients() ) );
 254  
 255          $thread_2 = new BP_Messages_Thread( $m->thread_id, 'ASC', array( 'recipients_page' => 1, 'recipients_per_page' => 5 ) );
 256          $this->assertTrue( 5 === count( $thread_2->recipients ) );
 257  
 258          $thread_3 = new BP_Messages_Thread( $m->thread_id );
 259          $this->assertTrue( 8 === count( $thread_3->get_recipients( $m->thread_id, array( 'recipients_page' => 1, 'recipients_per_page' => 8 ) ) ) );
 260      }
 261  
 262      /**
 263       * @group get_recipients
 264       * @group cache
 265       */
 266  	public function test_get_recipients_should_cache_its_values() {
 267          global $wpdb;
 268  
 269          $u1 = self::factory()->user->create();
 270          $u2 = self::factory()->user->create();
 271  
 272          $message = self::factory()->message->create_and_get( array(
 273              'sender_id' => $u1,
 274              'recipients' => array( $u2 ),
 275              'subject' => 'Foo',
 276          ) );
 277  
 278          $thread = new BP_Messages_Thread( $message->thread_id );
 279          $recipients = $thread->get_recipients();
 280  
 281          $num_queries = $wpdb->num_queries;
 282          $recipients_cached = $thread->get_recipients();
 283  
 284          $this->assertEquals( $recipients, $recipients_cached );
 285          $this->assertEquals( $num_queries, $wpdb->num_queries );
 286      }
 287  
 288      /**
 289       * @group get_recipients
 290       * @group cache
 291       */
 292      public function test_get_recipients_cache_should_be_busted_when_thread_message_is_sent() {
 293          global $wpdb;
 294  
 295          $u1 = self::factory()->user->create();
 296          $u2 = self::factory()->user->create();
 297  
 298          $message = self::factory()->message->create_and_get( array(
 299              'sender_id' => $u1,
 300              'recipients' => array( $u2 ),
 301              'subject' => 'Foo',
 302          ) );
 303  
 304          $thread = new BP_Messages_Thread( $message->thread_id );
 305          $thread->get_recipients();
 306  
 307          // Verify that the cache is populated.
 308          $num_queries = $wpdb->num_queries;
 309          $thread->get_recipients();
 310          $this->assertEquals( $num_queries, $wpdb->num_queries );
 311  
 312          messages_new_message( array(
 313              'sender_id' => $u2,
 314              'thread_id' => $message->thread_id,
 315              'recipients' => array( $u1 ),
 316              'subject' => 'Bar',
 317              'content' => 'Baz',
 318          ) );
 319  
 320          // Cache should be empty.
 321          $num_queries = $wpdb->num_queries;
 322          $thread->get_recipients();
 323          $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
 324      }
 325  
 326      /**
 327       * @group get_recipients
 328       * @group cache
 329       */
 330      public function test_get_recipients_cache_should_be_busted_when_single_thread_is_deleted() {
 331          global $wpdb;
 332  
 333          $u1 = self::factory()->user->create();
 334          $u2 = self::factory()->user->create();
 335  
 336          $message = self::factory()->message->create_and_get( array(
 337              'sender_id' => $u1,
 338              'recipients' => array( $u2 ),
 339              'subject' => 'Foo',
 340          ) );
 341  
 342          $t1 = $message->thread_id;
 343  
 344          $thread = new BP_Messages_Thread( $t1 );
 345          $thread->get_recipients();
 346  
 347          // Verify that the cache is populated.
 348          $num_queries = $wpdb->num_queries;
 349          $thread->get_recipients();
 350          $this->assertEquals( $num_queries, $wpdb->num_queries );
 351  
 352          messages_delete_thread( $t1 );
 353  
 354          // Cache should be empty.
 355          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 356      }
 357  
 358      /**
 359       * @group get_recipients
 360       * @group cache
 361       */
 362      public function test_get_recipients_cache_should_be_busted_when_array_of_threads_is_deleted() {
 363          global $wpdb;
 364  
 365          $u1 = self::factory()->user->create();
 366          $u2 = self::factory()->user->create();
 367  
 368          $message = self::factory()->message->create_and_get( array(
 369              'sender_id' => $u1,
 370              'recipients' => array( $u2 ),
 371              'subject' => 'Foo',
 372          ) );
 373  
 374          $t1 = $message->thread_id;
 375  
 376          $thread = new BP_Messages_Thread( $t1 );
 377          $thread->get_recipients();
 378  
 379          // Verify that the cache is populated.
 380          $num_queries = $wpdb->num_queries;
 381          $thread->get_recipients();
 382          $this->assertEquals( $num_queries, $wpdb->num_queries );
 383  
 384          messages_delete_thread( array( $t1 ) );
 385  
 386          // Cache should be empty.
 387          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 388      }
 389  
 390      /**
 391       * @group get_recipients
 392       * @group cache
 393       */
 394      public function test_get_recipients_cache_should_be_busted_when_thread_is_read() {
 395          global $wpdb;
 396  
 397          $u1 = self::factory()->user->create();
 398          $u2 = self::factory()->user->create();
 399  
 400          $message = self::factory()->message->create_and_get( array(
 401              'sender_id' => $u1,
 402              'recipients' => array( $u2 ),
 403              'subject' => 'Foo',
 404          ) );
 405  
 406          $t1 = $message->thread_id;
 407  
 408          $thread = new BP_Messages_Thread( $t1 );
 409          $recipients = $thread->get_recipients();
 410  
 411          // Verify that the cache is populated.
 412          $num_queries = $wpdb->num_queries;
 413          $recipients_cached = $thread->get_recipients();
 414          $this->assertEquals( $num_queries, $wpdb->num_queries );
 415  
 416          // Mark thread as read
 417          $current_user = get_current_user_id();
 418          $this->set_current_user( $u2 );
 419          messages_mark_thread_read( $t1 );
 420  
 421          // Cache should be empty.
 422          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 423  
 424          $this->set_current_user( $current_user );
 425      }
 426  
 427      /**
 428       * @group cache
 429       */
 430      public function test_marking_a_thread_as_read_with_specific_user_id() {
 431          $u1      = self::factory()->user->create();
 432          $u2      = self::factory()->user->create();
 433          $message = self::factory()->message->create_and_get( array(
 434              'sender_id'  => $u1,
 435              'recipients' => array( $u2 ),
 436              'subject'    => 'Foo',
 437          ) );
 438  
 439          $thread_id = $message->thread_id;
 440  
 441          // Cache should be populated.
 442          $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
 443  
 444          // Mark thread as read.
 445          messages_mark_thread_read( $thread_id, $u2 );
 446  
 447          // Cache should be empty.
 448          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
 449  
 450          $thread = new BP_Messages_Thread( $thread_id );
 451  
 452          $this->assertFalse( (bool) $thread->unread_count );
 453          $this->assertFalse( (bool) $thread->recipients[ $u1 ]->unread_count );
 454          $this->assertFalse( (bool) $thread->recipients[ $u2 ]->unread_count );
 455      }
 456  
 457      /**
 458       * @group cache
 459       */
 460      public function test_marking_a_thread_as_unread_with_specific_user_id() {
 461          $u1      = self::factory()->user->create();
 462          $u2      = self::factory()->user->create();
 463          $message = self::factory()->message->create_and_get( array(
 464              'sender_id'  => $u1,
 465              'recipients' => array( $u2 ),
 466              'subject'    => 'Foo',
 467          ) );
 468  
 469          $thread_id = $message->thread_id;
 470  
 471          // Cache should be populated.
 472          $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
 473  
 474          // Mark thread as unread.
 475          messages_mark_thread_unread( $thread_id, $u2 );
 476  
 477          // Cache should be empty.
 478          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
 479  
 480          $thread = new BP_Messages_Thread( $thread_id );
 481  
 482          $this->assertFalse( (bool) $thread->recipients[ $u1 ]->unread_count );
 483          $this->assertTrue( (bool) $thread->recipients[ $u2 ]->unread_count );
 484      }
 485  
 486      /**
 487       * @group get_recipients
 488       * @group cache
 489       */
 490      public function test_get_recipients_cache_should_be_busted_when_thread_is_unread() {
 491          global $wpdb;
 492  
 493          $u1 = self::factory()->user->create();
 494          $u2 = self::factory()->user->create();
 495  
 496          $message = self::factory()->message->create_and_get( array(
 497              'sender_id' => $u1,
 498              'recipients' => array( $u2 ),
 499              'subject' => 'Foo',
 500          ) );
 501  
 502          $t1 = $message->thread_id;
 503  
 504          $thread = new BP_Messages_Thread( $t1 );
 505          $thread->get_recipients();
 506  
 507          // Verify that the cache is populated.
 508          $num_queries = $wpdb->num_queries;
 509          $thread->get_recipients();
 510          $this->assertEquals( $num_queries, $wpdb->num_queries );
 511  
 512          // Mark thread as unread
 513          $current_user = get_current_user_id();
 514          $this->set_current_user( $u2 );
 515          messages_mark_thread_unread( $t1 );
 516  
 517          // Cache should be empty.
 518          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 519  
 520          $this->set_current_user( $current_user );
 521      }
 522  
 523      /**
 524       * @group check_access
 525       */
 526  	public function test_check_access_valid_thread() {
 527          $u1 = self::factory()->user->create();
 528          $u2 = self::factory()->user->create();
 529  
 530          $message = self::factory()->message->create_and_get( array(
 531              'sender_id' => $u1,
 532              'recipients' => array( $u2 ),
 533              'subject' => 'Foo',
 534          ) );
 535  
 536          $t1 = $message->thread_id;
 537  
 538          // save recipient ID
 539          $thread = new BP_Messages_Thread( $t1 );
 540          $r1 = wp_list_pluck( $thread->recipients, 'id' );
 541          $r1 = array_pop( $r1 );
 542  
 543          $this->assertEquals( $r1, BP_Messages_Thread::check_access( $t1, $u1 ) );
 544      }
 545  
 546      /**
 547       * @group check_access
 548       */
 549  	public function test_check_access_invalid_thread() {
 550          $this->assertEquals( null, BP_Messages_Thread::check_access( 999, 1 ) );
 551      }
 552  
 553      /**
 554       * @group is_valid
 555       */
 556  	public function test_is_valid_valid_thread() {
 557          $u1 = self::factory()->user->create();
 558          $u2 = self::factory()->user->create();
 559  
 560          $message = self::factory()->message->create_and_get( array(
 561              'sender_id' => $u1,
 562              'recipients' => array( $u2 ),
 563              'subject' => 'Foo',
 564          ) );
 565  
 566          $t1 = $message->thread_id;
 567  
 568          $this->assertEquals( $t1, BP_Messages_Thread::is_valid( $t1 ) );
 569      }
 570  
 571      /**
 572       * @group is_valid
 573       */
 574  	public function test_is_valid_invalid_thread() {
 575          $this->assertEquals( null, BP_Messages_Thread::is_valid( 999 ) );
 576      }
 577  
 578      /**
 579       * @group last_message
 580       */
 581  	public function test_last_message_populated() {
 582          $u1 = self::factory()->user->create();
 583          $u2 = self::factory()->user->create();
 584  
 585          $date = bp_core_current_time();
 586  
 587          $message = self::factory()->message->create_and_get( array(
 588              'sender_id' => $u1,
 589              'recipients' => array( $u2 ),
 590              'subject' => 'Foo',
 591              'date_sent' => $date,
 592              'content' => 'Bar and baz.',
 593          ) );
 594  
 595          $t1 = $message->thread_id;
 596  
 597          $thread = new BP_Messages_Thread( $t1 );
 598  
 599          $this->assertNotNull( $thread->last_message_id );
 600          $this->assertEquals( 'Foo', $thread->last_message_subject );
 601          $this->assertEquals( $u1, $thread->last_sender_id );
 602          $this->assertEquals( $date, $thread->last_message_date );
 603          $this->assertEquals( 'Bar and baz.', $thread->last_message_content );
 604      }
 605  }


Generated: Wed Oct 20 01:01:02 2021 Cross-referenced by PHPXref 0.7.1