From ed63ffc7152968ae6a465f30472477b989b27788 Mon Sep 17 00:00:00 2001 From: Darko Miletic Date: Thu, 16 Mar 2017 14:23:34 -0300 Subject: [PATCH 1/2] MDL-58079 core_enrol: Implementing fix and unit test --- enrol/tests/enrollib_special_test.php | 93 +++++++++++++++++++++++++++ lib/enrollib.php | 1 + 2 files changed, 94 insertions(+) create mode 100644 enrol/tests/enrollib_special_test.php diff --git a/enrol/tests/enrollib_special_test.php b/enrol/tests/enrollib_special_test.php new file mode 100644 index 00000000000..1f6dc846798 --- /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 8bb17af486c..18350d951cb 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -1744,6 +1744,7 @@ abstract class enrol_plugin { } $ue->modifierid = $USER->id; + $ue->timemodified = time(); $DB->update_record('user_enrolments', $ue); context_course::instance($instance->courseid)->mark_dirty(); // reset enrol caches From fa0029b11dcf804bcc5901c2e21e3a5692ea7a50 Mon Sep 17 00:00:00 2001 From: Darko Miletic Date: Mon, 20 Mar 2017 04:09:15 -0300 Subject: [PATCH 2/2] MDL-58079 core_enrol: Fixed invalid category tag --- enrol/tests/enrollib_special_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/enrol/tests/enrollib_special_test.php b/enrol/tests/enrollib_special_test.php index 1f6dc846798..132bda15274 100644 --- a/enrol/tests/enrollib_special_test.php +++ b/enrol/tests/enrollib_special_test.php @@ -18,7 +18,7 @@ * Test plugin enrollib parts. * * @package core_enrol - * @category phpunit + * @category test * @copyright 2017 Darko Miletic * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); * Test plugin enrollib parts. * * @package core - * @category phpunit + * @category test * @copyright 2017 Darko Miletic * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */