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   * Self enrol external PHPunit tests
  19   *
  20   * @package   enrol_guest
  21   * @copyright 2015 Juan Leyva <juan@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @since     Moodle 3.1
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  31  
  32  /**
  33   * Guest enrolment external functions tests
  34   *
  35   * @package    enrol_guest
  36   * @category   external
  37   * @copyright  2015 Juan Leyva <juan@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   * @since      Moodle 3.1
  40   */
  41  class enrol_guest_external_testcase extends externallib_advanced_testcase {
  42  
  43      /**
  44       * Test get_instance_info
  45       */
  46      public function test_get_instance_info() {
  47          global $DB;
  48  
  49          $this->resetAfterTest(true);
  50  
  51          // Check if guest enrolment plugin is enabled.
  52          $guestplugin = enrol_get_plugin('guest');
  53          $this->assertNotEmpty($guestplugin);
  54  
  55          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  56  
  57          $coursedata = new stdClass();
  58          $coursedata->visible = 0;
  59          $course = self::getDataGenerator()->create_course($coursedata);
  60  
  61          $student = self::getDataGenerator()->create_user();
  62          $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
  63  
  64          // Add enrolment methods for course.
  65          $instance = $guestplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
  66                                                                  'name' => 'Test instance',
  67                                                                  'customint6' => 1,
  68                                                                  'roleid' => $studentrole->id));
  69  
  70          $this->setAdminUser();
  71          $result = enrol_guest_external::get_instance_info($instance);
  72          $result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
  73  
  74          $this->assertEquals($instance, $result['instanceinfo']['id']);
  75          $this->assertEquals($course->id, $result['instanceinfo']['courseid']);
  76          $this->assertEquals('guest', $result['instanceinfo']['type']);
  77          $this->assertEquals('Test instance', $result['instanceinfo']['name']);
  78          $this->assertTrue($result['instanceinfo']['status']);
  79          $this->assertFalse($result['instanceinfo']['passwordrequired']);
  80  
  81          $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id' => $instance));
  82  
  83          $result = enrol_guest_external::get_instance_info($instance);
  84          $result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
  85          $this->assertEquals($instance, $result['instanceinfo']['id']);
  86          $this->assertEquals($course->id, $result['instanceinfo']['courseid']);
  87          $this->assertEquals('guest', $result['instanceinfo']['type']);
  88          $this->assertEquals('Test instance', $result['instanceinfo']['name']);
  89          $this->assertFalse($result['instanceinfo']['status']);
  90          $this->assertFalse($result['instanceinfo']['passwordrequired']);
  91  
  92          $DB->set_field('enrol', 'status', ENROL_INSTANCE_ENABLED, array('id' => $instance));
  93  
  94          // Try to retrieve information using a normal user for a hidden course.
  95          $user = self::getDataGenerator()->create_user();
  96          $this->setUser($user);
  97          try {
  98              enrol_guest_external::get_instance_info($instance);
  99          } catch (moodle_exception $e) {
 100              $this->assertEquals('coursehidden', $e->errorcode);
 101          }
 102  
 103          // Student user.
 104          $DB->set_field('course', 'visible', 1, array('id' => $course->id));
 105          $this->setUser($student);
 106          $result = enrol_guest_external::get_instance_info($instance);
 107          $result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
 108  
 109          $this->assertEquals($instance, $result['instanceinfo']['id']);
 110          $this->assertEquals($course->id, $result['instanceinfo']['courseid']);
 111          $this->assertEquals('guest', $result['instanceinfo']['type']);
 112          $this->assertEquals('Test instance', $result['instanceinfo']['name']);
 113          $this->assertTrue($result['instanceinfo']['status']);
 114          $this->assertFalse($result['instanceinfo']['passwordrequired']);
 115      }
 116  }