MDL-45131 enrol: Add validation for returned parameters in WS tests
[moodle.git] / enrol / self / tests / externallib_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_self
21  * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  * @since     Moodle 2.6
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31 require_once($CFG->dirroot . '/enrol/self/externallib.php');
33 class enrol_self_external_testcase extends externallib_advanced_testcase {
35     /**
36      * Test get_instance_info
37      */
38     public function test_get_instance_info() {
39         global $DB;
41         $this->resetAfterTest(true);
43         // Check if self enrolment plugin is enabled.
44         $selfplugin = enrol_get_plugin('self');
45         $this->assertNotEmpty($selfplugin);
47         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
48         $this->assertNotEmpty($studentrole);
50         $course = self::getDataGenerator()->create_course();
52         // Add enrolment methods for course.
53         $instanceid1 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
54                                                                 'name' => 'Test instance 1',
55                                                                 'customint6' => 1,
56                                                                 'roleid' => $studentrole->id));
57         $instanceid2 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_DISABLED,
58                                                                 'customint6' => 1,
59                                                                 'name' => 'Test instance 2',
60                                                                 'roleid' => $studentrole->id));
62         $instanceid3 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
63                                                                 'roleid' => $studentrole->id,
64                                                                 'customint6' => 1,
65                                                                 'name' => 'Test instance 3',
66                                                                 'password' => 'test'));
68         $enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course->id, 'status' => ENROL_INSTANCE_ENABLED));
69         $this->assertCount(3, $enrolmentmethods);
71         $instanceinfo1 = enrol_self_external::get_instance_info($instanceid1);
72         $instanceinfo1 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1);
74         $this->assertEquals($instanceid1, $instanceinfo1['id']);
75         $this->assertEquals($course->id, $instanceinfo1['courseid']);
76         $this->assertEquals('self', $instanceinfo1['type']);
77         $this->assertEquals('Test instance 1', $instanceinfo1['name']);
78         $this->assertTrue($instanceinfo1['status']);
79         $this->assertFalse(isset($instanceinfo1['enrolpassword']));
81         $instanceinfo2 = enrol_self_external::get_instance_info($instanceid2);
82         $instanceinfo2 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2);
83         $this->assertEquals($instanceid2, $instanceinfo2['id']);
84         $this->assertEquals($course->id, $instanceinfo2['courseid']);
85         $this->assertEquals('self', $instanceinfo2['type']);
86         $this->assertEquals('Test instance 2', $instanceinfo2['name']);
87         $this->assertEquals(get_string('canntenrol', 'enrol_self'), $instanceinfo2['status']);
88         $this->assertFalse(isset($instanceinfo2['enrolpassword']));
90         $instanceinfo3 = enrol_self_external::get_instance_info($instanceid3);
91         $instanceinfo3 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3);
92         $this->assertEquals($instanceid3, $instanceinfo3['id']);
93         $this->assertEquals($course->id, $instanceinfo3['courseid']);
94         $this->assertEquals('self', $instanceinfo3['type']);
95         $this->assertEquals('Test instance 3', $instanceinfo3['name']);
96         $this->assertTrue($instanceinfo3['status']);
97         $this->assertEquals(get_string('password', 'enrol_self'), $instanceinfo3['enrolpassword']);
98     }
99 }