Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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