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