mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
cm_completion_details_test.php
283 lines (256 loc) · 10.3 KB
/
cm_completion_details_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains unit tests for core_completion/cm_completion_details.
*
* @package core_completion
* @copyright Jun Pataleta <jun@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types = 1);
namespace core_completion;
use advanced_testcase;
use cm_info;
use completion_info;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/completionlib.php');
/**
* Class for unit testing core_completion/cm_completion_details.
*
* @package core_completion
* @copyright Jun Pataleta <jun@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cm_completion_details_test extends advanced_testcase {
/** @var completion_info A completion object. */
protected $completioninfo = null;
/**
* Fetches a mocked cm_completion_details instance.
*
* @param int|null $completion The completion tracking mode for the module.
* @param array $completionoptions Completion options (e.g. completionview, completionusegrade, etc.)
* @return cm_completion_details
*/
protected function setup_data(?int $completion, array $completionoptions = [],
object $mockcompletiondata = null): cm_completion_details {
if (is_null($completion)) {
$completion = COMPLETION_TRACKING_AUTOMATIC;
}
// Mock a completion_info instance so we can simply mock the returns of completion_info::get_data() later.
$this->completioninfo = $this->getMockBuilder(completion_info::class)
->disableOriginalConstructor()
->getMock();
// Mock return of completion_info's is_enabled() method to match the expected completion tracking for the module.
$this->completioninfo->expects($this->any())
->method('is_enabled')
->willReturn($completion);
if (!empty($mockcompletiondata)) {
$this->completioninfo->expects($this->any())
->method('get_data')
->willReturn($mockcompletiondata);
}
// Build a mock cm_info instance.
$mockcminfo = $this->getMockBuilder(cm_info::class)
->disableOriginalConstructor()
->onlyMethods(['__get'])
->getMock();
// Mock the return of the magic getter method when fetching the cm_info object's customdata and instance values.
$mockcminfo->expects($this->any())
->method('__get')
->will($this->returnValueMap([
['completion', $completion],
['instance', 1],
['modname', 'somenonexistentmod'],
['completionview', $completionoptions['completionview'] ?? COMPLETION_VIEW_NOT_REQUIRED],
['completiongradeitemnumber', $completionoptions['completionusegrade'] ?? null],
]));
return new cm_completion_details($this->completioninfo, $mockcminfo, 2);
}
/**
* Provides data for test_has_completion().
*
* @return array[]
*/
public function has_completion_provider(): array {
return [
'Automatic' => [
COMPLETION_TRACKING_AUTOMATIC, true
],
'Manual' => [
COMPLETION_TRACKING_MANUAL, true
],
'None' => [
COMPLETION_TRACKING_NONE, false
],
];
}
/**
* Test for has_completion().
*
* @dataProvider has_completion_provider
* @param int $completion The completion tracking mode.
* @param bool $expectedresult Expected result.
*/
public function test_has_completion(int $completion, bool $expectedresult) {
$cmcompletion = $this->setup_data($completion);
$this->assertEquals($expectedresult, $cmcompletion->has_completion());
}
/**
* Provides data for test_is_automatic().
*
* @return array[]
*/
public function is_automatic_provider(): array {
return [
'Automatic' => [
COMPLETION_TRACKING_AUTOMATIC, true
],
'Manual' => [
COMPLETION_TRACKING_MANUAL, false
],
'None' => [
COMPLETION_TRACKING_NONE, false
],
];
}
/**
* Test for is_available().
*
* @dataProvider is_automatic_provider
* @param int $completion The completion tracking mode.
* @param bool $expectedresult Expected result.
*/
public function test_is_automatic(int $completion, bool $expectedresult) {
$cmcompletion = $this->setup_data($completion);
$this->assertEquals($expectedresult, $cmcompletion->is_automatic());
}
/**
* Data provider for test_get_overall_completion().
* @return array[]
*/
public function overall_completion_provider(): array {
return [
'Complete' => [COMPLETION_COMPLETE],
'Incomplete' => [COMPLETION_INCOMPLETE],
];
}
/**
* Test for get_overall_completion().
*
* @dataProvider overall_completion_provider
* @param int $state
*/
public function test_get_overall_completion(int $state) {
$completiondata = (object)['completionstate' => $state];
$cmcompletion = $this->setup_data(COMPLETION_TRACKING_AUTOMATIC, [], $completiondata);
$this->assertEquals($state, $cmcompletion->get_overall_completion());
}
/**
* Data provider for test_get_details().
* @return array[]
*/
public function get_details_provider() {
return [
'No completion tracking' => [
COMPLETION_TRACKING_NONE, null, null, []
],
'Manual completion tracking' => [
COMPLETION_TRACKING_MANUAL, null, null, []
],
'Automatic, require view, not viewed' => [
COMPLETION_TRACKING_AUTOMATIC, COMPLETION_INCOMPLETE, null, [
'completionview' => (object)[
'status' => COMPLETION_INCOMPLETE,
'description' => get_string('detail_desc:view', 'completion'),
]
]
],
'Automatic, require view, viewed' => [
COMPLETION_TRACKING_AUTOMATIC, COMPLETION_COMPLETE, null, [
'completionview' => (object)[
'status' => COMPLETION_COMPLETE,
'description' => get_string('detail_desc:view', 'completion'),
]
]
],
'Automatic, require grade, incomplete' => [
COMPLETION_TRACKING_AUTOMATIC, null, COMPLETION_INCOMPLETE, [
'completionusegrade' => (object)[
'status' => COMPLETION_INCOMPLETE,
'description' => get_string('detail_desc:receivegrade', 'completion'),
]
]
],
'Automatic, require grade, complete' => [
COMPLETION_TRACKING_AUTOMATIC, null, COMPLETION_COMPLETE, [
'completionusegrade' => (object)[
'status' => COMPLETION_COMPLETE,
'description' => get_string('detail_desc:receivegrade', 'completion'),
]
]
],
'Automatic, require view (complete) and grade (incomplete)' => [
COMPLETION_TRACKING_AUTOMATIC, COMPLETION_COMPLETE, COMPLETION_INCOMPLETE, [
'completionview' => (object)[
'status' => COMPLETION_COMPLETE,
'description' => get_string('detail_desc:view', 'completion'),
],
'completionusegrade' => (object)[
'status' => COMPLETION_INCOMPLETE,
'description' => get_string('detail_desc:receivegrade', 'completion'),
]
]
],
'Automatic, require view (incomplete) and grade (complete)' => [
COMPLETION_TRACKING_AUTOMATIC, COMPLETION_INCOMPLETE, COMPLETION_COMPLETE, [
'completionview' => (object)[
'status' => COMPLETION_INCOMPLETE,
'description' => get_string('detail_desc:view', 'completion'),
],
'completionusegrade' => (object)[
'status' => COMPLETION_COMPLETE,
'description' => get_string('detail_desc:receivegrade', 'completion'),
]
]
],
];
}
/**
* Test for \core_completion\cm_completion_details::get_details().
*
* @dataProvider get_details_provider
* @param int $completion The completion tracking mode.
* @param int|null $completionview Completion status of the "view" completion condition.
* @param int|null $completiongrade Completion status of the "must receive grade" completion condition.
* @param array $expecteddetails Expected completion details returned by get_details().
*/
public function test_get_details(int $completion, ?int $completionview, ?int $completiongrade, array $expecteddetails) {
$options = [];
$getdatareturn = (object)[
'viewed' => $completionview,
'completiongrade' => $completiongrade,
];
if (!is_null($completionview)) {
$options['completionview'] = true;
}
if (!is_null($completiongrade)) {
$options['completionusegrade'] = true;
}
$cmcompletion = $this->setup_data($completion, $options, $getdatareturn);
$this->assertEquals($expecteddetails, $cmcompletion->get_details());
}
}