Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  }