diff --git a/enrol/cohort/tests/sync_test.php b/enrol/cohort/tests/sync_test.php new file mode 100644 index 0000000000000..b60a4cc4d909d --- /dev/null +++ b/enrol/cohort/tests/sync_test.php @@ -0,0 +1,424 @@ +. + +/** + * Cohort enrolment sync functional test. + * + * @package enrol_cohort + * @category phpunit + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot.'/enrol/cohort/locallib.php'); +require_once($CFG->dirroot.'/cohort/lib.php'); + +class enrol_cohort_testcase extends advanced_testcase { + + protected function enable_plugin() { + $enabled = enrol_get_plugins(true); + $enabled['cohort'] = true; + $enabled = array_keys($enabled); + set_config('enrol_plugins_enabled', implode(',', $enabled)); + } + + protected function disable_plugin() { + $enabled = enrol_get_plugins(true); + unset($enabled['cohort']); + $enabled = array_keys($enabled); + set_config('enrol_plugins_enabled', implode(',', $enabled)); + } + + public function test_handler_sync() { + global $DB; + + $this->resetAfterTest(); + + // Setup a few courses and categories. + + $cohortplugin = enrol_get_plugin('cohort'); + $manualplugin = enrol_get_plugin('manual'); + + $studentrole = $DB->get_record('role', array('shortname'=>'student')); + $this->assertNotEmpty($studentrole); + $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); + $this->assertNotEmpty($teacherrole); + $managerrole = $DB->get_record('role', array('shortname'=>'manager')); + $this->assertNotEmpty($managerrole); + + $cat1 = $this->getDataGenerator()->create_category(); + $cat2 = $this->getDataGenerator()->create_category(); + + $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); + $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); + $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); + $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); + $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); + + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $user3 = $this->getDataGenerator()->create_user(); + $user4 = $this->getDataGenerator()->create_user(); + + $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id)); + $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id)); + $cohort3 = $this->getDataGenerator()->create_cohort(); + + $this->enable_plugin(); + + $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id); + $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id); + + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertEquals(2, $DB->count_records('user_enrolments', array())); + + $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id)); + $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id)); + + $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id)); + $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id)); + + $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id)); + $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id)); + + + // Test cohort member add event. + + cohort_add_member($cohort1->id, $user1->id); + cohort_add_member($cohort1->id, $user2->id); + cohort_add_member($cohort1->id, $user4->id); + $this->assertEquals(5, $DB->count_records('user_enrolments', array())); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id))); + $this->assertEquals(5, $DB->count_records('role_assignments', array())); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + + cohort_add_member($cohort2->id, $user3->id); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id))); + $this->assertEquals(7, $DB->count_records('role_assignments', array())); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course2->id)->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id))); + + cohort_add_member($cohort3->id, $user3->id); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertEquals(7, $DB->count_records('role_assignments', array())); + + // Test cohort remove action. + + $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction')); + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); + + cohort_remove_member($cohort1->id, $user2->id); + cohort_remove_member($cohort1->id, $user4->id); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertEquals(5, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + + cohort_add_member($cohort1->id, $user2->id); + cohort_add_member($cohort1->id, $user4->id); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertEquals(7, $DB->count_records('role_assignments', array())); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); + cohort_remove_member($cohort1->id, $user2->id); + cohort_remove_member($cohort1->id, $user4->id); + $this->assertEquals(5, $DB->count_records('user_enrolments', array())); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id))); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id))); + $this->assertEquals(5, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + + cohort_remove_member($cohort2->id, $user3->id); + $this->assertEquals(3, $DB->count_records('user_enrolments', array())); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id))); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id))); + $this->assertEquals(3, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course2->id)->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id))); + + + // Test cohort deleting. + + cohort_add_member($cohort1->id, $user2->id); + cohort_add_member($cohort1->id, $user4->id); + cohort_add_member($cohort2->id, $user3->id); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertEquals(7, $DB->count_records('role_assignments', array())); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); + cohort_delete_cohort($cohort2); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertEquals(5, $DB->count_records('role_assignments', array())); + + $cohortinstance2 = $DB->get_record('enrol', array('id'=>$cohortinstance2->id), '*', MUST_EXIST); + $cohortinstance3 = $DB->get_record('enrol', array('id'=>$cohortinstance3->id), '*', MUST_EXIST); + + $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance2->status); + $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance3->status); + $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); + cohort_delete_cohort($cohort1); + $this->assertEquals(4, $DB->count_records('user_enrolments', array())); + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('enrol', array('id'=>$cohortinstance1->id))); + $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + + } + + public function test_sync_course() { + global $DB; + $this->resetAfterTest(); + + // Setup a few courses and categories. + + $cohortplugin = enrol_get_plugin('cohort'); + $manualplugin = enrol_get_plugin('manual'); + + $studentrole = $DB->get_record('role', array('shortname'=>'student')); + $this->assertNotEmpty($studentrole); + $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); + $this->assertNotEmpty($teacherrole); + $managerrole = $DB->get_record('role', array('shortname'=>'manager')); + $this->assertNotEmpty($managerrole); + + $cat1 = $this->getDataGenerator()->create_category(); + $cat2 = $this->getDataGenerator()->create_category(); + + $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); + $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); + $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); + $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); + $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); + + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $user3 = $this->getDataGenerator()->create_user(); + $user4 = $this->getDataGenerator()->create_user(); + + $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id)); + $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id)); + $cohort3 = $this->getDataGenerator()->create_cohort(); + + $this->disable_plugin(); // Prevents event sync. + + $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id); + $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id); + + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertEquals(2, $DB->count_records('user_enrolments', array())); + + $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id)); + $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id)); + + $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id)); + $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id)); + + $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id)); + $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id)); + + cohort_add_member($cohort1->id, $user1->id); + cohort_add_member($cohort1->id, $user2->id); + cohort_add_member($cohort1->id, $user4->id); + cohort_add_member($cohort2->id, $user3->id); + cohort_add_member($cohort3->id, $user3->id); + + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertEquals(2, $DB->count_records('user_enrolments', array())); + + + // Test sync of one course only. + + enrol_cohort_sync($course1->id, false); + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertEquals(2, $DB->count_records('user_enrolments', array())); + + + $this->enable_plugin(); + enrol_cohort_sync($course2->id, false); + $this->assertEquals(3, $DB->count_records('role_assignments', array())); + $this->assertEquals(3, $DB->count_records('user_enrolments', array())); + $DB->delete_records('cohort_members', array('cohortid'=>$cohort3->id)); // Use low level DB api to prevent events! + $DB->delete_records('cohort', array('id'=>$cohort3->id)); // Use low level DB api to prevent events! + + enrol_cohort_sync($course1->id, false); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id))); + $this->assertEquals(7, $DB->count_records('role_assignments', array())); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); + $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events! + enrol_cohort_sync($course1->id, false); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertEquals(6, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); + $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events! + enrol_cohort_sync($course1->id, false); + $this->assertEquals(5, $DB->count_records('user_enrolments', array())); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id))); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id))); + $this->assertEquals(5, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); + $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events! + $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events! + enrol_cohort_sync($course1->id, false); + $this->assertEquals(5, $DB->count_records('user_enrolments', array())); + $this->assertEquals(3, $DB->count_records('role_assignments', array())); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); + enrol_cohort_sync($course1->id, false); + $this->assertEquals(3, $DB->count_records('user_enrolments', array())); + $this->assertEquals(3, $DB->count_records('role_assignments', array())); + } + + public function test_sync_all_courses() { + global $DB; + + $this->resetAfterTest(); + + // Setup a few courses and categories. + + $cohortplugin = enrol_get_plugin('cohort'); + $manualplugin = enrol_get_plugin('manual'); + + $studentrole = $DB->get_record('role', array('shortname'=>'student')); + $this->assertNotEmpty($studentrole); + $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); + $this->assertNotEmpty($teacherrole); + $managerrole = $DB->get_record('role', array('shortname'=>'manager')); + $this->assertNotEmpty($managerrole); + + $cat1 = $this->getDataGenerator()->create_category(); + $cat2 = $this->getDataGenerator()->create_category(); + + $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); + $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); + $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); + $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); + $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST); + + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $user3 = $this->getDataGenerator()->create_user(); + $user4 = $this->getDataGenerator()->create_user(); + + $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id)); + $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id)); + $cohort3 = $this->getDataGenerator()->create_cohort(); + + $this->disable_plugin(); // Prevents event sync. + + $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id); + $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id); + + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertEquals(2, $DB->count_records('user_enrolments', array())); + + $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id)); + $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id)); + + $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id)); + $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id)); + + $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id)); + $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id)); + + cohort_add_member($cohort1->id, $user1->id); + cohort_add_member($cohort1->id, $user2->id); + cohort_add_member($cohort1->id, $user4->id); + cohort_add_member($cohort2->id, $user3->id); + cohort_add_member($cohort3->id, $user3->id); + + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertEquals(2, $DB->count_records('user_enrolments', array())); + + + // Test sync of one course only. + + enrol_cohort_sync(null, false); + $this->assertEquals(2, $DB->count_records('role_assignments', array())); + $this->assertEquals(2, $DB->count_records('user_enrolments', array())); + + + $this->enable_plugin(); + enrol_cohort_sync(null, false); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id))); + $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id))); + $this->assertEquals(7, $DB->count_records('role_assignments', array())); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); + $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events! + enrol_cohort_sync($course1->id, false); + $this->assertEquals(7, $DB->count_records('user_enrolments', array())); + $this->assertEquals(6, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); + $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events! + enrol_cohort_sync($course1->id, false); + $this->assertEquals(5, $DB->count_records('user_enrolments', array())); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id))); + $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id))); + $this->assertEquals(5, $DB->count_records('role_assignments', array())); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id))); + $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); + $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events! + $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events! + enrol_cohort_sync($course1->id, false); + $this->assertEquals(5, $DB->count_records('user_enrolments', array())); + $this->assertEquals(3, $DB->count_records('role_assignments', array())); + + $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); + enrol_cohort_sync($course1->id, false); + $this->assertEquals(3, $DB->count_records('user_enrolments', array())); + $this->assertEquals(3, $DB->count_records('role_assignments', array())); + + } +}