MDL-52558 Data generator: enrol_user should allow shortname

This commit is contained in:
sam marshall
2015-12-18 11:01:57 +00:00
parent 464f907c4c
commit 2fae9db702
2 changed files with 18 additions and 2 deletions
+10 -2
View File
@@ -881,7 +881,7 @@ EOD;
*
* @param int $userid
* @param int $courseid
* @param int $roleid optional role id, use only with manual plugin
* @param int|string $roleidorshortname optional role id or role shortname, use only with manual plugin
* @param string $enrol name of enrol plugin,
* there must be exactly one instance in course,
* it must support enrol_user() method.
@@ -890,9 +890,17 @@ EOD;
* @param int $status (optional) default to ENROL_USER_ACTIVE for new enrolments
* @return bool success
*/
public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual', $timestart = 0, $timeend = 0, $status = null) {
public function enrol_user($userid, $courseid, $roleidorshortname = null, $enrol = 'manual',
$timestart = 0, $timeend = 0, $status = null) {
global $DB;
// If role is specified by shortname, convert it into an id.
if (!is_numeric($roleidorshortname) && is_string($roleidorshortname)) {
$roleid = $DB->get_field('role', 'id', array('shortname' => $roleidorshortname), MUST_EXIST);
} else {
$roleid = $roleidorshortname;
}
if (!$plugin = enrol_get_plugin($enrol)) {
return false;
}
+8
View File
@@ -362,6 +362,7 @@ class core_test_generator_testcase extends advanced_testcase {
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
@@ -393,6 +394,13 @@ class core_test_generator_testcase extends advanced_testcase {
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user1->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
$result = $this->getDataGenerator()->enrol_user($user4->id, $course2->id, 'teacher', 'manual');
$this->assertTrue($result);
$this->assertTrue($DB->record_exists('user_enrolments',
array('enrolid' => $maninstance2->id, 'userid' => $user4->id)));
$this->assertTrue($DB->record_exists('role_assignments',
array('contextid' => $context2->id, 'userid' => $user4->id, 'roleid' => $teacherrole->id)));
$result = $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 0, 'manual');
$this->assertTrue($result);
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id)));