ae06d97af1a07b04400273923d49bf80a737d709
[moodle.git] / completion / 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  * 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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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  */
42 class core_completion_externallib_testcase extends externallib_advanced_testcase {
44     /**
45      * Test update_activity_completion_status_manually
46      */
47     public function test_update_activity_completion_status_manually() {
48         global $DB, $CFG;
50         $this->resetAfterTest(true);
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);
59         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
60         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
62         $this->setUser($user);
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);
69         // Check in DB.
70         $this->assertEquals(1, $DB->get_field('course_modules_completion', 'completionstate',
71                             array('coursemoduleid' => $data->cmid)));
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']);
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);
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     }
91     /**
92      * Test update_activity_completion_status
93      */
94     public function test_get_activities_completion_status() {
95         global $DB, $CFG;
97         $this->resetAfterTest(true);
99         $CFG->enablecompletion = true;
100         $student = $this->getDataGenerator()->create_user();
101         $teacher = $this->getDataGenerator()->create_user();
103         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
104                                                                     'groupmode' => SEPARATEGROUPS,
105                                                                     'groupmodeforce' => 1));
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));
115         $cmdata = get_coursemodule_from_id('data', $data->cmid);
116         $cmforum = get_coursemodule_from_id('forum', $forum->cmid);
118         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
119         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
120         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
121         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
123         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
124         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
126         // Teacher and student in different groups initially.
127         groups_add_member($group1->id, $student->id);
128         groups_add_member($group2->id, $teacher->id);
130         $this->setUser($student);
131         // Forum complete.
132         $completion = new completion_info($course);
133         $completion->update_state($cmforum, COMPLETION_COMPLETE);
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);
140         // We added 4 activities, but only 3 with completion enabled and one of those is hidden.
141         $this->assertCount(2, $result['statuses']);
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);
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);
164         // We added 4 activities, but only 3 with completion enabled and one of those is hidden.
165         $this->assertCount(3, $result['statuses']);
167         // Override status by teacher.
168         $completion->update_state($cmforum, COMPLETION_INCOMPLETE, $student->id, true);
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);
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         }
185         // Teacher should see his own completion status.
187         // Forum complete for teacher.
188         $completion = new completion_info($course);
189         $completion->update_state($cmforum, COMPLETION_COMPLETE);
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);
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']);
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);
213         // Change teacher role capabilities (disable access all groups).
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();
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         }
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);
231         // We added 4 activities, but only 3 with completion enabled and one of those is hidden.
232         $this->assertCount(3, $result['statuses']);
233     }
235     /**
236      * Test override_activity_completion_status
237      */
238     public function test_override_activity_completion_status() {
239         global $DB, $CFG;
240         $this->resetAfterTest(true);
242         // Create course with teacher and student enrolled.
243         $CFG->enablecompletion = true;
244         $course  = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
245         $student = $this->getDataGenerator()->create_user();
246         $teacher = $this->getDataGenerator()->create_user();
247         $studentrole = $DB->get_record('role', ['shortname' => 'student']);
248         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
249         $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
250         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
252         // Create 2 activities, one with manual completion (data), one with automatic completion triggered by viewing it (forum).
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);
259         // Manually complete the data activity as the student.
260         $this->setUser($student);
261         $completion = new completion_info($course);
262         $completion->update_state($cmdata, COMPLETION_COMPLETE);
264         // Test overriding the status of the manual-completion-activity 'incomplete'.
265         $this->setUser($teacher);
266         $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_INCOMPLETE);
267         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
268         $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
269         $completiondata = $completion->get_data($cmdata, false, $student->id);
270         $this->assertEquals(COMPLETION_INCOMPLETE, $completiondata->completionstate);
272         // Test overriding the status of the manual-completion-activity back to 'complete'.
273         $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_COMPLETE);
274         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
275         $this->assertEquals($result['state'], COMPLETION_COMPLETE);
276         $completiondata = $completion->get_data($cmdata, false, $student->id);
277         $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate);
279         // Test overriding the status of the auto-completion-activity to 'complete'.
280         $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE);
281         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
282         $this->assertEquals($result['state'], COMPLETION_COMPLETE);
283         $completionforum = $completion->get_data($cmforum, false, $student->id);
284         $this->assertEquals(COMPLETION_COMPLETE, $completionforum->completionstate);
286         // Test overriding the status of the auto-completion-activity to 'incomplete'.
287         $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_INCOMPLETE);
288         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
289         $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
290         $completionforum = $completion->get_data($cmforum, false, $student->id);
291         $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate);
293         // Test overriding the status of the auto-completion-activity to an invalid state.
294         $this->expectException('moodle_exception');
295         core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 3);
296     }
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);
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);
316         // Create an activity with automatic completion (a forum).
317         $forum   = $this->getDataGenerator()->create_module('forum',  ['course' => $course->id],
318             ['completion' => 2, 'completionview' => 1]);
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);
325     }
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');
341         $this->resetAfterTest(true);
343         $CFG->enablecompletion = true;
344         $student = $this->getDataGenerator()->create_user();
345         $teacher = $this->getDataGenerator()->create_user();
347         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
348                                                                     'groupmode' => SEPARATEGROUPS,
349                                                                     'groupmodeforce' => 1));
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));
357         $cmdata = get_coursemodule_from_id('data', $data->cmid);
358         $cmforum = get_coursemodule_from_id('forum', $forum->cmid);
360         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
361         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
362         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
363         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
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);
371         // Set completion rules.
372         $completion = new completion_info($course);
374         // Loop through each criteria type and run its update_config() method.
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;
383         // In a week criteria date value.
384         $criteriadata->criteria_date_value = time() + WEEKSECS;
386         // Self completion.
387         $criteriadata->criteria_self = 1;
389         foreach ($COMPLETION_CRITERIA_TYPES as $type) {
390             $class = 'completion_criteria_'.$type;
391             $criterion = new $class();
392             $criterion->update_config($criteriadata);
393         }
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();
404         $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
405         $aggregation = new completion_aggregation($aggdata);
406         $aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
407         $aggregation->save();
409         $this->setUser($student);
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);
416         // 3 different criteria.
417         $this->assertCount(3, $studentresult['completionstatus']['completions']);
419         $this->assertEquals(COMPLETION_AGGREGATION_ALL, $studentresult['completionstatus']['aggregation']);
420         $this->assertFalse($studentresult['completionstatus']['completed']);
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']);
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);
433         $this->assertEquals($studentresult, $teacherresult);
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();
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         }
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);
454         $this->assertEquals($studentresult, $teacherresult);
456     }
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');
465         $this->resetAfterTest(true);
467         $CFG->enablecompletion = true;
468         $student = $this->getDataGenerator()->create_user();
469         $teacher = $this->getDataGenerator()->create_user();
471         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
473         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
474         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
476         // Set completion rules.
477         $completion = new completion_info($course);
479         $criteriadata = new stdClass();
480         $criteriadata->id = $course->id;
481         $criteriadata->criteria_activity = array();
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);
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();
498         $this->setUser($student);
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);
505         // We expect a valid result.
506         $this->assertEquals(true, $result['status']);
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);
513         // Course must be completed.
514         $this->assertEquals(COMPLETION_COMPLETE, $result['completionstatus']['completions'][0]['complete']);
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         }
523     }