first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,112 @@
@enrol @enrol_cohort
Feature: Cohort enrolment management
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher001 | Teacher | 001 | teacher001@example.com |
And the following "cohorts" exist:
| name | idnumber | visible |
| Alpha1 | A1 | 1 |
| Beta2 | B1 | 1 |
And the following "courses" exist:
| fullname | shortname | format | startdate |
| Course 001 | C001 | weeks | ##1 month ago## |
And the following "course enrolments" exist:
| user | course | role | timestart |
| teacher001 | C001 | editingteacher | ##1 month ago## |
@javascript
Scenario: Add multiple cohorts to the course
When I log in as "teacher001"
And I am on the "Course 001" "enrolment methods" page
And I select "Cohort sync" from the "Add method" singleselect
And I open the autocomplete suggestions list
And I click on "Alpha1" item in the autocomplete list
And "Alpha1" "autocomplete_selection" should exist
And I click on "Beta2" item in the autocomplete list
And "Alpha1" "autocomplete_selection" should exist
And "Beta2" "autocomplete_selection" should exist
And I press "Add method"
Then I should see "Cohort sync (Beta2 - Student)"
And I should see "Cohort sync (Alpha1 - Student)"
@javascript
Scenario: Edit cohort enrolment
When I log in as "teacher001"
And I add "Cohort sync" enrolment method in "Course 001" with:
| Cohort | Alpha1 |
And I should see "Cohort sync (Alpha1 - Student)"
And I click on "Edit" "link" in the "Alpha1" "table_row"
And I set the field "Assign role" to "Non-editing teacher"
And I click on "Save" "button"
And I should see "Cohort sync (Alpha1 - Non-editing teacher)"
@javascript
Scenario: Course cohort enrolment sync cohorts members
Given the following "users" exist:
| username | firstname | lastname | email |
| s1 | Sandra | Cole | s1@example.com |
| s2 | John | Smith | s2@example.com |
| s4 | Jane | Doe | s4@example.com |
And the following "cohort members" exist:
| user | cohort |
| s1 | A1 |
| s2 | A1 |
When I log in as "teacher001"
And I add "Cohort sync" enrolment method in "Course 001" with:
| Cohort | A1 |
| customint2 | -1 |
Then I should see "Cohort sync (Alpha1 - Student)"
And I set the field "Participants tertiary navigation" to "Groups"
# Confirm that group was created and corresponding group members are present
And I set the field "groups[]" to "Alpha1 cohort (2)"
And the "members" select box should contain "Sandra Cole (s1@example.com)"
And the "members" select box should contain "John Smith (s2@example.com)"
And I log in as "admin"
And I navigate to "Users > Accounts > Cohorts" in site administration
And I press "Assign" action in the "Alpha1" report row
And I should see "Cohort 'Alpha1' members"
And I should see "Removing users from a cohort may result in unenrolling of users from multiple courses which includes deleting of user settings, grades, group membership and other user information from affected courses."
# Remove user s4 from cohort
And I set the field "removeselect[]" to "John Smith (s2@example.com)"
And I click on "Remove" "button"
# Add user s4 to the cohort.
And I set the field "addselect_searchtext" to "s4"
And I set the field "addselect[]" to "Jane Doe (s4@example.com)"
And I click on "Add" "button"
And the "removeselect[]" select box should contain "Sandra Cole (s1@example.com)"
And the "removeselect[]" select box should contain "Jane Doe (s4@example.com)"
And the "removeselect[]" select box should not contain "John Smith (s2@example.com)"
And I trigger cron
And I am on "Course 001" course homepage
And I navigate to course participants
# Verifies students 1 and 4 are in the cohort and student 2 is not any more.
And the following should exist in the "participants" table:
| First name | Email address | Roles | Groups |
| Sandra Cole | s1@example.com | Student | Alpha1 cohort |
| Jane Doe | s4@example.com | Student | Alpha1 cohort |
And the following should not exist in the "participants" table:
| First name | Email address | Roles | Groups |
| John Smith | s2@example.com | Student | Alpha1 cohort |
@javascript
Scenario: Course cohort enrolment creates a new group
Given the following "users" exist:
| username | firstname | lastname | email |
| s3 | Bianca | McAfee | s3@example.com |
| s5 | Abigail | Wyatt | s5@example.com |
And the following "cohort members" exist:
| user | cohort |
| s3 | B1 |
| s5 | B1 |
When I log in as "teacher001"
And I add "Cohort sync" enrolment method in "Course 001" with:
| Cohort | B1 |
And I click on "Edit" "link" in the "Beta2" "table_row"
And I set the field "Add to group" to "Create new group"
And I click on "Save changes" "button"
And I set the field "Participants tertiary navigation" to "Groups"
And I set the field "groups[]" to "Beta2 cohort (2)"
Then the "members" select box should contain "Bianca McAfee (s3@example.com)"
And the "members" select box should contain "Abigail Wyatt (s5@example.com)"
@@ -0,0 +1,87 @@
@enrol @enrol_cohort
Feature: Unenrol action to disable course enrolment
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher001 | Teacher | 001 | teacher001@example.com |
| student001 | Student | 001 | student001@example.com |
| student002 | Student | 002 | student002@example.com |
| student003 | Student | 003 | student003@example.com |
| student004 | Student | 004 | student004@example.com |
And the following "cohorts" exist:
| name | idnumber | visible |
| System cohort | CVO | 1 |
And the following "cohort members" exist:
| user | cohort |
| student001 | CVO |
| student002 | CVO |
| student003 | CVO |
| student004 | CVO |
And the following "courses" exist:
| fullname | shortname | format | startdate |
| Course 001 | C001 | weeks | ##1 month ago## |
And the following "course enrolments" exist:
| user | course | role | timestart |
| teacher001 | C001 | editingteacher | ##1 month ago## |
@javascript @skip_chrome_zerosize
Scenario: Removing the user from the cohort will suspend the enrolment but keep the role
When I log in as "teacher001"
And I am on the "Course 001" "enrolment methods" page
And I select "Cohort sync" from the "Add method" singleselect
And I open the autocomplete suggestions list
Then "System cohort" "autocomplete_suggestions" should exist
And I set the field "Cohort" to "System cohort"
And I press "Add method"
And I am on the "Course 001" "enrolled users" page
And I should see "student001@example.com"
And I should see "student002@example.com"
And I should see "student003@example.com"
And I should see "student004@example.com"
And I log out
When I log in as "admin"
Then I navigate to "Plugins > Enrolments > Cohort sync" in site administration
And I select "Disable course enrolment" from the "External unenrol action" singleselect
And I press "Save changes"
And I navigate to "Users > Accounts > Cohorts" in site administration
When I press "Assign" action in the "System cohort" report row
And I set the field "removeselect_searchtext" to "Student 001"
And I set the field "Current users" to "Student 001 (student001@example.com)"
And I wait "1" seconds
And I press "Remove"
And I am on "Course 001" course homepage
And I navigate to course participants
And I should see "Suspended" in the "Student 001" "table_row"
And I should see "Active" in the "Student 002" "table_row"
And I should see "Active" in the "Student 003" "table_row"
And I should see "Active" in the "Student 004" "table_row"
@javascript @skip_chrome_zerosize
Scenario: Deleting non-empty cohort will suspend the enrolment but keep the role
When I log in as "teacher001"
And I am on the "Course 001" "enrolment methods" page
And I select "Cohort sync" from the "Add method" singleselect
And I open the autocomplete suggestions list
Then "System cohort" "autocomplete_suggestions" should exist
And I set the field "Cohort" to "System cohort"
And I press "Add method"
And I am on the "Course 001" "enrolled users" page
And I should see "student001@example.com"
And I should see "student002@example.com"
And I should see "student003@example.com"
And I should see "student004@example.com"
And I log out
When I log in as "admin"
Then I navigate to "Plugins > Enrolments > Cohort sync" in site administration
And I select "Disable course enrolment" from the "External unenrol action" singleselect
And I press "Save changes"
And I navigate to "Users > Accounts > Cohorts" in site administration
When I press "Delete" action in the "System cohort" report row
And I press "Continue"
And I am on "Course 001" course homepage
And I navigate to course participants
And I should see "Suspended" in the "Student 001" "table_row"
And I should see "Suspended" in the "Student 002" "table_row"
And I should see "Suspended" in the "Student 003" "table_row"
And I should see "Suspended" in the "Student 004" "table_row"
+481
View File
@@ -0,0 +1,481 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace enrol_cohort;
use core\plugininfo\enrol;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/cohort/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
/**
* Contains tests for the cohort library.
*
* @package enrol_cohort
* @category test
* @copyright 2015 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lib_test extends \advanced_testcase {
/**
* Test that a new group with the name of the cohort is created.
*/
public function test_enrol_cohort_create_new_group(): void {
global $DB;
$this->resetAfterTest();
// Create a category.
$category = $this->getDataGenerator()->create_category();
// Create two courses.
$course = $this->getDataGenerator()->create_course(array('category' => $category->id));
$course2 = $this->getDataGenerator()->create_course(array('category' => $category->id));
// Create a cohort.
$cohort = $this->getDataGenerator()->create_cohort(array('context' => \context_coursecat::instance($category->id)->id));
// Run the function.
$groupid = enrol_cohort_create_new_group($course->id, $cohort->id);
// Check the results.
$group = $DB->get_record('groups', array('id' => $groupid));
// The group name should match the cohort name.
$this->assertEquals($cohort->name . ' cohort', $group->name);
// Group course id should match the course id.
$this->assertEquals($course->id, $group->courseid);
// Create a group that will have the same name as the cohort.
$groupdata = new \stdClass();
$groupdata->courseid = $course2->id;
$groupdata->name = $cohort->name . ' cohort';
groups_create_group($groupdata);
// Create a group for the cohort in course 2.
$groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
$groupinfo = $DB->get_record('groups', array('id' => $groupid));
// Check that the group name has been changed.
$this->assertEquals($cohort->name . ' cohort (2)', $groupinfo->name);
// Create another group that will have the same name as a generated cohort.
$groupdata = new \stdClass();
$groupdata->courseid = $course2->id;
$groupdata->name = $cohort->name . ' cohort (2)';
groups_create_group($groupdata);
// Create a group for the cohort in course 2.
$groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
$groupinfo = $DB->get_record('groups', array('id' => $groupid));
// Check that the group name has been changed.
$this->assertEquals($cohort->name . ' cohort (3)', $groupinfo->name);
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions(): void {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'cohort';
// Only enable the cohort enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Teachers don't have enrol/cohort:unenrol capability by default. Login as admin for simplicity.
$this->setAdminUser();
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new \course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Cohort-sync has no enrol actions for active students.
$this->assertCount(0, $actions);
// Enrol actions for a suspended student.
// Suspend the student.
$ue->status = ENROL_USER_SUSPENDED;
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Cohort-sync has enrol actions for suspended students -- unenrol.
$this->assertCount(1, $actions);
}
public function test_enrol_cohort_unenrolaction_suspend_only(): void {
global $CFG, $DB, $PAGE;
$this->resetAfterTest();
$trace = new \null_progress_trace();
$cohortplugin = enrol_get_plugin('cohort');
$cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->assertNotEmpty($studentrole);
// Setup a test course.
$course = $this->getDataGenerator()->create_course();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$cohort = $this->getDataGenerator()->create_cohort();
$cohortplugin->add_instance($course, ['customint1' => $cohort->id,
'roleid' => $studentrole->id]
);
cohort_add_member($cohort->id, $user1->id);
cohort_add_member($cohort->id, $user2->id);
cohort_add_member($cohort->id, $user3->id);
cohort_add_member($cohort->id, $user4->id);
// Test sync.
enrol_cohort_sync($trace, $course->id);
// All users should be enrolled.
$this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
$this->assertTrue(is_enrolled(\context_course::instance($course->id), $user2));
$this->assertTrue(is_enrolled(\context_course::instance($course->id), $user3));
$this->assertTrue(is_enrolled(\context_course::instance($course->id), $user4));
// Remove cohort member.
cohort_remove_member($cohort->id, $user1->id);
$this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
// Run the sync again.
enrol_cohort_sync($trace, $course->id);
$enrolid = $DB->get_field('enrol', 'id', ['enrol' => 'cohort', 'customint1' => $cohort->id]);
$ue = $DB->get_record('user_enrolments', ['enrolid' => $enrolid, 'userid' => $user1->id]);
// Check user is suspended.
$this->assertEquals($ue->status, ENROL_USER_SUSPENDED);
// Check that user4 still have student role.
$userrole = $DB->get_record('role_assignments', ['userid' => $user1->id]);
$this->assertNotEmpty($userrole);
$this->assertEquals($studentrole->id, $userrole->roleid);
// Delete the cohort.
cohort_delete_cohort($cohort);
// Run the sync again.
enrol_cohort_sync($trace, $course->id);
$ue = $DB->get_records('user_enrolments', ['enrolid' => $enrolid], '', 'userid, status, enrolid');
// Check users are suspended.
$this->assertEquals($ue[$user2->id]->status, ENROL_USER_SUSPENDED);
$this->assertEquals($ue[$user3->id]->status, ENROL_USER_SUSPENDED);
$this->assertEquals($ue[$user4->id]->status, ENROL_USER_SUSPENDED);
// Check that users still have student role.
$usersrole = $DB->get_records('role_assignments', ['itemid' => $enrolid], '', 'userid, roleid');
$this->assertNotEmpty($usersrole);
$this->assertEquals($studentrole->id, $usersrole[$user2->id]->roleid);
$this->assertEquals($studentrole->id, $usersrole[$user3->id]->roleid);
$this->assertEquals($studentrole->id, $usersrole[$user4->id]->roleid);
}
/**
* Test the behaviour of validate_plugin_data_context().
*
* @covers ::validate_plugin_data_context
*/
public function test_validate_plugin_data_context(): void {
$this->resetAfterTest();
$cohortplugin = enrol_get_plugin('cohort');
$cat = $this->getDataGenerator()->create_category();
$cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
$cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
$course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);
$cohort1 = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat1->id)->id,
'idnumber' => 'one',
]);
$cohort2 = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat2->id)->id,
'idnumber' => 'two',
]);
$enrolmentdata = [
'customint1' => $cohort2->id,
'cohortidnumber' => $cohort2->idnumber,
];
$error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
$this->assertInstanceOf('lang_string', $error);
$this->assertEquals('contextcohortnotallowed', $error->get_identifier());
$enrolmentdata = [
'customint1' => $cohort1->id,
'cohortidnumber' => $cohort1->idnumber,
'courseid' => $course->id,
'id' => null,
'status' => ENROL_INSTANCE_ENABLED,
];
$enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
$error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
$this->assertNull($error);
}
/**
* Test the behaviour of fill_enrol_custom_fields().
*
* @covers ::fill_enrol_custom_fields
*/
public function test_fill_enrol_custom_fields(): void {
$this->resetAfterTest();
$cohortplugin = enrol_get_plugin('cohort');
$cat = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
$cohort = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat->id)->id,
'idnumber' => 'one',
]);
$group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
$enrolmentdata['cohortidnumber'] = $cohort->idnumber;
$enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
$this->assertArrayHasKey('customint1', $enrolmentdata);
$this->assertEquals($cohort->id, $enrolmentdata['customint1']);
$this->assertArrayNotHasKey('customint2', $enrolmentdata);
$enrolmentdata['cohortidnumber'] = 'notexist';
$enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
$this->assertArrayHasKey('customint1', $enrolmentdata);
$this->assertFalse($enrolmentdata['customint1']);
$this->assertArrayNotHasKey('customint2', $enrolmentdata);
$enrolmentdata['cohortidnumber'] = $cohort->idnumber;
$enrolmentdata['addtogroup'] = COHORT_NOGROUP;
$enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
$this->assertArrayHasKey('customint1', $enrolmentdata);
$this->assertEquals($cohort->id, $enrolmentdata['customint1']);
$this->assertArrayHasKey('customint2', $enrolmentdata);
$this->assertEquals(COHORT_NOGROUP, $enrolmentdata['customint2']);
unset($enrolmentdata['addtogroup']);
$enrolmentdata['groupname'] = $group->name;
$enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
$this->assertArrayHasKey('customint1', $enrolmentdata);
$this->assertEquals($cohort->id, $enrolmentdata['customint1']);
$this->assertArrayHasKey('customint2', $enrolmentdata);
$this->assertEquals($group->id, $enrolmentdata['customint2']);
$enrolmentdata['groupname'] = 'notexist';
$enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
$this->assertArrayHasKey('customint1', $enrolmentdata);
$this->assertEquals($cohort->id, $enrolmentdata['customint1']);
$this->assertArrayHasKey('customint2', $enrolmentdata);
$this->assertFalse($enrolmentdata['customint2']);
}
/**
* Test the behaviour of validate_enrol_plugin_data().
*
* @covers ::validate_enrol_plugin_data
*/
public function test_validate_enrol_plugin_data(): void {
$this->resetAfterTest();
$this->setAdminUser();
$cat = $this->getDataGenerator()->create_category();
$cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
$cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
$course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);
$group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Group 1']);
$cohort1 = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat1->id)->id,
'idnumber' => 'one',
]);
$cohort2 = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat2->id)->id,
'idnumber' => 'two',
]);
enrol::enable_plugin('cohort', false);
$cohortplugin = enrol_get_plugin('cohort');
// Plugin is disabled in system and cohort name is missing in csv.
$enrolmentdata = [];
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertArrayHasKey('plugindisabled', $errors);
$this->assertArrayHasKey('missingmandatoryfields', $errors);
enrol::enable_plugin('cohort', true);
// Unknown cohort idnumber and missing role.
$enrolmentdata['cohortidnumber'] = 'test';
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertArrayHasKey('missingmandatoryfields', $errors);
$this->assertArrayHasKey('unknowncohort', $errors);
// Non-valid 'addtogroup' option.
$enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
$enrolmentdata['addtogroup'] = 2;
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('erroraddtogroup', $errors);
// Options 'addtogroup' and 'groupname' are not allowed together.
$enrolmentdata['addtogroup'] = 0;
$enrolmentdata['groupname'] = 'test';
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('erroraddtogroupgroupname', $errors);
// Cohort is not allowed on a given category context.
$enrolmentdata['cohortidnumber'] = $cohort2->idnumber;
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('contextnotallowed', $errors);
// Group does not exist.
unset($enrolmentdata['addtogroup']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('errorinvalidgroup', $errors);
// Unknown role.
$enrolmentdata['role'] = 'test';
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertArrayHasKey('unknownrole', $errors);
// Valid data when trying to create a group.
$enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
$enrolmentdata['role'] = 'student';
$enrolmentdata['addtogroup'] = 1;
unset($enrolmentdata['groupname']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertEmpty($errors);
// Valid data when trying to add to existing group.
$enrolmentdata['groupname'] = $group1->name;
unset($enrolmentdata['addtogroup']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertEmpty($errors);
// Valid data when trying without group mode.
$enrolmentdata['addtogroup'] = 0;
unset($enrolmentdata['groupname']);
$errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
$this->assertEmpty($errors);
}
/**
* Test the behaviour of find_instance().
*
* @covers ::find_instance
*/
public function test_find_instance(): void {
global $DB;
$this->resetAfterTest();
$cat = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
$cohort1 = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat->id)->id,
'idnumber' => 'one',
]);
$cohort2 = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat->id)->id,
'idnumber' => 'two',
]);
$cohort3 = $this->getDataGenerator()->create_cohort([
'contextid' => \context_coursecat::instance($cat->id)->id,
'idnumber' => 'three',
]);
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
$managerrole = $DB->get_record('role', ['shortname' => 'manager']);
$cohortplugin = enrol_get_plugin('cohort');
// Add three cohort enrol instances.
$instanceid1 = $cohortplugin->add_instance($course, ['customint1' => $cohort1->id, 'roleid' => $teacherrole->id]);
$instanceid2 = $cohortplugin->add_instance($course, ['customint1' => $cohort2->id, 'roleid' => $managerrole->id]);
$instanceid3 = $cohortplugin->add_instance($course, ['customint1' => $cohort2->id, 'roleid' => $studentrole->id]);
$instance1 = $DB->get_record('enrol', ['id' => $instanceid1]);
$instance2 = $DB->get_record('enrol', ['id' => $instanceid2]);
$enrolmentdata = [];
$instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
$this->assertNull($instance);
// Unknown idnumber.
$enrolmentdata['cohortidnumber'] = 'test';
$instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
$this->assertNull($instance);
// Unknown role.
$enrolmentdata['role'] = 'test';
$enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
$instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
$this->assertNull($instance);
// Cohort3 instance has not matching role and cohort.
$enrolmentdata['role'] = $teacherrole->shortname;
$enrolmentdata['cohortidnumber'] = $cohort3->idnumber;
$instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
$this->assertNull($instance);
// Cohort2 instance has matching cohort, but not matching role.
$enrolmentdata['role'] = $teacherrole->shortname;
$enrolmentdata['cohortidnumber'] = $cohort2->idnumber;
$instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
$this->assertNull($instance);
$enrolmentdata['role'] = $teacherrole->shortname;
$enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
$instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
$this->assertEquals($instance1->id, $instance->id);
$enrolmentdata['role'] = $managerrole->shortname;
$enrolmentdata['cohortidnumber'] = $cohort2->idnumber;
$instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
$this->assertEquals($instance2->id, $instance->id);
}
}
@@ -0,0 +1,395 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Base class for unit tests for enrol_cohort.
*
* @package enrol_cohort
* @category test
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_cohort\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\request\writer;
use core_privacy\local\request\approved_contextlist;
use enrol_cohort\privacy\provider;
/**
* Unit tests for the enrol_cohort implementation of the privacy API.
*
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends \core_privacy\tests\provider_testcase {
/**
* Test getting the context for the user ID related to this plugin.
*/
public function test_get_contexts_for_userid(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
$cohortplugin = enrol_get_plugin('cohort');
$user1 = $this->getDataGenerator()->create_user();
$cat1 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$cohort1 = $this->getDataGenerator()->create_cohort(
array('contextid' => \context_coursecat::instance($cat1->id)->id));
$cohortplugin->add_instance($course1, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group1->id)
);
cohort_add_member($cohort1->id, $user1->id);
enrol_cohort_sync($trace, $course1->id);
// Check if user1 is enrolled into course1 in group 1.
$this->assertEquals(1, $DB->count_records('role_assignments', array()));
$this->assertTrue($DB->record_exists('groups_members', array(
'groupid' => $group1->id,
'userid' => $user1->id,
'component' => 'enrol_cohort')
));
// Check context course fro provider to user1.
$context = \context_course::instance($course1->id);
$contextlist = provider::get_contexts_for_userid($user1->id);
$this->assertEquals($context->id, $contextlist->current()->id);
}
/**
* Test that user data is exported correctly.
*/
public function test_export_user_data(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
$cohortplugin = enrol_get_plugin('cohort');
$user1 = $this->getDataGenerator()->create_user();
$cat1 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$cohort1 = $this->getDataGenerator()->create_cohort(
array('contextid' => \context_coursecat::instance($cat1->id)->id));
$cohortplugin->add_instance($course1, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group1->id)
);
cohort_add_member($cohort1->id, $user1->id);
enrol_cohort_sync($trace, $course1->id);
// Check if user1 is enrolled into course1 in group 1.
$this->assertEquals(1, $DB->count_records('role_assignments', array()));
$this->assertTrue($DB->record_exists('groups_members', array(
'groupid' => $group1->id,
'userid' => $user1->id,
'component' => 'enrol_cohort')
));
$this->setUser($user1);
$contextlist = provider::get_contexts_for_userid($user1->id);
$approvedcontextlist = new approved_contextlist($user1, 'enrol_cohort', $contextlist->get_contextids());
provider::export_user_data($approvedcontextlist);
foreach ($contextlist as $context) {
/** @var \core_privacy\tests\request\content_writer $writer */
$writer = writer::with_context($context);
$data = $writer->get_data([
get_string('pluginname', 'enrol_cohort'),
get_string('groups', 'core_group')
]);
$this->assertTrue($writer->has_any_data());
if ($context->contextlevel == CONTEXT_COURSE) {
$exportedgroups = $data->groups;
// User1 only belongs to group1 via enrol_cohort.
$this->assertCount(1, $exportedgroups);
$exportedgroup = reset($exportedgroups);
$this->assertEquals($group1->name, $exportedgroup->name);
}
}
}
/**
* Test for provider::delete_data_for_all_users_in_context().
*/
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
$cohortplugin = enrol_get_plugin('cohort');
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$cat1 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$cohort1 = $this->getDataGenerator()->create_cohort(
array('contextid' => \context_coursecat::instance($cat1->id)->id));
$cohortplugin->add_instance($course1, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group1->id)
);
cohort_add_member($cohort1->id, $user1->id);
cohort_add_member($cohort1->id, $user2->id);
cohort_add_member($cohort1->id, $user3->id);
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(
3,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
$coursecontext1 = \context_course::instance($course1->id);
provider::delete_data_for_all_users_in_context($coursecontext1);
$this->assertEquals(
0,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
}
/**
* Test for provider::delete_data_for_user().
*/
public function test_delete_data_for_user(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
$cohortplugin = enrol_get_plugin('cohort');
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$cat1 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$cohort1 = $this->getDataGenerator()->create_cohort(
array('contextid' => \context_coursecat::instance($cat1->id)->id));
$cohortplugin->add_instance($course1, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group1->id)
);
$cohortplugin->add_instance($course2, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group2->id)
);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
$this->getDataGenerator()->enrol_user($user3->id, $course1->id);
$this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
cohort_add_member($cohort1->id, $user1->id);
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(
3,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
$this->assertEquals(
1,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
$this->setUser($user1);
$coursecontext1 = \context_course::instance($course1->id);
$coursecontext2 = \context_course::instance($course2->id);
$approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'enrol_cohort',
[$coursecontext1->id, $coursecontext2->id]);
provider::delete_data_for_user($approvedcontextlist);
// Check we have 2 users in groups because we are deleted user1.
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
// Check we have not users in groups.
$this->assertEquals(
0,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
}
/**
* Test for provider::delete_data_for_users().
*/
public function test_delete_data_for_users(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
$cohortplugin = enrol_get_plugin('cohort');
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$cat1 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$cohort1 = $this->getDataGenerator()->create_cohort(
array('contextid' => \context_coursecat::instance($cat1->id)->id));
$cohortplugin->add_instance($course1, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group1->id)
);
$cohortplugin->add_instance($course2, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group2->id)
);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
$this->getDataGenerator()->enrol_user($user3->id, $course1->id);
$this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
$this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
cohort_add_member($cohort1->id, $user1->id);
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(
3,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
$this->assertEquals(
1,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
$coursecontext1 = \context_course::instance($course1->id);
$approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_cohort',
[$user1->id, $user2->id]);
provider::delete_data_for_users($approveduserlist);
// Check we have 2 users in groups because we have deleted user1.
// User2's membership is manual and is not as the result of a cohort enrolment.
$this->assertEquals(
2,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course1->id])
);
// Check that course2 is not touched.
$this->assertEquals(
1,
$DB->count_records_sql("SELECT COUNT(gm.id)
FROM {groups_members} gm
JOIN {groups} g ON gm.groupid = g.id
WHERE g.courseid = ?", [$course2->id])
);
}
/**
* Test for provider::get_users_in_context().
*/
public function test_get_users_in_context(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
$cohortplugin = enrol_get_plugin('cohort');
$cat1 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$cohort1 = $this->getDataGenerator()->create_cohort(
array('contextid' => \context_coursecat::instance($cat1->id)->id));
$cohortplugin->add_instance($course1, array(
'customint1' => $cohort1->id,
'roleid' => $studentrole->id,
'customint2' => $group1->id)
);
$user1 = $this->getDataGenerator()->create_user();
cohort_add_member($cohort1->id, $user1->id);
enrol_cohort_sync($trace, $course1->id);
// Check if user1 is enrolled into course1 in group 1.
$this->assertEquals(1, $DB->count_records('role_assignments', array()));
$this->assertTrue($DB->record_exists('groups_members', array(
'groupid' => $group1->id,
'userid' => $user1->id,
'component' => 'enrol_cohort')
));
$context = \context_course::instance($course1->id);
$userlist = new \core_privacy\local\request\userlist($context, 'enrol_cohort');
\enrol_cohort\privacy\provider::get_users_in_context($userlist);
$this->assertEquals([$user1->id], $userlist->get_userids());
}
}
+749
View File
@@ -0,0 +1,749 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace enrol_cohort;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
require_once($CFG->dirroot.'/group/lib.php');
/**
* Cohort enrolment sync functional test.
*
* @package enrol_cohort
* @category test
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sync_test 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(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
// 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();
$user5 = $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();
$cohort4 = $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));
$id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id, 'status' => ENROL_INSTANCE_DISABLED));
$cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
$id = $cohortplugin->add_instance($course3, array('customint1' => $cohort4->id, 'roleid' => $studentrole->id));
$cohortinstance5 = $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->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance4->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);
cohort_add_member($cohort4->id, $user5->id);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $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(8, $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'=>$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)));
$this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
array('userid' => $user2->id, 'enrolid' => $cohortinstance1->id)));
$this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
array('userid' => $user4->id, 'enrolid' => $cohortinstance1->id)));
cohort_add_member($cohort1->id, $user2->id);
cohort_add_member($cohort1->id, $user4->id);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $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_SUSPEND);
cohort_remove_member($cohort1->id, $user2->id);
cohort_remove_member($cohort1->id, $user4->id);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $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)));
$this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
array('userid' => $user2->id, 'enrolid' => $cohortinstance1->id)));
$this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
array('userid' => $user4->id, 'enrolid' => $cohortinstance1->id)));
cohort_add_member($cohort1->id, $user2->id);
cohort_add_member($cohort1->id, $user4->id);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $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(6, $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(6, $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(4, $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(4, $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(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $DB->count_records('role_assignments', array()));
$cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
cohort_delete_cohort($cohort4);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $DB->count_records('role_assignments', array()));
$cohortinstance5 = $DB->get_record('enrol', array('id' => $cohortinstance5->id), '*', MUST_EXIST);
$this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance5->status);
$cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
cohort_delete_cohort($cohort2);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(6, $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(5, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $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)));
// Cleanup after previous test (remove the extra user_enrolment).
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
// Test group sync.
$id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
$group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
$id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
$group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
$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('role_assignments', array()));
$this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4));
$this->assertTrue(groups_add_member($group1, $user4));
$this->assertTrue(groups_add_member($group2, $user4));
$this->assertFalse(groups_is_member($group1->id, $user1->id));
cohort_add_member($cohort1->id, $user1->id);
$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)));
cohort_add_member($cohort1->id, $user4->id);
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
cohort_remove_member($cohort1->id, $user1->id);
$this->assertFalse(groups_is_member($group1->id, $user1->id));
cohort_remove_member($cohort1->id, $user4->id);
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertTrue(groups_is_member($group2->id, $user4->id));
$cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
cohort_add_member($cohort1->id, $user1->id);
cohort_remove_member($cohort1->id, $user1->id);
$this->assertTrue(groups_is_member($group1->id, $user1->id));
$cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
cohort_add_member($cohort1->id, $user1->id);
cohort_remove_member($cohort1->id, $user1->id);
$this->assertTrue(groups_is_member($group1->id, $user1->id));
// Test deleting of instances.
cohort_add_member($cohort1->id, $user1->id);
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('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('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()));
$this->assertEquals(0, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
}
public function test_sync_course(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
// 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();
$user5 = $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();
$cohort4 = $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));
$id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id, 'status' => ENROL_INSTANCE_DISABLED));
$cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
$id = $cohortplugin->add_instance($course3, array('customint1' => $cohort4->id, 'roleid' => $studentrole->id));
$cohortinstance5 = $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);
cohort_add_member($cohort4->id, $user5->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($trace, $course1->id);
$this->assertEquals(2, $DB->count_records('role_assignments', array()));
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->enable_plugin();
enrol_cohort_sync($trace, $course2->id);
$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($trace, $course1->id);
$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->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance4->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_SUSPEND);
// Use low level DB api to prevent events!
$DB->delete_records('cohort_members', array('cohortid' => $cohort2->id, 'userid' => $user3->id));
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->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)));
cohort_add_member($cohort2->id, $user3->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($trace, $course1->id);
$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($trace, $course1->id);
$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_SUSPEND);
$DB->delete_records('cohort_members', array('cohortid' => $cohort4->id));
$DB->delete_records('cohort', array('id' => $cohort4->id));
enrol_cohort_sync($trace, $course3->id);
$this->assertEquals(5, $DB->count_records('user_enrolments', array()));
$this->assertEquals(5, $DB->count_records('role_assignments', array()));
$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($trace, $course1->id);
$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($trace, $course1->id);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('role_assignments', array()));
// Test group 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);
$id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
$group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
$id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
$cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
$this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4));
$this->assertTrue(groups_add_member($group1, $user4));
$this->assertTrue(groups_add_member($group2, $user4));
$this->enable_plugin(); // No event sync.
$this->assertEquals(3, $DB->count_records('user_enrolments', 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);
cohort_add_member($cohort1->id, $user4->id);
cohort_add_member($cohort2->id, $user4->id);
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(7, $DB->count_records('user_enrolments', 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)));
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$cohortinstance1->customint2 = $group2->id;
$DB->update_record('enrol', $cohortinstance1);
enrol_cohort_sync($trace, $course1->id);
$this->assertFalse(groups_is_member($group1->id, $user1->id));
$this->assertTrue(groups_is_member($group2->id, $user1->id));
$this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertTrue(groups_is_member($group2->id, $user4->id));
$this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
cohort_remove_member($cohort1->id, $user1->id);
$this->assertFalse(groups_is_member($group1->id, $user1->id));
cohort_remove_member($cohort1->id, $user4->id);
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertTrue(groups_is_member($group2->id, $user4->id));
}
public function test_sync_all_courses(): void {
global $DB;
$this->resetAfterTest();
$trace = new \null_progress_trace();
// 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();
$user5 = $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();
$cohort4 = $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));
$id = $cohortplugin->add_instance($course3, array('customint1' => $cohort4->id, 'roleid' => $studentrole->id));
$cohortinstance4 = $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);
cohort_add_member($cohort4->id, $user5->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($trace, null);
$this->assertEquals(2, $DB->count_records('role_assignments', array()));
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->enable_plugin();
enrol_cohort_sync($trace, null);
$this->assertEquals(8, $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(8, $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_SUSPEND);
$DB->delete_records('cohort_members', array('cohortid' => $cohort2->id, 'userid' => $user3->id));
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $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)));
cohort_add_member($cohort2->id, $user3->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($trace, $course1->id);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(7, $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($trace, $course1->id);
$this->assertEquals(6, $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(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)));
$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_SUSPEND);
$DB->delete_records('cohort_members', array('cohortid' => $cohort4->id)); // Use low level DB api to prevent events!
$DB->delete_records('cohort', array('id' => $cohort4->id)); // Use low level DB api to prevent events!
enrol_cohort_sync($trace, $course3->id);
$this->assertEquals(6, $DB->count_records('user_enrolments', array()));
$this->assertEquals(6, $DB->count_records('role_assignments', array()));
$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($trace, $course1->id);
$this->assertEquals(6, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('role_assignments', array()));
$cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
enrol_cohort_sync($trace, $course1->id);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('role_assignments', array()));
// 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);
$id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
$group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
$id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course2->id));
$group3 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
$id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
$cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
$this->assertTrue(groups_add_member($group1, $user4));
$this->assertTrue(groups_add_member($group2, $user4));
$this->assertEquals(4, $DB->count_records('user_enrolments', 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);
cohort_add_member($cohort1->id, $user4->id);
cohort_add_member($cohort2->id, $user4->id);
cohort_add_member($cohort2->id, $user3->id);
$this->enable_plugin();
enrol_cohort_sync($trace, null);
$this->assertEquals(8, $DB->count_records('user_enrolments', array()));
$this->assertEquals(8, $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)));
$this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4));
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertTrue(is_enrolled(\context_course::instance($course2->id), $user3));
$this->assertFalse(groups_is_member($group3->id, $user3->id));
$cohortinstance1->customint2 = $group2->id;
$DB->update_record('enrol', $cohortinstance1);
$cohortinstance3->customint2 = $group3->id;
$DB->update_record('enrol', $cohortinstance3);
enrol_cohort_sync($trace, null);
$this->assertFalse(groups_is_member($group1->id, $user1->id));
$this->assertTrue(groups_is_member($group2->id, $user1->id));
$this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertTrue(groups_is_member($group2->id, $user4->id));
$this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
$this->assertTrue(groups_is_member($group3->id, $user3->id));
$this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group3->id, 'userid'=>$user3->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
cohort_remove_member($cohort1->id, $user1->id);
$this->assertFalse(groups_is_member($group1->id, $user1->id));
cohort_remove_member($cohort1->id, $user4->id);
$this->assertTrue(groups_is_member($group1->id, $user4->id));
$this->assertTrue(groups_is_member($group2->id, $user4->id));
}
}