Merge branch 'MDL-74631-invalid-tag-311' of https://github.com/leonstr/moodle into...
[moodle.git] / completion / tests / progress_test.php
blob08785fa3b25bb4c1c68e307ec5b9dae4a84dd498
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.
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 namespace core_completion;
19 use completion_completion;
21 /**
22 * Test completion progress API.
24 * @package core_completion
25 * @category test
26 * @copyright 2017 Mark Nelson <markn@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class progress_test extends \advanced_testcase {
31 /**
32 * Test setup.
34 public function setUp(): void {
35 global $CFG;
37 $CFG->enablecompletion = true;
38 $this->resetAfterTest();
41 /**
42 * Tests that the course progress percentage is returned correctly when we have only activity completion.
44 public function test_course_progress_percentage_with_just_activities() {
45 global $DB;
47 // Add a course that supports completion.
48 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
50 // Enrol a user in the course.
51 $user = $this->getDataGenerator()->create_user();
52 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
53 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
55 // Add four activities that use completion.
56 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
57 array('completion' => 1));
58 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
59 array('completion' => 1));
60 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
61 array('completion' => 1));
62 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
63 array('completion' => 1));
65 // Add an activity that does *not* use completion.
66 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
68 // Mark two of them as completed for a user.
69 $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
70 $cmdata = get_coursemodule_from_id('data', $data->cmid);
71 $completion = new \completion_info($course);
72 $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
73 $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
75 // Check we have received valid data.
76 // Note - only 4 out of the 5 activities support completion, and the user has completed 2 of those.
77 $this->assertEquals('50', \core_completion\progress::get_course_progress_percentage($course, $user->id));
80 /**
81 * Tests that the course progress percentage is returned correctly when we have a course and activity completion.
83 public function test_course_progress_percentage_with_activities_and_course() {
84 global $DB;
86 // Add a course that supports completion.
87 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
89 // Enrol a user in the course.
90 $user = $this->getDataGenerator()->create_user();
91 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
92 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
94 // Add four activities that use completion.
95 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
96 array('completion' => 1));
97 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
98 array('completion' => 1));
99 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
100 array('completion' => 1));
101 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
102 array('completion' => 1));
104 // Add an activity that does *not* use completion.
105 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
107 // Mark two of them as completed for a user.
108 $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
109 $cmdata = get_coursemodule_from_id('data', $data->cmid);
110 $completion = new \completion_info($course);
111 $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
112 $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
114 // Now, mark the course as completed.
115 $ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
116 $ccompletion->mark_complete();
118 // Check we have received valid data.
119 // The course completion takes priority, so should return 100.
120 $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
124 * Tests that the course progress returns null when the course does not support it.
126 public function test_course_progress_course_not_using_completion() {
127 // Create a course that does not use completion.
128 $course = $this->getDataGenerator()->create_course();
130 // Check that the result was null.
131 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
135 * Tests that the course progress returns null when there are no activities that support it.
137 public function test_course_progress_no_activities_using_completion() {
138 // Create a course that does support completion.
139 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
141 // Add an activity that does *not* support completion.
142 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
144 // Check that the result was null.
145 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
149 * Tests that the course progress returns null for a not tracked for completion user in a course.
151 public function test_course_progress_not_tracked_user() {
152 global $DB;
154 // Add a course that supports completion.
155 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
157 // Enrol a user in the course.
158 $user = $this->getDataGenerator()->create_user();
159 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
161 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
163 // Now, mark the course as completed.
164 $ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
165 $ccompletion->mark_complete();
167 // The course completion should return 100.
168 $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
170 // Now make the user's role to be not tracked for completion.
171 unassign_capability('moodle/course:isincompletionreports', $studentrole->id);
173 // Check that the result is null now.
174 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course, $user->id));