Revert "Merge branch 'MDL-43127-27' of git://github.com/FMCorz/moodle into MOODLE_27_...
[moodle.git] / group / tests / externallib_test.php
blobd3ca01ec3c27172e949ee427207c617d82b01e75
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.
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
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
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
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 $group2 = array();
54 $group2['courseid'] = $course->id;
55 $group2['name'] = 'Group Test 2';
56 $group2['description'] = 'Group Test 2 description';
57 $group3 = array();
58 $group3['courseid'] = $course->id;
59 $group3['name'] = 'Group Test 3';
60 $group3['description'] = 'Group Test 3 description';
62 // Set the required capabilities by the external function
63 $context = context_course::instance($course->id);
64 $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
65 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
67 // Call the external function.
68 $groups = core_group_external::create_groups(array($group1, $group2));
70 // We need to execute the return values cleaning process to simulate the web service server.
71 $groups = external_api::clean_returnvalue(core_group_external::create_groups_returns(), $groups);
73 // Checks against DB values
74 $this->assertEquals(2, count($groups));
75 foreach ($groups as $group) {
76 $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
77 switch ($dbgroup->name) {
78 case $group1['name']:
79 $groupdescription = $group1['description'];
80 $groupcourseid = $group1['courseid'];
81 $this->assertEquals($dbgroup->descriptionformat, $group1['descriptionformat']);
82 $this->assertEquals($dbgroup->enrolmentkey, $group1['enrolmentkey']);
83 break;
84 case $group2['name']:
85 $groupdescription = $group2['description'];
86 $groupcourseid = $group2['courseid'];
87 break;
88 default:
89 throw new moodle_exception('unknowgroupname');
90 break;
92 $this->assertEquals($dbgroup->description, $groupdescription);
93 $this->assertEquals($dbgroup->courseid, $groupcourseid);
96 // Call without required capability
97 $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
98 $this->setExpectedException('required_capability_exception');
99 $froups = core_group_external::create_groups(array($group3));
103 * Test get_groups
105 public function test_get_groups() {
106 global $DB;
108 $this->resetAfterTest(true);
110 $course = self::getDataGenerator()->create_course();
111 $group1data = array();
112 $group1data['courseid'] = $course->id;
113 $group1data['name'] = 'Group Test 1';
114 $group1data['description'] = 'Group Test 1 description';
115 $group1data['descriptionformat'] = FORMAT_MOODLE;
116 $group1data['enrolmentkey'] = 'Test group enrol secret phrase';
117 $group2data = array();
118 $group2data['courseid'] = $course->id;
119 $group2data['name'] = 'Group Test 2';
120 $group2data['description'] = 'Group Test 2 description';
121 $group1 = self::getDataGenerator()->create_group($group1data);
122 $group2 = self::getDataGenerator()->create_group($group2data);
124 // Set the required capabilities by the external function
125 $context = context_course::instance($course->id);
126 $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
127 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
129 // Call the external function.
130 $groups = core_group_external::get_groups(array($group1->id, $group2->id));
132 // We need to execute the return values cleaning process to simulate the web service server.
133 $groups = external_api::clean_returnvalue(core_group_external::get_groups_returns(), $groups);
135 // Checks against DB values
136 $this->assertEquals(2, count($groups));
137 foreach ($groups as $group) {
138 $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
139 switch ($dbgroup->name) {
140 case $group1->name:
141 $groupdescription = $group1->description;
142 $groupcourseid = $group1->courseid;
143 $this->assertEquals($dbgroup->descriptionformat, $group1->descriptionformat);
144 $this->assertEquals($dbgroup->enrolmentkey, $group1->enrolmentkey);
145 break;
146 case $group2->name:
147 $groupdescription = $group2->description;
148 $groupcourseid = $group2->courseid;
149 break;
150 default:
151 throw new moodle_exception('unknowgroupname');
152 break;
154 $this->assertEquals($dbgroup->description, $groupdescription);
155 $this->assertEquals($dbgroup->courseid, $groupcourseid);
158 // Call without required capability
159 $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
160 $this->setExpectedException('required_capability_exception');
161 $groups = core_group_external::get_groups(array($group1->id, $group2->id));
165 * Test delete_groups
167 public function test_delete_groups() {
168 global $DB;
170 $this->resetAfterTest(true);
172 $course = self::getDataGenerator()->create_course();
173 $group1data = array();
174 $group1data['courseid'] = $course->id;
175 $group1data['name'] = 'Group Test 1';
176 $group1data['description'] = 'Group Test 1 description';
177 $group1data['descriptionformat'] = FORMAT_MOODLE;
178 $group1data['enrolmentkey'] = 'Test group enrol secret phrase';
179 $group2data = array();
180 $group2data['courseid'] = $course->id;
181 $group2data['name'] = 'Group Test 2';
182 $group2data['description'] = 'Group Test 2 description';
183 $group3data['courseid'] = $course->id;
184 $group3data['name'] = 'Group Test 3';
185 $group3data['description'] = 'Group Test 3 description';
186 $group1 = self::getDataGenerator()->create_group($group1data);
187 $group2 = self::getDataGenerator()->create_group($group2data);
188 $group3 = self::getDataGenerator()->create_group($group3data);
190 // Set the required capabilities by the external function
191 $context = context_course::instance($course->id);
192 $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
193 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
195 // Checks against DB values
196 $groupstotal = $DB->count_records('groups', array());
197 $this->assertEquals(3, $groupstotal);
199 // Call the external function.
200 core_group_external::delete_groups(array($group1->id, $group2->id));
202 // Checks against DB values
203 $groupstotal = $DB->count_records('groups', array());
204 $this->assertEquals(1, $groupstotal);
206 // Call without required capability
207 $this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
208 $this->setExpectedException('required_capability_exception');
209 $froups = core_group_external::delete_groups(array($group3->id));
213 * Test get_groupings
215 public function test_get_groupings() {
216 global $DB;
218 $this->resetAfterTest(true);
220 $course = self::getDataGenerator()->create_course();
222 $groupingdata = array();
223 $groupingdata['courseid'] = $course->id;
224 $groupingdata['name'] = 'Grouping Test';
225 $groupingdata['description'] = 'Grouping Test description';
226 $groupingdata['descriptionformat'] = FORMAT_MOODLE;
228 $grouping = self::getDataGenerator()->create_grouping($groupingdata);
230 // Set the required capabilities by the external function.
231 $context = context_course::instance($course->id);
232 $roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
233 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
235 // Call the external function without specifying the optional parameter.
236 $groupings = core_group_external::get_groupings(array($grouping->id));
237 // We need to execute the return values cleaning process to simulate the web service server.
238 $groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings);
240 $this->assertEquals(1, count($groupings));
242 $group1data = array();
243 $group1data['courseid'] = $course->id;
244 $group1data['name'] = 'Group Test 1';
245 $group1data['description'] = 'Group Test 1 description';
246 $group1data['descriptionformat'] = FORMAT_MOODLE;
247 $group2data = array();
248 $group2data['courseid'] = $course->id;
249 $group2data['name'] = 'Group Test 2';
250 $group2data['description'] = 'Group Test 2 description';
251 $group2data['descriptionformat'] = FORMAT_MOODLE;
253 $group1 = self::getDataGenerator()->create_group($group1data);
254 $group2 = self::getDataGenerator()->create_group($group2data);
256 groups_assign_grouping($grouping->id, $group1->id);
257 groups_assign_grouping($grouping->id, $group2->id);
259 // Call the external function specifying that groups are returned.
260 $groupings = core_group_external::get_groupings(array($grouping->id), true);
261 // We need to execute the return values cleaning process to simulate the web service server.
262 $groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings);
263 $this->assertEquals(1, count($groupings));
264 $this->assertEquals(2, count($groupings[0]['groups']));
265 foreach ($groupings[0]['groups'] as $group) {
266 $dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
267 $dbgroupinggroups = $DB->get_record('groupings_groups',
268 array('groupingid' => $groupings[0]['id'],
269 'groupid' => $group['id']),
270 '*', MUST_EXIST);
271 switch ($dbgroup->name) {
272 case $group1->name:
273 $groupdescription = $group1->description;
274 $groupcourseid = $group1->courseid;
275 break;
276 case $group2->name:
277 $groupdescription = $group2->description;
278 $groupcourseid = $group2->courseid;
279 break;
280 default:
281 throw new moodle_exception('unknowgroupname');
282 break;
284 $this->assertEquals($dbgroup->description, $groupdescription);
285 $this->assertEquals($dbgroup->courseid, $groupcourseid);