Merge branch 'MDL-61811-master_enrol_manual_notifyall_not_updating' of https://github.com/vtos/moodle

This commit is contained in:
Jun Pataleta 2023-08-28 16:08:53 +08:00
commit 81ea8ad220
No known key found for this signature in database
GPG Key ID: F83510526D99E2C7
2 changed files with 117 additions and 0 deletions

View File

@ -173,6 +173,9 @@ class enrol_manual_plugin extends enrol_plugin {
}
}
}
$data->notifyall = $data->expirynotify == 2 ? 1 : 0;
return parent::update_instance($instance, $data);
}

View File

@ -647,4 +647,118 @@ class lib_test extends \advanced_testcase {
],
];
}
/**
* Tests an enrolment instance is updated properly.
*
* @covers \enrol_manual::update_instance
* @dataProvider update_enrolment_instance_data_provider
*
* @param stdClass $expectation
* @param stdClass $updatedata
*/
public function test_enrolment_instance_is_updated(stdClass $expectation, stdClass $updatedata): void {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$studentroles = get_archetype_roles('student');
$studentrole = array_shift($studentroles);
// Given the plugin is globally configured with the following settings.
$plugin = enrol_get_plugin('manual');
$plugin->set_config('status', ENROL_INSTANCE_ENABLED);
$plugin->set_config('roleid', $studentrole->id);
$plugin->set_config('enrolperiod', 30 * DAYSECS);
$plugin->set_config('expirynotify', 1);
$plugin->set_config('expirythreshold', 2 * DAYSECS);
// And a course is created with the default enrolment instance.
$course = $generator->create_course();
// When the enrolment instance is being updated.
$enrolinstance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
$successfullyupdated = $plugin->update_instance($enrolinstance, $updatedata);
// Then the update is successful.
$this->assertTrue($successfullyupdated);
// And the updated enrolment instance contains the expected values.
$enrolinstance = $DB->get_record('enrol', ['id' => $enrolinstance->id]);
$this->assertEquals($expectation->status, $enrolinstance->status);
$this->assertEquals($expectation->roleid, $enrolinstance->roleid);
$this->assertEquals($expectation->enrolperiod, $enrolinstance->enrolperiod);
$this->assertEquals($expectation->expirynotify, $enrolinstance->expirynotify);
$this->assertEquals($expectation->notifyall, $enrolinstance->notifyall);
$this->assertEquals($expectation->expirythreshold, $enrolinstance->expirythreshold);
}
/**
* Data provider for test_enrolment_instance_is_updated().
*
* @return array
*/
public function update_enrolment_instance_data_provider(): array {
$studentroles = get_archetype_roles('student');
$studentrole = array_shift($studentroles);
$teacherroles = get_archetype_roles('teacher');
$teacherrole = array_shift($teacherroles);
return [
'disabled, all the others are default' => [
'expectation' => (object) [
'status' => ENROL_INSTANCE_DISABLED,
'roleid' => $studentrole->id,
'enrolperiod' => 30 * DAYSECS,
'expirynotify' => 1,
'notifyall' => 0,
'expirythreshold' => 2 * DAYSECS,
],
'update data' => (object) [
'status' => ENROL_INSTANCE_DISABLED,
'roleid' => $studentrole->id,
'enrolperiod' => 30 * DAYSECS,
'expirynotify' => 1,
'expirythreshold' => 2 * DAYSECS,
],
],
'enabled, teacher role, no duration set, notify no one on expiry, 0 notification threshold' => [
'expectation' => (object) [
'status' => ENROL_INSTANCE_ENABLED,
'roleid' => $teacherrole->id,
'enrolperiod' => 0,
'expirynotify' => 0,
'notifyall' => 0,
'expirythreshold' => 0,
],
'update data' => (object) [
'status' => ENROL_INSTANCE_ENABLED,
'roleid' => $teacherrole->id,
'enrolperiod' => 0,
'expirynotify' => 0,
'expirythreshold' => 0,
],
],
'notify enroller and enrolled on expiry, all the others are default' => [
'expectation' => (object) [
'status' => ENROL_INSTANCE_ENABLED,
'roleid' => $studentrole->id,
'enrolperiod' => 30 * DAYSECS,
'expirynotify' => 2,
'notifyall' => 1,
'expirythreshold' => 2 * DAYSECS,
],
'update data' => (object) [
'status' => ENROL_INSTANCE_ENABLED,
'roleid' => $studentrole->id,
'enrolperiod' => 30 * DAYSECS,
'expirynotify' => 2,
'expirythreshold' => 2 * DAYSECS,
],
],
];
}
}