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