From 433df29b817478e9dd9f7ab84ee5323e0cc3cd1d Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 26 Jun 2024 09:20:17 +0800 Subject: [PATCH] MDL-63670 core_message: Tests should not use another testcase as base --- message/classes/tests/helper.php | 76 ++++- message/tests/api_test.php | 503 ++++++++++++++++++++-------- message/tests/event/events_test.php | 72 ++-- message/tests/messagelib_test.php | 97 +----- 4 files changed, 495 insertions(+), 253 deletions(-) diff --git a/message/classes/tests/helper.php b/message/classes/tests/helper.php index 5267621be06..67836780bee 100644 --- a/message/classes/tests/helper.php +++ b/message/classes/tests/helper.php @@ -24,7 +24,7 @@ namespace core_message\tests; -defined('MOODLE_INTERNAL') || die(); +use stdClass; /** * The helper class providing util methods for testing. @@ -33,6 +33,80 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class helper { + /** + * Send a fake message. + * + * {@link message_send()} does not support transaction, this function will simulate a message + * sent from a user to another. We should stop using it once {@link message_send()} will support + * transactions. This is not clean at all, this is just used to add rows to the table. + * + * @param \stdClass $userfrom user object of the one sending the message. + * @param \stdClass $userto user object of the one receiving the message. + * @param string $message message to send. + * @param int $notification if the message is a notification. + * @param int $time the time the message was sent + * @return int the id of the message + */ + public static function send_fake_message( + stdClass $userfrom, + stdClass $userto, + string $message = 'Hello world!', + int $notification = 0, + int $time = 0, + ): int { + global $DB; + + if (empty($time)) { + $time = time(); + } + + if ($notification) { + $record = new \stdClass(); + $record->useridfrom = $userfrom->id; + $record->useridto = $userto->id; + $record->subject = 'No subject'; + $record->fullmessage = $message; + $record->smallmessage = $message; + $record->timecreated = $time; + + return $DB->insert_record('notifications', $record); + } + + if ($userfrom->id == $userto->id) { + // It's a self conversation. + $conversation = \core_message\api::get_self_conversation($userfrom->id); + if (empty($conversation)) { + $conversation = \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, + [$userfrom->id], + ); + } + $conversationid = $conversation->id; + } else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) { + // It's an individual conversation between two different users. + $conversation = \core_message\api::create_conversation( + \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, + [ + $userfrom->id, + $userto->id, + ] + ); + $conversationid = $conversation->id; + } + + // Ok, send the message. + $record = (object) [ + 'useridfrom' => $userfrom->id, + 'conversationid' => $conversationid, + 'subject' => 'No subject', + 'fullmessage' => $message, + 'smallmessage' => $message, + 'timecreated' => $time, + ]; + + return $DB->insert_record('messages', $record); + } + /** * Sends a message to a conversation. * diff --git a/message/tests/api_test.php b/message/tests/api_test.php index b5801e8e492..53d3ad6e463 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -18,12 +18,6 @@ namespace core_message; use core_message\tests\helper as testhelper; -defined('MOODLE_INTERNAL') || die(); - -global $CFG; - -require_once($CFG->dirroot . '/message/tests/messagelib_test.php'); - /** * Test message API. * @@ -31,19 +25,32 @@ require_once($CFG->dirroot . '/message/tests/messagelib_test.php'); * @category test * @copyright 2016 Mark Nelson * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \core_message\api */ -class api_test extends messagelib_test { +final class api_test extends \advanced_testcase { + /** + * Test set up. + * + * This is executed before running any test in this file. + */ + public function setUp(): void { + parent::setUp(); + + $this->resetAfterTest(); + } public function test_mark_all_read_for_user_touser(): void { + $this->resetAfterTest(); + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - $this->send_fake_message($sender, $recipient, 'Notification', 1); - $this->send_fake_message($sender, $recipient, 'Notification', 1); - $this->send_fake_message($sender, $recipient, 'Notification', 1); - $this->send_fake_message($sender, $recipient); - $this->send_fake_message($sender, $recipient); - $this->send_fake_message($sender, $recipient); + testhelper::send_fake_message($sender, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender, $recipient); + testhelper::send_fake_message($sender, $recipient); + testhelper::send_fake_message($sender, $recipient); $this->assertEquals(1, api::count_unread_conversations($recipient)); @@ -55,22 +62,27 @@ class api_test extends messagelib_test { } public function test_mark_all_read_for_user_touser_with_fromuser(): void { + $this->resetAfterTest(); + $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3')); $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + $sender1 = $this->getDataGenerator()->create_user(['firstname' => 'Test1', 'lastname' => 'User1']); + $sender2 = $this->getDataGenerator()->create_user(['firstname' => 'Test3', 'lastname' => 'User3']); + $recipient = $this->getDataGenerator()->create_user(['firstname' => 'Test2', 'lastname' => 'User2']); - $this->send_fake_message($sender1, $recipient, 'Notification', 1); - $this->send_fake_message($sender1, $recipient, 'Notification', 1); - $this->send_fake_message($sender1, $recipient, 'Notification', 1); - $this->send_fake_message($sender1, $recipient); - $this->send_fake_message($sender1, $recipient); - $this->send_fake_message($sender1, $recipient); - $this->send_fake_message($sender2, $recipient, 'Notification', 1); - $this->send_fake_message($sender2, $recipient, 'Notification', 1); - $this->send_fake_message($sender2, $recipient, 'Notification', 1); - $this->send_fake_message($sender2, $recipient); - $this->send_fake_message($sender2, $recipient); - $this->send_fake_message($sender2, $recipient); + testhelper::send_fake_message($sender1, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender1, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender1, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender1, $recipient); + testhelper::send_fake_message($sender1, $recipient); + testhelper::send_fake_message($sender1, $recipient); + testhelper::send_fake_message($sender2, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender2, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender2, $recipient, 'Notification', 1); + testhelper::send_fake_message($sender2, $recipient); + testhelper::send_fake_message($sender2, $recipient); + testhelper::send_fake_message($sender2, $recipient); $this->assertEquals(2, api::count_unread_conversations($recipient)); @@ -87,6 +99,8 @@ class api_test extends messagelib_test { */ public function test_count_blocked_users(): void { global $USER; + $this->resetAfterTest(); + // Set this user as the admin. $this->setAdminUser(); @@ -509,6 +523,8 @@ class api_test extends messagelib_test { * Tests getting conversations between 2 users. */ public function test_get_conversations_between_users(): void { + $this->resetAfterTest(); + // Create some users. $user1 = new \stdClass(); $user1->firstname = 'User'; @@ -572,6 +588,8 @@ class api_test extends messagelib_test { * Tests getting self-conversations. */ public function test_get_self_conversation(): void { + $this->resetAfterTest(); + // Create some users. $user1 = new \stdClass(); $user1->firstname = 'User'; @@ -657,11 +675,11 @@ class api_test extends messagelib_test { $time = 1; testhelper::send_fake_message_to_conversation($user1, $sc->id, 'Test message to self!', $time); testhelper::send_fake_message_to_conversation($user1, $gc->id, 'My hero!', $time + 1); - $this->send_fake_message($user3, $user1, 'Don\'t block me.', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 4); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 5); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 6); + testhelper::send_fake_message($user3, $user1, 'Don\'t block me.', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 5); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 6); $convid = api::get_conversation_between_users([$user1->id, $user2->id]); $conv2id = api::get_conversation_between_users([$user1->id, $user3->id]); @@ -745,6 +763,8 @@ class api_test extends messagelib_test { * Test verifying that favourited conversations can be retrieved. */ public function test_get_favourite_conversations(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -759,19 +779,19 @@ class api_test extends messagelib_test { // Create some conversations for user1. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + $messageid1 = testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); - $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); - $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); - $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); - $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); + testhelper::send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); + testhelper::send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); + testhelper::send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); + $messageid2 = testhelper::send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); - $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9); - $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10); - $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11); + testhelper::send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9); + testhelper::send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10); + $messageid3 = testhelper::send_fake_message($user1, $user4, 'Dope.', 0, $time + 11); // Favourite the first 2 conversations for user1. $convoids = []; @@ -804,6 +824,8 @@ class api_test extends messagelib_test { * Tests retrieving favourite conversations with a limit and offset to ensure pagination works correctly. */ public function test_get_favourite_conversations_limit_offset(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -818,19 +840,19 @@ class api_test extends messagelib_test { // Create some conversations for user1. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $messageid1 = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + $messageid1 = testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); - $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); - $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); - $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); - $messageid2 = $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); + testhelper::send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); + testhelper::send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); + testhelper::send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); + $messageid2 = testhelper::send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); - $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9); - $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10); - $messageid3 = $this->send_fake_message($user1, $user4, 'Dope.', 0, $time + 11); + testhelper::send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 9); + testhelper::send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 10); + $messageid3 = testhelper::send_fake_message($user1, $user4, 'Dope.', 0, $time + 11); // Favourite the all conversations for user1. $convoids = []; @@ -858,6 +880,8 @@ class api_test extends messagelib_test { * Tests retrieving favourite conversations when a conversation contains a deleted user. */ public function test_get_favourite_conversations_with_deleted_user(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -865,15 +889,15 @@ class api_test extends messagelib_test { // Send some messages back and forth, have some different conversations with different users. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); - $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); - $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); - $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); - $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); + testhelper::send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); + testhelper::send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); + testhelper::send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); + testhelper::send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); // Favourite the all conversations for user1. $convoids = []; @@ -904,6 +928,8 @@ class api_test extends messagelib_test { * Test confirming that conversations can be marked as favourites. */ public function test_set_favourite_conversation(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -911,15 +937,15 @@ class api_test extends messagelib_test { // Send some messages back and forth, have some different conversations with different users. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); - $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); - $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); - $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); - $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); + testhelper::send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); + testhelper::send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); + testhelper::send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); + testhelper::send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); // Favourite the first conversation as user 1. $conversationid1 = api::get_conversation_between_users([$user1->id, $user2->id]); @@ -942,6 +968,8 @@ class api_test extends messagelib_test { * Test verifying that trying to mark a non-existent conversation as a favourite, results in an exception. */ public function test_set_favourite_conversation_nonexistent_conversation(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); // Try to favourite a non-existent conversation. @@ -953,6 +981,8 @@ class api_test extends messagelib_test { * Test verifying that a conversation cannot be marked as favourite unless the user is a member of that conversation. */ public function test_set_favourite_conversation_non_member(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -960,15 +990,15 @@ class api_test extends messagelib_test { // Send some messages back and forth, have some different conversations with different users. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); - $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); - $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); - $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); - $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); + testhelper::send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); + testhelper::send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); + testhelper::send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); + testhelper::send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); // Try to favourite the first conversation as user 3, who is not a member. $conversationid1 = api::get_conversation_between_users([$user1->id, $user2->id]); @@ -980,6 +1010,8 @@ class api_test extends messagelib_test { * Test confirming that those conversations marked as favourites can be unfavourited. */ public function test_unset_favourite_conversation(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -987,15 +1019,15 @@ class api_test extends messagelib_test { // Send some messages back and forth, have some different conversations with different users. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); - $this->send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); - $this->send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); - $this->send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); - $this->send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); + testhelper::send_fake_message($user1, $user3, 'Booyah', 0, $time + 5); + testhelper::send_fake_message($user3, $user1, 'Whaaat?', 0, $time + 6); + testhelper::send_fake_message($user1, $user3, 'Nothing.', 0, $time + 7); + testhelper::send_fake_message($user3, $user1, 'Cool.', 0, $time + 8); // Favourite the first conversation as user 1 and the second as user 3. $conversationid1 = api::get_conversation_between_users([$user1->id, $user2->id]); @@ -1023,16 +1055,18 @@ class api_test extends messagelib_test { * Test verifying that a valid conversation cannot be unset as a favourite if it's not marked as a favourite. */ public function test_unset_favourite_conversation_not_favourite(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); // Send some messages back and forth, have some different conversations with different users. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); // Now try to unfavourite the conversation as user 1. $conversationid1 = api::get_conversation_between_users([$user1->id, $user2->id]); @@ -1044,6 +1078,8 @@ class api_test extends messagelib_test { * Test verifying that a non-existent conversation cannot be unset as a favourite. */ public function test_unset_favourite_conversation_non_existent_conversation(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); @@ -1126,6 +1162,8 @@ class api_test extends messagelib_test { * Test verifying get_conversations when no limits, offsets, type filters or favourite restrictions are used. */ public function test_get_conversations_no_restrictions(): void { + $this->resetAfterTest(); + global $DB; $user1 = self::getDataGenerator()->create_user(); @@ -1251,6 +1289,8 @@ class api_test extends messagelib_test { */ public function test_get_conversations_message_format(): void { global $DB; + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -1278,6 +1318,8 @@ class api_test extends messagelib_test { * Test verifying get_conversations identifies if a conversation is muted or not. */ public function test_get_conversations_some_muted(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -1324,6 +1366,8 @@ class api_test extends messagelib_test { * Tests retrieving conversations with a limit and offset to ensure pagination works correctly. */ public function test_get_conversations_limit_offset(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -1363,6 +1407,8 @@ class api_test extends messagelib_test { * Test verifying the type filtering behaviour of the */ public function test_get_conversations_type_filter(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -1387,6 +1433,8 @@ class api_test extends messagelib_test { */ public function test_get_conversations_self_conversations(): void { global $DB; + $this->resetAfterTest(); + // Create a conversation between one user and themself. $user1 = self::getDataGenerator()->create_user(); @@ -1447,6 +1495,8 @@ class api_test extends messagelib_test { * Tests retrieving conversations when a conversation contains a deleted user. */ public function test_get_conversations_with_deleted_user(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -1504,6 +1554,8 @@ class api_test extends messagelib_test { * Test confirming the behaviour of get_conversations() when users delete all messages. */ public function test_get_conversations_deleted_messages(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -1542,6 +1594,8 @@ class api_test extends messagelib_test { * favourite. */ public function test_get_conversations_favourite_conversations_single(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -1591,6 +1645,8 @@ class api_test extends messagelib_test { * Test verifying the behaviour of get_conversations() when fetching favourite conversations. */ public function test_get_conversations_favourite_conversations(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -1659,6 +1715,8 @@ class api_test extends messagelib_test { public function test_get_conversations_user_in_group_and_individual_chat(): void { $this->resetAfterTest(); + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -1715,6 +1773,8 @@ class api_test extends messagelib_test { */ public function test_get_conversations_group_linked(): void { global $CFG, $DB; + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); @@ -2070,6 +2130,8 @@ class api_test extends messagelib_test { */ public function test_create_conversation_duplicate_conversations(): void { global $DB; + $this->resetAfterTest(); + $user1 = $this::getDataGenerator()->create_user(); api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_SELF, [$user1->id]); @@ -2091,6 +2153,10 @@ class api_test extends messagelib_test { */ public function test_get_conversations_mixed($usersdata, $contacts, $messagesdata, $expectations): void { global $DB; + $this->resetAfterTest(); + + + $this->redirectMessages(); // Create all of the users. $users = array(); @@ -2115,7 +2181,7 @@ class api_test extends messagelib_test { $subject = $messagedata['subject']; if (isset($messagedata['state']) && $messagedata['state'] == 'unread') { - $messageid = $this->send_fake_message($from, $to, $subject); + $messageid = testhelper::send_fake_message($from, $to, $subject); } else { // If there is no state, or the state is not 'unread', assume the message is read. $messageid = message_post_message($from, $to, $subject, FORMAT_PLAIN); @@ -2152,6 +2218,8 @@ class api_test extends messagelib_test { * Tests retrieving user contacts. */ public function test_get_user_contacts(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); @@ -2212,6 +2280,8 @@ class api_test extends messagelib_test { * Tests retrieving conversation messages. */ public function test_get_conversation_messages(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2267,6 +2337,8 @@ class api_test extends messagelib_test { * Tests retrieving group conversation messages. */ public function test_get_group_conversation_messages(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2330,6 +2402,8 @@ class api_test extends messagelib_test { * Test verifying the sorting param for get_conversation_messages is respected(). */ public function test_get_conversation_messages_sorting(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2397,6 +2471,8 @@ class api_test extends messagelib_test { * Test retrieving conversation messages by providing a minimum timecreated value. */ public function test_get_conversation_messages_time_from_only(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2469,6 +2545,8 @@ class api_test extends messagelib_test { * Test retrieving conversation messages by providing a maximum timecreated value. */ public function test_get_conversation_messages_time_to_only(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2542,6 +2620,8 @@ class api_test extends messagelib_test { * Test retrieving conversation messages by providing a minimum and maximum timecreated value. */ public function test_get_conversation_messages_time_from_and_to(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2591,6 +2671,8 @@ class api_test extends messagelib_test { * Test retrieving conversation messages by providing a limitfrom value. */ public function test_get_conversation_messages_limitfrom_only(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2638,6 +2720,8 @@ class api_test extends messagelib_test { * Test retrieving conversation messages by providing a limitnum value. */ public function test_get_conversation_messages_limitnum(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2684,6 +2768,8 @@ class api_test extends messagelib_test { * Tests retrieving most recent conversation message. */ public function test_get_most_recent_conversation_message(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2717,6 +2803,8 @@ class api_test extends messagelib_test { * Tests checking if a user can mark all messages as read. */ public function test_can_mark_all_messages_as_read(): void { + $this->resetAfterTest(); + // Set as the admin. $this->setAdminUser(); @@ -2727,10 +2815,10 @@ class api_test extends messagelib_test { // Send some messages back and forth. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); $conversationid = api::get_conversation_between_users([$user1->id, $user2->id]); @@ -2754,6 +2842,8 @@ class api_test extends messagelib_test { * Tests checking if a user can delete a conversation. */ public function test_can_delete_conversation(): void { + $this->resetAfterTest(); + // Set as the admin. $this->setAdminUser(); @@ -2763,10 +2853,10 @@ class api_test extends messagelib_test { // Send some messages back and forth. $time = 1; - $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); $conversationid = api::get_conversation_between_users([$user1->id, $user2->id]); @@ -2788,6 +2878,8 @@ class api_test extends messagelib_test { */ public function test_delete_conversation_by_id(): void { global $DB; + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); @@ -2802,10 +2894,10 @@ class api_test extends messagelib_test { // Send some messages back and forth. $time = 1; - $m1id = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $m2id = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $m3id = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $m4id = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + $m1id = testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + $m2id = testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + $m3id = testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + $m4id = testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); $m5id = testhelper::send_fake_message_to_conversation($user1, $sc1->id, 'Hi to myself!', $time + 5); $m6id = testhelper::send_fake_message_to_conversation($user2, $sc2->id, 'I am talking with myself', $time + 6); @@ -2877,19 +2969,19 @@ class api_test extends messagelib_test { $this->setUser($user1); // Send some messages back and forth, have some different conversations with different users. - $this->send_fake_message($user1, $user2, 'Yo!'); - $this->send_fake_message($user2, $user1, 'Sup mang?'); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!'); - $this->send_fake_message($user2, $user1, 'Word.'); + testhelper::send_fake_message($user1, $user2, 'Yo!'); + testhelper::send_fake_message($user2, $user1, 'Sup mang?'); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!'); + testhelper::send_fake_message($user2, $user1, 'Word.'); - $this->send_fake_message($user1, $user3, 'Booyah'); - $this->send_fake_message($user3, $user1, 'Whaaat?'); - $this->send_fake_message($user1, $user3, 'Nothing.'); - $this->send_fake_message($user3, $user1, 'Cool.'); + testhelper::send_fake_message($user1, $user3, 'Booyah'); + testhelper::send_fake_message($user3, $user1, 'Whaaat?'); + testhelper::send_fake_message($user1, $user3, 'Nothing.'); + testhelper::send_fake_message($user3, $user1, 'Cool.'); - $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?'); - $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.'); - $this->send_fake_message($user1, $user4, 'Dope.'); + testhelper::send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?'); + testhelper::send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.'); + testhelper::send_fake_message($user1, $user4, 'Dope.'); // Check the amount for the current user. $this->assertEquals(3, api::count_unread_conversations()); @@ -2914,19 +3006,19 @@ class api_test extends messagelib_test { $this->setUser($user1); // Send some messages back and forth, have some different conversations with different users. - $this->send_fake_message($user1, $user2, 'Yo!'); - $this->send_fake_message($user2, $user1, 'Sup mang?'); - $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!'); - $this->send_fake_message($user2, $user1, 'Word.'); + testhelper::send_fake_message($user1, $user2, 'Yo!'); + testhelper::send_fake_message($user2, $user1, 'Sup mang?'); + testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!'); + testhelper::send_fake_message($user2, $user1, 'Word.'); - $this->send_fake_message($user1, $user3, 'Booyah'); - $this->send_fake_message($user3, $user1, 'Whaaat?'); - $this->send_fake_message($user1, $user3, 'Nothing.'); - $this->send_fake_message($user3, $user1, 'Cool.'); + testhelper::send_fake_message($user1, $user3, 'Booyah'); + testhelper::send_fake_message($user3, $user1, 'Whaaat?'); + testhelper::send_fake_message($user1, $user3, 'Nothing.'); + testhelper::send_fake_message($user3, $user1, 'Cool.'); - $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?'); - $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.'); - $this->send_fake_message($user1, $user4, 'Dope.'); + testhelper::send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?'); + testhelper::send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.'); + testhelper::send_fake_message($user1, $user4, 'Dope.'); // Let's disable the last conversation. $conversationid = api::get_conversation_between_users([$user1->id, $user4->id]); @@ -2940,6 +3032,8 @@ class api_test extends messagelib_test { * Tests deleting a conversation. */ public function test_get_all_message_preferences(): void { + $this->resetAfterTest(); + $user = self::getDataGenerator()->create_user(); $this->setUser($user); @@ -2959,6 +3053,8 @@ class api_test extends messagelib_test { * Tests the user can send a message. */ public function test_can_send_message(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -2982,6 +3078,8 @@ class api_test extends messagelib_test { */ public function test_can_send_message_without_sendmessage_cap(): void { global $DB; + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); @@ -3003,6 +3101,8 @@ class api_test extends messagelib_test { * Tests the user can send a message when they are contact. */ public function test_can_send_message_when_contact(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3025,6 +3125,8 @@ class api_test extends messagelib_test { * has requested messages only from contacts. */ public function test_can_send_message_when_not_contact(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3043,6 +3145,8 @@ class api_test extends messagelib_test { * Tests the user can't send a message if they are blocked. */ public function test_can_send_message_when_blocked(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3062,6 +3166,8 @@ class api_test extends messagelib_test { * even if they are not a contact and are not members of the same course. */ public function test_can_send_message_site_messaging_setting(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3111,6 +3217,8 @@ class api_test extends messagelib_test { */ public function test_can_send_message_with_messageanyuser_cap(): void { global $DB; + $this->resetAfterTest(); + // Create some users. $teacher1 = self::getDataGenerator()->create_user(); @@ -3246,6 +3354,8 @@ class api_test extends messagelib_test { * Verify the expected behaviour of the can_send_message_to_conversation() method for authenticated users with default settings. */ public function test_can_send_message_to_conversation_basic(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3297,6 +3407,8 @@ class api_test extends messagelib_test { */ public function test_can_send_message_to_conversation_sendmessage_cap(): void { global $DB; + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3345,6 +3457,8 @@ class api_test extends messagelib_test { */ public function test_can_send_message_to_conversation_messageanyuser_cap(): void { global $DB; + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3396,6 +3510,8 @@ class api_test extends messagelib_test { * Test verifying that users cannot send messages to conversations they are not a part of. */ public function test_can_send_message_to_conversation_non_member(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3441,6 +3557,8 @@ class api_test extends messagelib_test { * Test verifying the behaviour of the can_send_message_to_conversation method when privacy is set to contacts only. */ public function test_can_send_message_to_conversation_privacy_contacts_only(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3484,6 +3602,8 @@ class api_test extends messagelib_test { * Test verifying the behaviour of the can_send_message_to_conversation method when privacy is set to contacts / course members. */ public function test_can_send_message_to_conversation_privacy_contacts_course(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3532,6 +3652,8 @@ class api_test extends messagelib_test { * Test verifying the behaviour of the can_send_message_to_conversation method when privacy is set to any user. */ public function test_can_send_message_to_conversation_privacy_sitewide(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3586,6 +3708,8 @@ class api_test extends messagelib_test { * Test verifying the behaviour of the can_send_message_to_conversation method when a user is blocked. */ public function test_can_send_message_to_conversation_when_blocked(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -3630,6 +3754,8 @@ class api_test extends messagelib_test { * Tests get_user_privacy_messaging_preference method. */ public function test_get_user_privacy_messaging_preference(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3678,6 +3804,8 @@ class api_test extends messagelib_test { * Tes get_message_processor api. */ public function test_get_message_processor(): void { + $this->resetAfterTest(); + $processors = get_message_processors(true); if (empty($processors)) { $this->markTestSkipped("No message processors found"); @@ -3713,6 +3841,8 @@ class api_test extends messagelib_test { * Test method update_processor_status. */ public function test_update_processor_status(): void { + $this->resetAfterTest(); + $processors = get_message_processors(); if (empty($processors)) { $this->markTestSkipped("No message processors found"); @@ -3768,6 +3898,8 @@ class api_test extends messagelib_test { */ public function test_get_blocked_users(): void { global $USER; + $this->resetAfterTest(); + // Set this user as the admin. $this->setAdminUser(); @@ -3795,14 +3927,15 @@ class api_test extends messagelib_test { */ public function test_mark_message_as_read(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); - $this->send_fake_message($user1, $user2); - $m2id = $this->send_fake_message($user1, $user2); - $this->send_fake_message($user2, $user1); - $m4id = $this->send_fake_message($user2, $user1); + testhelper::send_fake_message($user1, $user2); + $m2id = testhelper::send_fake_message($user1, $user2); + testhelper::send_fake_message($user2, $user1); + $m4id = testhelper::send_fake_message($user2, $user1); $m2 = $DB->get_record('messages', ['id' => $m2id]); $m4 = $DB->get_record('messages', ['id' => $m4id]); @@ -3835,14 +3968,15 @@ class api_test extends messagelib_test { */ public function test_mark_notification_as_read(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); - $this->send_fake_message($user1, $user2, 'Notification 1', 1); - $n2id = $this->send_fake_message($user1, $user2, 'Notification 2', 1); - $this->send_fake_message($user2, $user1, 'Notification 3', 1); - $n4id = $this->send_fake_message($user2, $user1, 'Notification 4', 1); + testhelper::send_fake_message($user1, $user2, 'Notification 1', 1); + $n2id = testhelper::send_fake_message($user1, $user2, 'Notification 2', 1); + testhelper::send_fake_message($user2, $user1, 'Notification 3', 1); + $n4id = testhelper::send_fake_message($user2, $user1, 'Notification 4', 1); $n2 = $DB->get_record('notifications', ['id' => $n2id]); $n4 = $DB->get_record('notifications', ['id' => $n4id]); @@ -3863,6 +3997,8 @@ class api_test extends messagelib_test { * Test a conversation is not returned if there is none. */ public function test_get_conversation_between_users_no_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3873,14 +4009,20 @@ class api_test extends messagelib_test { * Test count_conversation_members for non existing conversation. */ public function test_count_conversation_members_no_existing_conversation(): void { - $this->assertEquals(0, - api::count_conversation_members(0)); + $this->resetAfterTest(); + + $this->assertEquals( + 0, + api::count_conversation_members(0) + ); } /** * Test count_conversation_members for existing conversation. */ public function test_count_conversation_members_existing_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3901,6 +4043,8 @@ class api_test extends messagelib_test { * Test add_members_to_conversation for an individual conversation. */ public function test_add_members_to_individual_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -3922,6 +4066,8 @@ class api_test extends messagelib_test { * Test add_members_to_conversation for existing conversation. */ public function test_add_members_to_existing_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -3944,6 +4090,8 @@ class api_test extends messagelib_test { * Test add_members_to_conversation for non existing conversation. */ public function test_add_members_to_no_existing_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); // Throw dml_missing_record_exception for non existing conversation. @@ -3955,6 +4103,8 @@ class api_test extends messagelib_test { * Test add_member_to_conversation for non existing user. */ public function test_add_members_to_no_existing_user(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3977,6 +4127,8 @@ class api_test extends messagelib_test { * Test add_members_to_conversation for current conversation member. */ public function test_add_members_to_current_conversation_member(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -3999,6 +4151,8 @@ class api_test extends messagelib_test { * Test add_members_to_conversation for multiple users. */ public function test_add_members_for_multiple_users(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4022,6 +4176,8 @@ class api_test extends messagelib_test { * Test add_members_to_conversation for multiple users, included non existing and current conversation members */ public function test_add_members_for_multiple_not_valid_users(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4045,6 +4201,8 @@ class api_test extends messagelib_test { * Test remove_members_from_conversation for individual conversation. */ public function test_remove_members_from_individual_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4065,6 +4223,8 @@ class api_test extends messagelib_test { * Test remove_members_from_conversation for existing conversation. */ public function test_remove_members_from_existing_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4086,6 +4246,8 @@ class api_test extends messagelib_test { * Test remove_members_from_conversation for non existing conversation. */ public function test_remove_members_from_no_existing_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); // Throw dml_missing_record_exception for non existing conversation. @@ -4097,6 +4259,8 @@ class api_test extends messagelib_test { * Test remove_members_from_conversation for non existing user. */ public function test_remove_members_for_no_existing_user(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4118,6 +4282,8 @@ class api_test extends messagelib_test { * Test remove_members_from_conversation for multiple users. */ public function test_remove_members_for_multiple_users(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4142,6 +4308,8 @@ class api_test extends messagelib_test { * Test remove_members_from_conversation for multiple non valid users. */ public function test_remove_members_for_multiple_no_valid_users(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4168,6 +4336,8 @@ class api_test extends messagelib_test { * Test count_conversation_members for empty conversation. */ public function test_count_conversation_members_empty_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4191,6 +4361,7 @@ class api_test extends messagelib_test { */ public function test_can_create_contact_request(): void { global $CFG; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4222,6 +4393,7 @@ class api_test extends messagelib_test { */ public function test_create_contact_request(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4245,6 +4417,7 @@ class api_test extends messagelib_test { */ public function test_confirm_contact_request(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4270,6 +4443,7 @@ class api_test extends messagelib_test { */ public function test_decline_contact_request(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4287,6 +4461,7 @@ class api_test extends messagelib_test { */ public function test_get_contact_requests(): void { global $PAGE; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4320,6 +4495,7 @@ class api_test extends messagelib_test { * Test the get_contact_requests() function when the user has blocked the sender of the request. */ public function test_get_contact_requests_blocked_sender(): void { + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4370,6 +4546,7 @@ class api_test extends messagelib_test { */ public function test_add_contact(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4391,6 +4568,7 @@ class api_test extends messagelib_test { */ public function test_remove_contact(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4406,6 +4584,7 @@ class api_test extends messagelib_test { */ public function test_block_user(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4427,6 +4606,7 @@ class api_test extends messagelib_test { */ public function test_unblock_user(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4442,6 +4622,7 @@ class api_test extends messagelib_test { */ public function test_mute_conversation(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4473,6 +4654,7 @@ class api_test extends messagelib_test { */ public function test_unmute_conversation(): void { global $DB; + $this->resetAfterTest(); $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4496,6 +4678,8 @@ class api_test extends messagelib_test { * Test if a conversation is muted. */ public function test_is_conversation_muted(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4519,6 +4703,8 @@ class api_test extends messagelib_test { * Test is contact check. */ public function test_is_contact(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4534,6 +4720,8 @@ class api_test extends messagelib_test { * Test get contact. */ public function test_get_contact(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4549,6 +4737,8 @@ class api_test extends messagelib_test { * Test is blocked checked. */ public function test_is_blocked(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4565,6 +4755,8 @@ class api_test extends messagelib_test { * Test the contact request exist check. */ public function test_does_contact_request_exist(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4581,6 +4773,8 @@ class api_test extends messagelib_test { * Test the get_received_contact_requests_count() function. */ public function test_get_received_contact_requests_count(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4605,6 +4799,8 @@ class api_test extends messagelib_test { * Test the get_received_contact_requests_count() function when the user has blocked the sender of the request. */ public function test_get_received_contact_requests_count_blocked_sender(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4622,6 +4818,8 @@ class api_test extends messagelib_test { * Test the get_contact_requests_between_users() function. */ public function test_get_contact_requests_between_users(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4670,6 +4868,8 @@ class api_test extends messagelib_test { * Test the user in conversation check. */ public function test_is_user_in_conversation(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4689,6 +4889,8 @@ class api_test extends messagelib_test { * Test the user in conversation check when they are not. */ public function test_is_user_in_conversation_when_not(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4710,6 +4912,7 @@ class api_test extends messagelib_test { */ public function test_can_create_group_conversation(): void { global $CFG; + $this->resetAfterTest(); $student = self::getDataGenerator()->create_user(); $teacher = self::getDataGenerator()->create_user(); @@ -4738,6 +4941,8 @@ class api_test extends messagelib_test { * Test creating an individual conversation. */ public function test_create_conversation_individual(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -4770,6 +4975,8 @@ class api_test extends messagelib_test { * Test creating a group conversation. */ public function test_create_conversation_group(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -4824,6 +5031,8 @@ class api_test extends messagelib_test { * Test create message conversation with area. */ public function test_create_conversation_with_area(): void { + $this->resetAfterTest(); + $contextid = 111; $itemid = 222; $name = 'Name of conversation'; @@ -4849,6 +5058,8 @@ class api_test extends messagelib_test { * Test get_conversation_by_area. */ public function test_get_conversation_by_area(): void { + $this->resetAfterTest(); + $contextid = 111; $itemid = 222; $name = 'Name of conversation'; @@ -4877,6 +5088,7 @@ class api_test extends messagelib_test { */ public function test_enable_conversation(): void { global $DB; + $this->resetAfterTest(); $name = 'Name of conversation'; @@ -4898,6 +5110,8 @@ class api_test extends messagelib_test { */ public function test_disable_conversation(): void { global $DB; + $this->resetAfterTest(); + $name = 'Name of conversation'; @@ -4919,6 +5133,7 @@ class api_test extends messagelib_test { */ public function test_update_conversation_name(): void { global $DB; + $this->resetAfterTest(); $conversation = api::create_conversation(api::MESSAGE_CONVERSATION_TYPE_GROUP, []); @@ -4935,6 +5150,8 @@ class api_test extends messagelib_test { * Test returning members in a conversation with no contact requests. */ public function test_get_conversation_members(): void { + $this->resetAfterTest(); + $lastaccess = new \stdClass(); $lastaccess->lastaccess = time(); @@ -5006,6 +5223,8 @@ class api_test extends messagelib_test { * Test returning members in a conversation with contact requests. */ public function test_get_conversation_members_with_contact_requests(): void { + $this->resetAfterTest(); + $lastaccess = new \stdClass(); $lastaccess->lastaccess = time(); @@ -5097,6 +5316,8 @@ class api_test extends messagelib_test { * Test returning members of a self conversation. */ public function test_get_conversation_members_with_self_conversation(): void { + $this->resetAfterTest(); + $lastaccess = new \stdClass(); $lastaccess->lastaccess = time(); @@ -5124,6 +5345,8 @@ class api_test extends messagelib_test { * Test verifying that messages can be sent to existing individual conversations. */ public function test_send_message_to_conversation_individual_conversation(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -5174,6 +5397,7 @@ class api_test extends messagelib_test { * Test verifying that messages can be sent to existing group conversations. */ public function test_send_message_to_conversation_group_conversation(): void { + $this->resetAfterTest(); // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -5225,6 +5449,7 @@ class api_test extends messagelib_test { */ public function test_send_message_to_conversation_linked_group_conversation(): void { global $CFG, $PAGE; + $this->resetAfterTest(); // Create some users. $user1 = self::getDataGenerator()->create_user(); @@ -5287,6 +5512,8 @@ class api_test extends messagelib_test { * Test verifying that messages cannot be sent to conversations that don't exist. */ public function test_send_message_to_conversation_non_existent_conversation(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -5299,6 +5526,8 @@ class api_test extends messagelib_test { * Test verifying that messages cannot be sent to conversations by users who are not members. */ public function test_send_message_to_conversation_non_member(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -5318,6 +5547,8 @@ class api_test extends messagelib_test { * Test verifying that messages cannot be sent to conversations by users who are not members. */ public function test_send_message_to_conversation_blocked_user(): void { + $this->resetAfterTest(); + // Get a bunch of conversations, some group, some individual and in different states. list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3, $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data(); @@ -5750,6 +5981,8 @@ class api_test extends messagelib_test { $expectedunreadcounts, $deletedusers ): void { + $this->resetAfterTest(); + $generator = $this->getDataGenerator(); $users = [ $generator->create_user(), @@ -5827,6 +6060,8 @@ class api_test extends messagelib_test { * Test the count_contacts() function. */ public function test_count_contacts(): void { + $this->resetAfterTest(); + $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); @@ -6180,6 +6415,8 @@ class api_test extends messagelib_test { * messages but keep the return structure to not break when called from the ws. */ public function test_get_conversation_messages_timefrom_higher_than_last_timecreated(): void { + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); diff --git a/message/tests/event/events_test.php b/message/tests/event/events_test.php index 660245406d8..b004452086c 100644 --- a/message/tests/event/events_test.php +++ b/message/tests/event/events_test.php @@ -25,11 +25,7 @@ namespace core_message\event; -defined('MOODLE_INTERNAL') || die(); - -global $CFG; - -require_once($CFG->dirroot . '/message/tests/messagelib_test.php'); +use core_message\tests\helper as testhelper; /** * Class containing the tests for message related events. @@ -39,24 +35,15 @@ require_once($CFG->dirroot . '/message/tests/messagelib_test.php'); * @copyright 2014 Mark Nelson * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class events_test extends \core_message\messagelib_test { - - /** - * Test set up. - * - * This is executed before running any test in this file. - */ - public function setUp(): void { - parent::setUp(); - $this->resetAfterTest(); - } - +final class events_test extends \advanced_testcase { /** * Test the message contact added event. */ public function test_message_contact_added(): void { global $USER; + $this->resetAfterTest(); + // Set this user as the admin. $this->setAdminUser(); @@ -82,6 +69,8 @@ class events_test extends \core_message\messagelib_test { public function test_message_contact_removed(): void { global $USER; + $this->resetAfterTest(); + // Set this user as the admin. $this->setAdminUser(); @@ -110,6 +99,8 @@ class events_test extends \core_message\messagelib_test { public function test_message_user_blocked(): void { global $USER; + $this->resetAfterTest(); + // Set this user as the admin. $this->setAdminUser(); @@ -136,6 +127,8 @@ class events_test extends \core_message\messagelib_test { public function test_message_user_unblocked(): void { global $USER; + $this->resetAfterTest(); + // Set this user as the admin. $this->setAdminUser(); @@ -197,7 +190,6 @@ class events_test extends \core_message\messagelib_test { } public function test_mesage_sent_without_other_courseid(): void { - // Creating a message_sent event without other[courseid] leads to exception. $this->expectException('coding_exception'); $this->expectExceptionMessage('The \'courseid\' value must be set in other'); @@ -213,6 +205,8 @@ class events_test extends \core_message\messagelib_test { } public function test_mesage_sent_via_create_from_ids(): void { + $this->resetAfterTest(); + // Containing courseid. $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4); @@ -239,6 +233,8 @@ class events_test extends \core_message\messagelib_test { * processor code is called during testing. */ public function test_group_message_sent(): void { + $this->resetAfterTest(); + $event = \core\event\group_message_sent::create([ 'objectid' => 3, 'userid' => 1, @@ -307,6 +303,8 @@ class events_test extends \core_message\messagelib_test { * Test the group message sent event using the create_from_ids() method. */ public function test_group_message_sent_via_create_from_ids(): void { + $this->resetAfterTest(); + // Fields are: userfromid, conversationid, messageid, courseid. $event = \core\event\group_message_sent::create_from_ids(1, 2, 3, 4); @@ -332,11 +330,13 @@ class events_test extends \core_message\messagelib_test { public function test_message_viewed(): void { global $DB; + $this->resetAfterTest(); + // Create users to send messages between. $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); - $messageid = $this->send_fake_message($user1, $user2); + $messageid = testhelper::send_fake_message($user1, $user2); // Trigger and capture the event. $sink = $this->redirectEvents(); @@ -364,13 +364,15 @@ class events_test extends \core_message\messagelib_test { public function test_message_deleted(): void { global $DB, $USER; + $this->resetAfterTest(); + $this->setAdminUser(); // Create users to send messages between. $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); - $messageid = $this->send_fake_message($user1, $user2); + $messageid = testhelper::send_fake_message($user1, $user2); // Trigger and capture the event. $sink = $this->redirectEvents(); @@ -392,7 +394,7 @@ class events_test extends \core_message\messagelib_test { $this->setUser($user1); // Create a read message. - $messageid = $this->send_fake_message($user1, $user2); + $messageid = testhelper::send_fake_message($user1, $user2); $m = $DB->get_record('messages', ['id' => $messageid]); \core_message\api::mark_message_as_read($user2->id, $m); @@ -420,6 +422,8 @@ class events_test extends \core_message\messagelib_test { public function test_message_deleted_whole_conversation(): void { global $DB; + $this->resetAfterTest(); + // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); @@ -430,14 +434,14 @@ class events_test extends \core_message\messagelib_test { // Send some messages back and forth. $time = 1; $messages = []; - $messages[] = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); - $messages[] = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); - $messages[] = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); - $messages[] = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4); - $messages[] = $this->send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5); - $messages[] = $this->send_fake_message($user2, $user1, 'Nah', 0, $time + 6); - $messages[] = $this->send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7); - $messages[] = $this->send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8); + $messages[] = testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1); + $messages[] = testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2); + $messages[] = testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3); + $messages[] = testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4); + $messages[] = testhelper::send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5); + $messages[] = testhelper::send_fake_message($user2, $user1, 'Nah', 0, $time + 6); + $messages[] = testhelper::send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7); + $messages[] = testhelper::send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8); // Mark the last 4 messages as read. $m5 = $DB->get_record('messages', ['id' => $messages[4]]); @@ -489,6 +493,8 @@ class events_test extends \core_message\messagelib_test { * Test the notification sent event. */ public function test_notification_sent(): void { + $this->resetAfterTest(); + // Create a course. $course = $this->getDataGenerator()->create_course(); @@ -497,7 +503,7 @@ class events_test extends \core_message\messagelib_test { $user2 = $this->getDataGenerator()->create_user(); // Send a notification. - $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1); + $notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1); // Containing courseid. $event = \core\event\notification_sent::create_from_ids($user1->id, $user2->id, $notificationid, $course->id); @@ -525,6 +531,8 @@ class events_test extends \core_message\messagelib_test { public function test_notification_sent_with_null_course(): void { $event = \core\event\notification_sent::create_from_ids(1, 1, 1, null); + $this->resetAfterTest(); + // Trigger and capture the event. $sink = $this->redirectEvents(); $event->trigger(); @@ -542,12 +550,14 @@ class events_test extends \core_message\messagelib_test { public function test_notification_viewed(): void { global $DB; + $this->resetAfterTest(); + // Create users to send notifications between. $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); // Send a notification. - $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1); + $notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1); // Trigger and capture the event. $sink = $this->redirectEvents(); diff --git a/message/tests/messagelib_test.php b/message/tests/messagelib_test.php index 15103d9087a..888f1a0cef6 100644 --- a/message/tests/messagelib_test.php +++ b/message/tests/messagelib_test.php @@ -18,11 +18,6 @@ namespace core_message; use core_message\tests\helper as testhelper; -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->dirroot . '/message/lib.php'); - /** * Test api's in message lib. * @@ -31,88 +26,12 @@ require_once($CFG->dirroot . '/message/lib.php'); * @copyright 2014 Rajesh Taneja * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class messagelib_test extends \advanced_testcase { +final class messagelib_test extends \advanced_testcase { + public static function setUpBeforeClass(): void { + global $CFG; + require_once($CFG->dirroot . '/message/lib.php'); - /** @var phpunit_message_sink keep track of messages. */ - protected $messagesink = null; - - /** - * Test set up. - * - * This is executed before running any test in this file. - */ - public function setUp(): void { - parent::setUp(); - $this->preventResetByRollback(); // Messaging is not compatible with transactions. - $this->messagesink = $this->redirectMessages(); - $this->resetAfterTest(); - } - - /** - * Send a fake message. - * - * {@link message_send()} does not support transaction, this function will simulate a message - * sent from a user to another. We should stop using it once {@link message_send()} will support - * transactions. This is not clean at all, this is just used to add rows to the table. - * - * @param \stdClass $userfrom user object of the one sending the message. - * @param \stdClass $userto user object of the one receiving the message. - * @param string $message message to send. - * @param int $notification if the message is a notification. - * @param int $time the time the message was sent - * @return int the id of the message - */ - protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) { - global $DB; - - if (empty($time)) { - $time = time(); - } - - if ($notification) { - $record = new \stdClass(); - $record->useridfrom = $userfrom->id; - $record->useridto = $userto->id; - $record->subject = 'No subject'; - $record->fullmessage = $message; - $record->smallmessage = $message; - $record->timecreated = $time; - - return $DB->insert_record('notifications', $record); - } - - if ($userfrom->id == $userto->id) { - // It's a self conversation. - $conversation = \core_message\api::get_self_conversation($userfrom->id); - if (empty($conversation)) { - $conversation = \core_message\api::create_conversation( - \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF, - [$userfrom->id] - ); - } - $conversationid = $conversation->id; - } else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) { - // It's an individual conversation between two different users. - $conversation = \core_message\api::create_conversation( - \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, - [ - $userfrom->id, - $userto->id - ] - ); - $conversationid = $conversation->id; - } - - // Ok, send the message. - $record = new \stdClass(); - $record->useridfrom = $userfrom->id; - $record->conversationid = $conversationid; - $record->subject = 'No subject'; - $record->fullmessage = $message; - $record->smallmessage = $message; - $record->timecreated = $time; - - return $DB->insert_record('messages', $record); + parent::setUpBeforeClass(); } /** @@ -141,6 +60,8 @@ class messagelib_test extends \advanced_testcase { public function test_message_search_users(): void { global $USER; + $this->resetAfterTest(); + // Set this user as the admin. $this->setAdminUser(); @@ -164,7 +85,7 @@ class messagelib_test extends \advanced_testcase { public function test_message_get_messages(): void { global $DB; - $this->resetAfterTest(true); + $this->resetAfterTest(); // Set this user as the admin. $this->setAdminUser(); @@ -236,7 +157,7 @@ class messagelib_test extends \advanced_testcase { * Test message_get_messages with only group conversations between users. */ public function test_message_get_messages_only_group_conversations(): void { - $this->resetAfterTest(true); + $this->resetAfterTest(); // Set this user as the admin. $this->setAdminUser();