Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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_self;
  18  
  19  use core_external\external_api;
  20  use enrol_self_external;
  21  use externallib_advanced_testcase;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  
  27  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28  require_once($CFG->dirroot . '/enrol/self/externallib.php');
  29  
  30  /**
  31   * Self enrol external PHPunit tests
  32   *
  33   * @package   enrol_self
  34   * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @since     Moodle 2.6
  37   */
  38  class externallib_test extends externallib_advanced_testcase {
  39  
  40      /**
  41       * Test get_instance_info
  42       */
  43      public function test_get_instance_info() {
  44          global $DB;
  45  
  46          $this->resetAfterTest(true);
  47  
  48          // Check if self enrolment plugin is enabled.
  49          $selfplugin = enrol_get_plugin('self');
  50          $this->assertNotEmpty($selfplugin);
  51  
  52          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
  53          $this->assertNotEmpty($studentrole);
  54  
  55          $coursedata = new \stdClass();
  56          $coursedata->visible = 0;
  57          $course = self::getDataGenerator()->create_course($coursedata);
  58  
  59          // Add enrolment methods for course.
  60          $instanceid1 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
  61                                                                  'name' => 'Test instance 1',
  62                                                                  'customint6' => 1,
  63                                                                  'roleid' => $studentrole->id));
  64          $instanceid2 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_DISABLED,
  65                                                                  'customint6' => 1,
  66                                                                  'name' => 'Test instance 2',
  67                                                                  'roleid' => $studentrole->id));
  68  
  69          $instanceid3 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
  70                                                                  'roleid' => $studentrole->id,
  71                                                                  'customint6' => 1,
  72                                                                  'name' => 'Test instance 3',
  73                                                                  'password' => 'test'));
  74  
  75          $enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course->id, 'status' => ENROL_INSTANCE_ENABLED));
  76          $this->assertCount(3, $enrolmentmethods);
  77  
  78          $this->setAdminUser();
  79          $instanceinfo1 = enrol_self_external::get_instance_info($instanceid1);
  80          $instanceinfo1 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1);
  81  
  82          $this->assertEquals($instanceid1, $instanceinfo1['id']);
  83          $this->assertEquals($course->id, $instanceinfo1['courseid']);
  84          $this->assertEquals('self', $instanceinfo1['type']);
  85          $this->assertEquals('Test instance 1', $instanceinfo1['name']);
  86          $this->assertTrue($instanceinfo1['status']);
  87          $this->assertFalse(isset($instanceinfo1['enrolpassword']));
  88  
  89          $instanceinfo2 = enrol_self_external::get_instance_info($instanceid2);
  90          $instanceinfo2 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2);
  91          $this->assertEquals($instanceid2, $instanceinfo2['id']);
  92          $this->assertEquals($course->id, $instanceinfo2['courseid']);
  93          $this->assertEquals('self', $instanceinfo2['type']);
  94          $this->assertEquals('Test instance 2', $instanceinfo2['name']);
  95          $this->assertEquals(get_string('canntenrol', 'enrol_self'), $instanceinfo2['status']);
  96          $this->assertFalse(isset($instanceinfo2['enrolpassword']));
  97  
  98          $instanceinfo3 = enrol_self_external::get_instance_info($instanceid3);
  99          $instanceinfo3 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3);
 100          $this->assertEquals($instanceid3, $instanceinfo3['id']);
 101          $this->assertEquals($course->id, $instanceinfo3['courseid']);
 102          $this->assertEquals('self', $instanceinfo3['type']);
 103          $this->assertEquals('Test instance 3', $instanceinfo3['name']);
 104          $this->assertTrue($instanceinfo3['status']);
 105          $this->assertEquals(get_string('password', 'enrol_self'), $instanceinfo3['enrolpassword']);
 106  
 107          // Try to retrieve information using a normal user for a hidden course.
 108          $user = self::getDataGenerator()->create_user();
 109          $this->setUser($user);
 110          try {
 111              enrol_self_external::get_instance_info($instanceid3);
 112          } catch (\moodle_exception $e) {
 113              $this->assertEquals('coursehidden', $e->errorcode);
 114          }
 115      }
 116  
 117      /**
 118       * Test enrol_user
 119       */
 120      public function test_enrol_user() {
 121          global $DB;
 122  
 123          self::resetAfterTest(true);
 124  
 125          $user = self::getDataGenerator()->create_user();
 126          self::setUser($user);
 127  
 128          $course1 = self::getDataGenerator()->create_course();
 129          $course2 = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
 130          $user1 = self::getDataGenerator()->create_user();
 131          $user2 = self::getDataGenerator()->create_user();
 132          $user3 = self::getDataGenerator()->create_user();
 133          $user4 = self::getDataGenerator()->create_user();
 134  
 135          $context1 = \context_course::instance($course1->id);
 136          $context2 = \context_course::instance($course2->id);
 137  
 138          $selfplugin = enrol_get_plugin('self');
 139          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 140          $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
 141                                                                  'name' => 'Test instance 1',
 142                                                                  'customint6' => 1,
 143                                                                  'roleid' => $studentrole->id));
 144          $instance2id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_DISABLED,
 145                                                                  'customint6' => 1,
 146                                                                  'name' => 'Test instance 2',
 147                                                                  'roleid' => $studentrole->id));
 148          $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
 149          $instance2 = $DB->get_record('enrol', array('id' => $instance2id), '*', MUST_EXIST);
 150  
 151          self::setUser($user1);
 152  
 153          // Self enrol me.
 154          $result = enrol_self_external::enrol_user($course1->id);
 155          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 156  
 157          self::assertTrue($result['status']);
 158          self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
 159          self::assertTrue(is_enrolled($context1, $user1));
 160  
 161          // Add password.
 162          $instance2->password = 'abcdef';
 163          $DB->update_record('enrol', $instance2);
 164  
 165          // Try instance not enabled.
 166          try {
 167              enrol_self_external::enrol_user($course2->id);
 168          } catch (\moodle_exception $e) {
 169              self::assertEquals('canntenrol', $e->errorcode);
 170          }
 171  
 172          // Enable the instance.
 173          $selfplugin->update_status($instance2, ENROL_INSTANCE_ENABLED);
 174  
 175          // Try not passing a key.
 176          $result = enrol_self_external::enrol_user($course2->id);
 177          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 178          self::assertFalse($result['status']);
 179          self::assertCount(1, $result['warnings']);
 180          self::assertEquals('4', $result['warnings'][0]['warningcode']);
 181  
 182          // Try passing an invalid key.
 183          $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
 184          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 185          self::assertFalse($result['status']);
 186          self::assertCount(1, $result['warnings']);
 187          self::assertEquals('4', $result['warnings'][0]['warningcode']);
 188  
 189          // Try passing an invalid key with hint.
 190          $selfplugin->set_config('showhint', true);
 191          $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
 192          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 193          self::assertFalse($result['status']);
 194          self::assertCount(1, $result['warnings']);
 195          self::assertEquals('3', $result['warnings'][0]['warningcode']);
 196  
 197          // Everything correct, now.
 198          $result = enrol_self_external::enrol_user($course2->id, 'abcdef');
 199          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 200  
 201          self::assertTrue($result['status']);
 202          self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
 203          self::assertTrue(is_enrolled($context2, $user1));
 204  
 205          // Try group password now, other user.
 206          $instance2->customint1 = 1;
 207          $instance2->password = 'zyx';
 208          $DB->update_record('enrol', $instance2);
 209  
 210          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 211          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enrolmentkey' => 'zyx'));
 212  
 213          self::setUser($user2);
 214          // Try passing and invalid key for group.
 215          $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
 216          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 217          self::assertFalse($result['status']);
 218          self::assertCount(1, $result['warnings']);
 219          self::assertEquals('2', $result['warnings'][0]['warningcode']);
 220  
 221          // Now, everything ok.
 222          $result = enrol_self_external::enrol_user($course2->id, 'zyx');
 223          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 224  
 225          self::assertTrue($result['status']);
 226          self::assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
 227          self::assertTrue(is_enrolled($context2, $user2));
 228  
 229          // Try multiple instances now, multiple errors.
 230          $instance3id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_ENABLED,
 231                                                                  'customint6' => 1,
 232                                                                  'name' => 'Test instance 2',
 233                                                                  'roleid' => $studentrole->id));
 234          $instance3 = $DB->get_record('enrol', array('id' => $instance3id), '*', MUST_EXIST);
 235          $instance3->password = 'abcdef';
 236          $DB->update_record('enrol', $instance3);
 237  
 238          self::setUser($user3);
 239          $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
 240          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 241          self::assertFalse($result['status']);
 242          self::assertCount(2, $result['warnings']);
 243  
 244          // Now, everything ok.
 245          $result = enrol_self_external::enrol_user($course2->id, 'zyx');
 246          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 247          self::assertTrue($result['status']);
 248          self::assertTrue(is_enrolled($context2, $user3));
 249  
 250          // Now test passing an instance id.
 251          self::setUser($user4);
 252          $result = enrol_self_external::enrol_user($course2->id, 'abcdef', $instance3id);
 253          $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
 254          self::assertTrue($result['status']);
 255          self::assertTrue(is_enrolled($context2, $user3));
 256          self::assertCount(0, $result['warnings']);
 257          self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance3->id)));
 258      }
 259  }