/** * Test get_contacts. */ public function test_get_contacts() { $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user_stranger = self::getDataGenerator()->create_user(); $user_offline1 = self::getDataGenerator()->create_user(); $user_offline2 = self::getDataGenerator()->create_user(); $user_offline3 = self::getDataGenerator()->create_user(); $user_online = new stdClass(); $user_online->lastaccess = time(); $user_online = self::getDataGenerator()->create_user($user_online); $user_blocked = self::getDataGenerator()->create_user(); // Login as user1. $this->setUser($user1); $this->assertEquals(array(), core_message_external::create_contacts(array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id))); // User_stranger sends a couple of messages to user1. $this->send_message($user_stranger, $user1, 'Hello there!'); $this->send_message($user_stranger, $user1, 'How you goin?'); $this->send_message($user_stranger, $user1, 'Cya!'); // User_blocked sends a message to user1. $this->send_message($user_blocked, $user1, 'Here, have some spam.'); // Retrieve the contacts of the user. $this->setUser($user1); $contacts = core_message_external::get_contacts(); $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts); $this->assertCount(3, $contacts['offline']); $this->assertCount(1, $contacts['online']); $this->assertCount(2, $contacts['strangers']); core_message_external::block_contacts(array($user_blocked->id)); $contacts = core_message_external::get_contacts(); $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts); $this->assertCount(3, $contacts['offline']); $this->assertCount(1, $contacts['online']); $this->assertCount(1, $contacts['strangers']); // Checking some of the fields returned. $stranger = array_pop($contacts['strangers']); $this->assertEquals($user_stranger->id, $stranger['id']); $this->assertEquals(3, $stranger['unread']); }
/** * Test get_blocked_users. */ public function test_get_blocked_users() { $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $userstranger = self::getDataGenerator()->create_user(); $useroffline1 = self::getDataGenerator()->create_user(); $useroffline2 = self::getDataGenerator()->create_user(); $userblocked = self::getDataGenerator()->create_user(); // Login as user1. $this->setUser($user1); $this->assertEquals(array(), core_message_external::create_contacts(array($useroffline1->id, $useroffline2->id))); // The userstranger sends a couple of messages to user1. $this->send_message($userstranger, $user1, 'Hello there!'); $this->send_message($userstranger, $user1, 'How you goin?'); // The userblocked sends a message to user1. // Note that this user is not blocked at this point. $this->send_message($userblocked, $user1, 'Here, have some spam.'); // Retrieve the list of blocked users. $this->setUser($user1); $blockedusers = core_message_external::get_blocked_users($user1->id); $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); $this->assertCount(0, $blockedusers['users']); // Block the $userblocked and retrieve again the list. core_message_external::block_contacts(array($userblocked->id)); $blockedusers = core_message_external::get_blocked_users($user1->id); $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); $this->assertCount(1, $blockedusers['users']); }