MDL-43949 groups: Add idnumber to web services
[moodle.git] / group / 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  * Group external PHPunit tests
19  *
20  * @package    core_group
21  * @category   external
22  * @copyright  2012 Jerome Mouneyrac
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @since Moodle 2.4
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/group/externallib.php');
33 require_once($CFG->dirroot . '/group/lib.php');
35 class core_group_externallib_testcase extends externallib_advanced_testcase {
37     /**
38      * Test create_groups
39      */
40     public function test_create_groups() {
41         global $DB;
43         $this->resetAfterTest(true);
45         $course  = self::getDataGenerator()->create_course();
47         $group1 = array();
48         $group1['courseid'] = $course->id;
49         $group1['name'] = 'Group Test 1';
50         $group1['description'] = 'Group Test 1 description';
51         $group1['descriptionformat'] = FORMAT_MOODLE;
52         $group1['enrolmentkey'] = 'Test group enrol secret phrase';
53         $group1['idnumber'] = 'TEST1';
54         $group2 = array();
55         $group2['courseid'] = $course->id;
56         $group2['name'] = 'Group Test 2';
57         $group2['description'] = 'Group Test 2 description';
58         $group3 = array();
59         $group3['courseid'] = $course->id;
60         $group3['name'] = 'Group Test 3';
61         $group3['description'] = 'Group Test 3 description';
62         $group3['idnumber'] = 'TEST1';
63         $group4 = array();
64         $group4['courseid'] = $course->id;
65         $group4['name'] = 'Group Test 4';
66         $group4['description'] = 'Group Test 4 description';
68         // Set the required capabilities by the external function
69         $context = context_course::instance($course->id);
70         $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
71         $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
73         // Call the external function.
74         $groups = core_group_external::create_groups(array($group1, $group2));
76         // We need to execute the return values cleaning process to simulate the web service server.
77         $groups = external_api::clean_returnvalue(core_group_external::create_groups_returns(), $groups);
79         // Checks against DB values
80         $this->assertEquals(2, count($groups));
81         foreach ($groups as $group) {
82             $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
83             switch ($dbgroup->name) {
84                 case $group1['name']:
85                     $groupdescription = $group1['description'];
86                     $groupcourseid = $group1['courseid'];
87                     $this->assertEquals($dbgroup->descriptionformat, $group1['descriptionformat']);
88                     $this->assertEquals($dbgroup->enrolmentkey, $group1['enrolmentkey']);
89                     $this->assertEquals($dbgroup->idnumber, $group1['idnumber']);
90                     break;
91                 case $group2['name']:
92                     $groupdescription = $group2['description'];
93                     $groupcourseid = $group2['courseid'];
94                     break;
95                 default:
96                     throw new moodle_exception('unknowgroupname');
97                     break;
98             }
99             $this->assertEquals($dbgroup->description, $groupdescription);
100             $this->assertEquals($dbgroup->courseid, $groupcourseid);
101         }
103         try {
104             $froups = core_group_external::create_groups(array($group3));
105             $this->fail('Exception expected due to already existing idnumber.');
106         } catch (moodle_exception $e) {
107             $this->assertInstanceOf('invalid_parameter_exception', $e);
108             $this->assertEquals('Invalid parameter value detected (Group with the same idnumber already exists)',
109                 $e->getMessage());
110         }
112         // Call without required capability
113         $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
114         $this->setExpectedException('required_capability_exception');
115         $froups = core_group_external::create_groups(array($group4));
116     }
118     /**
119      * Test get_groups
120      */
121     public function test_get_groups() {
122         global $DB;
124         $this->resetAfterTest(true);
126         $course = self::getDataGenerator()->create_course();
127         $group1data = array();
128         $group1data['courseid'] = $course->id;
129         $group1data['name'] = 'Group Test 1';
130         $group1data['description'] = 'Group Test 1 description';
131         $group1data['descriptionformat'] = FORMAT_MOODLE;
132         $group1data['enrolmentkey'] = 'Test group enrol secret phrase';
133         $group1data['idnumber'] = 'TEST1';
134         $group2data = array();
135         $group2data['courseid'] = $course->id;
136         $group2data['name'] = 'Group Test 2';
137         $group2data['description'] = 'Group Test 2 description';
138         $group1 = self::getDataGenerator()->create_group($group1data);
139         $group2 = self::getDataGenerator()->create_group($group2data);
141         // Set the required capabilities by the external function
142         $context = context_course::instance($course->id);
143         $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
144         $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
146         // Call the external function.
147         $groups = core_group_external::get_groups(array($group1->id, $group2->id));
149         // We need to execute the return values cleaning process to simulate the web service server.
150         $groups = external_api::clean_returnvalue(core_group_external::get_groups_returns(), $groups);
152         // Checks against DB values
153         $this->assertEquals(2, count($groups));
154         foreach ($groups as $group) {
155             $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
156             switch ($dbgroup->name) {
157                 case $group1->name:
158                     $groupdescription = $group1->description;
159                     $groupcourseid = $group1->courseid;
160                     $this->assertEquals($dbgroup->descriptionformat, $group1->descriptionformat);
161                     $this->assertEquals($dbgroup->enrolmentkey, $group1->enrolmentkey);
162                     $this->assertEquals($dbgroup->idnumber, $group1->idnumber);
163                     break;
164                 case $group2->name:
165                     $groupdescription = $group2->description;
166                     $groupcourseid = $group2->courseid;
167                     break;
168                 default:
169                     throw new moodle_exception('unknowgroupname');
170                     break;
171             }
172             $this->assertEquals($dbgroup->description, $groupdescription);
173             $this->assertEquals($dbgroup->courseid, $groupcourseid);
174         }
176         // Call without required capability
177         $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
178         $this->setExpectedException('required_capability_exception');
179         $groups = core_group_external::get_groups(array($group1->id, $group2->id));
180     }
182     /**
183      * Test delete_groups
184      */
185     public function test_delete_groups() {
186         global $DB;
188         $this->resetAfterTest(true);
190         $course = self::getDataGenerator()->create_course();
191         $group1data = array();
192         $group1data['courseid'] = $course->id;
193         $group1data['name'] = 'Group Test 1';
194         $group1data['description'] = 'Group Test 1 description';
195         $group1data['descriptionformat'] = FORMAT_MOODLE;
196         $group1data['enrolmentkey'] = 'Test group enrol secret phrase';
197         $group2data = array();
198         $group2data['courseid'] = $course->id;
199         $group2data['name'] = 'Group Test 2';
200         $group2data['description'] = 'Group Test 2 description';
201         $group3data['courseid'] = $course->id;
202         $group3data['name'] = 'Group Test 3';
203         $group3data['description'] = 'Group Test 3 description';
204         $group1 = self::getDataGenerator()->create_group($group1data);
205         $group2 = self::getDataGenerator()->create_group($group2data);
206         $group3 = self::getDataGenerator()->create_group($group3data);
208         // Set the required capabilities by the external function
209         $context = context_course::instance($course->id);
210         $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
211         $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
213         // Checks against DB values
214         $groupstotal = $DB->count_records('groups', array());
215         $this->assertEquals(3, $groupstotal);
217         // Call the external function.
218         core_group_external::delete_groups(array($group1->id, $group2->id));
220         // Checks against DB values
221         $groupstotal = $DB->count_records('groups', array());
222         $this->assertEquals(1, $groupstotal);
224         // Call without required capability
225         $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
226         $this->setExpectedException('required_capability_exception');
227         $froups = core_group_external::delete_groups(array($group3->id));
228     }
230     /**
231      * Test create and update groupings.
232      * @return void
233      */
234     public function test_create_update_groupings() {
235         global $DB;
237         $this->resetAfterTest(true);
239         $this->setAdminUser();
241         $course = self::getDataGenerator()->create_course();
243         $grouping1data = array();
244         $grouping1data['courseid'] = $course->id;
245         $grouping1data['name'] = 'Grouping 1 Test';
246         $grouping1data['description'] = 'Grouping 1 Test description';
247         $grouping1data['descriptionformat'] = FORMAT_MOODLE;
248         $grouping1data['idnumber'] = 'TEST';
250         $grouping1 = self::getDataGenerator()->create_grouping($grouping1data);
252         $grouping1data['name'] = 'Another group';
254         try {
255             $groupings = core_group_external::create_groupings(array($grouping1data));
256             $this->fail('Exception expected due to already existing idnumber.');
257         } catch (moodle_exception $e) {
258             $this->assertInstanceOf('invalid_parameter_exception', $e);
259             $this->assertEquals('Invalid parameter value detected (Grouping with the same idnumber already exists)',
260                 $e->getMessage());
261         }
263         // No exception should be triggered.
264         $grouping1data['id'] = $grouping1->id;
265         $grouping1data['idnumber'] = 'CHANGED';
266         unset($grouping1data['courseid']);
267         core_group_external::update_groupings(array($grouping1data));
269         $grouping2data = array();
270         $grouping2data['courseid'] = $course->id;
271         $grouping2data['name'] = 'Grouping 2 Test';
272         $grouping2data['description'] = 'Grouping 2 Test description';
273         $grouping2data['descriptionformat'] = FORMAT_MOODLE;
274         $grouping2data['idnumber'] = 'TEST';
276         $grouping2 = self::getDataGenerator()->create_grouping($grouping2data);
278         $grouping2data['id'] = $grouping2->id;
279         $grouping2data['idnumber'] = 'CHANGED';
280         unset($grouping2data['courseid']);
281         try {
282             $groupings = core_group_external::update_groupings(array($grouping2data));
283             $this->fail('Exception expected due to already existing idnumber.');
284         } catch (moodle_exception $e) {
285             $this->assertInstanceOf('invalid_parameter_exception', $e);
286             $this->assertEquals('Invalid parameter value detected (A different grouping with the same idnumber already exists)',
287                 $e->getMessage());
288         }
289     }
291     /**
292      * Test get_groupings
293      */
294     public function test_get_groupings() {
295         global $DB;
297         $this->resetAfterTest(true);
299         $course = self::getDataGenerator()->create_course();
301         $groupingdata = array();
302         $groupingdata['courseid'] = $course->id;
303         $groupingdata['name'] = 'Grouping Test';
304         $groupingdata['description'] = 'Grouping Test description';
305         $groupingdata['descriptionformat'] = FORMAT_MOODLE;
307         $grouping = self::getDataGenerator()->create_grouping($groupingdata);
309         // Set the required capabilities by the external function.
310         $context = context_course::instance($course->id);
311         $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
312         $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
314         // Call the external function without specifying the optional parameter.
315         $groupings = core_group_external::get_groupings(array($grouping->id));
316         // We need to execute the return values cleaning process to simulate the web service server.
317         $groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings);
319         $this->assertEquals(1, count($groupings));
321         $group1data = array();
322         $group1data['courseid'] = $course->id;
323         $group1data['name'] = 'Group Test 1';
324         $group1data['description'] = 'Group Test 1 description';
325         $group1data['descriptionformat'] = FORMAT_MOODLE;
326         $group2data = array();
327         $group2data['courseid'] = $course->id;
328         $group2data['name'] = 'Group Test 2';
329         $group2data['description'] = 'Group Test 2 description';
330         $group2data['descriptionformat'] = FORMAT_MOODLE;
332         $group1 = self::getDataGenerator()->create_group($group1data);
333         $group2 = self::getDataGenerator()->create_group($group2data);
335         groups_assign_grouping($grouping->id, $group1->id);
336         groups_assign_grouping($grouping->id, $group2->id);
338         // Call the external function specifying that groups are returned.
339         $groupings = core_group_external::get_groupings(array($grouping->id), true);
340         // We need to execute the return values cleaning process to simulate the web service server.
341         $groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings);
342         $this->assertEquals(1, count($groupings));
343         $this->assertEquals(2, count($groupings[0]['groups']));
344         foreach ($groupings[0]['groups'] as $group) {
345             $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
346             $dbgroupinggroups = $DB->get_record('groupings_groups',
347                                                 array('groupingid' => $groupings[0]['id'],
348                                                       'groupid' => $group['id']),
349                                                 '*', MUST_EXIST);
350             switch ($dbgroup->name) {
351                 case $group1->name:
352                     $groupdescription = $group1->description;
353                     $groupcourseid = $group1->courseid;
354                     break;
355                 case $group2->name:
356                     $groupdescription = $group2->description;
357                     $groupcourseid = $group2->courseid;
358                     break;
359                 default:
360                     throw new moodle_exception('unknowgroupname');
361                     break;
362             }
363             $this->assertEquals($dbgroup->description, $groupdescription);
364             $this->assertEquals($dbgroup->courseid, $groupcourseid);
365         }
366     }
368     /**
369      * Test get_groups
370      */
371     public function test_get_course_user_groups() {
372         global $DB;
374         $this->resetAfterTest(true);
376         $student1 = self::getDataGenerator()->create_user();
377         $student2 = self::getDataGenerator()->create_user();
378         $teacher = self::getDataGenerator()->create_user();
380         $course = self::getDataGenerator()->create_course();
381         $emptycourse = self::getDataGenerator()->create_course();
383         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
384         $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
385         $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
387         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
388         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
389         $this->getDataGenerator()->enrol_user($teacher->id, $emptycourse->id, $teacherrole->id);
391         $group1data = array();
392         $group1data['courseid'] = $course->id;
393         $group1data['name'] = 'Group Test 1';
394         $group1data['description'] = 'Group Test 1 description';
395         $group1data['idnumber'] = 'TEST1';
396         $group2data = array();
397         $group2data['courseid'] = $course->id;
398         $group2data['name'] = 'Group Test 2';
399         $group2data['description'] = 'Group Test 2 description';
400         $group1 = self::getDataGenerator()->create_group($group1data);
401         $group2 = self::getDataGenerator()->create_group($group2data);
403         groups_add_member($group1->id, $student1->id);
404         groups_add_member($group1->id, $student2->id);
405         groups_add_member($group2->id, $student1->id);
407         $this->setUser($student1);
409         $groups = core_group_external::get_course_user_groups($course->id, $student1->id);
410         $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
411         // Check that I see my groups.
412         $this->assertCount(2, $groups['groups']);
414         $this->setUser($student2);
415         $groups = core_group_external::get_course_user_groups($course->id, $student2->id);
416         $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
417         // Check that I see my groups.
418         $this->assertCount(1, $groups['groups']);
420         $this->assertEquals($group1data['name'], $groups['groups'][0]['name']);
421         $this->assertEquals($group1data['description'], $groups['groups'][0]['description']);
422         $this->assertEquals($group1data['idnumber'], $groups['groups'][0]['idnumber']);
424         $this->setUser($teacher);
425         $groups = core_group_external::get_course_user_groups($course->id, $student1->id);
426         $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
427         // Check that a teacher can see student groups.
428         $this->assertCount(2, $groups['groups']);
430         $groups = core_group_external::get_course_user_groups($course->id, $student2->id);
431         $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
432         // Check that a teacher can see student groups.
433         $this->assertCount(1, $groups['groups']);
435         // Check permissions.
436         $this->setUser($student1);
437         try {
438             $groups = core_group_external::get_course_user_groups($course->id, $student2->id);
439         } catch (moodle_exception $e) {
440             $this->assertEquals('accessdenied', $e->errorcode);
441         }
443         try {
444             $groups = core_group_external::get_course_user_groups($emptycourse->id, $student2->id);
445         } catch (moodle_exception $e) {
446             $this->assertEquals('requireloginerror', $e->errorcode);
447         }
449         $this->setUser($teacher);
450         // Check warnings.
451         $groups = core_group_external::get_course_user_groups($emptycourse->id, $student1->id);
452         $groups = external_api::clean_returnvalue(core_group_external::get_course_user_groups_returns(), $groups);
453         $this->assertCount(1, $groups['warnings']);
455     }