Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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  /**
  18   * Cohort enrolment sync functional test.
  19   *
  20   * @package    enrol_cohort
  21   * @category   test
  22   * @copyright  2015 Adrian Greeve <adrian@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot.'/cohort/lib.php');
  30  require_once($CFG->dirroot.'/group/lib.php');
  31  
  32  /**
  33   * Contains tests for the cohort library.
  34   *
  35   * @package   enrol_cohort
  36   * @copyright 2015 Adrian Greeve <adrian@moodle.com>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class enrol_cohort_lib_testcase extends advanced_testcase {
  40  
  41      /**
  42       * Test that a new group with the name of the cohort is created.
  43       */
  44      public function test_enrol_cohort_create_new_group() {
  45          global $DB;
  46          $this->resetAfterTest();
  47          // Create a category.
  48          $category = $this->getDataGenerator()->create_category();
  49          // Create two courses.
  50          $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
  51          $course2 = $this->getDataGenerator()->create_course(array('category' => $category->id));
  52          // Create a cohort.
  53          $cohort = $this->getDataGenerator()->create_cohort(array('context' => context_coursecat::instance($category->id)->id));
  54          // Run the function.
  55          $groupid = enrol_cohort_create_new_group($course->id, $cohort->id);
  56          // Check the results.
  57          $group = $DB->get_record('groups', array('id' => $groupid));
  58          // The group name should match the cohort name.
  59          $this->assertEquals($cohort->name . ' cohort', $group->name);
  60          // Group course id should match the course id.
  61          $this->assertEquals($course->id, $group->courseid);
  62  
  63          // Create a group that will have the same name as the cohort.
  64          $groupdata = new stdClass();
  65          $groupdata->courseid = $course2->id;
  66          $groupdata->name = $cohort->name . ' cohort';
  67          groups_create_group($groupdata);
  68          // Create a group for the cohort in course 2.
  69          $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
  70          $groupinfo = $DB->get_record('groups', array('id' => $groupid));
  71          // Check that the group name has been changed.
  72          $this->assertEquals($cohort->name . ' cohort (2)', $groupinfo->name);
  73  
  74          // Create another group that will have the same name as a generated cohort.
  75          $groupdata = new stdClass();
  76          $groupdata->courseid = $course2->id;
  77          $groupdata->name = $cohort->name . ' cohort (2)';
  78          groups_create_group($groupdata);
  79          // Create a group for the cohort in course 2.
  80          $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
  81          $groupinfo = $DB->get_record('groups', array('id' => $groupid));
  82          // Check that the group name has been changed.
  83          $this->assertEquals($cohort->name . ' cohort (3)', $groupinfo->name);
  84  
  85      }
  86  
  87      /**
  88       * Test for getting user enrolment actions.
  89       */
  90      public function test_get_user_enrolment_actions() {
  91          global $CFG, $PAGE;
  92          $this->resetAfterTest();
  93  
  94          // Set page URL to prevent debugging messages.
  95          $PAGE->set_url('/enrol/editinstance.php');
  96  
  97          $pluginname = 'cohort';
  98  
  99          // Only enable the cohort enrol plugin.
 100          $CFG->enrol_plugins_enabled = $pluginname;
 101  
 102          $generator = $this->getDataGenerator();
 103  
 104          // Get the enrol plugin.
 105          $plugin = enrol_get_plugin($pluginname);
 106  
 107          // Create a course.
 108          $course = $generator->create_course();
 109          // Enable this enrol plugin for the course.
 110          $plugin->add_instance($course);
 111  
 112          // Create a student.
 113          $student = $generator->create_user();
 114          // Enrol the student to the course.
 115          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 116  
 117          // Teachers don't have enrol/cohort:unenrol capability by default. Login as admin for simplicity.
 118          $this->setAdminUser();
 119          require_once($CFG->dirroot . '/enrol/locallib.php');
 120          $manager = new course_enrolment_manager($PAGE, $course);
 121  
 122          $userenrolments = $manager->get_user_enrolments($student->id);
 123          $this->assertCount(1, $userenrolments);
 124  
 125          $ue = reset($userenrolments);
 126          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 127          // Cohort-sync has no enrol actions for active students.
 128          $this->assertCount(0, $actions);
 129  
 130          // Enrol actions for a suspended student.
 131          // Suspend the student.
 132          $ue->status = ENROL_USER_SUSPENDED;
 133  
 134          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 135          // Cohort-sync has enrol actions for suspended students -- unenrol.
 136          $this->assertCount(1, $actions);
 137      }
 138  }