Merge branch 'MDL-53140-master' of git://github.com/rezaies/moodle
[moodle.git] / completion / tests / progress_test.php
CommitLineData
5b3c691f
MN
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 * Test completion progress API.
19 *
20 * @package core_completion
21 * @category test
22 * @copyright 2017 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Test completion progress API.
30 *
31 * @package core_completion
32 * @category test
33 * @copyright 2017 Mark Nelson <markn@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class core_completion_progress_testcase extends advanced_testcase {
37
38 /**
39 * Test setup.
40 */
41 public function setUp() {
42 global $CFG;
43
44 $CFG->enablecompletion = true;
45 $this->resetAfterTest();
46 }
47
48 /**
49 * Tests that the course progress percentage is returned correctly when we have only activity completion.
50 */
51 public function test_course_progress_percentage_with_just_activities() {
52 global $DB;
53
54 // Add a course that supports completion.
55 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
56
57 // Enrol a user in the course.
58 $user = $this->getDataGenerator()->create_user();
59 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
60 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
61
62 // Add four activities that use completion.
63 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
64 array('completion' => 1));
65 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
66 array('completion' => 1));
67 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
68 array('completion' => 1));
69 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
70 array('completion' => 1));
71
72 // Add an activity that does *not* use completion.
73 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
74
75 // Mark two of them as completed for a user.
76 $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
77 $cmdata = get_coursemodule_from_id('data', $data->cmid);
78 $completion = new completion_info($course);
79 $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
80 $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
81
82 // Check we have received valid data.
83 // Note - only 4 out of the 5 activities support completion, and the user has completed 2 of those.
84 $this->assertEquals('50', \core_completion\progress::get_course_progress_percentage($course, $user->id));
85 }
86
87 /**
88 * Tests that the course progress percentage is returned correctly when we have a course and activity completion.
89 */
90 public function test_course_progress_percentage_with_activities_and_course() {
91 global $DB;
92
93 // Add a course that supports completion.
94 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
95
96 // Enrol a user in the course.
97 $user = $this->getDataGenerator()->create_user();
98 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
99 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
100
101 // Add four activities that use completion.
102 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
103 array('completion' => 1));
104 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
105 array('completion' => 1));
106 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
107 array('completion' => 1));
108 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
109 array('completion' => 1));
110
111 // Add an activity that does *not* use completion.
112 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
113
114 // Mark two of them as completed for a user.
115 $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
116 $cmdata = get_coursemodule_from_id('data', $data->cmid);
117 $completion = new completion_info($course);
118 $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
119 $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
120
121 // Now, mark the course as completed.
122 $ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
123 $ccompletion->mark_complete();
124
125 // Check we have received valid data.
126 // The course completion takes priority, so should return 100.
127 $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
128 }
129
130 /**
131 * Tests that the course progress returns null when the course does not support it.
132 */
133 public function test_course_progress_course_not_using_completion() {
134 // Create a course that does not use completion.
135 $course = $this->getDataGenerator()->create_course();
136
137 // Check that the result was null.
138 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
139 }
140
141 /**
142 * Tests that the course progress returns null when there are no activities that support it.
143 */
144 public function test_course_progress_no_activities_using_completion() {
145 // Create a course that does support completion.
146 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
147
148 // Add an activity that does *not* support completion.
149 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
150
151 // Check that the result was null.
152 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
153 }
34945fa3
DM
154
155 /**
156 * Tests that the course progress returns null for a not tracked for completion user in a course.
157 */
158 public function test_course_progress_not_tracked_user() {
159 global $DB;
160
161 // Add a course that supports completion.
162 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
163
164 // Enrol a user in the course.
165 $user = $this->getDataGenerator()->create_user();
166 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
167
168 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
169
170 // Now, mark the course as completed.
171 $ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
172 $ccompletion->mark_complete();
173
174 // The course completion should return 100.
175 $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
176
177 // Now make the user's role to be not tracked for completion.
178 unassign_capability('moodle/course:isincompletionreports', $studentrole->id);
179
180 // Check that the result is null now.
181 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course, $user->id));
182 }
5b3c691f 183}