MDL-52073 enrol_guest: New Web Service enrol_guest_get_instance_info
[moodle.git] / enrol / guest / tests / external_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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 {
43     /**
44      * Test get_instance_info
45      */
46     public function test_get_instance_info() {
47         global $DB;
49         $this->resetAfterTest(true);
51         // Check if guest enrolment plugin is enabled.
52         $guestplugin = enrol_get_plugin('guest');
53         $this->assertNotEmpty($guestplugin);
55         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
57         $coursedata = new stdClass();
58         $coursedata->visible = 0;
59         $course = self::getDataGenerator()->create_course($coursedata);
61         $student = self::getDataGenerator()->create_user();
62         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
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));
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);
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']);
81         $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id' => $instance));
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']);
92         $DB->set_field('enrol', 'status', ENROL_INSTANCE_ENABLED, array('id' => $instance));
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         }
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);
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     }