/** * Creates csv file and add it to the threadgroup for use case. * * @Given /^I login as any "([^"]*)" enrolled in course "([^"]*)"$/ */ public function i_login_as_any_enrolled_in_course($rolearchtype, $courseshortname) { global $DB, $CFG; if (!($id = $DB->get_field('course', 'id', array('shortname' => $courseshortname)))) { util::performance_exception('The specified course with shortname "' . $courseshortname . '" does not exist'); } $coursecontext = \context_course::instance($id); if ($roles = get_archetype_roles($rolearchtype)) { $roles = array_keys($roles); } $roleid = $roles[0]; $users = get_role_users($roleid, $coursecontext, false, 'u.id,u.username', 'u.id ASC'); if (!$users) { util::performance_exception("Course without users with role: " . $rolearchtype); } $data = ""; foreach ($users as $user) { $data .= $user->username . "," . $user->username . "," . $user->id . PHP_EOL; } $testplanfilename = $rolearchtype . '_' . behat_hooks::$featurefile . '.csv'; $csvfile = util::get_final_testplan_path() . DIRECTORY_SEPARATOR . $testplanfilename; $testplanfilepath = ""; if (isset($CFG->testplanfiles_dataroot)) { $testplanfilepath = $CFG->testplanfiles_dataroot . DIRECTORY_SEPARATOR; } file_put_contents($csvfile, $data); testplan_writer::create_csv_data($testplanfilepath, $testplanfilename, $rolearchtype); $firstuser = array_shift($users); return new Given('I log in as "' . $firstuser->username . '"'); }