weekly release 4.0dev
[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         $coursedata = new stdClass();
51         $coursedata->visible = 0;
52         $course = self::getDataGenerator()->create_course($coursedata);
54         // Add enrolment methods for course.
55         $instanceid1 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
56                                                                 'name' => 'Test instance 1',
57                                                                 'customint6' => 1,
58                                                                 'roleid' => $studentrole->id));
59         $instanceid2 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_DISABLED,
60                                                                 'customint6' => 1,
61                                                                 'name' => 'Test instance 2',
62                                                                 'roleid' => $studentrole->id));
64         $instanceid3 = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
65                                                                 'roleid' => $studentrole->id,
66                                                                 'customint6' => 1,
67                                                                 'name' => 'Test instance 3',
68                                                                 'password' => 'test'));
70         $enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course->id, 'status' => ENROL_INSTANCE_ENABLED));
71         $this->assertCount(3, $enrolmentmethods);
73         $this->setAdminUser();
74         $instanceinfo1 = enrol_self_external::get_instance_info($instanceid1);
75         $instanceinfo1 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo1);
77         $this->assertEquals($instanceid1, $instanceinfo1['id']);
78         $this->assertEquals($course->id, $instanceinfo1['courseid']);
79         $this->assertEquals('self', $instanceinfo1['type']);
80         $this->assertEquals('Test instance 1', $instanceinfo1['name']);
81         $this->assertTrue($instanceinfo1['status']);
82         $this->assertFalse(isset($instanceinfo1['enrolpassword']));
84         $instanceinfo2 = enrol_self_external::get_instance_info($instanceid2);
85         $instanceinfo2 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo2);
86         $this->assertEquals($instanceid2, $instanceinfo2['id']);
87         $this->assertEquals($course->id, $instanceinfo2['courseid']);
88         $this->assertEquals('self', $instanceinfo2['type']);
89         $this->assertEquals('Test instance 2', $instanceinfo2['name']);
90         $this->assertEquals(get_string('canntenrol', 'enrol_self'), $instanceinfo2['status']);
91         $this->assertFalse(isset($instanceinfo2['enrolpassword']));
93         $instanceinfo3 = enrol_self_external::get_instance_info($instanceid3);
94         $instanceinfo3 = external_api::clean_returnvalue(enrol_self_external::get_instance_info_returns(), $instanceinfo3);
95         $this->assertEquals($instanceid3, $instanceinfo3['id']);
96         $this->assertEquals($course->id, $instanceinfo3['courseid']);
97         $this->assertEquals('self', $instanceinfo3['type']);
98         $this->assertEquals('Test instance 3', $instanceinfo3['name']);
99         $this->assertTrue($instanceinfo3['status']);
100         $this->assertEquals(get_string('password', 'enrol_self'), $instanceinfo3['enrolpassword']);
102         // Try to retrieve information using a normal user for a hidden course.
103         $user = self::getDataGenerator()->create_user();
104         $this->setUser($user);
105         try {
106             enrol_self_external::get_instance_info($instanceid3);
107         } catch (moodle_exception $e) {
108             $this->assertEquals('coursehidden', $e->errorcode);
109         }
110     }
112     /**
113      * Test enrol_user
114      */
115     public function test_enrol_user() {
116         global $DB;
118         self::resetAfterTest(true);
120         $user = self::getDataGenerator()->create_user();
121         self::setUser($user);
123         $course1 = self::getDataGenerator()->create_course();
124         $course2 = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
125         $user1 = self::getDataGenerator()->create_user();
126         $user2 = self::getDataGenerator()->create_user();
127         $user3 = self::getDataGenerator()->create_user();
128         $user4 = self::getDataGenerator()->create_user();
130         $context1 = context_course::instance($course1->id);
131         $context2 = context_course::instance($course2->id);
133         $selfplugin = enrol_get_plugin('self');
134         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
135         $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
136                                                                 'name' => 'Test instance 1',
137                                                                 'customint6' => 1,
138                                                                 'roleid' => $studentrole->id));
139         $instance2id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_DISABLED,
140                                                                 'customint6' => 1,
141                                                                 'name' => 'Test instance 2',
142                                                                 'roleid' => $studentrole->id));
143         $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
144         $instance2 = $DB->get_record('enrol', array('id' => $instance2id), '*', MUST_EXIST);
146         self::setUser($user1);
148         // Self enrol me.
149         $result = enrol_self_external::enrol_user($course1->id);
150         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
152         self::assertTrue($result['status']);
153         self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
154         self::assertTrue(is_enrolled($context1, $user1));
156         // Add password.
157         $instance2->password = 'abcdef';
158         $DB->update_record('enrol', $instance2);
160         // Try instance not enabled.
161         try {
162             enrol_self_external::enrol_user($course2->id);
163         } catch (moodle_exception $e) {
164             self::assertEquals('canntenrol', $e->errorcode);
165         }
167         // Enable the instance.
168         $selfplugin->update_status($instance2, ENROL_INSTANCE_ENABLED);
170         // Try not passing a key.
171         $result = enrol_self_external::enrol_user($course2->id);
172         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
173         self::assertFalse($result['status']);
174         self::assertCount(1, $result['warnings']);
175         self::assertEquals('4', $result['warnings'][0]['warningcode']);
177         // Try passing an invalid key.
178         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
179         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
180         self::assertFalse($result['status']);
181         self::assertCount(1, $result['warnings']);
182         self::assertEquals('4', $result['warnings'][0]['warningcode']);
184         // Try passing an invalid key with hint.
185         $selfplugin->set_config('showhint', true);
186         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
187         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
188         self::assertFalse($result['status']);
189         self::assertCount(1, $result['warnings']);
190         self::assertEquals('3', $result['warnings'][0]['warningcode']);
192         // Everything correct, now.
193         $result = enrol_self_external::enrol_user($course2->id, 'abcdef');
194         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
196         self::assertTrue($result['status']);
197         self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
198         self::assertTrue(is_enrolled($context2, $user1));
200         // Try group password now, other user.
201         $instance2->customint1 = 1;
202         $instance2->password = 'zyx';
203         $DB->update_record('enrol', $instance2);
205         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
206         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enrolmentkey' => 'zyx'));
208         self::setUser($user2);
209         // Try passing and invalid key for group.
210         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
211         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
212         self::assertFalse($result['status']);
213         self::assertCount(1, $result['warnings']);
214         self::assertEquals('2', $result['warnings'][0]['warningcode']);
216         // Now, everything ok.
217         $result = enrol_self_external::enrol_user($course2->id, 'zyx');
218         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
220         self::assertTrue($result['status']);
221         self::assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
222         self::assertTrue(is_enrolled($context2, $user2));
224         // Try multiple instances now, multiple errors.
225         $instance3id = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_ENABLED,
226                                                                 'customint6' => 1,
227                                                                 'name' => 'Test instance 2',
228                                                                 'roleid' => $studentrole->id));
229         $instance3 = $DB->get_record('enrol', array('id' => $instance3id), '*', MUST_EXIST);
230         $instance3->password = 'abcdef';
231         $DB->update_record('enrol', $instance3);
233         self::setUser($user3);
234         $result = enrol_self_external::enrol_user($course2->id, 'invalidkey');
235         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
236         self::assertFalse($result['status']);
237         self::assertCount(2, $result['warnings']);
239         // Now, everything ok.
240         $result = enrol_self_external::enrol_user($course2->id, 'zyx');
241         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
242         self::assertTrue($result['status']);
243         self::assertTrue(is_enrolled($context2, $user3));
245         // Now test passing an instance id.
246         self::setUser($user4);
247         $result = enrol_self_external::enrol_user($course2->id, 'abcdef', $instance3id);
248         $result = external_api::clean_returnvalue(enrol_self_external::enrol_user_returns(), $result);
249         self::assertTrue($result['status']);
250         self::assertTrue(is_enrolled($context2, $user3));
251         self::assertCount(0, $result['warnings']);
252         self::assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $instance3->id)));
253     }