MDL-80803 course: observe mod availability before update notification.

This commit is contained in:
Paul Holden 2024-02-01 14:47:32 +00:00
parent f30110b5eb
commit 222af1c976
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
2 changed files with 23 additions and 3 deletions

View File

@ -70,10 +70,9 @@ class content_notification_task extends adhoc_task {
\core\cron::setup_user($user, $course);
// Ensure that the activity is available/visible to the user.
$cm = get_fast_modinfo($course)->cms[$cm->id];
if (!$cm->uservisible && !$cm->is_visible_on_course_page()) {
// User can't access or see the activity in the course page.
if (!\core_availability\info_module::is_user_visible($cm, $user->id, false)) {
$this->log("Ignoring user {$user->id} (no permissions to see the module)", 1);
continue;
}

View File

@ -16,7 +16,9 @@
namespace core_course\task;
use availability_date\condition;
use context_user;
use core_availability\tree;
/**
* Contains tests for course related notifications.
@ -61,10 +63,12 @@ class content_notification_task_test extends \advanced_testcase {
$url = self::getDataGenerator()->create_module('url', ['course' => $course]);
// Test update.
$moduleavailability = tree::get_root_json([condition::get_json(condition::DIRECTION_FROM, time() + HOURSECS)]);
$moduleinfo = $DB->get_record('course_modules', array('id' => $url->cmid));
$moduleinfo->modulename = 'url';
$moduleinfo->coursemodule = $url->cmid;
$moduleinfo->display = 1;
$moduleinfo->availability = json_encode($moduleavailability);
$moduleinfo->externalurl = '';
$moduleinfo->update = 1;
$draftid = 0;
@ -91,6 +95,23 @@ class content_notification_task_test extends \advanced_testcase {
$messages = $sink->get_messages();
$sink->close();
// The module isn't available for one hour, there should be no notifications.
$this->assertCount(0, $messages);
// Remove availability condition.
$this->setAdminUser();
$moduleinfo->availability = null;
update_module(clone $moduleinfo);
// Redirect messages to sink and stop buffer output from CLI task.
$sink = $this->redirectMessages();
ob_start();
$this->runAdhocTasks('\core_course\task\content_notification_task');
$output = ob_get_contents();
ob_end_clean();
$messages = $sink->get_messages();
$sink->close();
// We have 3 students, one with a non-active enrolment that should not receive a notification.
$this->assertCount(2, $messages);
foreach ($messages as $message) {