mirrored from git://git.moodle.org/moodle.git
/
testlocallib.php
429 lines (377 loc) · 18.1 KB
/
testlocallib.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?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/>.
/**
* Unit tests for workshop api class defined in mod/workshop/locallib.php
*
* @package mod
* @subpackage workshop
* @copyright 2009 David Mudrak <david.mudrak@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test
global $DB;
Mock::generate(get_class($DB), 'mockDB');
/**
* Test subclass that makes all the protected methods we want to test public.
*/
class testable_workshop extends workshop {
public function __construct() {
$this->id = 16;
$this->cm = new stdclass();
$this->course = new stdclass();
$this->context = new stdclass();
}
public function aggregate_submission_grades_process(array $assessments) {
parent::aggregate_submission_grades_process($assessments);
}
public function aggregate_grading_grades_process(array $assessments, $timegraded = null) {
parent::aggregate_grading_grades_process($assessments, $timegraded);
}
}
/**
* Test cases for the internal workshop api
*/
class workshop_internal_api_test extends UnitTestCase {
/** workshop instance emulation */
protected $workshop;
/** setup testing environment */
public function setUp() {
global $DB;
$this->realDB = $DB;
$DB = new mockDB();
$this->workshop = new testable_workshop();
}
public function tearDown() {
global $DB;
$DB = $this->realDB;
$this->workshop = null;
}
public function test_aggregate_submission_grades_process_notgraded() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null);
$DB->expectNever('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_single() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.12345);
$expected = 10.12345;
$DB->expectOnce('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_null_doesnt_influence() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 45.54321);
$batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null);
$expected = 45.54321;
$DB->expectOnce('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_weighted_single() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 4, 'grade' => 14.00012);
$expected = 14.00012;
$DB->expectOnce('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_mean() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 56.12000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 12.59000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 0.00000);
$expected = 19.67750;
$DB->expectOnce('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_mean_changed() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 56.12000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 12.59000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 10.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 0.00000);
$expected = 19.67750;
$DB->expectOnce('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_mean_nochange() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 56.12000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 12.59000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 10.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 0.00000);
$DB->expectNever('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_rounding() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 4.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 2.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 1.00000);
$expected = 2.33333;
$DB->expectOnce('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_submission_grades_process_weighted_mean() {
global $DB;
// fixture set-up
$batch = array(); // batch of a submission's assessments
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 3, 'grade' => 12.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 2, 'grade' => 30.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000);
$batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 0, 'grade' => 1000.00000);
$expected = 17.66667;
$DB->expectOnce('update_record');
// exercise SUT
$this->workshop->aggregate_submission_grades_process($batch);
}
public function test_aggregate_grading_grades_process_nograding() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
// expectation
$DB->expectNever('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_aggregate_grading_grades_process_single_grade_new() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>82.87670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
// expectation
$now = time();
$expected = new stdclass();
$expected->workshopid = $this->workshop->id;
$expected->userid = 3;
$expected->gradinggrade = 82.87670;
$expected->timegraded = $now;
$DB->expectOnce('insert_record', array('workshop_aggregations', $expected));
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch, $now);
}
public function test_aggregate_grading_grades_process_single_grade_update() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>82.87670);
// expectation
$DB->expectOnce('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_aggregate_grading_grades_process_single_grade_uptodate() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>90.00000);
// expectation
$DB->expectNever('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_aggregate_grading_grades_process_single_grade_overridden() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>4, 'gradinggrade'=>91.56700, 'gradinggradeover'=>82.32105, 'aggregationid'=>2, 'aggregatedgrade'=>91.56700);
// expectation
$DB->expectOnce('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_aggregate_grading_grades_process_multiple_grades_new() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>99.45670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
// expectation
$now = time();
$expected = new stdclass();
$expected->workshopid = $this->workshop->id;
$expected->userid = 5;
$expected->gradinggrade = 79.3066;
$expected->timegraded = $now;
$DB->expectOnce('insert_record', array('workshop_aggregations', $expected));
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch, $now);
}
public function test_aggregate_grading_grades_process_multiple_grades_update() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
// expectation
$DB->expectOnce('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_aggregate_grading_grades_process_multiple_grades_overriden() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>99.45670, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
$batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
// expectation
$DB->expectOnce('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_aggregate_grading_grades_process_multiple_grades_one_missing() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
$batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
$batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
// expectation
$DB->expectOnce('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_aggregate_grading_grades_process_multiple_grades_missing_overridden() {
global $DB;
// fixture set-up
$batch = array();
$batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
$batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>69.00000, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
$batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
// expectation
$DB->expectOnce('update_record');
// excersise SUT
$this->workshop->aggregate_grading_grades_process($batch);
}
public function test_percent_to_value() {
// fixture setup
$total = 185;
$percent = 56.6543;
// exercise SUT
$part = workshop::percent_to_value($percent, $total);
// verify
$this->assertEqual($part, $total * $percent / 100);
}
public function test_percent_to_value_negative() {
// fixture setup
$total = 185;
$percent = -7.098;
// set expectation
$this->expectException('coding_exception');
// exercise SUT
$part = workshop::percent_to_value($percent, $total);
}
public function test_percent_to_value_over_hundred() {
// fixture setup
$total = 185;
$percent = 121.08;
// set expectation
$this->expectException('coding_exception');
// exercise SUT
$part = workshop::percent_to_value($percent, $total);
}
public function test_lcm() {
// fixture setup + exercise SUT + verify in one step
$this->assertEqual(workshop::lcm(1,4), 4);
$this->assertEqual(workshop::lcm(2,4), 4);
$this->assertEqual(workshop::lcm(4,2), 4);
$this->assertEqual(workshop::lcm(2,3), 6);
$this->assertEqual(workshop::lcm(6,4), 12);
}
public function test_lcm_array() {
// fixture setup
$numbers = array(5,3,15);
// excersise SUT
$lcm = array_reduce($numbers, 'workshop::lcm', 1);
// verify
$this->assertEqual($lcm, 15);
}
public function test_prepare_example_assessment() {
// fixture setup
$fakerawrecord = (object)array(
'id' => 42,
'submissionid' => 56,
'weight' => 0,
'timecreated' => time() - 10,
'timemodified' => time() - 5,
'grade' => null,
'gradinggrade' => null,
'gradinggradeover' => null,
);
// excersise SUT
$a = $this->workshop->prepare_example_assessment($fakerawrecord);
// verify
$this->assertTrue($a instanceof workshop_example_assessment);
$this->assertTrue($a->url instanceof moodle_url);
// modify setup
$fakerawrecord->weight = 1;
$this->expectException('coding_exception');
// excersise SUT
$a = $this->workshop->prepare_example_assessment($fakerawrecord);
}
public function test_prepare_example_reference_assessment() {
global $USER;
// fixture setup
$fakerawrecord = (object)array(
'id' => 38,
'submissionid' => 56,
'weight' => 1,
'timecreated' => time() - 100,
'timemodified' => time() - 50,
'grade' => 0.75000,
'gradinggrade' => 1.00000,
'gradinggradeover' => null,
);
// excersise SUT
$a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
// verify
$this->assertTrue($a instanceof workshop_example_reference_assessment);
// modify setup
$fakerawrecord->weight = 0;
$this->expectException('coding_exception');
// excersise SUT
$a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
}
}