MDL-55609 testing: Add a create_and_enrol helper

This commit is contained in:
Andrew Nicols 2018-05-25 11:40:26 +08:00
parent 5b4ca9eb5b
commit e984917db8

View File

@ -1163,4 +1163,24 @@ EOD;
return $event->properties();
}
/**
* Create a new user, and enrol them in the specified course as the supplied role.
*
* @param \stdClass $course The course to enrol in
* @param string $role The role to give within the course
* @param \stdClass $userparams User parameters
* @return \stdClass The created user
*/
public function create_and_enrol($course, $role = 'student', $userparams = null, $enrol = 'manual',
$timestart = 0, $timeend = 0, $status = null) {
global $DB;
$user = $this->create_user($userparams);
$roleid = $DB->get_field('role', 'id', ['shortname' => $role ]);
$this->enrol_user($user->id, $course->id, $roleid, $enrol, $timestart, $timeend, $status);
return $user;
}
}