[ 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 test_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      /**
  34       * @group order
  35       */
  36  	public function test_construct_order_desc() {
  37          $u1 = self::factory()->user->create();
  38          $u2 = self::factory()->user->create();
  39  
  40          // create thread
  41          $message_1 = self::factory()->message->create_and_get( array(
  42              'sender_id' => $u1,
  43              'recipients' => array( $u2 ),
  44              'subject' => 'Foo',
  45          ) );
  46          $m1 = $message_1->id;
  47  
  48          // create reply
  49          $message_2 = self::factory()->message->create_and_get( array(
  50              'thread_id' => $message_1->thread_id,
  51              'sender_id' => $u1,
  52              'recipients' => array( $u2 ),
  53              'content' => 'Bar'
  54          ) );
  55          $m2 = $message_2->id;
  56  
  57          // now get thread by DESC
  58          $thread = new BP_Messages_Thread( $message_1->thread_id, 'DESC' );
  59  
  60          // assert!
  61          $this->assertEquals(
  62              array( $m2, $m1 ),
  63              wp_list_pluck( $thread->messages, 'id' )
  64          );
  65      }
  66  
  67      /**
  68       * @group get_current_threads_for_user
  69       */
  70      public function test_get_current_threads_for_user_with_search_terms_inbox() {
  71          $u1 = self::factory()->user->create();
  72          $u2 = self::factory()->user->create();
  73  
  74          $message_1 = self::factory()->message->create_and_get( array(
  75              'sender_id' => $u1,
  76              'recipients' => array( $u2 ),
  77              'subject' => 'Foo',
  78          ) );
  79  
  80          $message_2 = self::factory()->message->create_and_get( array(
  81              'sender_id' => $u1,
  82              'recipients' => array( $u2 ),
  83              'subject' => 'Bar',
  84          ) );
  85  
  86          $threads = BP_Messages_Thread::get_current_threads_for_user( array(
  87              'user_id' => $u2,
  88              'search_terms' => 'ar',
  89          ) );
  90  
  91          $expected = array( $message_2->thread_id );
  92          $found = wp_parse_id_list( wp_list_pluck( $threads['threads'], 'thread_id' ) );
  93  
  94          $this->assertSame( $expected, $found );
  95      }
  96  
  97      /**
  98       * @group get_current_threads_for_user
  99       */
 100      public function test_get_current_threads_for_user_with_search_terms_sentbox() {
 101          $u1 = self::factory()->user->create();
 102          $u2 = self::factory()->user->create();
 103  
 104          $message_1 = self::factory()->message->create_and_get( array(
 105              'sender_id' => $u1,
 106              'recipients' => array( $u2 ),
 107              'subject' => 'Foo',
 108          ) );
 109  
 110          $message_2 = self::factory()->message->create_and_get( array(
 111              'sender_id' => $u1,
 112              'recipients' => array( $u2 ),
 113              'subject' => 'Bar',
 114          ) );
 115  
 116          $threads = BP_Messages_Thread::get_current_threads_for_user( array(
 117              'user_id' => $u1,
 118              'box' => 'sentbox',
 119              'search_terms' => 'ar',
 120          ) );
 121  
 122          $expected = array( $message_2->thread_id );
 123          $found = wp_parse_id_list( wp_list_pluck( $threads['threads'], 'thread_id' ) );
 124  
 125          $this->assertSame( $expected, $found );
 126      }
 127  
 128      /**
 129       * @group get_current_threads_for_user
 130       * @expectedDeprecated BP_Messages_Thread::get_current_threads_for_user
 131       */
 132      public function test_get_current_threads_for_user_with_old_args() {
 133          $u1 = self::factory()->user->create();
 134          $u2 = self::factory()->user->create();
 135  
 136          $message_1 = self::factory()->message->create_and_get( array(
 137              'sender_id' => $u1,
 138              'recipients' => array( $u2 ),
 139              'subject' => 'Foo',
 140          ) );
 141  
 142          $message_2 = self::factory()->message->create_and_get( array(
 143              'sender_id' => $u1,
 144              'recipients' => array( $u2 ),
 145              'subject' => 'Bar',
 146          ) );
 147  
 148          $threads = BP_Messages_Thread::get_current_threads_for_user( $u1, 'sentbox', 'all', null, null, 'ar' );
 149  
 150          $expected = array( $message_2->thread_id );
 151          $found = wp_parse_id_list( wp_list_pluck( $threads['threads'], 'thread_id' ) );
 152  
 153          $this->assertSame( $expected, $found );
 154      }
 155  
 156      /**
 157       * @group get_recipients
 158       * @group cache
 159       */
 160  	public function test_get_recipients_should_cache_its_values() {
 161          global $wpdb;
 162  
 163          $u1 = self::factory()->user->create();
 164          $u2 = self::factory()->user->create();
 165  
 166          $message = self::factory()->message->create_and_get( array(
 167              'sender_id' => $u1,
 168              'recipients' => array( $u2 ),
 169              'subject' => 'Foo',
 170          ) );
 171  
 172          $thread = new BP_Messages_Thread( $message->thread_id );
 173          $recipients = $thread->get_recipients();
 174  
 175          $num_queries = $wpdb->num_queries;
 176          $recipients_cached = $thread->get_recipients();
 177  
 178          $this->assertEquals( $recipients, $recipients_cached );
 179          $this->assertEquals( $num_queries, $wpdb->num_queries );
 180      }
 181  
 182      /**
 183       * @group get_recipients
 184       * @group cache
 185       */
 186      public function test_get_recipients_cache_should_be_busted_when_thread_message_is_sent() {
 187          global $wpdb;
 188  
 189          $u1 = self::factory()->user->create();
 190          $u2 = self::factory()->user->create();
 191  
 192          $message = self::factory()->message->create_and_get( array(
 193              'sender_id' => $u1,
 194              'recipients' => array( $u2 ),
 195              'subject' => 'Foo',
 196          ) );
 197  
 198          $thread = new BP_Messages_Thread( $message->thread_id );
 199          $recipients = $thread->get_recipients();
 200  
 201          // Verify that the cache is populated.
 202          $num_queries = $wpdb->num_queries;
 203          $recipients_cached = $thread->get_recipients();
 204          $this->assertEquals( $num_queries, $wpdb->num_queries );
 205  
 206          messages_new_message( array(
 207              'sender_id' => $u2,
 208              'thread_id' => $message->thread_id,
 209              'recipients' => array( $u1 ),
 210              'subject' => 'Bar',
 211              'content' => 'Baz',
 212          ) );
 213  
 214          // Cache should be empty.
 215          $num_queries = $wpdb->num_queries;
 216          $recipients_uncached = $thread->get_recipients();
 217          $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
 218      }
 219  
 220      /**
 221       * @group get_recipients
 222       * @group cache
 223       */
 224      public function test_get_recipients_cache_should_be_busted_when_single_thread_is_deleted() {
 225          global $wpdb;
 226  
 227          $u1 = self::factory()->user->create();
 228          $u2 = self::factory()->user->create();
 229  
 230          $message = self::factory()->message->create_and_get( array(
 231              'sender_id' => $u1,
 232              'recipients' => array( $u2 ),
 233              'subject' => 'Foo',
 234          ) );
 235  
 236          $t1 = $message->thread_id;
 237  
 238          $thread = new BP_Messages_Thread( $t1 );
 239          $recipients = $thread->get_recipients();
 240  
 241          // Verify that the cache is populated.
 242          $num_queries = $wpdb->num_queries;
 243          $recipients_cached = $thread->get_recipients();
 244          $this->assertEquals( $num_queries, $wpdb->num_queries );
 245  
 246          messages_delete_thread( $t1 );
 247  
 248          // Cache should be empty.
 249          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 250      }
 251  
 252      /**
 253       * @group get_recipients
 254       * @group cache
 255       */
 256      public function test_get_recipients_cache_should_be_busted_when_array_of_threads_is_deleted() {
 257          global $wpdb;
 258  
 259          $u1 = self::factory()->user->create();
 260          $u2 = self::factory()->user->create();
 261  
 262          $message = self::factory()->message->create_and_get( array(
 263              'sender_id' => $u1,
 264              'recipients' => array( $u2 ),
 265              'subject' => 'Foo',
 266          ) );
 267  
 268          $t1 = $message->thread_id;
 269  
 270          $thread = new BP_Messages_Thread( $t1 );
 271          $recipients = $thread->get_recipients();
 272  
 273          // Verify that the cache is populated.
 274          $num_queries = $wpdb->num_queries;
 275          $recipients_cached = $thread->get_recipients();
 276          $this->assertEquals( $num_queries, $wpdb->num_queries );
 277  
 278          messages_delete_thread( array( $t1 ) );
 279  
 280          // Cache should be empty.
 281          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 282      }
 283  
 284      /**
 285       * @group get_recipients
 286       * @group cache
 287       */
 288      public function test_get_recipients_cache_should_be_busted_when_thread_is_read() {
 289          global $wpdb;
 290  
 291          $u1 = self::factory()->user->create();
 292          $u2 = self::factory()->user->create();
 293  
 294          $message = self::factory()->message->create_and_get( array(
 295              'sender_id' => $u1,
 296              'recipients' => array( $u2 ),
 297              'subject' => 'Foo',
 298          ) );
 299  
 300          $t1 = $message->thread_id;
 301  
 302          $thread = new BP_Messages_Thread( $t1 );
 303          $recipients = $thread->get_recipients();
 304  
 305          // Verify that the cache is populated.
 306          $num_queries = $wpdb->num_queries;
 307          $recipients_cached = $thread->get_recipients();
 308          $this->assertEquals( $num_queries, $wpdb->num_queries );
 309  
 310          // Mark thread as read
 311          $current_user = get_current_user_id();
 312          $this->set_current_user( $u2 );
 313          messages_mark_thread_read( $t1 );
 314  
 315          // Cache should be empty.
 316          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 317  
 318          $this->set_current_user( $current_user );
 319      }
 320  
 321      /**
 322       * @group get_recipients
 323       * @group cache
 324       */
 325      public function test_get_recipients_cache_should_be_busted_when_thread_is_unread() {
 326          global $wpdb;
 327  
 328          $u1 = self::factory()->user->create();
 329          $u2 = self::factory()->user->create();
 330  
 331          $message = self::factory()->message->create_and_get( array(
 332              'sender_id' => $u1,
 333              'recipients' => array( $u2 ),
 334              'subject' => 'Foo',
 335          ) );
 336  
 337          $t1 = $message->thread_id;
 338  
 339          $thread = new BP_Messages_Thread( $t1 );
 340          $recipients = $thread->get_recipients();
 341  
 342          // Verify that the cache is populated.
 343          $num_queries = $wpdb->num_queries;
 344          $recipients_cached = $thread->get_recipients();
 345          $this->assertEquals( $num_queries, $wpdb->num_queries );
 346  
 347          // Mark thread as unread
 348          $current_user = get_current_user_id();
 349          $this->set_current_user( $u2 );
 350          messages_mark_thread_unread( $t1 );
 351  
 352          // Cache should be empty.
 353          $this->assertFalse( wp_cache_get( 'thread_recipients_' . $t1, 'bp_messages' ) );
 354  
 355          $this->set_current_user( $current_user );
 356      }
 357  
 358      /**
 359       * @group check_access
 360       */
 361  	public function test_check_access_valid_thread() {
 362          $u1 = self::factory()->user->create();
 363          $u2 = self::factory()->user->create();
 364  
 365          $message = self::factory()->message->create_and_get( array(
 366              'sender_id' => $u1,
 367              'recipients' => array( $u2 ),
 368              'subject' => 'Foo',
 369          ) );
 370  
 371          $t1 = $message->thread_id;
 372  
 373          // save recipient ID
 374          $thread = new BP_Messages_Thread( $t1 );
 375          $r1 = wp_list_pluck( $thread->recipients, 'id' );
 376          $r1 = array_pop( $r1 );
 377  
 378          $this->assertEquals( $r1, BP_Messages_Thread::check_access( $t1, $u1 ) );
 379      }
 380  
 381      /**
 382       * @group check_access
 383       */
 384  	public function test_check_access_invalid_thread() {
 385          $this->assertEquals( null, BP_Messages_Thread::check_access( 999, 1 ) );
 386      }
 387  
 388      /**
 389       * @group is_valid
 390       */
 391  	public function test_is_valid_valid_thread() {
 392          $u1 = self::factory()->user->create();
 393          $u2 = self::factory()->user->create();
 394  
 395          $message = self::factory()->message->create_and_get( array(
 396              'sender_id' => $u1,
 397              'recipients' => array( $u2 ),
 398              'subject' => 'Foo',
 399          ) );
 400  
 401          $t1 = $message->thread_id;
 402  
 403          $this->assertEquals( $t1, BP_Messages_Thread::is_valid( $t1 ) );
 404      }
 405  
 406      /**
 407       * @group is_valid
 408       */
 409  	public function test_is_valid_invalid_thread() {
 410          $this->assertEquals( null, BP_Messages_Thread::is_valid( 999 ) );
 411      }
 412  
 413      /**
 414       * @group last_message
 415       */
 416  	public function test_last_message_populated() {
 417          $u1 = self::factory()->user->create();
 418          $u2 = self::factory()->user->create();
 419  
 420          $date = bp_core_current_time();
 421  
 422          $message = self::factory()->message->create_and_get( array(
 423              'sender_id' => $u1,
 424              'recipients' => array( $u2 ),
 425              'subject' => 'Foo',
 426              'date_sent' => $date,
 427              'content' => 'Bar and baz.',
 428          ) );
 429  
 430          $t1 = $message->thread_id;
 431  
 432          $thread = new BP_Messages_Thread( $t1 );
 433  
 434          $this->assertNotNull( $thread->last_message_id );
 435          $this->assertEquals( 'Foo', $thread->last_message_subject );
 436          $this->assertEquals( $u1, $thread->last_sender_id );
 437          $this->assertEquals( $date, $thread->last_message_date );
 438          $this->assertEquals( 'Bar and baz.', $thread->last_message_content );
 439      }
 440  }


Generated: Mon Jul 15 01:01:40 2019 Cross-referenced by PHPXref 0.7.1