Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace enrol_cohort;
  18  
  19  use core\plugininfo\enrol;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot.'/cohort/lib.php');
  25  require_once($CFG->dirroot.'/group/lib.php');
  26  
  27  /**
  28   * Contains tests for the cohort library.
  29   *
  30   * @package   enrol_cohort
  31   * @category  test
  32   * @copyright 2015 Adrian Greeve <adrian@moodle.com>
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class lib_test extends \advanced_testcase {
  36  
  37      /**
  38       * Test that a new group with the name of the cohort is created.
  39       */
  40      public function test_enrol_cohort_create_new_group() {
  41          global $DB;
  42          $this->resetAfterTest();
  43          // Create a category.
  44          $category = $this->getDataGenerator()->create_category();
  45          // Create two courses.
  46          $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
  47          $course2 = $this->getDataGenerator()->create_course(array('category' => $category->id));
  48          // Create a cohort.
  49          $cohort = $this->getDataGenerator()->create_cohort(array('context' => \context_coursecat::instance($category->id)->id));
  50          // Run the function.
  51          $groupid = enrol_cohort_create_new_group($course->id, $cohort->id);
  52          // Check the results.
  53          $group = $DB->get_record('groups', array('id' => $groupid));
  54          // The group name should match the cohort name.
  55          $this->assertEquals($cohort->name . ' cohort', $group->name);
  56          // Group course id should match the course id.
  57          $this->assertEquals($course->id, $group->courseid);
  58  
  59          // Create a group that will have the same name as the cohort.
  60          $groupdata = new \stdClass();
  61          $groupdata->courseid = $course2->id;
  62          $groupdata->name = $cohort->name . ' cohort';
  63          groups_create_group($groupdata);
  64          // Create a group for the cohort in course 2.
  65          $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
  66          $groupinfo = $DB->get_record('groups', array('id' => $groupid));
  67          // Check that the group name has been changed.
  68          $this->assertEquals($cohort->name . ' cohort (2)', $groupinfo->name);
  69  
  70          // Create another group that will have the same name as a generated cohort.
  71          $groupdata = new \stdClass();
  72          $groupdata->courseid = $course2->id;
  73          $groupdata->name = $cohort->name . ' cohort (2)';
  74          groups_create_group($groupdata);
  75          // Create a group for the cohort in course 2.
  76          $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
  77          $groupinfo = $DB->get_record('groups', array('id' => $groupid));
  78          // Check that the group name has been changed.
  79          $this->assertEquals($cohort->name . ' cohort (3)', $groupinfo->name);
  80  
  81      }
  82  
  83      /**
  84       * Test for getting user enrolment actions.
  85       */
  86      public function test_get_user_enrolment_actions() {
  87          global $CFG, $PAGE;
  88          $this->resetAfterTest();
  89  
  90          // Set page URL to prevent debugging messages.
  91          $PAGE->set_url('/enrol/editinstance.php');
  92  
  93          $pluginname = 'cohort';
  94  
  95          // Only enable the cohort enrol plugin.
  96          $CFG->enrol_plugins_enabled = $pluginname;
  97  
  98          $generator = $this->getDataGenerator();
  99  
 100          // Get the enrol plugin.
 101          $plugin = enrol_get_plugin($pluginname);
 102  
 103          // Create a course.
 104          $course = $generator->create_course();
 105          // Enable this enrol plugin for the course.
 106          $plugin->add_instance($course);
 107  
 108          // Create a student.
 109          $student = $generator->create_user();
 110          // Enrol the student to the course.
 111          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 112  
 113          // Teachers don't have enrol/cohort:unenrol capability by default. Login as admin for simplicity.
 114          $this->setAdminUser();
 115          require_once($CFG->dirroot . '/enrol/locallib.php');
 116          $manager = new \course_enrolment_manager($PAGE, $course);
 117  
 118          $userenrolments = $manager->get_user_enrolments($student->id);
 119          $this->assertCount(1, $userenrolments);
 120  
 121          $ue = reset($userenrolments);
 122          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 123          // Cohort-sync has no enrol actions for active students.
 124          $this->assertCount(0, $actions);
 125  
 126          // Enrol actions for a suspended student.
 127          // Suspend the student.
 128          $ue->status = ENROL_USER_SUSPENDED;
 129  
 130          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 131          // Cohort-sync has enrol actions for suspended students -- unenrol.
 132          $this->assertCount(1, $actions);
 133      }
 134  
 135      public function test_enrol_cohort_unenrolaction_suspend_only() {
 136          global $CFG, $DB, $PAGE;
 137          $this->resetAfterTest();
 138  
 139          $trace = new \null_progress_trace();
 140  
 141          $cohortplugin = enrol_get_plugin('cohort');
 142          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 143  
 144          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 145          $this->assertNotEmpty($studentrole);
 146  
 147          // Setup a test course.
 148          $course = $this->getDataGenerator()->create_course();
 149  
 150          $user1 = $this->getDataGenerator()->create_user();
 151          $user2 = $this->getDataGenerator()->create_user();
 152          $user3 = $this->getDataGenerator()->create_user();
 153          $user4 = $this->getDataGenerator()->create_user();
 154  
 155          $cohort = $this->getDataGenerator()->create_cohort();
 156  
 157          $cohortplugin->add_instance($course, ['customint1' => $cohort->id,
 158              'roleid' => $studentrole->id]
 159          );
 160  
 161          cohort_add_member($cohort->id, $user1->id);
 162          cohort_add_member($cohort->id, $user2->id);
 163          cohort_add_member($cohort->id, $user3->id);
 164          cohort_add_member($cohort->id, $user4->id);
 165  
 166          // Test sync.
 167          enrol_cohort_sync($trace, $course->id);
 168  
 169          // All users should be enrolled.
 170          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
 171          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user2));
 172          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user3));
 173          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user4));
 174  
 175          // Remove cohort member.
 176          cohort_remove_member($cohort->id, $user1->id);
 177          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
 178  
 179          // Run the sync again.
 180          enrol_cohort_sync($trace, $course->id);
 181  
 182          $enrolid = $DB->get_field('enrol', 'id', ['enrol' => 'cohort', 'customint1' => $cohort->id]);
 183          $ue = $DB->get_record('user_enrolments', ['enrolid' => $enrolid, 'userid' => $user1->id]);
 184  
 185          // Check user is suspended.
 186          $this->assertEquals($ue->status, ENROL_USER_SUSPENDED);
 187          // Check that user4 still have student role.
 188          $userrole = $DB->get_record('role_assignments', ['userid' => $user1->id]);
 189          $this->assertNotEmpty($userrole);
 190          $this->assertEquals($studentrole->id, $userrole->roleid);
 191  
 192          // Delete the cohort.
 193          cohort_delete_cohort($cohort);
 194  
 195          // Run the sync again.
 196          enrol_cohort_sync($trace, $course->id);
 197  
 198          $ue = $DB->get_records('user_enrolments', ['enrolid' => $enrolid], '', 'userid, status, enrolid');
 199  
 200          // Check users are suspended.
 201          $this->assertEquals($ue[$user2->id]->status, ENROL_USER_SUSPENDED);
 202          $this->assertEquals($ue[$user3->id]->status, ENROL_USER_SUSPENDED);
 203          $this->assertEquals($ue[$user4->id]->status, ENROL_USER_SUSPENDED);
 204  
 205          // Check that users still have student role.
 206          $usersrole = $DB->get_records('role_assignments', ['itemid' => $enrolid], '', 'userid, roleid');
 207          $this->assertNotEmpty($usersrole);
 208          $this->assertEquals($studentrole->id, $usersrole[$user2->id]->roleid);
 209          $this->assertEquals($studentrole->id, $usersrole[$user3->id]->roleid);
 210          $this->assertEquals($studentrole->id, $usersrole[$user4->id]->roleid);
 211      }
 212  
 213      /**
 214       * Test the behaviour of validate_plugin_data_context().
 215       *
 216       * @covers ::validate_plugin_data_context
 217       */
 218      public function test_validate_plugin_data_context() {
 219          $this->resetAfterTest();
 220  
 221          $cohortplugin = enrol_get_plugin('cohort');
 222  
 223          $cat = $this->getDataGenerator()->create_category();
 224          $cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
 225          $cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
 226  
 227          $course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);
 228  
 229          $cohort1 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat1->id)->id]);
 230          $cohort2 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat2->id)->id]);
 231  
 232          $enrolmentdata = [
 233              'customint1' => $cohort2->id,
 234              'cohortname' => $cohort2->name,
 235          ];
 236          $error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
 237          $this->assertInstanceOf('lang_string', $error);
 238          $this->assertEquals('contextcohortnotallowed', $error->get_identifier());
 239  
 240          $enrolmentdata = [
 241              'customint1' => $cohort1->id,
 242              'cohortname' => $cohort1->name,
 243          ];
 244          $error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
 245          $this->assertNull($error);
 246      }
 247  
 248      /**
 249       * Test the behaviour of fill_enrol_custom_fields().
 250       *
 251       * @covers ::fill_enrol_custom_fields
 252       */
 253      public function test_fill_enrol_custom_fields() {
 254          $this->resetAfterTest();
 255  
 256          $cohortplugin = enrol_get_plugin('cohort');
 257  
 258          $cat = $this->getDataGenerator()->create_category();
 259          $course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
 260          $cohort = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat->id)->id]);
 261          $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
 262  
 263          $enrolmentdata['cohortname'] = $cohort->name;
 264          $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
 265          $this->assertArrayHasKey('customint1', $enrolmentdata);
 266          $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
 267          $this->assertArrayNotHasKey('customint2', $enrolmentdata);
 268  
 269          $enrolmentdata['cohortname'] = 'notexist';
 270          $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
 271          $this->assertArrayHasKey('customint1', $enrolmentdata);
 272          $this->assertNull($enrolmentdata['customint1']);
 273          $this->assertArrayNotHasKey('customint2', $enrolmentdata);
 274  
 275          $enrolmentdata['cohortname'] = $cohort->name;
 276  
 277          $enrolmentdata['addtogroup'] = COHORT_NOGROUP;
 278          $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
 279          $this->assertArrayHasKey('customint1', $enrolmentdata);
 280          $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
 281          $this->assertArrayHasKey('customint2', $enrolmentdata);
 282          $this->assertEquals(COHORT_NOGROUP, $enrolmentdata['customint2']);
 283  
 284          unset($enrolmentdata['addtogroup']);
 285          $enrolmentdata['groupname'] = $group->name;
 286          $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
 287          $this->assertArrayHasKey('customint1', $enrolmentdata);
 288          $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
 289          $this->assertArrayHasKey('customint2', $enrolmentdata);
 290          $this->assertEquals($group->id, $enrolmentdata['customint2']);
 291  
 292          $enrolmentdata['groupname'] = 'notexist';
 293          $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
 294          $this->assertArrayHasKey('customint1', $enrolmentdata);
 295          $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
 296          $this->assertArrayHasKey('customint2', $enrolmentdata);
 297          $this->assertFalse($enrolmentdata['customint2']);
 298      }
 299  
 300      /**
 301       * Test the behaviour of validate_enrol_plugin_data().
 302       *
 303       * @covers ::validate_enrol_plugin_data
 304       */
 305      public function test_validate_enrol_plugin_data() {
 306          $this->resetAfterTest();
 307  
 308          $cat = $this->getDataGenerator()->create_category();
 309          $cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
 310          $cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
 311  
 312          $course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);
 313  
 314          $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Group 1']);
 315  
 316          $cohort1 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat1->id)->id]);
 317          $cohort2 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat2->id)->id]);
 318  
 319          enrol::enable_plugin('cohort', false);
 320  
 321          $cohortplugin = enrol_get_plugin('cohort');
 322  
 323          // Plugin is disabled in system and cohort name is missing in csv.
 324          $enrolmentdata = [];
 325          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata);
 326          $this->assertArrayHasKey('plugindisabled', $errors);
 327          $this->assertArrayHasKey('missingmandatoryfields', $errors);
 328  
 329          enrol::enable_plugin('cohort', true);
 330  
 331          // Unknown cohort name.
 332          $enrolmentdata['cohortname'] = 'test';
 333          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata);
 334          $this->assertArrayHasKey('unknowncohort', $errors);
 335  
 336          // Non-valid 'addtogroup' option.
 337          $enrolmentdata['cohortname'] = $cohort1->name;
 338          $enrolmentdata['addtogroup'] = 2;
 339          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
 340          $this->assertArrayHasKey('erroraddtogroup', $errors);
 341  
 342          // Options 'addtogroup' and 'groupname' are not allowed together.
 343          $enrolmentdata['addtogroup'] = 0;
 344          $enrolmentdata['groupname'] = 'test';
 345          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
 346          $this->assertArrayHasKey('erroraddtogroupgroupname', $errors);
 347  
 348          // Cohort is not allowed on a given category context.
 349          $enrolmentdata['cohortname'] = $cohort2->name;
 350          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
 351          $this->assertArrayHasKey('contextnotallowed', $errors);
 352  
 353          // Group does not exist.
 354          unset($enrolmentdata['addtogroup']);
 355          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
 356          $this->assertArrayHasKey('errorinvalidgroup', $errors);
 357  
 358          // Valid data when trying to create a group.
 359          $enrolmentdata['cohortname'] = $cohort1->name;
 360          $enrolmentdata['addtogroup'] = 1;
 361          unset($enrolmentdata['groupname']);
 362          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
 363          $this->assertEmpty($errors);
 364  
 365          // Valid data when trying to add to existing group.
 366          $enrolmentdata['groupname'] = $group1->name;
 367          unset($enrolmentdata['addtogroup']);
 368          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
 369          $this->assertEmpty($errors);
 370  
 371          // Valid data when trying without group mode.
 372          $enrolmentdata['addtogroup'] = 0;
 373          unset($enrolmentdata['groupname']);
 374          $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
 375          $this->assertEmpty($errors);
 376      }
 377  
 378  }