Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 401 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  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot.'/cohort/lib.php');
  23  require_once($CFG->dirroot.'/group/lib.php');
  24  
  25  /**
  26   * Contains tests for the cohort library.
  27   *
  28   * @package   enrol_cohort
  29   * @category  test
  30   * @copyright 2015 Adrian Greeve <adrian@moodle.com>
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class lib_test extends \advanced_testcase {
  34  
  35      /**
  36       * Test that a new group with the name of the cohort is created.
  37       */
  38      public function test_enrol_cohort_create_new_group() {
  39          global $DB;
  40          $this->resetAfterTest();
  41          // Create a category.
  42          $category = $this->getDataGenerator()->create_category();
  43          // Create two courses.
  44          $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
  45          $course2 = $this->getDataGenerator()->create_course(array('category' => $category->id));
  46          // Create a cohort.
  47          $cohort = $this->getDataGenerator()->create_cohort(array('context' => \context_coursecat::instance($category->id)->id));
  48          // Run the function.
  49          $groupid = enrol_cohort_create_new_group($course->id, $cohort->id);
  50          // Check the results.
  51          $group = $DB->get_record('groups', array('id' => $groupid));
  52          // The group name should match the cohort name.
  53          $this->assertEquals($cohort->name . ' cohort', $group->name);
  54          // Group course id should match the course id.
  55          $this->assertEquals($course->id, $group->courseid);
  56  
  57          // Create a group that will have the same name as the cohort.
  58          $groupdata = new \stdClass();
  59          $groupdata->courseid = $course2->id;
  60          $groupdata->name = $cohort->name . ' cohort';
  61          groups_create_group($groupdata);
  62          // Create a group for the cohort in course 2.
  63          $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
  64          $groupinfo = $DB->get_record('groups', array('id' => $groupid));
  65          // Check that the group name has been changed.
  66          $this->assertEquals($cohort->name . ' cohort (2)', $groupinfo->name);
  67  
  68          // Create another group that will have the same name as a generated cohort.
  69          $groupdata = new \stdClass();
  70          $groupdata->courseid = $course2->id;
  71          $groupdata->name = $cohort->name . ' cohort (2)';
  72          groups_create_group($groupdata);
  73          // Create a group for the cohort in course 2.
  74          $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
  75          $groupinfo = $DB->get_record('groups', array('id' => $groupid));
  76          // Check that the group name has been changed.
  77          $this->assertEquals($cohort->name . ' cohort (3)', $groupinfo->name);
  78  
  79      }
  80  
  81      /**
  82       * Test for getting user enrolment actions.
  83       */
  84      public function test_get_user_enrolment_actions() {
  85          global $CFG, $PAGE;
  86          $this->resetAfterTest();
  87  
  88          // Set page URL to prevent debugging messages.
  89          $PAGE->set_url('/enrol/editinstance.php');
  90  
  91          $pluginname = 'cohort';
  92  
  93          // Only enable the cohort enrol plugin.
  94          $CFG->enrol_plugins_enabled = $pluginname;
  95  
  96          $generator = $this->getDataGenerator();
  97  
  98          // Get the enrol plugin.
  99          $plugin = enrol_get_plugin($pluginname);
 100  
 101          // Create a course.
 102          $course = $generator->create_course();
 103          // Enable this enrol plugin for the course.
 104          $plugin->add_instance($course);
 105  
 106          // Create a student.
 107          $student = $generator->create_user();
 108          // Enrol the student to the course.
 109          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 110  
 111          // Teachers don't have enrol/cohort:unenrol capability by default. Login as admin for simplicity.
 112          $this->setAdminUser();
 113          require_once($CFG->dirroot . '/enrol/locallib.php');
 114          $manager = new \course_enrolment_manager($PAGE, $course);
 115  
 116          $userenrolments = $manager->get_user_enrolments($student->id);
 117          $this->assertCount(1, $userenrolments);
 118  
 119          $ue = reset($userenrolments);
 120          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 121          // Cohort-sync has no enrol actions for active students.
 122          $this->assertCount(0, $actions);
 123  
 124          // Enrol actions for a suspended student.
 125          // Suspend the student.
 126          $ue->status = ENROL_USER_SUSPENDED;
 127  
 128          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 129          // Cohort-sync has enrol actions for suspended students -- unenrol.
 130          $this->assertCount(1, $actions);
 131      }
 132  
 133      public function test_enrol_cohort_unenrolaction_suspend_only() {
 134          global $CFG, $DB, $PAGE;
 135          $this->resetAfterTest();
 136  
 137          $trace = new \null_progress_trace();
 138  
 139          $cohortplugin = enrol_get_plugin('cohort');
 140          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 141  
 142          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 143          $this->assertNotEmpty($studentrole);
 144  
 145          // Setup a test course.
 146          $course = $this->getDataGenerator()->create_course();
 147  
 148          $user1 = $this->getDataGenerator()->create_user();
 149          $user2 = $this->getDataGenerator()->create_user();
 150          $user3 = $this->getDataGenerator()->create_user();
 151          $user4 = $this->getDataGenerator()->create_user();
 152  
 153          $cohort = $this->getDataGenerator()->create_cohort();
 154  
 155          $cohortplugin->add_instance($course, ['customint1' => $cohort->id,
 156              'roleid' => $studentrole->id]
 157          );
 158  
 159          cohort_add_member($cohort->id, $user1->id);
 160          cohort_add_member($cohort->id, $user2->id);
 161          cohort_add_member($cohort->id, $user3->id);
 162          cohort_add_member($cohort->id, $user4->id);
 163  
 164          // Test sync.
 165          enrol_cohort_sync($trace, $course->id);
 166  
 167          // All users should be enrolled.
 168          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
 169          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user2));
 170          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user3));
 171          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user4));
 172  
 173          // Remove cohort member.
 174          cohort_remove_member($cohort->id, $user1->id);
 175          $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
 176  
 177          // Run the sync again.
 178          enrol_cohort_sync($trace, $course->id);
 179  
 180          $enrolid = $DB->get_field('enrol', 'id', ['enrol' => 'cohort', 'customint1' => $cohort->id]);
 181          $ue = $DB->get_record('user_enrolments', ['enrolid' => $enrolid, 'userid' => $user1->id]);
 182  
 183          // Check user is suspended.
 184          $this->assertEquals($ue->status, ENROL_USER_SUSPENDED);
 185          // Check that user4 still have student role.
 186          $userrole = $DB->get_record('role_assignments', ['userid' => $user1->id]);
 187          $this->assertNotEmpty($userrole);
 188          $this->assertEquals($studentrole->id, $userrole->roleid);
 189  
 190          // Delete the cohort.
 191          cohort_delete_cohort($cohort);
 192  
 193          // Run the sync again.
 194          enrol_cohort_sync($trace, $course->id);
 195  
 196          $ue = $DB->get_records('user_enrolments', ['enrolid' => $enrolid], '', 'userid, status, enrolid');
 197  
 198          // Check users are suspended.
 199          $this->assertEquals($ue[$user2->id]->status, ENROL_USER_SUSPENDED);
 200          $this->assertEquals($ue[$user3->id]->status, ENROL_USER_SUSPENDED);
 201          $this->assertEquals($ue[$user4->id]->status, ENROL_USER_SUSPENDED);
 202  
 203          // Check that users still have student role.
 204          $usersrole = $DB->get_records('role_assignments', ['itemid' => $enrolid], '', 'userid, roleid');
 205          $this->assertNotEmpty($usersrole);
 206          $this->assertEquals($studentrole->id, $usersrole[$user2->id]->roleid);
 207          $this->assertEquals($studentrole->id, $usersrole[$user3->id]->roleid);
 208          $this->assertEquals($studentrole->id, $usersrole[$user4->id]->roleid);
 209      }
 210  }