From 2fae9db702126bce0c82f7df7fc6a261b4f12efb Mon Sep 17 00:00:00 2001 From: sam marshall Date: Fri, 18 Dec 2015 09:44:52 +0000 Subject: [PATCH] MDL-52558 Data generator: enrol_user should allow shortname --- lib/testing/generator/data_generator.php | 12 ++++++++++-- lib/testing/tests/generator_test.php | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 82c92dbad35..8e7a32d3d2c 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -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; } diff --git a/lib/testing/tests/generator_test.php b/lib/testing/tests/generator_test.php index 0104d82c66e..615eae28021 100644 --- a/lib/testing/tests/generator_test.php +++ b/lib/testing/tests/generator_test.php @@ -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)));