MDL-51568 self_enrol: New Web Service enrol_self_enrol_user
[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     }
100     /**
101      * Test enrol_user
102      */
103     public function test_enrol_user() {
104         global $DB;
106         self::resetAfterTest(true);
108         $user = self::getDataGenerator()->create_user();
109         self::setUser($user);
111         $course1 = self::getDataGenerator()->create_course();
112         $course2 = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
113         $user1 = self::getDataGenerator()->create_user();
114         $user2 = self::getDataGenerator()->create_user();
115         $user3 = self::getDataGenerator()->create_user();
116         $user4 = self::getDataGenerator()->create_user();
118         $context1 = context_course::instance($course1->id);
119         $context2 = context_course::instance($course2->id);
121         $selfplugin = enrol_get_plugin('self');
122         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
123         $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
124                                                                 'name' => 'Test instance 1',
125                                                                 'customint6' => 1,
126                                                                 'roleid' => $studentrole->id));
127         $instance2id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_DISABLED,
128                                                                 'customint6' => 1,
129                                                                 'name' => 'Test instance 2',
130                                                                 'roleid' => $studentrole->id));
131         $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
132         $instance2 = $DB->get_record('enrol', array('id' => $instance2id), '*', MUST_EXIST);
134         self::setUser($user1);
136         // Self enrol me.
137         $result = enrol_self_external::enrol_user($course1->id);
138         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
140         self::assertTrue($result['status']);
141         self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
142         self::assertTrue(is_enrolled($context1, $user1));
144         // Add password.
145         $instance2->password = 'abcdef';
146         $DB->update_record('enrol', $instance2);
148         // Try instance not enabled.
149         try {
150             enrol_self_external::enrol_user($course2->id);
151         } catch (moodle_exception $e) {
152             self::assertEquals('canntenrol', $e->errorcode);
153         }
155         // Enable the instance.
156         $selfplugin->update_status($instance2, ENROL_INSTANCE_ENABLED);
158         // Try not passing a key.
159         $result = enrol_self_external::enrol_user($course2->id);
160         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
161         self::assertFalse($result['status']);
162         self::assertCount(1, $result['warnings']);
163         self::assertEquals('4', $result['warnings'][0]['warningcode']);
165         // Try passing an invalid key.
166         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
167         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
168         self::assertFalse($result['status']);
169         self::assertCount(1, $result['warnings']);
170         self::assertEquals('4', $result['warnings'][0]['warningcode']);
172         // Try passing an invalid key with hint.
173         $selfplugin->set_config('showhint', true);
174         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
175         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
176         self::assertFalse($result['status']);
177         self::assertCount(1, $result['warnings']);
178         self::assertEquals('3', $result['warnings'][0]['warningcode']);
180         // Everything correct, now.
181         $result = enrol_self_external::enrol_user($course2->id, 'abcdef');
182         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
184         self::assertTrue($result['status']);
185         self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
186         self::assertTrue(is_enrolled($context2, $user1));
188         // Try group password now, other user.
189         $instance2->customint1 = 1;
190         $instance2->password = 'zyx';
191         $DB->update_record('enrol', $instance2);
193         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
194         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enrolmentkey' => 'zyx'));
196         self::setUser($user2);
197         // Try passing and invalid key for group.
198         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
199         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
200         self::assertFalse($result['status']);
201         self::assertCount(1, $result['warnings']);
202         self::assertEquals('2', $result['warnings'][0]['warningcode']);
204         // Now, everything ok.
205         $result = enrol_self_external::enrol_user($course2->id, 'zyx');
206         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
208         self::assertTrue($result['status']);
209         self::assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
210         self::assertTrue(is_enrolled($context2, $user2));
212         // Try multiple instances now, multiple errors.
213         $instance3id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_ENABLED,
214                                                                 'customint6' => 1,
215                                                                 'name' => 'Test instance 2',
216                                                                 'roleid' => $studentrole->id));
217         $instance3 = $DB->get_record('enrol', array('id' => $instance3id), '*', MUST_EXIST);
218         $instance3->password = 'abcdef';
219         $DB->update_record('enrol', $instance3);
221         self::setUser($user3);
222         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
223         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
224         self::assertFalse($result['status']);
225         self::assertCount(2, $result['warnings']);
227         // Now, everything ok.
228         $result = enrol_self_external::enrol_user($course2->id, 'zyx');
229         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
230         self::assertTrue($result['status']);
231         self::assertTrue(is_enrolled($context2, $user3));
233         // Now test passing an instance id.
234         self::setUser($user4);
235         $result = enrol_self_external::enrol_user($course2->id, 'abcdef', $instance3id);
236         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
237         self::assertTrue($result['status']);
238         self::assertTrue(is_enrolled($context2, $user3));
239         self::assertCount(0, $result['warnings']);
240         self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance3->id)));
241     }