forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternallib_test.php
288 lines (246 loc) · 12.4 KB
/
externallib_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Group external PHPunit tests
*
* @package core_group
* @category external
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.4
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/group/externallib.php');
require_once($CFG->dirroot . '/group/lib.php');
class core_group_external_testcase extends externallib_advanced_testcase {
/**
* Test create_groups
*/
public function test_create_groups() {
global $DB;
$this->resetAfterTest(true);
$course = self::getDataGenerator()->create_course();
$group1 = array();
$group1['courseid'] = $course->id;
$group1['name'] = 'Group Test 1';
$group1['description'] = 'Group Test 1 description';
$group1['descriptionformat'] = FORMAT_MOODLE;
$group1['enrolmentkey'] = 'Test group enrol secret phrase';
$group2 = array();
$group2['courseid'] = $course->id;
$group2['name'] = 'Group Test 2';
$group2['description'] = 'Group Test 2 description';
$group3 = array();
$group3['courseid'] = $course->id;
$group3['name'] = 'Group Test 3';
$group3['description'] = 'Group Test 3 description';
// Set the required capabilities by the external function
$context = context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
$this->assignUserCapability('moodle/course:view', $context->id, $roleid);
// Call the external function.
$groups = core_group_external::create_groups(array($group1, $group2));
// We need to execute the return values cleaning process to simulate the web service server.
$groups = external_api::clean_returnvalue(core_group_external::create_groups_returns(), $groups);
// Checks against DB values
$this->assertEquals(2, count($groups));
foreach ($groups as $group) {
$dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
switch ($dbgroup->name) {
case $group1['name']:
$groupdescription = $group1['description'];
$groupcourseid = $group1['courseid'];
$this->assertEquals($dbgroup->descriptionformat, $group1['descriptionformat']);
$this->assertEquals($dbgroup->enrolmentkey, $group1['enrolmentkey']);
break;
case $group2['name']:
$groupdescription = $group2['description'];
$groupcourseid = $group2['courseid'];
break;
default:
throw new moodle_exception('unknowgroupname');
break;
}
$this->assertEquals($dbgroup->description, $groupdescription);
$this->assertEquals($dbgroup->courseid, $groupcourseid);
}
// Call without required capability
$this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
$this->setExpectedException('required_capability_exception');
$froups = core_group_external::create_groups(array($group3));
}
/**
* Test get_groups
*/
public function test_get_groups() {
global $DB;
$this->resetAfterTest(true);
$course = self::getDataGenerator()->create_course();
$group1data = array();
$group1data['courseid'] = $course->id;
$group1data['name'] = 'Group Test 1';
$group1data['description'] = 'Group Test 1 description';
$group1data['descriptionformat'] = FORMAT_MOODLE;
$group1data['enrolmentkey'] = 'Test group enrol secret phrase';
$group2data = array();
$group2data['courseid'] = $course->id;
$group2data['name'] = 'Group Test 2';
$group2data['description'] = 'Group Test 2 description';
$group1 = self::getDataGenerator()->create_group($group1data);
$group2 = self::getDataGenerator()->create_group($group2data);
// Set the required capabilities by the external function
$context = context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
$this->assignUserCapability('moodle/course:view', $context->id, $roleid);
// Call the external function.
$groups = core_group_external::get_groups(array($group1->id, $group2->id));
// We need to execute the return values cleaning process to simulate the web service server.
$groups = external_api::clean_returnvalue(core_group_external::get_groups_returns(), $groups);
// Checks against DB values
$this->assertEquals(2, count($groups));
foreach ($groups as $group) {
$dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
switch ($dbgroup->name) {
case $group1->name:
$groupdescription = $group1->description;
$groupcourseid = $group1->courseid;
$this->assertEquals($dbgroup->descriptionformat, $group1->descriptionformat);
$this->assertEquals($dbgroup->enrolmentkey, $group1->enrolmentkey);
break;
case $group2->name:
$groupdescription = $group2->description;
$groupcourseid = $group2->courseid;
break;
default:
throw new moodle_exception('unknowgroupname');
break;
}
$this->assertEquals($dbgroup->description, $groupdescription);
$this->assertEquals($dbgroup->courseid, $groupcourseid);
}
// Call without required capability
$this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
$this->setExpectedException('required_capability_exception');
$groups = core_group_external::get_groups(array($group1->id, $group2->id));
}
/**
* Test delete_groups
*/
public function test_delete_groups() {
global $DB;
$this->resetAfterTest(true);
$course = self::getDataGenerator()->create_course();
$group1data = array();
$group1data['courseid'] = $course->id;
$group1data['name'] = 'Group Test 1';
$group1data['description'] = 'Group Test 1 description';
$group1data['descriptionformat'] = FORMAT_MOODLE;
$group1data['enrolmentkey'] = 'Test group enrol secret phrase';
$group2data = array();
$group2data['courseid'] = $course->id;
$group2data['name'] = 'Group Test 2';
$group2data['description'] = 'Group Test 2 description';
$group3data['courseid'] = $course->id;
$group3data['name'] = 'Group Test 3';
$group3data['description'] = 'Group Test 3 description';
$group1 = self::getDataGenerator()->create_group($group1data);
$group2 = self::getDataGenerator()->create_group($group2data);
$group3 = self::getDataGenerator()->create_group($group3data);
// Set the required capabilities by the external function
$context = context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
$this->assignUserCapability('moodle/course:view', $context->id, $roleid);
// Checks against DB values
$groupstotal = $DB->count_records('groups', array());
$this->assertEquals(3, $groupstotal);
// Call the external function.
core_group_external::delete_groups(array($group1->id, $group2->id));
// Checks against DB values
$groupstotal = $DB->count_records('groups', array());
$this->assertEquals(1, $groupstotal);
// Call without required capability
$this->unassignUserCapability('moodle/course:managegroups', $context->id, $roleid);
$this->setExpectedException('required_capability_exception');
$froups = core_group_external::delete_groups(array($group3->id));
}
/**
* Test get_groupings
*/
public function test_get_groupings() {
global $DB;
$this->resetAfterTest(true);
$course = self::getDataGenerator()->create_course();
$groupingdata = array();
$groupingdata['courseid'] = $course->id;
$groupingdata['name'] = 'Grouping Test';
$groupingdata['description'] = 'Grouping Test description';
$groupingdata['descriptionformat'] = FORMAT_MOODLE;
$grouping = self::getDataGenerator()->create_grouping($groupingdata);
// Set the required capabilities by the external function.
$context = context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:managegroups', $context->id);
$this->assignUserCapability('moodle/course:view', $context->id, $roleid);
// Call the external function without specifying the optional parameter.
$groupings = core_group_external::get_groupings(array($grouping->id));
// We need to execute the return values cleaning process to simulate the web service server.
$groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings);
$this->assertEquals(1, count($groupings));
$group1data = array();
$group1data['courseid'] = $course->id;
$group1data['name'] = 'Group Test 1';
$group1data['description'] = 'Group Test 1 description';
$group1data['descriptionformat'] = FORMAT_MOODLE;
$group2data = array();
$group2data['courseid'] = $course->id;
$group2data['name'] = 'Group Test 2';
$group2data['description'] = 'Group Test 2 description';
$group2data['descriptionformat'] = FORMAT_MOODLE;
$group1 = self::getDataGenerator()->create_group($group1data);
$group2 = self::getDataGenerator()->create_group($group2data);
groups_assign_grouping($grouping->id, $group1->id);
groups_assign_grouping($grouping->id, $group2->id);
// Call the external function specifying that groups are returned.
$groupings = core_group_external::get_groupings(array($grouping->id), true);
// We need to execute the return values cleaning process to simulate the web service server.
$groupings = external_api::clean_returnvalue(core_group_external::get_groupings_returns(), $groupings);
$this->assertEquals(1, count($groupings));
$this->assertEquals(2, count($groupings[0]['groups']));
foreach ($groupings[0]['groups'] as $group) {
$dbgroup = $DB->get_record('groups', array('id' => $group['id']), '*', MUST_EXIST);
$dbgroupinggroups = $DB->get_record('groupings_groups',
array('groupingid' => $groupings[0]['id'],
'groupid' => $group['id']),
'*', MUST_EXIST);
switch ($dbgroup->name) {
case $group1->name:
$groupdescription = $group1->description;
$groupcourseid = $group1->courseid;
break;
case $group2->name:
$groupdescription = $group2->description;
$groupcourseid = $group2->courseid;
break;
default:
throw new moodle_exception('unknowgroupname');
break;
}
$this->assertEquals($dbgroup->description, $groupdescription);
$this->assertEquals($dbgroup->courseid, $groupcourseid);
}
}
}