Navigation Menu

Skip to content

Commit

Permalink
MDL-50459 messages: Handle deleted users in external functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Jun 8, 2015
1 parent c3661ca commit e003e65
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
41 changes: 29 additions & 12 deletions message/externallib.php
Expand Up @@ -444,11 +444,16 @@ public static function get_contacts() {
'unread' => $contact->messagecount
);

$usercontextid = context_user::instance($contact->id)->id;
$newcontact['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontextid, 'user', 'icon', null, '/', 'f1')->out(false);
$newcontact['profileimageurlsmall'] = moodle_url::make_webservice_pluginfile_url(
$usercontextid, 'user', 'icon', null, '/', 'f2')->out(false);
$usercontext = context_user::instance($contact->id, IGNORE_MISSING);
if ($usercontext) {
$newcontact['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
$newcontact['profileimageurlsmall'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f2')->out(false);
} else {
$newcontact['profileimageurl'] = '';
$newcontact['profileimageurlsmall'] = '';
}

$allcontacts[$mode][$key] = $newcontact;
}
Expand Down Expand Up @@ -576,11 +581,17 @@ public static function search_contacts($searchtext, $onlymycourses = false) {
$user->phone1 = null;
$user->phone2 = null;

$usercontextid = context_user::instance($user->id)->id;
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontextid, 'user', 'icon', null, '/', 'f1')->out(false);
$newuser['profileimageurlsmall'] = moodle_url::make_webservice_pluginfile_url(
$usercontextid, 'user', 'icon', null, '/', 'f2')->out(false);
$usercontext = context_user::instance($user->id, IGNORE_MISSING);

if ($usercontext) {
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
$newuser['profileimageurlsmall'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f2')->out(false);
} else {
$newuser['profileimageurl'] = '';
$newuser['profileimageurlsmall'] = '';
}

$user = $newuser;
}
Expand Down Expand Up @@ -890,8 +901,14 @@ public static function get_blocked_users($userid) {
'id' => $user->id,
'fullname' => fullname($user),
);
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
context_user::instance($user->id)->id, 'user', 'icon', null, '/', 'f1')->out(false);

$usercontext = context_user::instance($user->id, IGNORE_MISSING);
if ($usercontext) {
$newuser['profileimageurl'] = moodle_url::make_webservice_pluginfile_url(
$usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
} else {
$newuser['profileimageurl'] = '';
}

$blockedusers[] = $newuser;
}
Expand Down
10 changes: 6 additions & 4 deletions message/tests/externallib_test.php
Expand Up @@ -290,6 +290,7 @@ public function test_get_contacts() {
$user_online->lastaccess = time();
$user_online = self::getDataGenerator()->create_user($user_online);
$user_blocked = self::getDataGenerator()->create_user();
$noreplyuser = core_user::get_user(core_user::NOREPLY_USER);

// Login as user1.
$this->setUser($user1);
Expand All @@ -300,6 +301,7 @@ public function test_get_contacts() {
$this->send_message($user_stranger, $user1, 'Hello there!');
$this->send_message($user_stranger, $user1, 'How you goin?');
$this->send_message($user_stranger, $user1, 'Cya!');
$this->send_message($noreplyuser, $user1, 'I am not a real user');

// User_blocked sends a message to user1.
$this->send_message($user_blocked, $user1, 'Here, have some spam.');
Expand All @@ -310,18 +312,18 @@ public function test_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']);
$this->assertCount(3, $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']);
$this->assertCount(2, $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']);
$this->assertEquals(core_user::NOREPLY_USER, $stranger['id']);
$this->assertEquals(1, $stranger['unread']);
}

/**
Expand Down

0 comments on commit e003e65

Please sign in to comment.