From bde99c5a4b707a229c72fdcde2497a4e2f390a91 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 7af3e2eed8f..7dfe2a44d50 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -895,7 +895,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. @@ -904,9 +904,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 e90ab386a85..32cb7f309d1 100644 --- a/lib/testing/tests/generator_test.php +++ b/lib/testing/tests/generator_test.php @@ -371,6 +371,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); @@ -402,6 +403,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)));