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.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  18   * Enrol manual external PHPunit tests
  19   *
  20   * @package    enrol_manual
  21   * @category   phpunit
  22   * @copyright  2012 Jerome Mouneyrac
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since Moodle 2.4
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  require_once($CFG->dirroot . '/enrol/manual/externallib.php');
  33  
  34  class enrol_manual_externallib_testcase extends externallib_advanced_testcase {
  35  
  36      /**
  37       * Test get_enrolled_users
  38       */
  39      public function test_enrol_users() {
  40          global $DB;
  41  
  42          $this->resetAfterTest(true);
  43  
  44          $user = self::getDataGenerator()->create_user();
  45          $this->setUser($user);
  46  
  47          $course1 = self::getDataGenerator()->create_course();
  48          $course2 = self::getDataGenerator()->create_course();
  49          $user1 = self::getDataGenerator()->create_user();
  50          $user2 = self::getDataGenerator()->create_user();
  51  
  52          $context1 = context_course::instance($course1->id);
  53          $context2 = context_course::instance($course2->id);
  54          $instance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  55          $instance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
  56  
  57          // Set the required capabilities by the external function.
  58          $roleid = $this->assignUserCapability('enrol/manual:enrol', $context1->id);
  59          $this->assignUserCapability('moodle/course:view', $context1->id, $roleid);
  60          $this->assignUserCapability('moodle/role:assign', $context1->id, $roleid);
  61          $this->assignUserCapability('enrol/manual:enrol', $context2->id, $roleid);
  62          $this->assignUserCapability('moodle/course:view', $context2->id, $roleid);
  63          $this->assignUserCapability('moodle/role:assign', $context2->id, $roleid);
  64  
  65          core_role_set_assign_allowed($roleid, 3);
  66  
  67          // Call the external function.
  68          enrol_manual_external::enrol_users(array(
  69              array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
  70              array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course1->id),
  71          ));
  72  
  73          $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
  74          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
  75          $this->assertTrue(is_enrolled($context1, $user1));
  76          $this->assertTrue(is_enrolled($context1, $user2));
  77  
  78          // Call without required capability.
  79          $DB->delete_records('user_enrolments');
  80          $this->unassignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
  81          try {
  82              enrol_manual_external::enrol_users(array(
  83                  array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
  84              ));
  85              $this->fail('Exception expected if not having capability to enrol');
  86          } catch (moodle_exception $e) {
  87              $this->assertInstanceOf('required_capability_exception', $e);
  88              $this->assertSame('nopermissions', $e->errorcode);
  89          }
  90          $this->assignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
  91          $this->assertEquals(0, $DB->count_records('user_enrolments'));
  92  
  93          // Call with forbidden role.
  94          try {
  95              enrol_manual_external::enrol_users(array(
  96                  array('roleid' => 1, 'userid' => $user1->id, 'courseid' => $course1->id),
  97              ));
  98              $this->fail('Exception expected if not allowed to assign role.');
  99          } catch (moodle_exception $e) {
 100              $this->assertSame('wsusercannotassign', $e->errorcode);
 101          }
 102          $this->assertEquals(0, $DB->count_records('user_enrolments'));
 103  
 104          // Call for course without manual instance.
 105          $DB->delete_records('user_enrolments');
 106          $DB->delete_records('enrol', array('courseid' => $course2->id));
 107          try {
 108              enrol_manual_external::enrol_users(array(
 109                  array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
 110                  array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course2->id),
 111              ));
 112              $this->fail('Exception expected if course does not have manual instance');
 113          } catch (moodle_exception $e) {
 114              $this->assertSame('wsnoinstance', $e->errorcode);
 115          }
 116      }
 117  
 118      /**
 119       * Test for unerolling a single user.
 120       * @throws coding_exception
 121       * @throws invalid_parameter_exception
 122       * @throws moodle_exception
 123       */
 124      public function test_unenrol_user_single() {
 125          global $CFG, $DB;
 126          require_once($CFG->libdir . '/enrollib.php');
 127          $this->resetAfterTest(true);
 128          // The user who perform the action.
 129          $user = $this->getDataGenerator()->create_user();
 130          $this->setUser($user); // Log this user in.
 131          $enrol = enrol_get_plugin('manual');
 132          // Create a course.
 133          $course = self::getDataGenerator()->create_course();
 134          $coursecontext = context_course::instance($course->id);
 135          $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
 136          // Set the capability for the user.
 137          $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext);
 138          $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
 139          $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid);
 140          $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid);
 141          // Create a student and enrol them into the course.
 142          $student = $this->getDataGenerator()->create_user();
 143          $enrol->enrol_user($enrolinstance, $student->id);
 144          $this->assertTrue(is_enrolled($coursecontext, $student));
 145          // Call the web service to unenrol.
 146          enrol_manual_external::unenrol_users(array(
 147              array('userid' => $student->id, 'courseid' => $course->id),
 148          ));
 149          $this->assertFalse(is_enrolled($coursecontext, $student));
 150      }
 151  
 152      /**
 153       * Test for unenrolling multiple users.
 154       * @throws coding_exception
 155       * @throws invalid_parameter_exception
 156       * @throws moodle_exception
 157       */
 158      public function test_unenrol_user_multiple() {
 159          global $CFG, $DB;
 160          require_once($CFG->libdir . '/enrollib.php');
 161          $this->resetAfterTest(true);
 162          // The user who perform the action.
 163          $user = $this->getDataGenerator()->create_user();
 164          $this->setUser($user); // Log this user in.
 165          // Create a course.
 166          $course = self::getDataGenerator()->create_course();
 167          $coursecontext = context_course::instance($course->id);
 168          $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
 169          // Set the capability for the user.
 170          $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext);
 171          $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
 172          $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid);
 173          $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid);
 174          $enrol = enrol_get_plugin('manual');
 175          // Create a student and enrol them into the course.
 176          $student1 = $this->getDataGenerator()->create_user();
 177          $enrol->enrol_user($enrolinstance, $student1->id);
 178          $this->assertTrue(is_enrolled($coursecontext, $student1));
 179          $student2 = $this->getDataGenerator()->create_user();
 180          $enrol->enrol_user($enrolinstance, $student2->id);
 181          $this->assertTrue(is_enrolled($coursecontext, $student2));
 182          // Call the web service to unenrol.
 183          enrol_manual_external::unenrol_users(array(
 184              array('userid' => $student1->id, 'courseid' => $course->id),
 185              array('userid' => $student2->id, 'courseid' => $course->id),
 186          ));
 187          $this->assertFalse(is_enrolled($coursecontext, $student1));
 188          $this->assertFalse(is_enrolled($coursecontext, $student2));
 189      }
 190  
 191      /**
 192       * Test for unenrol capability.
 193       * @throws coding_exception
 194       * @throws invalid_parameter_exception
 195       * @throws moodle_exception
 196       */
 197      public function test_unenrol_user_error_no_capability() {
 198          global $CFG, $DB;
 199          require_once($CFG->libdir . '/enrollib.php');
 200          $this->resetAfterTest(true);
 201          // The user who perform the action.
 202          $user = $this->getDataGenerator()->create_user();
 203          $this->setUser($user); // Log this user in.
 204          // Create a course.
 205          $course = self::getDataGenerator()->create_course();
 206          $coursecontext = context_course::instance($course->id);
 207          $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
 208          $enrol = enrol_get_plugin('manual');
 209          // Create a student and enrol them into the course.
 210          $student = $this->getDataGenerator()->create_user();
 211          $enrol->enrol_user($enrolinstance, $student->id);
 212          $this->assertTrue(is_enrolled($coursecontext, $student));
 213          // Call the web service to unenrol.
 214          try {
 215              enrol_manual_external::unenrol_users(array(
 216                  array('userid' => $student->id, 'courseid' => $course->id),
 217              ));
 218              $this->fail('Exception expected: User cannot log in to the course');
 219          } catch (Exception $ex) {
 220              $this->assertTrue($ex instanceof require_login_exception);
 221          }
 222          // Set the capability for the course, then try again.
 223          $roleid = $this->assignUserCapability('moodle/course:view', $coursecontext);
 224          try {
 225              enrol_manual_external::unenrol_users(array(
 226                  array('userid' => $student->id, 'courseid' => $course->id),
 227              ));
 228              $this->fail('Exception expected: User cannot log in to the course');
 229          } catch (Exception $ex) {
 230              $this->assertTrue($ex instanceof required_capability_exception);
 231          }
 232          // Assign unenrol capability.
 233          $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
 234          enrol_manual_external::unenrol_users(array(
 235              array('userid' => $student->id, 'courseid' => $course->id),
 236          ));
 237          $this->assertFalse(is_enrolled($coursecontext, $student));
 238      }
 239  
 240      /**
 241       * Test for unenrol if user does not exist.
 242       * @throws coding_exception
 243       */
 244      public function test_unenrol_user_error_not_exist() {
 245          global $CFG, $DB;
 246          require_once($CFG->libdir . '/enrollib.php');
 247          $this->resetAfterTest(true);
 248          // The user who perform the action.
 249          $user = $this->getDataGenerator()->create_user();
 250          $this->setUser($user); // Log this user in.
 251          $enrol = enrol_get_plugin('manual');
 252          // Create a course.
 253          $course = self::getDataGenerator()->create_course();
 254          $coursecontext = context_course::instance($course->id);
 255          $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
 256          // Set the capability for the user.
 257          $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext);
 258          $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
 259          $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid);
 260          $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid);
 261          // Create a student and enrol them into the course.
 262          $student = $this->getDataGenerator()->create_user();
 263          $enrol->enrol_user($enrolinstance, $student->id);
 264          $this->assertTrue(is_enrolled($coursecontext, $student));
 265          try {
 266              enrol_manual_external::unenrol_users(array(
 267                  array('userid' => $student->id + 1, 'courseid' => $course->id),
 268              ));
 269              $this->fail('Exception expected: invalid student id');
 270          } catch (Exception $ex) {
 271              $this->assertTrue($ex instanceof invalid_parameter_exception);
 272          }
 273          $DB->delete_records('enrol', array('id' => $enrolinstance->id));
 274          try {
 275              enrol_manual_external::unenrol_users(array(
 276                  array('userid' => $student->id + 1, 'courseid' => $course->id),
 277              ));
 278              $this->fail('Exception expected: invalid student id');
 279          } catch (Exception $ex) {
 280              $this->assertTrue($ex instanceof moodle_exception);
 281          }
 282      }
 283  }