Skip to content

Commit

Permalink
[open-lms-open-source#11] Only delete programs events in cron task
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Apr 15, 2024
1 parent 1373422 commit a5b0e42
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions classes/local/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ public static function fix_program_events(?stdClass $program): void {
LEFT JOIN {enrol_programs_allocations} pa ON pa.id = e.instance AND e.component = 'enrol_programs'
LEFT JOIN {enrol_programs_programs} p ON p.id = pa.programid
WHERE (pa.id IS NULL OR pa.archived = 1 OR p.archived = 1 OR pa.timecompleted IS NOT NULL)
AND e.component = 'enrol_programs'
$programselect
ORDER BY e.id ASC";
$rs = $DB->get_recordset_sql($sql, $params);
Expand Down
36 changes: 36 additions & 0 deletions tests/task/cron_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,40 @@ public function test_execute() {
$task = new \enrol_programs\task\cron();
$task->execute();
}

/**
* Tests that manual events are not deleted by the programs cron task.
*
* @see https://github.com/open-lms-open-source/moodle-enrol_programs/issues/11 Issue
*/
public function test_manual_calendar_events_not_deleted() {
global $DB, $USER;
$this->setAdminUser();

$event = new \stdClass();
$event->userid = $USER->id;
$event->timestart = 0;
$event->name = 'test';

// Manually created events have modulename 0 and component as null.
$event->modulename = 0;
$event->component = null;

\calendar_event::create($event);

$events = $DB->get_records('event');
$this->assertCount(1, $events);
$event = current($events);
$this->assertNull($event->component);
$this->assertEquals(0, $event->modulename);

// Run cron.
$task = new \enrol_programs\task\cron();
$task->execute();

// Ensure calendar event that was created is left untouched.
$events = $DB->get_records('event');
$eventids = array_column($events, 'id');
$this->assertTrue(in_array($event->id, $eventids));
}
}

0 comments on commit a5b0e42

Please sign in to comment.