Skip to content

Commit

Permalink
MDL-58079 core_enrol: Implementing fix and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kiklop74 committed Mar 16, 2017
1 parent 98c4094 commit ed63ffc
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
93 changes: 93 additions & 0 deletions enrol/tests/enrollib_special_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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);
}

}
1 change: 1 addition & 0 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ed63ffc

Please sign in to comment.