MDL-76023 course: fix access to custom data in notification task.

The message class `customdata` property is automatically JSON encoded
via magic setter method, so we can't append to it directly.
This commit is contained in:
Paul Holden 2022-10-19 18:37:21 +01:00
parent f8d28e4ca6
commit 06f013eec6
2 changed files with 30 additions and 20 deletions

View File

@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class handling course content updates notifications.
*
* @package core_course
* @copyright 2021 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\task;
use core\task\adhoc_task;
@ -40,8 +32,6 @@ class content_notification_task extends adhoc_task {
/**
* Run the main task.
*
* @throws \coding_exception if something wrong happens.
*/
public function execute() {
global $CFG, $OUTPUT;
@ -122,12 +112,13 @@ class content_notification_task extends adhoc_task {
$eventdata->contexturl = (new \moodle_url('/mod/' . $cm->modname . '/view.php', ['id' => $cm->id]))->out(false);
$eventdata->contexturlname = $cm->name;
$eventdata->notification = 1;
$eventdata->customdata = [
'notificationiconurl' => $cm->get_icon_url()->out(false),
];
// Add notification custom data.
$eventcustomdata = ['notificationiconurl' => $cm->get_icon_url()->out(false)];
if ($courseimage = \core_course\external\course_summary_exporter::get_course_image($course)) {
$eventdata->customdata['notificationpictureurl'] = $courseimage;
$eventcustomdata['notificationpictureurl'] = $courseimage;
}
$eventdata->customdata = $eventcustomdata;
$completion = \core_completion\cm_completion_details::get_instance($cm, $user->id);
$activitydates = \core\activity_dates::get_dates_for_module($cm, $user->id);

View File

@ -14,28 +14,42 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_course;
namespace core_course\task;
use context_user;
/**
* Contains tests for course related notifications.
*
* @package core
* @subpackage course
* @covers \core_course\task\content_notification_task
* @copyright 2021 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class notifications_test extends \advanced_testcase {
class content_notification_task_test extends \advanced_testcase {
/**
* Test coursecontentupdated class.
* Test execution of task
*/
public function test_coursecontentupdated_notifications() {
global $DB;
public function test_execute(): void {
global $DB, $CFG, $USER;
$this->resetAfterTest();
$this->setAdminUser();
$course = self::getDataGenerator()->create_course();
// Create course, with a course image.
$draft = get_file_storage()->create_file_from_pathname([
'component' => 'user',
'filearea' => 'draft',
'contextid' => context_user::instance($USER->id)->id,
'itemid' => file_get_unused_draft_itemid(),
'filename' => 'gd-logo.png',
'filepath' => '/',
], "{$CFG->libdir}/tests/fixtures/gd-logo.png");
$course = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draft->get_itemid()]);
// Enrol couple of students to receive a notification and one unactive enrolment.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
@ -82,6 +96,11 @@ class notifications_test extends \advanced_testcase {
foreach ($messages as $message) {
$this->assertEquals('coursecontentupdated', $message->eventtype);
$this->assertEquals($modurl, $message->contexturl);
$messagecustomdata = json_decode($message->customdata);
$this->assertEquals($course->id, $messagecustomdata->courseid);
$this->assertObjectHasAttribute('notificationiconurl', $messagecustomdata);
$this->assertObjectHasAttribute('notificationpictureurl', $messagecustomdata);
}
// Now, set the course to not visible.