Skip to content

Commit

Permalink
MDL-55609 testing: Add a create_and_enrol helper
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and David Monllao committed Jun 12, 2018
1 parent d2b6ce1 commit 3714b3d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/testing/generator/data_generator.php
Expand Up @@ -1163,4 +1163,24 @@ public function create_event($data = []) {

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;
}
}

0 comments on commit 3714b3d

Please sign in to comment.