1
0
Fork 0
mirror of https://github.com/buddypress/buddypress.git synced 2026-07-22 20:56:55 +08:00
buddypress/tests/phpunit/testcases/messages/functions.php
Boone B Gorges 0ed016254c Use static factory method throughout PHPUnit tests.
See #7620.

git-svn-id: https://buddypress.svn.wordpress.org/trunk@11737 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2017-11-03 19:44:10 +00:00

92 lines
2.5 KiB
PHP

<?php
/**
* @group messages
* @group functions
*/
class BP_Tests_Messages_Functions extends BP_UnitTestCase {
/**
* @group counts
*/
public function test_get_unread_count() {
$u1 = self::factory()->user->create();
$u2 = self::factory()->user->create();
// send a private message
$t1 = messages_new_message( array(
'sender_id' => $u1,
'recipients' => array( $u2 ),
'subject' => 'A new message',
'content' => 'Hey there!',
) );
// get unread count for $u2
$this->set_current_user( $u2 );
$this->assertEquals( 1, messages_get_unread_count( $u2 ) );
// send another message and get recheck unread count
$t2 = messages_new_message( array(
'sender_id' => $u1,
'recipients' => array( $u2 ),
'subject' => 'A new message',
'content' => 'Hey there!',
) );
$this->assertEquals( 2, messages_get_unread_count( $u2 ) );
// mark one message as read
messages_mark_thread_read( $t1 );
// recheck unread count
$this->assertEquals( 1, messages_get_unread_count( $u2 ) );
}
/**
* @group messages_new_message
*/
public function test_messages_new_message_invalid_recipient_error_message() {
$u1 = self::factory()->user->create();
// attempt to send a private message to an invalid username
$t1 = messages_new_message( array(
'sender_id' => $u1,
'recipients' => array( 'homerglumpkin' ),
'subject' => 'A new message',
'content' => 'Hey there!',
'error_type' => 'wp_error'
) );
$this->assertSame( 'Message could not be sent because you have entered an invalid username. Please try again.', $t1->get_error_message() );
}
/**
* @group messages_new_message
*/
public function test_messages_new_message_wp_error_generic() {
$u1 = self::factory()->user->create();
$u2 = self::factory()->user->create();
// Emulate a plugin disabling messages.
add_action( 'messages_message_before_save', array( $this, 'remove_recipients_before_save' ) );
// send a private message
$t1 = messages_new_message( array(
'sender_id' => $u1,
'recipients' => array( $u2 ),
'subject' => 'A new message',
'content' => 'Hey there!',
'error_type' => 'wp_error'
) );
$this->assertNotEmpty( $t1->get_error_code() );
remove_action( 'messages_message_before_save', array( $this, 'remove_recipients_before_save' ) );
}
/**
* Helper method for test_messages_new_message_wp_error_generic().
*/
public function remove_recipients_before_save( $message ) {
$message->recipients = array();
}
}