Permalink
Browse files

MDL-37964 very basic support for enrolments in data generator

  • Loading branch information...
1 parent c192132 commit 9ba2682e382e886e9214d2d3d76a0f40ac1935c6 @skodak skodak committed with markn86 Nov 19, 2012
Showing with 117 additions and 0 deletions.
  1. +35 −0 lib/phpunit/classes/data_generator.php
  2. +82 −0 lib/phpunit/tests/generator_test.php
@@ -543,4 +543,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;
+ }
}
@@ -109,4 +109,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 9ba2682

Please sign in to comment.