diff --git a/lib/classes/event/notification_sent.php b/lib/classes/event/notification_sent.php index 1c2080c3c5b..f00fb51227b 100644 --- a/lib/classes/event/notification_sent.php +++ b/lib/classes/event/notification_sent.php @@ -58,6 +58,11 @@ class notification_sent extends base { $userfromid = 0; } + // If the courseid is null, then set it to the site id. + if (is_null($courseid)) { + $courseid = SITEID; + } + $event = self::create( [ 'objectid' => $notificationid, diff --git a/message/tests/events_test.php b/message/tests/events_test.php index 0f31ae0e0d1..9c9c4158edb 100644 --- a/message/tests/events_test.php +++ b/message/tests/events_test.php @@ -484,6 +484,23 @@ class core_message_events_testcase extends core_message_messagelib_testcase { $this->assertEquals($url, $event->get_url()); } + /** + * Test the notification sent event when null passed as course. + */ + public function test_notification_sent_with_null_course() { + $event = \core\event\notification_sent::create_from_ids(1, 1, 1, null); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\notification_sent', $event); + $this->assertEquals(SITEID, $event->other['courseid']); + } + /** * Test the notification viewed event. */