Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace enrol_manual;
18
 
19
use enrol_manual_external;
20
use externallib_advanced_testcase;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
 
26
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
27
require_once($CFG->dirroot . '/enrol/manual/externallib.php');
28
 
29
/**
30
 * Enrol manual external PHPunit tests
31
 *
32
 * @package    enrol_manual
33
 * @category   phpunit
34
 * @copyright  2012 Jerome Mouneyrac
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 36
 * @covers     \enrol_manual_external
1 efrain 37
 * @since Moodle 2.4
38
 */
1441 ariadna 39
final class externallib_test extends externallib_advanced_testcase {
1 efrain 40
 
41
    /**
42
     * Test get_enrolled_users
43
     */
11 efrain 44
    public function test_enrol_users(): void {
1 efrain 45
        global $DB;
46
 
47
        $this->resetAfterTest(true);
48
 
49
        $user = self::getDataGenerator()->create_user();
50
        $this->setUser($user);
51
 
52
        $course1 = self::getDataGenerator()->create_course();
53
        $course2 = self::getDataGenerator()->create_course();
54
        $user1 = self::getDataGenerator()->create_user();
55
        $user2 = self::getDataGenerator()->create_user();
56
 
57
        $context1 = \context_course::instance($course1->id);
58
        $context2 = \context_course::instance($course2->id);
59
        $instance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
60
        $instance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
61
 
62
        // Set the required capabilities by the external function.
63
        $roleid = $this->assignUserCapability('enrol/manual:enrol', $context1->id);
64
        $this->assignUserCapability('moodle/course:view', $context1->id, $roleid);
65
        $this->assignUserCapability('moodle/role:assign', $context1->id, $roleid);
66
        $this->assignUserCapability('enrol/manual:enrol', $context2->id, $roleid);
67
        $this->assignUserCapability('moodle/course:view', $context2->id, $roleid);
68
        $this->assignUserCapability('moodle/role:assign', $context2->id, $roleid);
69
 
70
        core_role_set_assign_allowed($roleid, 3);
71
 
72
        // Call the external function.
73
        enrol_manual_external::enrol_users(array(
74
            array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
75
            array('roleid' => 3, 'userid' => $user2->id, 'courseid' => $course1->id),
76
        ));
77
 
78
        $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid' => $instance1->id)));
79
        $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $instance2->id)));
80
        $this->assertTrue(is_enrolled($context1, $user1));
81
        $this->assertTrue(is_enrolled($context1, $user2));
82
 
83
        // Call without required capability.
84
        $DB->delete_records('user_enrolments');
85
        $this->unassignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
86
        try {
87
            enrol_manual_external::enrol_users(array(
88
                array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
89
            ));
90
            $this->fail('Exception expected if not having capability to enrol');
91
        } catch (\moodle_exception $e) {
92
            $this->assertInstanceOf('required_capability_exception', $e);
93
            $this->assertSame('nopermissions', $e->errorcode);
94
        }
95
        $this->assignUserCapability('enrol/manual:enrol', $context1->id, $roleid);
96
        $this->assertEquals(0, $DB->count_records('user_enrolments'));
97
 
1441 ariadna 98
        // Call with invalid user.
99
        try {
100
            enrol_manual_external::enrol_users([
101
                ['roleid' => 1, 'userid' => 654321, 'courseid' => $course1->id],
102
            ]);
103
            $this->fail('Exception expected for invalid user.');
104
        } catch (\moodle_exception $e) {
105
            $this->assertSame('invaliduser', $e->errorcode);
106
        }
107
 
1 efrain 108
        // Call with forbidden role.
109
        try {
110
            enrol_manual_external::enrol_users(array(
111
                array('roleid' => 1, 'userid' => $user1->id, 'courseid' => $course1->id),
112
            ));
113
            $this->fail('Exception expected if not allowed to assign role.');
114
        } catch (\moodle_exception $e) {
115
            $this->assertSame('wsusercannotassign', $e->errorcode);
116
        }
117
        $this->assertEquals(0, $DB->count_records('user_enrolments'));
118
 
119
        // Call for course without manual instance.
120
        $DB->delete_records('user_enrolments');
121
        $DB->delete_records('enrol', array('courseid' => $course2->id));
122
        try {
123
            enrol_manual_external::enrol_users(array(
124
                array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course1->id),
125
                array('roleid' => 3, 'userid' => $user1->id, 'courseid' => $course2->id),
126
            ));
127
            $this->fail('Exception expected if course does not have manual instance');
128
        } catch (\moodle_exception $e) {
129
            $this->assertSame('wsnoinstance', $e->errorcode);
130
            $this->assertSame(
131
                "Manual enrolment plugin instance doesn't exist or is disabled for the course (id = {$course2->id})",
132
                $e->getMessage()
133
            );
134
        }
135
    }
136
 
137
    /**
138
     * Test for unerolling a single user.
139
     * @throws coding_exception
140
     * @throws invalid_parameter_exception
141
     * @throws moodle_exception
142
     */
11 efrain 143
    public function test_unenrol_user_single(): void {
1 efrain 144
        global $CFG, $DB;
145
        require_once($CFG->libdir . '/enrollib.php');
146
        $this->resetAfterTest(true);
147
        // The user who perform the action.
148
        $user = $this->getDataGenerator()->create_user();
149
        $this->setUser($user); // Log this user in.
150
        $enrol = enrol_get_plugin('manual');
151
        // Create a course.
152
        $course = self::getDataGenerator()->create_course();
153
        $coursecontext = \context_course::instance($course->id);
154
        $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
155
        // Set the capability for the user.
156
        $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext);
157
        $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
158
        $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid);
159
        $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid);
160
        // Create a student and enrol them into the course.
161
        $student = $this->getDataGenerator()->create_user();
162
        $enrol->enrol_user($enrolinstance, $student->id);
163
        $this->assertTrue(is_enrolled($coursecontext, $student));
164
        // Call the web service to unenrol.
165
        enrol_manual_external::unenrol_users(array(
166
            array('userid' => $student->id, 'courseid' => $course->id),
167
        ));
168
        $this->assertFalse(is_enrolled($coursecontext, $student));
169
    }
170
 
171
    /**
172
     * Test for unenrolling multiple users.
173
     * @throws coding_exception
174
     * @throws invalid_parameter_exception
175
     * @throws moodle_exception
176
     */
11 efrain 177
    public function test_unenrol_user_multiple(): void {
1 efrain 178
        global $CFG, $DB;
179
        require_once($CFG->libdir . '/enrollib.php');
180
        $this->resetAfterTest(true);
181
        // The user who perform the action.
182
        $user = $this->getDataGenerator()->create_user();
183
        $this->setUser($user); // Log this user in.
184
        // Create a course.
185
        $course = self::getDataGenerator()->create_course();
186
        $coursecontext = \context_course::instance($course->id);
187
        $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
188
        // Set the capability for the user.
189
        $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext);
190
        $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
191
        $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid);
192
        $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid);
193
        $enrol = enrol_get_plugin('manual');
194
        // Create a student and enrol them into the course.
195
        $student1 = $this->getDataGenerator()->create_user();
196
        $enrol->enrol_user($enrolinstance, $student1->id);
197
        $this->assertTrue(is_enrolled($coursecontext, $student1));
198
        $student2 = $this->getDataGenerator()->create_user();
199
        $enrol->enrol_user($enrolinstance, $student2->id);
200
        $this->assertTrue(is_enrolled($coursecontext, $student2));
201
        // Call the web service to unenrol.
202
        enrol_manual_external::unenrol_users(array(
203
            array('userid' => $student1->id, 'courseid' => $course->id),
204
            array('userid' => $student2->id, 'courseid' => $course->id),
205
        ));
206
        $this->assertFalse(is_enrolled($coursecontext, $student1));
207
        $this->assertFalse(is_enrolled($coursecontext, $student2));
208
    }
209
 
210
    /**
211
     * Test for unenrol capability.
212
     * @throws coding_exception
213
     * @throws invalid_parameter_exception
214
     * @throws moodle_exception
215
     */
11 efrain 216
    public function test_unenrol_user_error_no_capability(): void {
1 efrain 217
        global $CFG, $DB;
218
        require_once($CFG->libdir . '/enrollib.php');
219
        $this->resetAfterTest(true);
220
        // The user who perform the action.
221
        $user = $this->getDataGenerator()->create_user();
222
        $this->setUser($user); // Log this user in.
223
        // Create a course.
224
        $course = self::getDataGenerator()->create_course();
225
        $coursecontext = \context_course::instance($course->id);
226
        $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
227
        $enrol = enrol_get_plugin('manual');
228
        // Create a student and enrol them into the course.
229
        $student = $this->getDataGenerator()->create_user();
230
        $enrol->enrol_user($enrolinstance, $student->id);
231
        $this->assertTrue(is_enrolled($coursecontext, $student));
232
        // Call the web service to unenrol.
233
        try {
234
            enrol_manual_external::unenrol_users(array(
235
                array('userid' => $student->id, 'courseid' => $course->id),
236
            ));
237
            $this->fail('Exception expected: User cannot log in to the course');
238
        } catch (\Exception $ex) {
239
            $this->assertTrue($ex instanceof \require_login_exception);
240
        }
241
        // Set the capability for the course, then try again.
242
        $roleid = $this->assignUserCapability('moodle/course:view', $coursecontext);
243
        try {
244
            enrol_manual_external::unenrol_users(array(
245
                array('userid' => $student->id, 'courseid' => $course->id),
246
            ));
247
            $this->fail('Exception expected: User cannot log in to the course');
248
        } catch (\Exception $ex) {
249
            $this->assertTrue($ex instanceof \required_capability_exception);
250
        }
251
        // Assign unenrol capability.
252
        $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
253
        enrol_manual_external::unenrol_users(array(
254
            array('userid' => $student->id, 'courseid' => $course->id),
255
        ));
256
        $this->assertFalse(is_enrolled($coursecontext, $student));
257
    }
258
 
259
    /**
260
     * Test for unenrol if user does not exist.
261
     * @throws coding_exception
262
     */
11 efrain 263
    public function test_unenrol_user_error_not_exist(): void {
1 efrain 264
        global $CFG, $DB;
265
        require_once($CFG->libdir . '/enrollib.php');
266
        $this->resetAfterTest(true);
267
        // The user who perform the action.
268
        $user = $this->getDataGenerator()->create_user();
269
        $this->setUser($user); // Log this user in.
270
        $enrol = enrol_get_plugin('manual');
271
        // Create a course.
272
        $course = self::getDataGenerator()->create_course();
273
        $coursecontext = \context_course::instance($course->id);
274
        $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
275
        // Set the capability for the user.
276
        $roleid = $this->assignUserCapability('enrol/manual:enrol', $coursecontext);
277
        $this->assignUserCapability('enrol/manual:unenrol', $coursecontext, $roleid);
278
        $this->assignUserCapability('moodle/course:view', $coursecontext, $roleid);
279
        $this->assignUserCapability('moodle/role:assign', $coursecontext, $roleid);
280
        // Create a student and enrol them into the course.
281
        $student = $this->getDataGenerator()->create_user();
282
        $enrol->enrol_user($enrolinstance, $student->id);
283
        $this->assertTrue(is_enrolled($coursecontext, $student));
284
        try {
285
            enrol_manual_external::unenrol_users(array(
286
                array('userid' => $student->id + 1, 'courseid' => $course->id),
287
            ));
288
            $this->fail('Exception expected: invalid student id');
289
        } catch (\Exception $ex) {
1441 ariadna 290
            $this->assertSame('invaliduser', $ex->errorcode);
1 efrain 291
        }
292
 
293
        // Call for course without manual instance.
294
        $DB->delete_records('user_enrolments');
295
        $DB->delete_records('enrol', ['courseid' => $course->id]);
296
        try {
297
            enrol_manual_external::unenrol_users(array(
298
                array('userid' => $student->id + 1, 'courseid' => $course->id),
299
            ));
300
            $this->fail('Exception expected if course does not have manual instance');
301
        } catch (\moodle_exception $e) {
302
            $this->assertSame('wsnoinstance', $e->errorcode);
303
            $this->assertSame(
304
                "Manual enrolment plugin instance doesn't exist or is disabled for the course (id = {$course->id})",
305
                $e->getMessage()
306
            );
307
        }
308
    }
309
}