Skip to content

Commit

Permalink
MDL-48362 enrol_cohort: Fix unit test by manually triggering sync
Browse files Browse the repository at this point in the history
Sync is now automatically triggered by add/update instance. We need
to manually trigger sync before each part of the test to clean up
role assignments from the previous test.
  • Loading branch information
Damyon Wiese committed Mar 1, 2016
1 parent e3f24ac commit 74aece8
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions enrol/cohort/tests/sync_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function test_handler_sync() {
global $DB;

$this->resetAfterTest();
$trace = new null_progress_trace();

// Setup a few courses and categories.

Expand Down Expand Up @@ -191,6 +192,9 @@ public function test_handler_sync() {
$this->assertFalse($DB->record_exists('enrol', array('id'=>$cohortinstance1->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));

// Cleanup after previous test (remove the extra user_enrolment).
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));

// Test group sync.

Expand All @@ -203,7 +207,7 @@ public function test_handler_sync() {
$id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
$cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));

$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('role_assignments', array()));

$this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
Expand Down Expand Up @@ -241,15 +245,15 @@ public function test_handler_sync() {
cohort_add_member($cohort1->id, $user2->id);
cohort_add_member($cohort1->id, $user3->id);

$this->assertEquals(7, $DB->count_records('user_enrolments', array()));
$this->assertEquals(6, $DB->count_records('user_enrolments', array()));
$this->assertEquals(5, $DB->count_records('role_assignments', array()));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertEquals(5, $DB->count_records('groups_members', array()));
$this->assertEquals(3, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));

$cohortplugin->delete_instance($cohortinstance1);

$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('role_assignments', array()));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertEquals(2, $DB->count_records('groups_members', array()));
Expand Down Expand Up @@ -377,7 +381,11 @@ public function test_sync_course() {

// Test group sync.

$this->disable_plugin(); // No event sync
$this->disable_plugin(); // No event sync.

// Trigger sync to remove left over role assignments.
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(2, $DB->count_records('role_assignments', array()));

$id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
$group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
Expand All @@ -392,10 +400,10 @@ public function test_sync_course() {
$this->assertTrue(groups_add_member($group1, $user4));
$this->assertTrue(groups_add_member($group2, $user4));

$this->enable_plugin(); // No event sync
$this->enable_plugin(); // No event sync.

$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('role_assignments', array()));
$this->assertEquals(2, $DB->count_records('role_assignments', array()));

$this->assertFalse(groups_is_member($group1->id, $user1->id));
cohort_add_member($cohort1->id, $user1->id);
Expand All @@ -405,7 +413,8 @@ public function test_sync_course() {
enrol_cohort_sync($trace, $course1->id);

$this->assertEquals(7, $DB->count_records('user_enrolments', array()));
$this->assertEquals(7, $DB->count_records('role_assignments', array()));
// This used to be 7 - but now add_instance triggers an immediate sync.
$this->assertEquals(6, $DB->count_records('role_assignments', array()));

$this->assertTrue(groups_is_member($group1->id, $user1->id));
$this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
Expand Down Expand Up @@ -551,6 +560,9 @@ public function test_sync_all_courses() {
// Test group sync.

$this->disable_plugin(); // No event sync
// Trigger sync to remove extra role assignments.
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(2, $DB->count_records('role_assignments', array()));

$id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
$group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
Expand All @@ -567,7 +579,7 @@ public function test_sync_all_courses() {
$this->assertTrue(groups_add_member($group2, $user4));

$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('role_assignments', array()));
$this->assertEquals(2, $DB->count_records('role_assignments', array()));

$this->assertFalse(groups_is_member($group1->id, $user1->id));
cohort_add_member($cohort1->id, $user1->id);
Expand Down

0 comments on commit 74aece8

Please sign in to comment.