cd1e4fa75e9dc422ed00038383ee1f384a1be29a
[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', array('shortname' => 'student'));
248         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
249         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
250         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
251         $coursecontext = context_course::instance($course->id);
253         // Create 2 activities, one with manual completion (data), one with automatic completion triggered by viewiung it (forum).
254         $data    = $this->getDataGenerator()->create_module('data', ['course' => $course->id], ['completion' => 1]);
255         $forum   = $this->getDataGenerator()->create_module('forum',  ['course' => $course->id],
256                                                             ['completion' => 2, 'completionview' => 1]);
257         $cmdata = get_coursemodule_from_id('data', $data->cmid);
258         $cmforum = get_coursemodule_from_id('forum', $forum->cmid);
260         // Manually complete the data activity as the student.
261         $this->setUser($student);
262         $completion = new completion_info($course);
263         $completion->update_state($cmdata, COMPLETION_COMPLETE);
265         // Test overriding the status of the manual-completion-activity 'incomplete'.
266         $this->setUser($teacher);
267         $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_INCOMPLETE);
268         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
269         $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
270         $completiondata = $completion->get_data($cmdata, false, $student->id);
271         $this->assertEquals(COMPLETION_INCOMPLETE, $completiondata->completionstate);
273         // Test overriding the status of the manual-completion-activity back to 'complete'.
274         $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_COMPLETE);
275         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
276         $this->assertEquals($result['state'], COMPLETION_COMPLETE);
277         $completiondata = $completion->get_data($cmdata, false, $student->id);
278         $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate);
280         // Test overriding the status of the auto-completion-activity to 'complete'.
281         $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE);
282         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
283         $this->assertEquals($result['state'], COMPLETION_COMPLETE);
284         $completionforum = $completion->get_data($cmforum, false, $student->id);
285         $this->assertEquals(COMPLETION_COMPLETE, $completionforum->completionstate);
287         // Test overriding the status of the auto-completion-activity to 'incomplete'.
288         $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_INCOMPLETE);
289         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
290         $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
291         $completionforum = $completion->get_data($cmforum, false, $student->id);
292         $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate);
294         // Test overriding the status of the auto-completion-activity to an invalid state. It should remain incomplete.
295         $this->expectException('moodle_exception');
296         $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 3);
297         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
298         $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
299         $completionforum = $completion->get_data($cmforum, false, $student->id);
300         $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate);
302         // Test overriding the status of the auto-completion-activity for a user without capabilities. It should remain incomplete.
303         $this->expectException('moodle_exception');
304         unassign_capability('moodle/course:overridecompletion', $teacherrole->id, $coursecontext);
305         $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 1);
306         $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result);
307         $this->assertEquals($result['state'], COMPLETION_INCOMPLETE);
308         $completionforum = $completion->get_data($cmforum, false, $student->id);
309         $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate);
310     }
312     /**
313      * Test get_course_completion_status
314      */
315     public function test_get_course_completion_status() {
316         global $DB, $CFG, $COMPLETION_CRITERIA_TYPES;
317         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
318         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
319         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_unenrol.php');
320         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
321         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
322         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
323         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_role.php');
324         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
326         $this->resetAfterTest(true);
328         $CFG->enablecompletion = true;
329         $student = $this->getDataGenerator()->create_user();
330         $teacher = $this->getDataGenerator()->create_user();
332         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1,
333                                                                     'groupmode' => SEPARATEGROUPS,
334                                                                     'groupmodeforce' => 1));
336         $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
337                                                              array('completion' => 1));
338         $forum = $this->getDataGenerator()->create_module('forum',  array('course' => $course->id),
339                                                              array('completion' => 1));
340         $assign = $this->getDataGenerator()->create_module('assign',  array('course' => $course->id));
342         $cmdata = get_coursemodule_from_id('data', $data->cmid);
343         $cmforum = get_coursemodule_from_id('forum', $forum->cmid);
345         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
346         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
347         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
348         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
350         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
351         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
352         // Teacher and student in different groups initially.
353         groups_add_member($group1->id, $student->id);
354         groups_add_member($group2->id, $teacher->id);
356         // Set completion rules.
357         $completion = new completion_info($course);
359         // Loop through each criteria type and run its update_config() method.
361         $criteriadata = new stdClass();
362         $criteriadata->id = $course->id;
363         $criteriadata->criteria_activity = array();
364         // Some activities.
365         $criteriadata->criteria_activity[$cmdata->id] = 1;
366         $criteriadata->criteria_activity[$cmforum->id] = 1;
368         // In a week criteria date value.
369         $criteriadata->criteria_date_value = time() + WEEKSECS;
371         // Self completion.
372         $criteriadata->criteria_self = 1;
374         foreach ($COMPLETION_CRITERIA_TYPES as $type) {
375             $class = 'completion_criteria_'.$type;
376             $criterion = new $class();
377             $criterion->update_config($criteriadata);
378         }
380         // Handle overall aggregation.
381         $aggdata = array(
382             'course'        => $course->id,
383             'criteriatype'  => null
384         );
385         $aggregation = new completion_aggregation($aggdata);
386         $aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
387         $aggregation->save();
389         $aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
390         $aggregation = new completion_aggregation($aggdata);
391         $aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
392         $aggregation->save();
394         $this->setUser($student);
396         $result = core_completion_external::get_course_completion_status($course->id, $student->id);
397         // We need to execute the return values cleaning process to simulate the web service server.
398         $studentresult = external_api::clean_returnvalue(
399             core_completion_external::get_course_completion_status_returns(), $result);
401         // 3 different criteria.
402         $this->assertCount(3, $studentresult['completionstatus']['completions']);
404         $this->assertEquals(COMPLETION_AGGREGATION_ALL, $studentresult['completionstatus']['aggregation']);
405         $this->assertFalse($studentresult['completionstatus']['completed']);
407         $this->assertEquals('No', $studentresult['completionstatus']['completions'][0]['status']);
408         $this->assertEquals('No', $studentresult['completionstatus']['completions'][1]['status']);
409         $this->assertEquals('No', $studentresult['completionstatus']['completions'][2]['status']);
411         // Teacher should see students status, they are in different groups but the teacher can access all groups.
412         $this->setUser($teacher);
413         $result = core_completion_external::get_course_completion_status($course->id, $student->id);
414         // We need to execute the return values cleaning process to simulate the web service server.
415         $teacherresult = external_api::clean_returnvalue(
416             core_completion_external::get_course_completion_status_returns(), $result);
418         $this->assertEquals($studentresult, $teacherresult);
420         // Change teacher role capabilities (disable access al goups).
421         $context = context_course::instance($course->id);
422         assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $context);
423         accesslib_clear_all_caches_for_unit_testing();
425         try {
426             $result = core_completion_external::get_course_completion_status($course->id, $student->id);
427             $this->fail('Exception expected due to groups permissions.');
428         } catch (moodle_exception $e) {
429             $this->assertEquals('accessdenied', $e->errorcode);
430         }
432         // Now add the teacher in the same group.
433         groups_add_member($group1->id, $teacher->id);
434         $result = core_completion_external::get_course_completion_status($course->id, $student->id);
435         // We need to execute the return values cleaning process to simulate the web service server.
436         $teacherresult = external_api::clean_returnvalue(
437             core_completion_external::get_course_completion_status_returns(), $result);
439         $this->assertEquals($studentresult, $teacherresult);
441     }
443     /**
444      * Test mark_course_self_completed
445      */
446     public function test_mark_course_self_completed() {
447         global $DB, $CFG;
448         require_once($CFG->dirroot.'/completion/criteria/completion_criteria_self.php');
450         $this->resetAfterTest(true);
452         $CFG->enablecompletion = true;
453         $student = $this->getDataGenerator()->create_user();
454         $teacher = $this->getDataGenerator()->create_user();
456         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
458         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
459         $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
461         // Set completion rules.
462         $completion = new completion_info($course);
464         $criteriadata = new stdClass();
465         $criteriadata->id = $course->id;
466         $criteriadata->criteria_activity = array();
468         // Self completion.
469         $criteriadata->criteria_self = COMPLETION_CRITERIA_TYPE_SELF;
470         $class = 'completion_criteria_self';
471         $criterion = new $class();
472         $criterion->update_config($criteriadata);
474         // Handle overall aggregation.
475         $aggdata = array(
476             'course'        => $course->id,
477             'criteriatype'  => null
478         );
479         $aggregation = new completion_aggregation($aggdata);
480         $aggregation->setMethod(COMPLETION_AGGREGATION_ALL);
481         $aggregation->save();
483         $this->setUser($student);
485         $result = core_completion_external::mark_course_self_completed($course->id);
486         // We need to execute the return values cleaning process to simulate the web service server.
487         $result = external_api::clean_returnvalue(
488             core_completion_external::mark_course_self_completed_returns(), $result);
490         // We expect a valid result.
491         $this->assertEquals(true, $result['status']);
493         $result = core_completion_external::get_course_completion_status($course->id, $student->id);
494         // We need to execute the return values cleaning process to simulate the web service server.
495         $result = external_api::clean_returnvalue(
496             core_completion_external::get_course_completion_status_returns(), $result);
498         // Course must be completed.
499         $this->assertEquals(COMPLETION_COMPLETE, $result['completionstatus']['completions'][0]['complete']);
501         try {
502             $result = core_completion_external::mark_course_self_completed($course->id);
503             $this->fail('Exception expected due course already self completed.');
504         } catch (moodle_exception $e) {
505             $this->assertEquals('useralreadymarkedcomplete', $e->errorcode);
506         }
508     }