MDL-50459 messages: Handle deleted users in external functions
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 8 Jun 2015 12:38:01 +0000 (14:38 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Sun, 14 Jun 2015 20:12:00 +0000 (22:12 +0200)
message/externallib.php
message/tests/externallib_test.php

index 345d042..77b4d30 100644 (file)
@@ -444,11 +444,16 @@ class core_message_external extends external_api {
                     '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;
             }
@@ -576,11 +581,17 @@ class core_message_external extends external_api {
             $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;
         }
@@ -890,8 +901,14 @@ class core_message_external extends external_api {
                 '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;
         }
index 09a7386..0cfd90c 100644 (file)
@@ -290,6 +290,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $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);
@@ -300,6 +301,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $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.');
@@ -310,18 +312,18 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $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']);
     }
 
     /**