MDL-63884 core_message: fix for groups without images
authorJake Dallimore <jake@moodle.com>
Fri, 9 Nov 2018 09:27:04 +0000 (17:27 +0800)
committerJake Dallimore <jake@moodle.com>
Fri, 9 Nov 2018 09:59:53 +0000 (17:59 +0800)
If we don't have an image for the group, leave this extra field empty.

message/classes/api.php
message/tests/api_test.php

index 62995e3..43fa325 100644 (file)
@@ -450,7 +450,10 @@ class api {
                     $extrafields[$convid]['subname'] = format_string($courseinfo[$groupid]->courseshortname);
 
                     // Imageurl.
-                    $extrafields[$convid]['imageurl'] = get_group_picture_url($group, $group->courseid, true)->out(false);
+                    $extrafields[$convid]['imageurl'] = '';
+                    if ($url = get_group_picture_url($group, $group->courseid, true)) {
+                        $extrafields[$convid]['imageurl'] = $url->out(false);
+                    }
                 }
             }
         }
index 76f2777..dceda6f 100644 (file)
@@ -1335,7 +1335,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
 
         $course1 = $this->getDataGenerator()->create_course();
 
-        // Create a group with a linked conversation.
+        // Create a group with a linked conversation and a valid image.
         $this->setAdminUser();
         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
@@ -1350,11 +1350,29 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
         $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 
+        // Verify the group with the image works as expected.
         $conversations = \core_message\api::get_conversations($user1->id);
         $this->assertEquals(2, $conversations[0]->membercount);
         $this->assertEquals($course1->shortname, $conversations[0]->subname);
         $groupimageurl = get_group_picture_url($group1, $group1->courseid, true);
         $this->assertEquals($groupimageurl, $conversations[0]->imageurl);
+
+        // Create a group with a linked conversation and without any image.
+        $group2 = $this->getDataGenerator()->create_group([
+            'courseid' => $course1->id,
+            'enablemessaging' => 1,
+        ]);
+
+        // Add users to group2.
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
+        $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user3->id));
+
+        // Verify the group without any image works as expected too.
+        $conversations = \core_message\api::get_conversations($user3->id);
+        $this->assertEquals(2, $conversations[0]->membercount);
+        $this->assertEquals($course1->shortname, $conversations[0]->subname);
+        $groupimageurl = get_group_picture_url($group2, $group2->courseid, true);
+        $this->assertEquals($groupimageurl, $conversations[0]->imageurl);
     }
 
    /**