diff --git a/enrol/tests/enrollib_special_test.php b/enrol/tests/enrollib_special_test.php new file mode 100644 index 0000000000000..1f6dc8467984c --- /dev/null +++ b/enrol/tests/enrollib_special_test.php @@ -0,0 +1,93 @@ +. + +/** + * Test plugin enrollib parts. + * + * @package core_enrol + * @category phpunit + * @copyright 2017 Darko Miletic + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Test plugin enrollib parts. + * + * @package core + * @category phpunit + * @copyright 2017 Darko Miletic + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_enrollib_special_testcase extends advanced_testcase { + + /** + * Confirms that timemodified field was updated after modification of user enrollment + */ + public function test_enrollment_update_timemodified() { + global $DB; + + $this->resetAfterTest(true); + $datagen = $this->getDataGenerator(); + + /** @var enrol_manual_plugin $manualplugin */ + $manualplugin = enrol_get_plugin('manual'); + $this->assertNotNull($manualplugin); + + $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST); + $course = $datagen->create_course(); + $user = $datagen->create_user(); + + $instanceid = null; + $instances = enrol_get_instances($course->id, true); + foreach ($instances as $inst) { + if ($inst->enrol == 'manual') { + $instanceid = (int)$inst->id; + break; + } + } + if (empty($instanceid)) { + $instanceid = $manualplugin->add_default_instance($course); + if (empty($instanceid)) { + $instanceid = $manualplugin->add_instance($course); + } + } + $this->assertNotNull($instanceid); + + $instance = $DB->get_record('enrol', ['id' => $instanceid], '*', MUST_EXIST); + $manualplugin->enrol_user($instance, $user->id, $studentroleid, 0, 0, ENROL_USER_ACTIVE); + $userenrolorig = (int)$DB->get_field( + 'user_enrolments', + 'timemodified', + ['enrolid' => $instance->id, 'userid' => $user->id], + MUST_EXIST + ); + $this->waitForSecond(); + $this->waitForSecond(); + $manualplugin->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED); + $userenrolpost = (int)$DB->get_field( + 'user_enrolments', + 'timemodified', + ['enrolid' => $instance->id, 'userid' => $user->id], + MUST_EXIST + ); + + $this->assertGreaterThan($userenrolorig, $userenrolpost); + } + +} diff --git a/lib/enrollib.php b/lib/enrollib.php index 8bb17af486c58..18350d951cb2d 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -1744,6 +1744,7 @@ public function update_user_enrol(stdClass $instance, $userid, $status = NULL, $ } $ue->modifierid = $USER->id; + $ue->timemodified = time(); $DB->update_record('user_enrolments', $ue); context_course::instance($instance->courseid)->mark_dirty(); // reset enrol caches