MDL-61859 core_completion: fix unit tests expecting exceptions twice
[moodle.git] / completion / tests / externallib_test.php
CommitLineData
1490586e
JL
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/**
18 * External completion functions unit tests
19 *
20 * @package core_completion
21 * @category external
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 2.9
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33/**
34 * External completion functions unit tests
35 *
36 * @package core_completion
37 * @category external
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 2.9
41 */
42class core_completion_externallib_testcase extends externallib_advanced_testcase {
43
44 /**
45 * Test update_activity_completion_status_manually
46 */
47 public function test_update_activity_completion_status_manually() {
48 global $DB, $CFG;
49
50 $this->resetAfterTest(true);
51
52 $CFG->enablecompletion = true;
53 $user = $this->getDataGenerator()->create_user();
54 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
55 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
56 array('completion' => 1));
57 $cm = get_coursemodule_from_id('data', $data->cmid);
58
59 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
60 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
61
62 $this->setUser($user);
63
64 $result = core_completion_external::update_activity_completion_status_manually($data->cmid, true);
65 // We need to execute the return values cleaning process to simulate the web service server.
66 $result = external_api::clean_returnvalue(
67 core_completion_external::update_activity_completion_status_manually_returns(), $result);
68
69 // Check in DB.
70 $this->assertEquals(1, $DB->get_field('course_modules_completion', 'completionstate',
71 array('coursemoduleid' => $data->cmid)));
72
73 // Check using the API.
74 $completion = new completion_info($course);
75 $completiondata = $completion->get_data($cm);
76 $this->assertEquals(1, $completiondata->completionstate);
77 $this->assertTrue($result['status']);
78
79 $result = core_completion_external::update_activity_completion_status_manually($data->cmid, false);
80 // We need to execute the return values cleaning process to simulate the web service server.
81 $result = external_api::clean_returnvalue(
82 core_completion_external::update_activity_completion_status_manually_returns(), $result);
83
84 $this->assertEquals(0, $DB->get_field('course_modules_completion', 'completionstate',
85 array('coursemoduleid' => $data->cmid)));
86 $completiondata = $completion->get_data($cm);
87 $this->assertEquals(0, $completiondata->completionstate);
88 $this->assertTrue($result['status']);
89 }
f578820b
JL
90
91 /**
92 * Test update_activity_completion_status
93 */
94 public function test_get_activities_completion_status() {
95 global $DB, $CFG;
96
97 $this->resetAfterTest(true);
98
99 $CFG->enablecompletion = true;
100 $student = $this->getDataGenerator()->create_user();
101 $teacher = $this->getDataGenerator()->create_user();
102
103 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
104 'groupmode' => SEPARATEGROUPS,
105 'groupmodeforce' => 1));
106
107 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
108 array('completion' => 1));
109 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
110 array('completion' => 1));
111 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
112 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
113 array('completion' => 1, 'visible' => 0));
114
115 $cmdata = get_coursemodule_from_id('data', $data->cmid);
116 $cmforum = get_coursemodule_from_id('forum', $forum->cmid);
117
118 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
4ceed4df 119 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
f578820b
JL
120 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
121 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
122
123 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
124 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
125
126 // Teacher and student in different groups initially.
127 groups_add_member($group1->id, $student->id);
128 groups_add_member($group2->id, $teacher->id);
129
130 $this->setUser($student);
131 // Forum complete.
132 $completion = new completion_info($course);
133 $completion->update_state($cmforum, COMPLETION_COMPLETE);
134
135 $result = core_completion_external::get_activities_completion_status($course->id, $student->id);
136 // We need to execute the return values cleaning process to simulate the web service server.
137 $result = external_api::clean_returnvalue(
138 core_completion_external::get_activities_completion_status_returns(), $result);
139
140 // We added 4 activities, but only 3 with completion enabled and one of those is hidden.
141 $this->assertCount(2, $result['statuses']);
142
143 $activitiesfound = 0;
144 foreach ($result['statuses'] as $status) {
145 if ($status['cmid'] == $forum->cmid and $status['modname'] == 'forum' and $status['instance'] == $forum->id) {
146 $activitiesfound++;
147 $this->assertEquals(COMPLETION_COMPLETE, $status['state']);
148 $this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
149 } else if ($status['cmid'] == $data->cmid and $status['modname'] == 'data' and $status['instance'] == $data->id) {
150 $activitiesfound++;
151 $this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
152 $this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
153 }
154 }
155 $this->assertEquals(2, $activitiesfound);
156
157 // Teacher should see students status, they are in different groups but the teacher can access all groups.
158 $this->setUser($teacher);
159 $result = core_completion_external::get_activities_completion_status($course->id, $student->id);
160 // We need to execute the return values cleaning process to simulate the web service server.
161 $result = external_api::clean_returnvalue(
162 core_completion_external::get_activities_completion_status_returns(), $result);
163
ea2f0e3e
JL
164 // We added 4 activities, but only 3 with completion enabled and one of those is hidden.
165 $this->assertCount(3, $result['statuses']);
f578820b 166
15ead7a1
PFO
167 // Override status by teacher.
168 $completion->update_state($cmforum, COMPLETION_INCOMPLETE, $student->id, true);
169
170 $result = core_completion_external::get_activities_completion_status($course->id, $student->id);
171 // We need to execute the return values cleaning process to simulate the web service server.
172 $result = external_api::clean_returnvalue(
173 core_completion_external::get_activities_completion_status_returns(), $result);
174
175 // Check forum has been overriden by the teacher.
176 foreach ($result['statuses'] as $status) {
177 if ($status['cmid'] == $forum->cmid) {
178 $this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
179 $this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
180 $this->assertEquals($teacher->id, $status['overrideby']);
181 break;
182 }
183 }
184
0e8b3d92
JL
185 // Teacher should see his own completion status.
186
187 // Forum complete for teacher.
188 $completion = new completion_info($course);
189 $completion->update_state($cmforum, COMPLETION_COMPLETE);
190
191 $result = core_completion_external::get_activities_completion_status($course->id, $teacher->id);
192 // We need to execute the return values cleaning process to simulate the web service server.
193 $result = external_api::clean_returnvalue(
194 core_completion_external::get_activities_completion_status_returns(), $result);
195
196 // We added 4 activities, but only 3 with completion enabled (one of those is hidden but the teacher can see it).
197 $this->assertCount(3, $result['statuses']);
198
199 $activitiesfound = 0;
200 foreach ($result['statuses'] as $status) {
201 if ($status['cmid'] == $forum->cmid and $status['modname'] == 'forum' and $status['instance'] == $forum->id) {
202 $activitiesfound++;
203 $this->assertEquals(COMPLETION_COMPLETE, $status['state']);
204 $this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
205 } else {
206 $activitiesfound++;
207 $this->assertEquals(COMPLETION_INCOMPLETE, $status['state']);
208 $this->assertEquals(COMPLETION_TRACKING_MANUAL, $status['tracking']);
209 }
210 }
211 $this->assertEquals(3, $activitiesfound);
212
15ead7a1 213 // Change teacher role capabilities (disable access all groups).
f578820b
JL
214 $context = context_course::instance($course->id);
215 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
216 accesslib_clear_all_caches_for_unit_testing();
217
218 try {
219 $result = core_completion_external::get_activities_completion_status($course->id, $student->id);
220 $this->fail('Exception expected due to groups permissions.');
221 } catch (moodle_exception $e) {
222 $this->assertEquals('accessdenied', $e->errorcode);
223 }
224
225 // Now add the teacher in the same group.
226 groups_add_member($group1->id, $teacher->id);
227 $result = core_completion_external::get_activities_completion_status($course->id, $student->id);
228 // We need to execute the return values cleaning process to simulate the web service server.
229 $result = external_api::clean_returnvalue(
230 core_completion_external::get_activities_completion_status_returns(), $result);
ea2f0e3e
JL
231 // We added 4 activities, but only 3 with completion enabled and one of those is hidden.
232 $this->assertCount(3, $result['statuses']);
f578820b
JL
233 }
234
60a6b36c
EEAK
235 /**
236 * Test override_activity_completion_status
237 */
238 public function test_override_activity_completion_status() {
239 global $DB, $CFG;
60a6b36c
EEAK
240 $this->resetAfterTest(true);
241
86f359bf 242 // Create course with teacher and student enrolled.
60a6b36c 243 $CFG->enablecompletion = true;
86f359bf 244 $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
60a6b36c
EEAK
245 $student = $this->getDataGenerator()->create_user();
246 $teacher = $this->getDataGenerator()->create_user();
fdae6f77 247 $studentrole = $DB->get_record('role', ['shortname' => 'student']);
60a6b36c 248 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
fdae6f77 249 $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
60a6b36c
EEAK
250 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
251
fdae6f77 252 // Create 2 activities, one with manual completion (data), one with automatic completion triggered by viewing it (forum).
86f359bf
JD
253 $data = $this->getDataGenerator()->create_module('data', ['course' => $course->id], ['completion' => 1]);
254 $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id],
255 ['completion' => 2, 'completionview' => 1]);
256 $cmdata = get_coursemodule_from_id('data', $data->cmid);
257 $cmforum = get_coursemodule_from_id('forum', $forum->cmid);
60a6b36c 258
86f359bf 259 // Manually complete the data activity as the student.
60a6b36c 260 $this->setUser($student);
86f359bf 261 $completion = new completion_info($course);
60a6b36c 262 $completion->update_state($cmdata, COMPLETION_COMPLETE);
60a6b36c 263
86f359bf 264 // Test overriding the status of the manual-completion-activity 'incomplete'.
60a6b36c 265 $this->setUser($teacher);
60a6b36c 266 $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_INCOMPLETE);
86f359bf 267 $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
b08337c3 268 $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
60a6b36c
EEAK
269 $completiondata = $completion->get_data($cmdata, false, $student->id);
270 $this->assertEquals(COMPLETION_INCOMPLETE, $completiondata->completionstate);
271
86f359bf 272 // Test overriding the status of the manual-completion-activity back to 'complete'.
60a6b36c 273 $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_COMPLETE);
86f359bf 274 $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
b08337c3 275 $this->assertEquals($result['state'], COMPLETION_COMPLETE);
60a6b36c
EEAK
276 $completiondata = $completion->get_data($cmdata, false, $student->id);
277 $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate);
278
86f359bf 279 // Test overriding the status of the auto-completion-activity to 'complete'.
60a6b36c 280 $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE);
86f359bf 281 $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
b08337c3 282 $this->assertEquals($result['state'], COMPLETION_COMPLETE);
60a6b36c
EEAK
283 $completionforum = $completion->get_data($cmforum, false, $student->id);
284 $this->assertEquals(COMPLETION_COMPLETE, $completionforum->completionstate);
285
86f359bf 286 // Test overriding the status of the auto-completion-activity to 'incomplete'.
60a6b36c 287 $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_INCOMPLETE);
86f359bf 288 $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
b08337c3 289 $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
8fbc41d8
JD
290 $completionforum = $completion->get_data($cmforum, false, $student->id);
291 $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate);
292
fdae6f77 293 // Test overriding the status of the auto-completion-activity to an invalid state.
8fbc41d8 294 $this->expectException('moodle_exception');
fdae6f77
JD
295 core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 3);
296 }
8fbc41d8 297
fdae6f77
JD
298 /**
299 * Test overriding the activity completion status as a user without the capability to do so.
300 */
301 public function test_override_status_user_without_capability() {
302 global $DB, $CFG;
303 $this->resetAfterTest(true);
304
305 // Create course with teacher and student enrolled.
306 $CFG->enablecompletion = true;
307 $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
308 $student = $this->getDataGenerator()->create_user();
309 $teacher = $this->getDataGenerator()->create_user();
310 $studentrole = $DB->get_record('role', ['shortname' => 'student']);
311 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
312 $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
313 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
314 $coursecontext = context_course::instance($course->id);
315
316 // Create an activity with automatic completion (a forum).
317 $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id],
318 ['completion' => 2, 'completionview' => 1]);
319
320 // Test overriding the status of the activity for a user without the capability.
321 $this->setUser($teacher);
322 assign_capability('moodle/course:overridecompletion', CAP_PREVENT, $teacherrole->id, $coursecontext);
323 $this->expectException('required_capability_exception');
324 core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE);
60a6b36c
EEAK
325 }
326
b5e60638
JL
327 /**
328 * Test get_course_completion_status
329 */
330 public function test_get_course_completion_status() {
331 global $DB, $CFG, $COMPLETION_CRITERIA_TYPES;
332 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
333 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
334 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_unenrol.php');
335 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
336 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
337 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
338 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_role.php');
339 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
340
341 $this->resetAfterTest(true);
342
343 $CFG->enablecompletion = true;
344 $student = $this->getDataGenerator()->create_user();
345 $teacher = $this->getDataGenerator()->create_user();
346
347 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
348 'groupmode' => SEPARATEGROUPS,
349 'groupmodeforce' => 1));
350
351 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
352 array('completion' => 1));
353 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
354 array('completion' => 1));
355 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
356
357 $cmdata = get_coursemodule_from_id('data', $data->cmid);
358 $cmforum = get_coursemodule_from_id('forum', $forum->cmid);
359
360 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
4ceed4df 361 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
b5e60638
JL
362 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
363 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
364
365 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
366 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
367 // Teacher and student in different groups initially.
368 groups_add_member($group1->id, $student->id);
369 groups_add_member($group2->id, $teacher->id);
370
371 // Set completion rules.
372 $completion = new completion_info($course);
373
374 // Loop through each criteria type and run its update_config() method.
375
376 $criteriadata = new stdClass();
377 $criteriadata->id = $course->id;
378 $criteriadata->criteria_activity = array();
379 // Some activities.
380 $criteriadata->criteria_activity[$cmdata->id] = 1;
381 $criteriadata->criteria_activity[$cmforum->id] = 1;
382
383 // In a week criteria date value.
384 $criteriadata->criteria_date_value = time() + WEEKSECS;
385
386 // Self completion.
387 $criteriadata->criteria_self = 1;
388
389 foreach ($COMPLETION_CRITERIA_TYPES as $type) {
390 $class = 'completion_criteria_'.$type;
391 $criterion = new $class();
392 $criterion->update_config($criteriadata);
393 }
394
395 // Handle overall aggregation.
396 $aggdata = array(
397 'course' => $course->id,
398 'criteriatype' => null
399 );
400 $aggregation = new completion_aggregation($aggdata);
401 $aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
402 $aggregation->save();
403
404 $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
405 $aggregation = new completion_aggregation($aggdata);
406 $aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
407 $aggregation->save();
408
409 $this->setUser($student);
410
411 $result = core_completion_external::get_course_completion_status($course->id, $student->id);
412 // We need to execute the return values cleaning process to simulate the web service server.
413 $studentresult = external_api::clean_returnvalue(
414 core_completion_external::get_course_completion_status_returns(), $result);
415
416 // 3 different criteria.
417 $this->assertCount(3, $studentresult['completionstatus']['completions']);
418
419 $this->assertEquals(COMPLETION_AGGREGATION_ALL, $studentresult['completionstatus']['aggregation']);
420 $this->assertFalse($studentresult['completionstatus']['completed']);
421
422 $this->assertEquals('No', $studentresult['completionstatus']['completions'][0]['status']);
423 $this->assertEquals('No', $studentresult['completionstatus']['completions'][1]['status']);
424 $this->assertEquals('No', $studentresult['completionstatus']['completions'][2]['status']);
425
426 // Teacher should see students status, they are in different groups but the teacher can access all groups.
427 $this->setUser($teacher);
428 $result = core_completion_external::get_course_completion_status($course->id, $student->id);
429 // We need to execute the return values cleaning process to simulate the web service server.
430 $teacherresult = external_api::clean_returnvalue(
431 core_completion_external::get_course_completion_status_returns(), $result);
432
433 $this->assertEquals($studentresult, $teacherresult);
434
435 // Change teacher role capabilities (disable access al goups).
436 $context = context_course::instance($course->id);
437 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
438 accesslib_clear_all_caches_for_unit_testing();
439
440 try {
441 $result = core_completion_external::get_course_completion_status($course->id, $student->id);
442 $this->fail('Exception expected due to groups permissions.');
443 } catch (moodle_exception $e) {
444 $this->assertEquals('accessdenied', $e->errorcode);
445 }
446
447 // Now add the teacher in the same group.
448 groups_add_member($group1->id, $teacher->id);
449 $result = core_completion_external::get_course_completion_status($course->id, $student->id);
450 // We need to execute the return values cleaning process to simulate the web service server.
451 $teacherresult = external_api::clean_returnvalue(
452 core_completion_external::get_course_completion_status_returns(), $result);
453
454 $this->assertEquals($studentresult, $teacherresult);
455
456 }
457
23b398fc
JL
458 /**
459 * Test mark_course_self_completed
460 */
461 public function test_mark_course_self_completed() {
462 global $DB, $CFG;
463 require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
464
465 $this->resetAfterTest(true);
466
467 $CFG->enablecompletion = true;
468 $student = $this->getDataGenerator()->create_user();
469 $teacher = $this->getDataGenerator()->create_user();
470
471 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
472
473 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
474 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
475
476 // Set completion rules.
477 $completion = new completion_info($course);
478
479 $criteriadata = new stdClass();
480 $criteriadata->id = $course->id;
481 $criteriadata->criteria_activity = array();
482
483 // Self completion.
484 $criteriadata->criteria_self = COMPLETION_CRITERIA_TYPE_SELF;
485 $class = 'completion_criteria_self';
486 $criterion = new $class();
487 $criterion->update_config($criteriadata);
488
489 // Handle overall aggregation.
490 $aggdata = array(
491 'course' => $course->id,
492 'criteriatype' => null
493 );
494 $aggregation = new completion_aggregation($aggdata);
495 $aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
496 $aggregation->save();
497
498 $this->setUser($student);
499
500 $result = core_completion_external::mark_course_self_completed($course->id);
501 // We need to execute the return values cleaning process to simulate the web service server.
502 $result = external_api::clean_returnvalue(
503 core_completion_external::mark_course_self_completed_returns(), $result);
504
505 // We expect a valid result.
506 $this->assertEquals(true, $result['status']);
507
508 $result = core_completion_external::get_course_completion_status($course->id, $student->id);
509 // We need to execute the return values cleaning process to simulate the web service server.
510 $result = external_api::clean_returnvalue(
511 core_completion_external::get_course_completion_status_returns(), $result);
512
513 // Course must be completed.
514 $this->assertEquals(COMPLETION_COMPLETE, $result['completionstatus']['completions'][0]['complete']);
515
516 try {
517 $result = core_completion_external::mark_course_self_completed($course->id);
518 $this->fail('Exception expected due course already self completed.');
519 } catch (moodle_exception $e) {
520 $this->assertEquals('useralreadymarkedcomplete', $e->errorcode);
521 }
522
523 }
524
1490586e 525}