Skip to content

Commit

Permalink
MDL-36703 very basic support for enrolments in data generator
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Nov 19, 2012
1 parent 2d7c5ee commit 4f5789e
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/phpunit/classes/data_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,39 @@ public function create_scale($record=null, array $options=null) {

return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST);
}

/**
* Simplified enrolment of user to course using default options.
*
* It is strongly recommended to use only this method for 'manual' and 'self' plugins only!!!
*
* @param int $userid
* @param int $courseid
* @param int $roleid optional role id, 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.
* @return bool success
*/
public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual') {
global $DB;

if (!$plugin = enrol_get_plugin($enrol)) {
return false;
}

$instances = $DB->get_records('enrol', array('courseid'=>$courseid, 'enrol'=>$enrol));
if (count($instances) != 1) {
return false;
}
$instance = reset($instances);

if (is_null($roleid) and $instance->roleid) {
$roleid = $instance->roleid;
}

$plugin->enrol_user($instance, $userid, $roleid);

return true;
}
}
82 changes: 82 additions & 0 deletions lib/phpunit/tests/generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,86 @@ public function test_create_block() {
$page = $generator->create_block('online_users');
$this->assertNotEmpty($page);
}

public function test_enrol_user() {
global $DB;

$this->resetAfterTest();

$selfplugin = enrol_get_plugin('self');
$this->assertNotEmpty($selfplugin);

$manualplugin = enrol_get_plugin('manual');
$this->assertNotEmpty($manualplugin);

// Prepare some data.

$studentrole = $DB->get_record('role', array('shortname'=>'student'));
$this->assertNotEmpty($studentrole);
$teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
$this->assertNotEmpty($teacherrole);

$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();

$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$context3 = context_course::instance($course3->id);

$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $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);
$instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
$instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);

$this->assertEquals($studentrole->id, $instance1->roleid);
$this->assertEquals($studentrole->id, $instance2->roleid);
$this->assertEquals($studentrole->id, $instance3->roleid);

$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual')));
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$maninstance3->roleid = $teacherrole->id;
$DB->update_record('enrol', $maninstance3, array('id'=>$maninstance3->id));

$this->assertEquals($studentrole->id, $maninstance1->roleid);
$this->assertEquals($studentrole->id, $maninstance2->roleid);
$this->assertEquals($teacherrole->id, $maninstance3->roleid);

$result = $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->assertTrue($result);
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));

$result = $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $teacherrole->id, 'manual');
$this->assertTrue($result);
$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($user1->id, $course3->id, 0, 'manual');
$this->assertTrue($result);
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id)));


$result = $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self');
$this->assertTrue($result);
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));


$selfplugin->add_instance($course2, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
$result = $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self');
$this->assertFalse($result);

$DB->delete_records('enrol', array('enrol'=>'self', 'courseid'=>$course3->id));
$result = $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self');
$this->assertFalse($result);

}
}

0 comments on commit 4f5789e

Please sign in to comment.