[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group messages
   5   * @group cache
   6   */
   7  class BP_Tests_Message_Cache extends BP_UnitTestCase {
   8      /**
   9       * @group bp_messages_update_meta_cache
  10       */
  11  	public function test_bp_messages_update_meta_cache() {
  12          $u1 = self::factory()->user->create();
  13          $u2 = self::factory()->user->create();
  14  
  15          // create the thread
  16          $message_1 = self::factory()->message->create_and_get( array(
  17              'sender_id'  => $u1,
  18              'recipients' => array( $u2 ),
  19              'subject'    => 'This is a knive',
  20          ) );
  21  
  22          // create a reply
  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              'content'    => "That's a spoon",
  28          ) );
  29  
  30          $m1 = $message_1->id;
  31          $m2 = $message_2->id;
  32  
  33          // add cache for each message
  34          bp_messages_update_meta( $m1, 'utensil',  'knive' );
  35          bp_messages_update_meta( $m1, 'is_knive', 'yes' );
  36  
  37          bp_messages_update_meta( $m2, 'utensil',  'spoon' );
  38          bp_messages_update_meta( $m2, 'is_knive', 'no' );
  39          bp_messages_update_meta( $m2, 'is_spoon', 'yes' );
  40  
  41          // prime cache
  42          bp_messages_get_meta( $m1, 'utensil' );
  43  
  44          // Ensure an empty cache for second message
  45          wp_cache_delete( $m2, 'message_meta' );
  46  
  47          // update message meta cache
  48          bp_messages_update_meta_cache( array( $m1, $m2 ) );
  49  
  50          $expected = array(
  51              $m1 => array(
  52                  'utensil' => array(
  53                      'knive',
  54                  ),
  55                  'is_knive' => array(
  56                      'yes',
  57                  ),
  58              ),
  59              $m2 => array(
  60                  'utensil' => array(
  61                      'spoon',
  62                  ),
  63                  'is_knive' => array(
  64                      'no',
  65                  ),
  66                  'is_spoon' => array(
  67                      'yes',
  68                  ),
  69              ),
  70          );
  71  
  72          $found = array(
  73              $m1 => wp_cache_get( $m1, 'message_meta' ),
  74              $m2 => wp_cache_get( $m2, 'message_meta' ),
  75          );
  76  
  77          $this->assertEquals( $expected, $found );
  78      }
  79  
  80      /**
  81       * @group bp_messages_update_meta_cache
  82       * @group bp_thread_has_messages
  83       */
  84      public function test_bp_messages_metadata_cache_in_message_loop() {
  85          $u1 = self::factory()->user->create();
  86          $u2 = self::factory()->user->create();
  87  
  88          // create the message and thread
  89          $m = self::factory()->message->create_and_get( array(
  90              'sender_id'  => $u1,
  91              'recipients' => array( $u2 ),
  92              'subject'    => 'Oy',
  93          ) );
  94  
  95          // add message cache
  96          bp_messages_update_meta( $m->id, 'yolo', 'gah' );
  97  
  98          // prime meta cache in message loop
  99          bp_thread_has_messages( array(
 100              'thread_id' => $m->thread_id,
 101              'update_meta_cache' => true
 102          ) );
 103  
 104          $this->assertNotEmpty( wp_cache_get( $m->id, 'message_meta' ) );
 105      }
 106  
 107      /**
 108       * @group bp_messages_delete_meta
 109       * @group messages_delete_thread
 110       */
 111      public function test_bp_messages_delete_metadata_cache_on_thread_delete() {
 112          $this->old_current_user = get_current_user_id();
 113  
 114          $u1 = self::factory()->user->create();
 115          $u2 = self::factory()->user->create();
 116  
 117          // create the thread
 118          $message_1 = self::factory()->message->create_and_get( array(
 119              'sender_id'  => $u1,
 120              'recipients' => array( $u2 ),
 121              'subject'    => 'Oy',
 122          ) );
 123  
 124          // create a reply
 125          $message_2 = self::factory()->message->create_and_get( array(
 126              'thread_id'  => $message_1->thread_id,
 127              'sender_id'  => $u2,
 128              'recipients' => array( $u1 ),
 129              'content'    => 'Yo',
 130          ) );
 131  
 132          $m1 = $message_1->id;
 133          $m2 = $message_2->id;
 134          $t1 = $message_1->thread_id;
 135  
 136          // add message meta
 137          bp_messages_update_meta( $m1, 'yolo', 'gah' );
 138          bp_messages_update_meta( $m2, 'yolo', 'bah' );
 139  
 140          // prime message meta cache
 141          bp_messages_get_meta( $m1, 'yolo' );
 142          bp_messages_get_meta( $m2, 'yolo' );
 143  
 144          // delete thread
 145          // to outright delete a thread, both recipients must delete it
 146          $this->set_current_user( $u1 );
 147          messages_delete_thread( $t1 );
 148          $this->set_current_user( $u2 );
 149          messages_delete_thread( $t1 );
 150  
 151          // assert empty meta cache
 152          $this->assertEmpty( wp_cache_get( $m1, 'message_meta' ) );
 153          $this->assertEmpty( wp_cache_get( $m2, 'message_meta' ) );
 154  
 155          // cleanup
 156          $this->set_current_user( $this->old_current_user );
 157      }
 158  
 159      /**
 160       * Helper method to grab the message IDs from a message thread.
 161       *
 162       * @param int $thread_id The message thread ID
 163       * @return array
 164       */
 165  	protected function get_message_ids( $thread_id = 0 ) {
 166          $thread = new BP_Messages_Thread( $thread_id );
 167          return wp_list_pluck( $thread->messages, 'id' );
 168      }
 169  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1