/** * 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']); }