[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group messages
   5   * @group functions
   6   */
   7  class BP_Tests_Messages_Functions extends BP_UnitTestCase {
   8  
   9      /**
  10       * @group counts
  11       */
  12  	public function test_get_unread_count() {
  13          $u1 = self::factory()->user->create();
  14          $u2 = self::factory()->user->create();
  15  
  16          // send a private message
  17          $t1 = messages_new_message( array(
  18              'sender_id'  => $u1,
  19              'recipients' => array( $u2 ),
  20              'subject'    => 'A new message',
  21              'content'    => 'Hey there!',
  22          ) );
  23  
  24          // get unread count for $u2
  25          $this->set_current_user( $u2 );
  26          $this->assertEquals( 1, messages_get_unread_count( $u2 ) );
  27  
  28          // send another message and get recheck unread count
  29          $t2 = messages_new_message( array(
  30              'sender_id'  => $u1,
  31              'recipients' => array( $u2 ),
  32              'subject'    => 'A new message',
  33              'content'    => 'Hey there!',
  34          ) );
  35          $this->assertEquals( 2, messages_get_unread_count( $u2 ) );
  36  
  37          // mark one message as read
  38          messages_mark_thread_read( $t1 );
  39  
  40          // recheck unread count
  41          $this->assertEquals( 1, messages_get_unread_count( $u2 ) );
  42      }
  43  
  44      /**
  45       * @group messages_new_message
  46       */
  47      public function test_messages_new_message_invalid_recipient_error_message() {
  48          $u1 = self::factory()->user->create();
  49  
  50          // attempt to send a private message to an invalid username
  51          $t1 = messages_new_message( array(
  52              'sender_id'  => $u1,
  53              'recipients' => array( 'homerglumpkin' ),
  54              'subject'    => 'A new message',
  55              'content'    => 'Hey there!',
  56              'error_type' => 'wp_error'
  57          ) );
  58  
  59          $this->assertSame( 'Message could not be sent because you have entered an invalid username. Please try again.', $t1->get_error_message() );
  60      }
  61  
  62      /**
  63       * @group messages_new_message
  64       */
  65  	public function test_messages_new_message_wp_error_generic() {
  66          $u1 = self::factory()->user->create();
  67          $u2 = self::factory()->user->create();
  68  
  69          // Emulate a plugin disabling messages.
  70          add_action( 'messages_message_before_save', array( $this, 'remove_recipients_before_save' ) );
  71  
  72          // send a private message
  73          $t1 = messages_new_message( array(
  74              'sender_id'  => $u1,
  75              'recipients' => array( $u2 ),
  76              'subject'    => 'A new message',
  77              'content'    => 'Hey there!',
  78              'error_type' => 'wp_error'
  79          ) );
  80  
  81          $this->assertNotEmpty( $t1->get_error_code() );
  82  
  83          remove_action( 'messages_message_before_save', array( $this, 'remove_recipients_before_save' ) );
  84      }
  85  
  86      /**
  87       * Helper method for test_messages_new_message_wp_error_generic().
  88       */
  89  	public function remove_recipients_before_save( $message ) {
  90          $message->recipients = array();
  91      }
  92  
  93      /**
  94       * @ticket BP7819
  95       * @ticket BP7698
  96       */
  97  	public function test_bp_messages_personal_data_exporter() {
  98          $u1 = self::factory()->user->create();
  99          $u2 = self::factory()->user->create();
 100  
 101          $time = time();
 102  
 103          $t1 = messages_new_message( array(
 104              'sender_id'  => $u1,
 105              'recipients' => array( $u2 ),
 106              'subject'    => 'A new message',
 107              'content'    => 'Hey there!',
 108              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 3 * HOUR_IN_SECONDS ) ),
 109          ) );
 110  
 111          $t1m1 = messages_new_message( array(
 112              'sender_id'  => $u2,
 113              'thread_id'  => $t1,
 114              'recipients' => array( $u1 ),
 115              'subject'    => 'Reply to ' . $t1,
 116              'content'    => 'Hey there!',
 117              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 2 * HOUR_IN_SECONDS ) ),
 118          ) );
 119  
 120          $t1m2 = messages_new_message( array(
 121              'sender_id'  => $u1,
 122              'thread_id'  => $t1,
 123              'recipients' => array( $u2 ),
 124              'subject'    => 'Reply to ' . $t1,
 125              'content'    => 'Hey there!',
 126              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 2 * HOUR_IN_SECONDS ) ),
 127          ) );
 128  
 129          $t2 = messages_new_message( array(
 130              'sender_id'  => $u2,
 131              'recipients' => array( $u1 ),
 132              'subject'    => 'A new message',
 133              'content'    => 'Hey there!',
 134              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 5 * HOUR_IN_SECONDS ) ),
 135          ) );
 136  
 137          $t2m1 = messages_new_message( array(
 138              'sender_id'  => $u1,
 139              'thread_id'  => $t2,
 140              'recipients' => array( $u2 ),
 141              'subject'    => 'Reply to ' . $t2,
 142              'content'    => 'Hey there!',
 143              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 4 * HOUR_IN_SECONDS ) ),
 144          ) );
 145  
 146          $test_user = new WP_User( $u1 );
 147  
 148          $actual = bp_messages_personal_data_exporter( $test_user->user_email, 1 );
 149  
 150          $this->assertTrue( $actual['done'] );
 151  
 152          // Number of exported messages.
 153          $this->assertSame( 3, count( $actual['data'] ) );
 154      }
 155  
 156      /**
 157       * @ticket BP8080
 158       */
 159      public function test_bp_messages_personal_data_exporter_check_sender() {
 160          $u1       = self::factory()->user->create();
 161          $u2       = self::factory()->user->create();
 162          $expected = array(
 163              'Hey u2!',
 164              'You could have replied to my first message u2!',
 165          );
 166  
 167          $time = time();
 168  
 169          $t1 = messages_new_message( array(
 170              'sender_id'  => $u1,
 171              'recipients' => array( $u2 ),
 172              'subject'    => 'A new message',
 173              'content'    => $expected[0],
 174              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 3 * HOUR_IN_SECONDS ) ),
 175          ) );
 176  
 177          $t2 = messages_new_message( array(
 178              'sender_id'  => $u2,
 179              'recipients' => array( $u1 ),
 180              'subject'    => 'A new message',
 181              'content'    => 'Hey u1!',
 182              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 5 * HOUR_IN_SECONDS ) ),
 183          ) );
 184  
 185          $t3 = messages_new_message( array(
 186              'sender_id'  => $u1,
 187              'thread_id'  => $t2,
 188              'recipients' => array( $u2 ),
 189              'subject'    => 'Reply to ' . $t2,
 190              'content'    => $expected[1],
 191              'date_sent'  => date( 'Y-m-d H:i:s', $time - ( 4 * HOUR_IN_SECONDS ) ),
 192          ) );
 193  
 194          $test_user = new WP_User( $u1 );
 195  
 196          $threads      = bp_messages_personal_data_exporter( $test_user->user_email, 1 );
 197          $threads_data = wp_list_pluck( $threads['data'], 'data' );
 198          $actual       = array();
 199  
 200          foreach ( $threads_data as $thread ) {
 201              foreach ( $thread as $data ) {
 202                  if ( 'Message Content' !== $data['name'] ) {
 203                      continue;
 204                  }
 205  
 206                  $actual[] = $data['value'];
 207              }
 208          }
 209  
 210          // Only messages sent by u1 should be exported.
 211          $this->assertEquals( $expected, $actual );
 212      }
 213  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1