Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Unit tests for workshop api class defined in mod/workshop/locallib.php
19
 *
20
 * @package    mod_workshop
21
 * @category   phpunit
22
 * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace mod_workshop;
26
 
27
use testable_workshop;
28
use workshop;
29
use workshop_example_assessment;
30
use workshop_example_reference_assessment;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test
36
require_once(__DIR__ . '/fixtures/testable.php');
37
 
38
 
39
/**
40
 * Test cases for the internal workshop api
41
 */
1441 ariadna 42
final class locallib_test extends \advanced_testcase {
1 efrain 43
 
44
    /** @var object */
45
    protected $course;
46
 
47
    /** @var workshop */
48
    protected $workshop;
49
 
50
    /** setup testing environment */
51
    protected function setUp(): void {
52
        parent::setUp();
53
        $this->setAdminUser();
54
        $this->course = $this->getDataGenerator()->create_course();
55
        $workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course));
56
        $cm = get_coursemodule_from_instance('workshop', $workshop->id, $this->course->id, false, MUST_EXIST);
57
        $this->workshop = new testable_workshop($workshop, $cm, $this->course);
58
    }
59
 
60
    protected function tearDown(): void {
61
        $this->workshop = null;
62
        parent::tearDown();
63
    }
64
 
11 efrain 65
    public function test_aggregate_submission_grades_process_notgraded(): void {
1 efrain 66
        $this->resetAfterTest(true);
67
 
68
        // fixture set-up
69
        $batch = array();   // batch of a submission's assessments
70
        $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null);
71
        //$DB->expectNever('update_record');
72
        // exercise SUT
73
        $this->workshop->aggregate_submission_grades_process($batch);
74
    }
75
 
11 efrain 76
    public function test_aggregate_submission_grades_process_single(): void {
1 efrain 77
        $this->resetAfterTest(true);
78
 
79
        // fixture set-up
80
        $batch = array();   // batch of a submission's assessments
81
        $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.12345);
82
        $expected = 10.12345;
83
        //$DB->expectOnce('update_record');
84
        // exercise SUT
85
        $this->workshop->aggregate_submission_grades_process($batch);
86
    }
87
 
11 efrain 88
    public function test_aggregate_submission_grades_process_null_doesnt_influence(): void {
1 efrain 89
        $this->resetAfterTest(true);
90
 
91
        // fixture set-up
92
        $batch = array();   // batch of a submission's assessments
93
        $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 45.54321);
94
        $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null);
95
        $expected = 45.54321;
96
        //$DB->expectOnce('update_record');
97
        // exercise SUT
98
        $this->workshop->aggregate_submission_grades_process($batch);
99
    }
100
 
11 efrain 101
    public function test_aggregate_submission_grades_process_weighted_single(): void {
1 efrain 102
        $this->resetAfterTest(true);
103
 
104
        // fixture set-up
105
        $batch = array();   // batch of a submission's assessments
106
        $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 4, 'grade' => 14.00012);
107
        $expected = 14.00012;
108
        //$DB->expectOnce('update_record');
109
        // exercise SUT
110
        $this->workshop->aggregate_submission_grades_process($batch);
111
    }
112
 
11 efrain 113
    public function test_aggregate_submission_grades_process_mean(): void {
1 efrain 114
        $this->resetAfterTest(true);
115
 
116
        // fixture set-up
117
        $batch = array();   // batch of a submission's assessments
118
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 56.12000);
119
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 12.59000);
120
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000);
121
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 0.00000);
122
        $expected = 19.67750;
123
        //$DB->expectOnce('update_record');
124
        // exercise SUT
125
        $this->workshop->aggregate_submission_grades_process($batch);
126
    }
127
 
11 efrain 128
    public function test_aggregate_submission_grades_process_mean_changed(): void {
1 efrain 129
        $this->resetAfterTest(true);
130
 
131
        // fixture set-up
132
        $batch = array();   // batch of a submission's assessments
133
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 56.12000);
134
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 12.59000);
135
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 10.00000);
136
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 0.00000);
137
        $expected = 19.67750;
138
        //$DB->expectOnce('update_record');
139
        // exercise SUT
140
        $this->workshop->aggregate_submission_grades_process($batch);
141
    }
142
 
11 efrain 143
    public function test_aggregate_submission_grades_process_mean_nochange(): void {
1 efrain 144
        $this->resetAfterTest(true);
145
 
146
        // fixture set-up
147
        $batch = array();   // batch of a submission's assessments
148
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 56.12000);
149
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 12.59000);
150
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 10.00000);
151
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 0.00000);
152
        //$DB->expectNever('update_record');
153
        // exercise SUT
154
        $this->workshop->aggregate_submission_grades_process($batch);
155
    }
156
 
11 efrain 157
    public function test_aggregate_submission_grades_process_rounding(): void {
1 efrain 158
        $this->resetAfterTest(true);
159
 
160
        // fixture set-up
161
        $batch = array();   // batch of a submission's assessments
162
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 4.00000);
163
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 2.00000);
164
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 1.00000);
165
        $expected = 2.33333;
166
        //$DB->expectOnce('update_record');
167
        // exercise SUT
168
        $this->workshop->aggregate_submission_grades_process($batch);
169
    }
170
 
11 efrain 171
    public function test_aggregate_submission_grades_process_weighted_mean(): void {
1 efrain 172
        $this->resetAfterTest(true);
173
 
174
        // fixture set-up
175
        $batch = array();   // batch of a submission's assessments
176
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 3, 'grade' => 12.00000);
177
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 2, 'grade' => 30.00000);
178
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000);
179
        $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 0, 'grade' => 1000.00000);
180
        $expected = 17.66667;
181
        //$DB->expectOnce('update_record');
182
        // exercise SUT
183
        $this->workshop->aggregate_submission_grades_process($batch);
184
    }
185
 
11 efrain 186
    public function test_aggregate_grading_grades_process_nograding(): void {
1 efrain 187
        $this->resetAfterTest(true);
188
        // fixture set-up
189
        $batch = array();
190
        $batch[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
191
        // expectation
192
        //$DB->expectNever('update_record');
193
        // excersise SUT
194
        $this->workshop->aggregate_grading_grades_process($batch);
195
    }
196
 
11 efrain 197
    public function test_aggregate_grading_grades_process_single_grade_new(): void {
1 efrain 198
        $this->resetAfterTest(true);
199
        // fixture set-up
200
        $batch = array();
201
        $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>82.87670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
202
        // expectation
203
        $now = time();
204
        $expected = new \stdClass();
205
        $expected->workshopid = $this->workshop->id;
206
        $expected->userid = 3;
207
        $expected->gradinggrade = 82.87670;
208
        $expected->timegraded = $now;
209
        //$DB->expectOnce('insert_record', array('workshop_aggregations', $expected));
210
        // excersise SUT
211
        $this->workshop->aggregate_grading_grades_process($batch, $now);
212
    }
213
 
11 efrain 214
    public function test_aggregate_grading_grades_process_single_grade_update(): void {
1 efrain 215
        $this->resetAfterTest(true);
216
        // fixture set-up
217
        $batch = array();
218
        $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>82.87670);
219
        // expectation
220
        //$DB->expectOnce('update_record');
221
        // excersise SUT
222
        $this->workshop->aggregate_grading_grades_process($batch);
223
    }
224
 
11 efrain 225
    public function test_aggregate_grading_grades_process_single_grade_uptodate(): void {
1 efrain 226
        $this->resetAfterTest(true);
227
        // fixture set-up
228
        $batch = array();
229
        $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>90.00000);
230
        // expectation
231
        //$DB->expectNever('update_record');
232
        // excersise SUT
233
        $this->workshop->aggregate_grading_grades_process($batch);
234
    }
235
 
11 efrain 236
    public function test_aggregate_grading_grades_process_single_grade_overridden(): void {
1 efrain 237
        $this->resetAfterTest(true);
238
        // fixture set-up
239
        $batch = array();
240
        $batch[] = (object)array('reviewerid'=>4, 'gradinggrade'=>91.56700, 'gradinggradeover'=>82.32105, 'aggregationid'=>2, 'aggregatedgrade'=>91.56700);
241
        // expectation
242
        //$DB->expectOnce('update_record');
243
        // excersise SUT
244
        $this->workshop->aggregate_grading_grades_process($batch);
245
    }
246
 
11 efrain 247
    public function test_aggregate_grading_grades_process_multiple_grades_new(): void {
1 efrain 248
        $this->resetAfterTest(true);
249
        // fixture set-up
250
        $batch = array();
251
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>99.45670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
252
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
253
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null);
254
        // expectation
255
        $now = time();
256
        $expected = new \stdClass();
257
        $expected->workshopid = $this->workshop->id;
258
        $expected->userid = 5;
259
        $expected->gradinggrade = 79.3066;
260
        $expected->timegraded = $now;
261
        //$DB->expectOnce('insert_record', array('workshop_aggregations', $expected));
262
        // excersise SUT
263
        $this->workshop->aggregate_grading_grades_process($batch, $now);
264
    }
265
 
11 efrain 266
    public function test_aggregate_grading_grades_process_multiple_grades_update(): void {
1 efrain 267
        $this->resetAfterTest(true);
268
        // fixture set-up
269
        $batch = array();
270
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
271
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
272
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660);
273
        // expectation
274
        //$DB->expectOnce('update_record');
275
        // excersise SUT
276
        $this->workshop->aggregate_grading_grades_process($batch);
277
    }
278
 
11 efrain 279
    public function test_aggregate_grading_grades_process_multiple_grades_overriden(): void {
1 efrain 280
        $this->resetAfterTest(true);
281
        // fixture set-up
282
        $batch = array();
283
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>99.45670, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
284
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
285
        $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904);
286
        // expectation
287
        //$DB->expectOnce('update_record');
288
        // excersise SUT
289
        $this->workshop->aggregate_grading_grades_process($batch);
290
    }
291
 
11 efrain 292
    public function test_aggregate_grading_grades_process_multiple_grades_one_missing(): void {
1 efrain 293
        $this->resetAfterTest(true);
294
        // fixture set-up
295
        $batch = array();
296
        $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
297
        $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
298
        $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
299
        // expectation
300
        //$DB->expectOnce('update_record');
301
        // excersise SUT
302
        $this->workshop->aggregate_grading_grades_process($batch);
303
    }
304
 
11 efrain 305
    public function test_aggregate_grading_grades_process_multiple_grades_missing_overridden(): void {
1 efrain 306
        $this->resetAfterTest(true);
307
        // fixture set-up
308
        $batch = array();
309
        $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
310
        $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>69.00000, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
311
        $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000);
312
        // expectation
313
        //$DB->expectOnce('update_record');
314
        // excersise SUT
315
        $this->workshop->aggregate_grading_grades_process($batch);
316
    }
317
 
11 efrain 318
    public function test_percent_to_value(): void {
1 efrain 319
        $this->resetAfterTest(true);
320
        // fixture setup
321
        $total = 185;
322
        $percent = 56.6543;
323
        // exercise SUT
324
        $part = workshop::percent_to_value($percent, $total);
325
        // verify
326
        $this->assertEquals($part, $total * $percent / 100);
327
    }
328
 
11 efrain 329
    public function test_percent_to_value_negative(): void {
1 efrain 330
        $this->resetAfterTest(true);
331
        // fixture setup
332
        $total = 185;
333
        $percent = -7.098;
334
 
335
        // exercise SUT
336
        $this->expectException(\coding_exception::class);
337
        $part = workshop::percent_to_value($percent, $total);
338
    }
339
 
11 efrain 340
    public function test_percent_to_value_over_hundred(): void {
1 efrain 341
        $this->resetAfterTest(true);
342
        // fixture setup
343
        $total = 185;
344
        $percent = 121.08;
345
 
346
        // exercise SUT
347
        $this->expectException(\coding_exception::class);
348
        $part = workshop::percent_to_value($percent, $total);
349
    }
350
 
11 efrain 351
    public function test_lcm(): void {
1 efrain 352
        $this->resetAfterTest(true);
353
        // fixture setup + exercise SUT + verify in one step
354
        $this->assertEquals(workshop::lcm(1,4), 4);
355
        $this->assertEquals(workshop::lcm(2,4), 4);
356
        $this->assertEquals(workshop::lcm(4,2), 4);
357
        $this->assertEquals(workshop::lcm(2,3), 6);
358
        $this->assertEquals(workshop::lcm(6,4), 12);
359
    }
360
 
11 efrain 361
    public function test_lcm_array(): void {
1 efrain 362
        $this->resetAfterTest(true);
363
        // fixture setup
364
        $numbers = array(5,3,15);
365
        // excersise SUT
366
        $lcm = array_reduce($numbers, 'workshop::lcm', 1);
367
        // verify
368
        $this->assertEquals($lcm, 15);
369
    }
370
 
11 efrain 371
    public function test_prepare_example_assessment(): void {
1 efrain 372
        $this->resetAfterTest(true);
373
        // fixture setup
374
        $fakerawrecord = (object)array(
375
            'id'                => 42,
376
            'submissionid'      => 56,
377
            'weight'            => 0,
378
            'timecreated'       => time() - 10,
379
            'timemodified'      => time() - 5,
380
            'grade'             => null,
381
            'gradinggrade'      => null,
382
            'gradinggradeover'  => null,
383
            'feedbackauthor'    => null,
384
            'feedbackauthorformat' => 0,
385
            'feedbackauthorattachment' => 0,
386
        );
387
        // excersise SUT
388
        $a = $this->workshop->prepare_example_assessment($fakerawrecord);
389
        // verify
390
        $this->assertTrue($a instanceof workshop_example_assessment);
391
        $this->assertTrue($a->url instanceof \moodle_url);
392
 
393
        // modify setup
394
        $fakerawrecord->weight = 1;
395
        $this->expectException('coding_exception');
396
        // excersise SUT
397
        $a = $this->workshop->prepare_example_assessment($fakerawrecord);
398
    }
399
 
11 efrain 400
    public function test_prepare_example_reference_assessment(): void {
1 efrain 401
        global $USER;
402
        $this->resetAfterTest(true);
403
        // fixture setup
404
        $fakerawrecord = (object)array(
405
            'id'                => 38,
406
            'submissionid'      => 56,
407
            'weight'            => 1,
408
            'timecreated'       => time() - 100,
409
            'timemodified'      => time() - 50,
410
            'grade'             => 0.75000,
411
            'gradinggrade'      => 1.00000,
412
            'gradinggradeover'  => null,
413
            'feedbackauthor'    => null,
414
            'feedbackauthorformat' => 0,
415
            'feedbackauthorattachment' => 0,
416
        );
417
        // excersise SUT
418
        $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
419
        // verify
420
        $this->assertTrue($a instanceof workshop_example_reference_assessment);
421
 
422
        // modify setup
423
        $fakerawrecord->weight = 0;
424
        $this->expectException('coding_exception');
425
        // excersise SUT
426
        $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord);
427
    }
428
 
429
    /**
430
     * Tests user restrictions, as they affect lists of users returned by
431
     * core API functions.
432
     *
433
     * This includes the groupingid option (when group mode is in use), and
434
     * standard activity restrictions using the availability API.
435
     */
11 efrain 436
    public function test_user_restrictions(): void {
1 efrain 437
        global $DB, $CFG;
438
 
439
        $this->resetAfterTest();
440
 
441
        // Use existing sample course from setUp.
442
        $courseid = $this->workshop->course->id;
443
 
444
        // Make a test grouping and two groups.
445
        $generator = $this->getDataGenerator();
446
        $grouping = $generator->create_grouping(array('courseid' => $courseid));
447
        $group1 = $generator->create_group(array('courseid' => $courseid));
448
        groups_assign_grouping($grouping->id, $group1->id);
449
        $group2 = $generator->create_group(array('courseid' => $courseid));
450
        groups_assign_grouping($grouping->id, $group2->id);
451
 
452
        // Group 3 is not in the grouping.
453
        $group3 = $generator->create_group(array('courseid' => $courseid));
454
 
455
        // Enrol some students.
456
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
457
        $student1 = $generator->create_user();
458
        $student2 = $generator->create_user();
459
        $student3 = $generator->create_user();
460
        $generator->enrol_user($student1->id, $courseid, $roleids['student']);
461
        $generator->enrol_user($student2->id, $courseid, $roleids['student']);
462
        $generator->enrol_user($student3->id, $courseid, $roleids['student']);
463
 
464
        // Place students in groups (except student 3).
465
        groups_add_member($group1, $student1);
466
        groups_add_member($group2, $student2);
467
        groups_add_member($group3, $student3);
468
 
469
        // The existing workshop doesn't have any restrictions, so user lists
470
        // should include all three users.
471
        $allusers = get_enrolled_users(\context_course::instance($courseid));
472
        $result = $this->workshop->get_grouped($allusers);
473
        $this->assertCount(4, $result);
474
        $users = array_keys($result[0]);
475
        sort($users);
476
        $this->assertEquals(array($student1->id, $student2->id, $student3->id), $users);
477
        $this->assertEquals(array($student1->id), array_keys($result[$group1->id]));
478
        $this->assertEquals(array($student2->id), array_keys($result[$group2->id]));
479
        $this->assertEquals(array($student3->id), array_keys($result[$group3->id]));
480
 
481
        $users = $this->workshop->get_potential_authors(false);
482
        $this->assertCount(3, $users);
483
        $users = $this->workshop->get_potential_authors(false, $group2->id);
484
        $this->assertEquals(array($student2->id), array_keys($users));
485
 
486
        // Create another test workshop with grouping set.
487
        $workshopitem = $this->getDataGenerator()->create_module('workshop',
488
                array('course' => $courseid, 'groupmode' => SEPARATEGROUPS,
489
                'groupingid' => $grouping->id));
490
        $cm = get_coursemodule_from_instance('workshop', $workshopitem->id,
491
                $courseid, false, MUST_EXIST);
492
        $workshopgrouping = new testable_workshop($workshopitem, $cm, $this->workshop->course);
493
 
494
        // This time the result should only include users and groups in the
495
        // selected grouping.
496
        $result = $workshopgrouping->get_grouped($allusers);
497
        $this->assertCount(3, $result);
498
        $users = array_keys($result[0]);
499
        sort($users);
500
        $this->assertEquals(array($student1->id, $student2->id), $users);
501
        $this->assertEquals(array($student1->id), array_keys($result[$group1->id]));
502
        $this->assertEquals(array($student2->id), array_keys($result[$group2->id]));
503
 
504
        $users = $workshopgrouping->get_potential_authors(false);
505
        $userids = array_keys($users);
506
        sort($userids);
507
        $this->assertEquals(array($student1->id, $student2->id), $userids);
508
        $users = $workshopgrouping->get_potential_authors(false, $group2->id);
509
        $this->assertEquals(array($student2->id), array_keys($users));
510
 
511
        // Enable the availability system and create another test workshop with
512
        // availability restriction on grouping.
513
        $CFG->enableavailability = true;
514
        $workshopitem = $this->getDataGenerator()->create_module('workshop',
515
                array('course' => $courseid, 'availability' => json_encode(
516
                    \core_availability\tree::get_root_json(array(
517
                    \availability_grouping\condition::get_json($grouping->id)),
518
                    \core_availability\tree::OP_AND, false))));
519
        $cm = get_coursemodule_from_instance('workshop', $workshopitem->id,
520
                $courseid, false, MUST_EXIST);
521
        $workshoprestricted = new testable_workshop($workshopitem, $cm, $this->workshop->course);
522
 
523
        // The get_grouped function isn't intended to apply this restriction,
524
        // so it should be the same as the base workshop. (Note: in reality,
525
        // get_grouped is always run with the parameter being the result of
526
        // one of the get_potential_xxx functions, so it works.)
527
        $result = $workshoprestricted->get_grouped($allusers);
528
        $this->assertCount(4, $result);
529
        $this->assertCount(3, $result[0]);
530
 
531
        $users = $workshoprestricted->get_potential_authors(false);
532
        $userids = array_keys($users);
533
        sort($userids);
534
        $this->assertEquals(array($student1->id, $student2->id), $userids);
535
        $users = $workshoprestricted->get_potential_authors(false, $group2->id);
536
        $this->assertEquals(array($student2->id), array_keys($users));
537
    }
538
 
539
    /**
540
     * Test the workshop reset feature.
541
     */
11 efrain 542
    public function test_reset_phase(): void {
1 efrain 543
        $this->resetAfterTest(true);
544
 
545
        $this->workshop->switch_phase(workshop::PHASE_CLOSED);
546
        $this->assertEquals(workshop::PHASE_CLOSED, $this->workshop->phase);
547
 
548
        $settings = (object)array(
549
            'reset_workshop_phase' => 0,
550
        );
551
        $status = $this->workshop->reset_userdata($settings);
552
        $this->assertEquals(workshop::PHASE_CLOSED, $this->workshop->phase);
553
 
554
        $settings = (object)array(
555
            'reset_workshop_phase' => 1,
556
        );
557
        $status = $this->workshop->reset_userdata($settings);
558
        $this->assertEquals(workshop::PHASE_SETUP, $this->workshop->phase);
559
        foreach ($status as $result) {
560
            $this->assertFalse($result['error']);
561
        }
562
    }
563
 
564
    /**
565
     * Test deleting assessments related data on workshop reset.
566
     */
11 efrain 567
    public function test_reset_userdata_assessments(): void {
1 efrain 568
        global $DB;
569
        $this->resetAfterTest(true);
570
 
571
        $student1 = $this->getDataGenerator()->create_user();
572
        $student2 = $this->getDataGenerator()->create_user();
573
 
574
        $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
575
        $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
576
 
577
        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
578
 
579
        $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
580
        $subid2 = $workshopgenerator->create_submission($this->workshop->id, $student2->id);
581
 
582
        $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
583
        $asid2 = $workshopgenerator->create_assessment($subid2, $student1->id);
584
 
585
        $settings = (object)array(
586
            'reset_workshop_assessments' => 1,
587
        );
588
        $status = $this->workshop->reset_userdata($settings);
589
 
590
        foreach ($status as $result) {
591
            $this->assertFalse($result['error']);
592
        }
593
 
594
        $this->assertEquals(2, $DB->count_records('workshop_submissions', array('workshopid' => $this->workshop->id)));
595
        $this->assertEquals(0, $DB->count_records('workshop_assessments'));
596
    }
597
 
598
    /**
599
     * Test deleting submissions related data on workshop reset.
600
     */
11 efrain 601
    public function test_reset_userdata_submissions(): void {
1 efrain 602
        global $DB;
603
        $this->resetAfterTest(true);
604
 
605
        $student1 = $this->getDataGenerator()->create_user();
606
        $student2 = $this->getDataGenerator()->create_user();
607
 
608
        $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
609
        $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
610
 
611
        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
612
 
613
        $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
614
        $subid2 = $workshopgenerator->create_submission($this->workshop->id, $student2->id);
615
 
616
        $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
617
        $asid2 = $workshopgenerator->create_assessment($subid2, $student1->id);
618
 
619
        $settings = (object)array(
620
            'reset_workshop_submissions' => 1,
621
        );
622
        $status = $this->workshop->reset_userdata($settings);
623
 
624
        foreach ($status as $result) {
625
            $this->assertFalse($result['error']);
626
        }
627
 
628
        $this->assertEquals(0, $DB->count_records('workshop_submissions', array('workshopid' => $this->workshop->id)));
629
        $this->assertEquals(0, $DB->count_records('workshop_assessments'));
630
    }
631
 
632
    /**
633
     * Test normalizing list of extensions.
634
     */
11 efrain 635
    public function test_normalize_file_extensions(): void {
1 efrain 636
        $this->resetAfterTest(true);
637
 
638
        workshop::normalize_file_extensions('');
639
        $this->assertDebuggingCalled();
640
    }
641
 
642
    /**
643
     * Test cleaning list of extensions.
644
     */
11 efrain 645
    public function test_clean_file_extensions(): void {
1 efrain 646
        $this->resetAfterTest(true);
647
 
648
        workshop::clean_file_extensions('');
649
        $this->assertDebuggingCalledCount(2);
650
    }
651
 
652
    /**
653
     * Test validation of the list of file extensions.
654
     */
11 efrain 655
    public function test_invalid_file_extensions(): void {
1 efrain 656
        $this->resetAfterTest(true);
657
 
658
        workshop::invalid_file_extensions('', '');
659
        $this->assertDebuggingCalledCount(3);
660
    }
661
 
662
    /**
663
     * Test checking file name against the list of allowed extensions.
664
     */
11 efrain 665
    public function test_is_allowed_file_type(): void {
1 efrain 666
        $this->resetAfterTest(true);
667
 
668
        workshop::is_allowed_file_type('', '');
669
        $this->assertDebuggingCalledCount(2);
670
    }
671
 
672
    /**
673
     * Test workshop::check_group_membership() functionality.
674
     */
11 efrain 675
    public function test_check_group_membership(): void {
1 efrain 676
        global $DB, $CFG;
677
 
678
        $this->resetAfterTest();
679
 
680
        $courseid = $this->course->id;
681
        $generator = $this->getDataGenerator();
682
 
683
        // Make test groups.
684
        $group1 = $generator->create_group(array('courseid' => $courseid));
685
        $group2 = $generator->create_group(array('courseid' => $courseid));
686
        $group3 = $generator->create_group(array('courseid' => $courseid));
687
 
688
        // Revoke the accessallgroups from non-editing teachers (tutors).
689
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
690
        unassign_capability('moodle/site:accessallgroups', $roleids['teacher']);
691
 
692
        // Create test use accounts.
693
        $teacher1 = $generator->create_user();
694
        $tutor1 = $generator->create_user();
695
        $tutor2 = $generator->create_user();
696
        $student1 = $generator->create_user();
697
        $student2 = $generator->create_user();
698
        $student3 = $generator->create_user();
699
 
700
        // Enrol the teacher (has the access all groups permission).
701
        $generator->enrol_user($teacher1->id, $courseid, $roleids['editingteacher']);
702
 
703
        // Enrol tutors (can not access all groups).
704
        $generator->enrol_user($tutor1->id, $courseid, $roleids['teacher']);
705
        $generator->enrol_user($tutor2->id, $courseid, $roleids['teacher']);
706
 
707
        // Enrol students.
708
        $generator->enrol_user($student1->id, $courseid, $roleids['student']);
709
        $generator->enrol_user($student2->id, $courseid, $roleids['student']);
710
        $generator->enrol_user($student3->id, $courseid, $roleids['student']);
711
 
712
        // Add users in groups.
713
        groups_add_member($group1, $tutor1);
714
        groups_add_member($group2, $tutor2);
715
        groups_add_member($group1, $student1);
716
        groups_add_member($group2, $student2);
717
        groups_add_member($group3, $student3);
718
 
719
        // Workshop with no groups.
720
        $workshopitem1 = $this->getDataGenerator()->create_module('workshop', [
721
            'course' => $courseid,
722
            'groupmode' => NOGROUPS,
723
        ]);
724
        $cm = get_coursemodule_from_instance('workshop', $workshopitem1->id, $courseid, false, MUST_EXIST);
725
        $workshop1 = new testable_workshop($workshopitem1, $cm, $this->course);
726
 
727
        $this->setUser($teacher1);
728
        $this->assertTrue($workshop1->check_group_membership($student1->id));
729
        $this->assertTrue($workshop1->check_group_membership($student2->id));
730
        $this->assertTrue($workshop1->check_group_membership($student3->id));
731
 
732
        $this->setUser($tutor1);
733
        $this->assertTrue($workshop1->check_group_membership($student1->id));
734
        $this->assertTrue($workshop1->check_group_membership($student2->id));
735
        $this->assertTrue($workshop1->check_group_membership($student3->id));
736
 
737
        // Workshop in visible groups mode.
738
        $workshopitem2 = $this->getDataGenerator()->create_module('workshop', [
739
            'course' => $courseid,
740
            'groupmode' => VISIBLEGROUPS,
741
        ]);
742
        $cm = get_coursemodule_from_instance('workshop', $workshopitem2->id, $courseid, false, MUST_EXIST);
743
        $workshop2 = new testable_workshop($workshopitem2, $cm, $this->course);
744
 
745
        $this->setUser($teacher1);
746
        $this->assertTrue($workshop2->check_group_membership($student1->id));
747
        $this->assertTrue($workshop2->check_group_membership($student2->id));
748
        $this->assertTrue($workshop2->check_group_membership($student3->id));
749
 
750
        $this->setUser($tutor1);
751
        $this->assertTrue($workshop2->check_group_membership($student1->id));
752
        $this->assertTrue($workshop2->check_group_membership($student2->id));
753
        $this->assertTrue($workshop2->check_group_membership($student3->id));
754
 
755
        // Workshop in separate groups mode.
756
        $workshopitem3 = $this->getDataGenerator()->create_module('workshop', [
757
            'course' => $courseid,
758
            'groupmode' => SEPARATEGROUPS,
759
        ]);
760
        $cm = get_coursemodule_from_instance('workshop', $workshopitem3->id, $courseid, false, MUST_EXIST);
761
        $workshop3 = new testable_workshop($workshopitem3, $cm, $this->course);
762
 
763
        $this->setUser($teacher1);
764
        $this->assertTrue($workshop3->check_group_membership($student1->id));
765
        $this->assertTrue($workshop3->check_group_membership($student2->id));
766
        $this->assertTrue($workshop3->check_group_membership($student3->id));
767
 
768
        $this->setUser($tutor1);
769
        $this->assertTrue($workshop3->check_group_membership($student1->id));
770
        $this->assertFalse($workshop3->check_group_membership($student2->id));
771
        $this->assertFalse($workshop3->check_group_membership($student3->id));
772
 
773
        $this->setUser($tutor2);
774
        $this->assertFalse($workshop3->check_group_membership($student1->id));
775
        $this->assertTrue($workshop3->check_group_membership($student2->id));
776
        $this->assertFalse($workshop3->check_group_membership($student3->id));
777
    }
778
 
779
    /**
780
     * Test init_initial_bar function.
781
     *
782
     * @covers \workshop::init_initial_bar
783
     */
784
    public function test_init_initial_bar(): void {
785
        global $SESSION;
786
        $this->resetAfterTest();
787
 
788
        $_GET['ifirst'] = 'A';
789
        $_GET['ilast'] = 'B';
790
        $contextid = $this->workshop->context->id;
791
 
792
        $this->workshop->init_initial_bar();
793
        $initialbarprefs = $this->get_initial_bar_prefs_property();
794
 
795
        $this->assertEquals('A', $initialbarprefs['i_first']);
796
        $this->assertEquals('B', $initialbarprefs['i_last']);
797
        $this->assertEquals('A', $SESSION->mod_workshop->initialbarprefs['id-' . $contextid]['i_first']);
798
        $this->assertEquals('B', $SESSION->mod_workshop->initialbarprefs['id-' . $contextid]['i_last']);
799
 
800
        $_GET['ifirst'] = null;
801
        $_GET['ilast'] = null;
802
        $SESSION->mod_workshop->initialbarprefs['id-' . $contextid]['i_first'] = 'D';
803
        $SESSION->mod_workshop->initialbarprefs['id-' . $contextid]['i_last'] = 'E';
804
 
805
        $this->workshop->init_initial_bar();
806
        $initialbarprefs = $this->get_initial_bar_prefs_property();
807
 
808
        $this->assertEquals('D', $initialbarprefs['i_first']);
809
        $this->assertEquals('E', $initialbarprefs['i_last']);
810
    }
811
 
812
    /**
813
     * Test empty init_initial_bar
814
     *
815
     * @covers \workshop::init_initial_bar
816
     */
817
    public function test_init_initial_bar_empty(): void {
818
        $this->resetAfterTest();
819
 
820
        $this->workshop->init_initial_bar();
821
        $initialbarprefs = $this->get_initial_bar_prefs_property();
822
 
823
        $this->assertEmpty($initialbarprefs);
824
    }
825
 
826
    /**
827
     * Test get_initial_first function
828
     *
829
     * @covers \workshop::get_initial_first
830
     */
831
    public function test_get_initial_first(): void {
832
        $this->resetAfterTest();
833
        $this->workshop->init_initial_bar();
834
        $this->assertEquals(null, $this->workshop->get_initial_first());
835
 
836
        $_GET['ifirst'] = 'D';
837
        $this->workshop->init_initial_bar();
838
        $this->assertEquals('D', $this->workshop->get_initial_first());
839
    }
840
 
841
    /**
842
     * Test get_initial_last function
843
     *
844
     * @covers \workshop::get_initial_last
845
     */
846
    public function test_get_initial_last(): void {
847
        $this->resetAfterTest();
848
        $this->workshop->init_initial_bar();
849
        $this->assertEquals(null, $this->workshop->get_initial_last());
850
 
851
        $_GET['ilast'] = 'D';
852
        $this->workshop->init_initial_bar();
853
        $this->assertEquals('D', $this->workshop->get_initial_last());
854
    }
855
 
856
    /**
857
     * Get the protected propertyinitialbarprefs from workshop class.
858
     *
859
     * @coversNothing
860
     * @return array initialbarspref property. eg ['i_first' => 'A', 'i_last' => 'B']
861
     */
862
    private function get_initial_bar_prefs_property(): array {
863
 
864
        $reflector = new \ReflectionObject($this->workshop);
865
        $initialbarprefsprop = $reflector->getProperty('initialbarprefs');
866
        $initialbarprefs = $initialbarprefsprop->getValue($this->workshop);
867
 
868
        return $initialbarprefs;
869
    }
1441 ariadna 870
 
871
    /**
872
     * Test count_submissions and count_assessments methods.
873
     *
874
     * @covers \workshop::count_submissions
875
     * @covers \workshop::count_assessments
876
     */
877
    public function test_count_submissions_count_assessments(): void {
878
        $this->resetAfterTest();
879
        $this->setAdminUser();
880
 
881
        $course = $this->getDataGenerator()->create_course();
882
        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
883
        $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
884
        $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
885
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
886
        $student3 = $this->getDataGenerator()->create_and_enrol($course, 'student');
887
        $student4 = $this->getDataGenerator()->create_and_enrol($course, 'student');
888
 
889
        groups_add_member($group1, $student1);
890
        groups_add_member($group1, $student3);
891
        groups_add_member($group2, $student2);
892
 
893
        $activity = $this->getDataGenerator()->create_module(
894
            'workshop',
895
            ['course' => $course->id , 'groupmode' => SEPARATEGROUPS],
896
        );
897
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
898
 
899
        // Set up a generator to create content.
900
        /** @var \mod_workshop_generator $generator */
901
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
902
 
903
        // Create some submissions.
904
        $submission1id = $generator->create_submission(
905
            $activity->id,
906
            $student1->id,
907
            ['title' => 'My custom title', 'grade' => 85.00000],
908
        );
909
        $submission2id = $generator->create_submission(
910
            $activity->id,
911
            $student2->id,
912
            ['title' => 'My custom title', 'grade' => null],
913
        );
914
        $submission3id = $generator->create_submission(
915
            $activity->id,
916
            $student3->id,
917
            ['title' => 'My custom title', 'grade' => null],
918
        );
919
        $submission4id = $generator->create_submission(
920
            $activity->id,
921
            $student4->id,
922
            ['title' => 'My custom title', 'grade' => null],
923
        );
924
        // Assess one submission.
925
        $generator->create_assessment(
926
            $submission1id,
927
            $student1->id,
928
            ['weight' => 3, 'grade' => 95.00000],
929
        );
930
        $generator->create_assessment(
931
            $submission2id,
932
            $student2->id,
933
            ['weight' => 3, 'grade' => null],
934
        );
935
        $generator->create_assessment(
936
            $submission3id,
937
            $student3->id,
938
            ['weight' => 3, 'grade' => null],
939
        );
940
        $generator->create_assessment(
941
            $submission4id,
942
            $student4->id,
943
            ['weight' => 3, 'grade' => 35.00000],
944
        );
945
 
946
        $manager = new workshop($activity, $cm, $course, $cm->context);
947
 
948
        $this->assertEquals(4, $manager->count_submissions());
949
        $this->assertEquals(4, $manager->count_submissions('all'));
950
        $this->assertEquals(1, $manager->count_submissions($student1->id));
951
        $this->assertEquals(1, $manager->count_submissions($student2->id));
952
 
953
        $this->assertEquals(2, $manager->count_submissions('all', $group1->id));
954
        $this->assertEquals(1, $manager->count_submissions('all', $group2->id));
955
        $this->assertEquals(1, $manager->count_submissions($student1->id, $group1->id));
956
        $this->assertEquals(0, $manager->count_submissions($student2->id, $group1->id));
957
 
958
        $this->assertEquals(4, $manager->count_assessments());
959
        $this->assertEquals(2, $manager->count_assessments(true));
960
 
961
        $this->assertEquals(2, $manager->count_assessments(false, $group1->id));
962
        $this->assertEquals(1, $manager->count_assessments(true, $group1->id));
963
 
964
        $this->assertEquals(1, $manager->count_assessments(false, $group2->id));
965
        $this->assertEquals(0, $manager->count_assessments(true, $group2->id));
966
    }
1 efrain 967
}