forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib_test.php
525 lines (426 loc) · 26.3 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?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/>.
/**
* External completion functions unit tests
*
* @package core_completion
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External completion functions unit tests
*
* @package core_completion
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class core_completion_externallib_testcase extends externallib_advanced_testcase {
/**
* Test update_activity_completion_status_manually
*/
public function test_update_activity_completion_status_manually() {
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$cm = get_coursemodule_from_id('data', $data->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
$this->setUser($user);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, true);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::update_activity_completion_status_manually_returns(), $result);
// Check in DB.
$this->assertEquals(1, $DB->get_field('course_modules_completion', 'completionstate',
array('coursemoduleid' => $data->cmid)));
// Check using the API.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
$this->assertTrue($result['status']);
$result = core_completion_external::update_activity_completion_status_manually($data->cmid, false);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::update_activity_completion_status_manually_returns(), $result);
$this->assertEquals(0, $DB->get_field('course_modules_completion', 'completionstate',
array('coursemoduleid' => $data->cmid)));
$completiondata = $completion->get_data($cm);
$this->assertEquals(0, $completiondata->completionstate);
$this->assertTrue($result['status']);
}
/**
* Test update_activity_completion_status
*/
public function test_get_activities_completion_status() {
global $DB, $CFG;
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
'groupmode' => SEPARATEGROUPS,
'groupmodeforce' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
array('completion' => 1, 'visible' => 0));
$cmdata = get_coursemodule_from_id('data', $data->cmid);
$cmforum = get_coursemodule_from_id('forum', $forum->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
// Teacher and student in different groups initially.
groups_add_member($group1->id, $student->id);
groups_add_member($group2->id, $teacher->id);
$this->setUser($student);
// Forum complete.
$completion = new completion_info($course);
$completion->update_state($cmforum, COMPLETION_COMPLETE);
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// We added 4 activities, but only 3 with completion enabled and one of those is hidden.
$this->assertCount(2, $result['statuses']);
$activitiesfound = 0;
foreach ($result['statuses'] as $status) {
if ($status['cmid'] == $forum->cmid and $status['modname'] == 'forum' and $status['instance'] == $forum->id) {
$activitiesfound++;
$this->assertEquals(COMPLETION_COMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
} else if ($status['cmid'] == $data->cmid and $status['modname'] == 'data' and $status['instance'] == $data->id) {
$activitiesfound++;
$this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
}
}
$this->assertEquals(2, $activitiesfound);
// Teacher should see students status, they are in different groups but the teacher can access all groups.
$this->setUser($teacher);
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// We added 4 activities, but only 3 with completion enabled and one of those is hidden.
$this->assertCount(3, $result['statuses']);
// Override status by teacher.
$completion->update_state($cmforum, COMPLETION_INCOMPLETE, $student->id, true);
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// Check forum has been overriden by the teacher.
foreach ($result['statuses'] as $status) {
if ($status['cmid'] == $forum->cmid) {
$this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
$this->assertEquals($teacher->id, $status['overrideby']);
break;
}
}
// Teacher should see his own completion status.
// Forum complete for teacher.
$completion = new completion_info($course);
$completion->update_state($cmforum, COMPLETION_COMPLETE);
$result = core_completion_external::get_activities_completion_status($course->id, $teacher->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// We added 4 activities, but only 3 with completion enabled (one of those is hidden but the teacher can see it).
$this->assertCount(3, $result['statuses']);
$activitiesfound = 0;
foreach ($result['statuses'] as $status) {
if ($status['cmid'] == $forum->cmid and $status['modname'] == 'forum' and $status['instance'] == $forum->id) {
$activitiesfound++;
$this->assertEquals(COMPLETION_COMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
} else {
$activitiesfound++;
$this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
$this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
}
}
$this->assertEquals(3, $activitiesfound);
// Change teacher role capabilities (disable access all groups).
$context = context_course::instance($course->id);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
accesslib_clear_all_caches_for_unit_testing();
try {
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
$this->fail('Exception expected due to groups permissions.');
} catch (moodle_exception $e) {
$this->assertEquals('accessdenied', $e->errorcode);
}
// Now add the teacher in the same group.
groups_add_member($group1->id, $teacher->id);
$result = core_completion_external::get_activities_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_activities_completion_status_returns(), $result);
// We added 4 activities, but only 3 with completion enabled and one of those is hidden.
$this->assertCount(3, $result['statuses']);
}
/**
* Test override_activity_completion_status
*/
public function test_override_activity_completion_status() {
global $DB, $CFG;
$this->resetAfterTest(true);
// Create course with teacher and student enrolled.
$CFG->enablecompletion = true;
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
// Create 2 activities, one with manual completion (data), one with automatic completion triggered by viewing it (forum).
$data = $this->getDataGenerator()->create_module('data', ['course' => $course->id], ['completion' => 1]);
$forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id],
['completion' => 2, 'completionview' => 1]);
$cmdata = get_coursemodule_from_id('data', $data->cmid);
$cmforum = get_coursemodule_from_id('forum', $forum->cmid);
// Manually complete the data activity as the student.
$this->setUser($student);
$completion = new completion_info($course);
$completion->update_state($cmdata, COMPLETION_COMPLETE);
// Test overriding the status of the manual-completion-activity 'incomplete'.
$this->setUser($teacher);
$result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_INCOMPLETE);
$result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
$this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
$completiondata = $completion->get_data($cmdata, false, $student->id);
$this->assertEquals(COMPLETION_INCOMPLETE, $completiondata->completionstate);
// Test overriding the status of the manual-completion-activity back to 'complete'.
$result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_COMPLETE);
$result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
$this->assertEquals($result['state'], COMPLETION_COMPLETE);
$completiondata = $completion->get_data($cmdata, false, $student->id);
$this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate);
// Test overriding the status of the auto-completion-activity to 'complete'.
$result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE);
$result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
$this->assertEquals($result['state'], COMPLETION_COMPLETE);
$completionforum = $completion->get_data($cmforum, false, $student->id);
$this->assertEquals(COMPLETION_COMPLETE, $completionforum->completionstate);
// Test overriding the status of the auto-completion-activity to 'incomplete'.
$result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_INCOMPLETE);
$result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
$this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
$completionforum = $completion->get_data($cmforum, false, $student->id);
$this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate);
// Test overriding the status of the auto-completion-activity to an invalid state.
$this->expectException('moodle_exception');
core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 3);
}
/**
* Test overriding the activity completion status as a user without the capability to do so.
*/
public function test_override_status_user_without_capability() {
global $DB, $CFG;
$this->resetAfterTest(true);
// Create course with teacher and student enrolled.
$CFG->enablecompletion = true;
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
$coursecontext = context_course::instance($course->id);
// Create an activity with automatic completion (a forum).
$forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id],
['completion' => 2, 'completionview' => 1]);
// Test overriding the status of the activity for a user without the capability.
$this->setUser($teacher);
assign_capability('moodle/course:overridecompletion', CAP_PREVENT, $teacherrole->id, $coursecontext);
$this->expectException('required_capability_exception');
core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE);
}
/**
* Test get_course_completion_status
*/
public function test_get_course_completion_status() {
global $DB, $CFG, $COMPLETION_CRITERIA_TYPES;
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_unenrol.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_role.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
'groupmode' => SEPARATEGROUPS,
'groupmodeforce' => 1));
$data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
array('completion' => 1));
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 1));
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$cmdata = get_coursemodule_from_id('data', $data->cmid);
$cmforum = get_coursemodule_from_id('forum', $forum->cmid);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
// Teacher and student in different groups initially.
groups_add_member($group1->id, $student->id);
groups_add_member($group2->id, $teacher->id);
// Set completion rules.
$completion = new completion_info($course);
// Loop through each criteria type and run its update_config() method.
$criteriadata = new stdClass();
$criteriadata->id = $course->id;
$criteriadata->criteria_activity = array();
// Some activities.
$criteriadata->criteria_activity[$cmdata->id] = 1;
$criteriadata->criteria_activity[$cmforum->id] = 1;
// In a week criteria date value.
$criteriadata->criteria_date_value = time() + WEEKSECS;
// Self completion.
$criteriadata->criteria_self = 1;
foreach ($COMPLETION_CRITERIA_TYPES as $type) {
$class = 'completion_criteria_'.$type;
$criterion = new $class();
$criterion->update_config($criteriadata);
}
// Handle overall aggregation.
$aggdata = array(
'course' => $course->id,
'criteriatype' => null
);
$aggregation = new completion_aggregation($aggdata);
$aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
$aggregation->save();
$aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
$aggregation = new completion_aggregation($aggdata);
$aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
$aggregation->save();
$this->setUser($student);
$result = core_completion_external::get_course_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$studentresult = external_api::clean_returnvalue(
core_completion_external::get_course_completion_status_returns(), $result);
// 3 different criteria.
$this->assertCount(3, $studentresult['completionstatus']['completions']);
$this->assertEquals(COMPLETION_AGGREGATION_ALL, $studentresult['completionstatus']['aggregation']);
$this->assertFalse($studentresult['completionstatus']['completed']);
$this->assertEquals('No', $studentresult['completionstatus']['completions'][0]['status']);
$this->assertEquals('No', $studentresult['completionstatus']['completions'][1]['status']);
$this->assertEquals('No', $studentresult['completionstatus']['completions'][2]['status']);
// Teacher should see students status, they are in different groups but the teacher can access all groups.
$this->setUser($teacher);
$result = core_completion_external::get_course_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$teacherresult = external_api::clean_returnvalue(
core_completion_external::get_course_completion_status_returns(), $result);
$this->assertEquals($studentresult, $teacherresult);
// Change teacher role capabilities (disable access al goups).
$context = context_course::instance($course->id);
assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
accesslib_clear_all_caches_for_unit_testing();
try {
$result = core_completion_external::get_course_completion_status($course->id, $student->id);
$this->fail('Exception expected due to groups permissions.');
} catch (moodle_exception $e) {
$this->assertEquals('accessdenied', $e->errorcode);
}
// Now add the teacher in the same group.
groups_add_member($group1->id, $teacher->id);
$result = core_completion_external::get_course_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$teacherresult = external_api::clean_returnvalue(
core_completion_external::get_course_completion_status_returns(), $result);
$this->assertEquals($studentresult, $teacherresult);
}
/**
* Test mark_course_self_completed
*/
public function test_mark_course_self_completed() {
global $DB, $CFG;
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
// Set completion rules.
$completion = new completion_info($course);
$criteriadata = new stdClass();
$criteriadata->id = $course->id;
$criteriadata->criteria_activity = array();
// Self completion.
$criteriadata->criteria_self = COMPLETION_CRITERIA_TYPE_SELF;
$class = 'completion_criteria_self';
$criterion = new $class();
$criterion->update_config($criteriadata);
// Handle overall aggregation.
$aggdata = array(
'course' => $course->id,
'criteriatype' => null
);
$aggregation = new completion_aggregation($aggdata);
$aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
$aggregation->save();
$this->setUser($student);
$result = core_completion_external::mark_course_self_completed($course->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::mark_course_self_completed_returns(), $result);
// We expect a valid result.
$this->assertEquals(true, $result['status']);
$result = core_completion_external::get_course_completion_status($course->id, $student->id);
// We need to execute the return values cleaning process to simulate the web service server.
$result = external_api::clean_returnvalue(
core_completion_external::get_course_completion_status_returns(), $result);
// Course must be completed.
$this->assertEquals(COMPLETION_COMPLETE, $result['completionstatus']['completions'][0]['complete']);
try {
$result = core_completion_external::mark_course_self_completed($course->id);
$this->fail('Exception expected due course already self completed.');
} catch (moodle_exception $e) {
$this->assertEquals('useralreadymarkedcomplete', $e->errorcode);
}
}
}