Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 the lib/upgradelib.php library.
19
 *
20
 * @package   core
21
 * @category  phpunit
22
 * @copyright 2013 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
require_once($CFG->libdir.'/upgradelib.php');
30
require_once($CFG->libdir.'/db/upgradelib.php');
31
require_once($CFG->dirroot . '/calendar/tests/helpers.php');
32
 
33
/**
34
 * Tests various classes and functions in upgradelib.php library.
35
 */
36
class upgradelib_test extends advanced_testcase {
37
 
38
    /**
39
     * Test the {@link upgrade_stale_php_files_present() function
40
     */
11 efrain 41
    public function test_upgrade_stale_php_files_present(): void {
1 efrain 42
        // Just call the function, must return bool false always
43
        // if there aren't any old files in the codebase.
44
        $this->assertFalse(upgrade_stale_php_files_present());
45
    }
46
 
47
    /**
48
     * Populate some fake grade items into the database with specified
49
     * sortorder and course id.
50
     *
51
     * NOTE: This function doesn't make much attempt to respect the
52
     * gradebook internals, its simply used to fake some data for
53
     * testing the upgradelib function. Please don't use it for other
54
     * purposes.
55
     *
56
     * @param int $courseid id of course
57
     * @param int $sortorder numeric sorting order of item
58
     * @return stdClass grade item object from the database.
59
     */
60
    private function insert_fake_grade_item_sortorder($courseid, $sortorder) {
61
        global $DB, $CFG;
62
        require_once($CFG->libdir.'/gradelib.php');
63
 
64
        $item = new stdClass();
65
        $item->courseid = $courseid;
66
        $item->sortorder = $sortorder;
67
        $item->gradetype = GRADE_TYPE_VALUE;
68
        $item->grademin = 30;
69
        $item->grademax = 110;
70
        $item->itemnumber = 1;
71
        $item->iteminfo = '';
72
        $item->timecreated = time();
73
        $item->timemodified = time();
74
 
75
        $item->id = $DB->insert_record('grade_items', $item);
76
 
77
        return $DB->get_record('grade_items', array('id' => $item->id));
78
    }
79
 
11 efrain 80
    public function test_upgrade_extra_credit_weightoverride(): void {
1 efrain 81
        global $DB, $CFG;
82
 
83
        $this->resetAfterTest(true);
84
 
85
        require_once($CFG->libdir . '/db/upgradelib.php');
86
 
87
        $c = array();
88
        $a = array();
89
        $gi = array();
90
        for ($i=0; $i<5; $i++) {
91
            $c[$i] = $this->getDataGenerator()->create_course();
92
            $a[$i] = array();
93
            $gi[$i] = array();
94
            for ($j=0;$j<3;$j++) {
95
                $a[$i][$j] = $this->getDataGenerator()->create_module('assign', array('course' => $c[$i], 'grade' => 100));
96
                $giparams = array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $a[$i][$j]->id,
97
                    'courseid' => $c[$i]->id, 'itemnumber' => 0);
98
                $gi[$i][$j] = grade_item::fetch($giparams);
99
            }
100
        }
101
 
102
        // Case 1: Course $c[0] has aggregation method different from natural.
103
        $coursecategory = grade_category::fetch_course_category($c[0]->id);
104
        $coursecategory->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN;
105
        $coursecategory->update();
106
        $gi[0][1]->aggregationcoef = 1;
107
        $gi[0][1]->update();
108
        $gi[0][2]->weightoverride = 1;
109
        $gi[0][2]->update();
110
 
111
        // Case 2: Course $c[1] has neither extra credits nor overrides
112
 
113
        // Case 3: Course $c[2] has extra credits but no overrides
114
        $gi[2][1]->aggregationcoef = 1;
115
        $gi[2][1]->update();
116
 
117
        // Case 4: Course $c[3] has no extra credits and has overrides
118
        $gi[3][2]->weightoverride = 1;
119
        $gi[3][2]->update();
120
 
121
        // Case 5: Course $c[4] has both extra credits and overrides
122
        $gi[4][1]->aggregationcoef = 1;
123
        $gi[4][1]->update();
124
        $gi[4][2]->weightoverride = 1;
125
        $gi[4][2]->update();
126
 
127
        // Run the upgrade script and make sure only course $c[4] was marked as needed to be fixed.
128
        upgrade_extra_credit_weightoverride();
129
 
130
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[0]->id}));
131
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[1]->id}));
132
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[2]->id}));
133
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[3]->id}));
134
        $this->assertEquals(20150619, $CFG->{'gradebook_calculations_freeze_' . $c[4]->id});
135
 
136
        set_config('gradebook_calculations_freeze_' . $c[4]->id, null);
137
 
138
        // Run the upgrade script for a single course only.
139
        upgrade_extra_credit_weightoverride($c[0]->id);
140
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $c[0]->id}));
141
        upgrade_extra_credit_weightoverride($c[4]->id);
142
        $this->assertEquals(20150619, $CFG->{'gradebook_calculations_freeze_' . $c[4]->id});
143
    }
144
 
145
    /**
146
     * Test the upgrade function for flagging courses with calculated grade item problems.
147
     */
11 efrain 148
    public function test_upgrade_calculated_grade_items_freeze(): void {
1 efrain 149
        global $DB, $CFG;
150
 
151
        $this->resetAfterTest();
152
 
153
        require_once($CFG->libdir . '/db/upgradelib.php');
154
 
155
        // Create a user.
156
        $user = $this->getDataGenerator()->create_user();
157
 
158
        // Create a couple of courses.
159
        $course1 = $this->getDataGenerator()->create_course();
160
        $course2 = $this->getDataGenerator()->create_course();
161
        $course3 = $this->getDataGenerator()->create_course();
162
 
163
        // Enrol the user in the courses.
164
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
165
        $maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
166
        $maninstance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
167
        $maninstance3 = $DB->get_record('enrol', array('courseid' => $course3->id, 'enrol' => 'manual'), '*', MUST_EXIST);
168
        $manual = enrol_get_plugin('manual');
169
        $manual->enrol_user($maninstance1, $user->id, $studentrole->id);
170
        $manual->enrol_user($maninstance2, $user->id, $studentrole->id);
171
        $manual->enrol_user($maninstance3, $user->id, $studentrole->id);
172
 
173
        // To create the data we need we freeze the grade book to use the old behaviour.
174
        set_config('gradebook_calculations_freeze_' . $course1->id, 20150627);
175
        set_config('gradebook_calculations_freeze_' . $course2->id, 20150627);
176
        set_config('gradebook_calculations_freeze_' . $course3->id, 20150627);
177
        $CFG->grade_minmaxtouse = 2;
178
 
179
        // Creating a category for a grade item.
180
        $gradecategory = new grade_category();
181
        $gradecategory->fullname = 'calculated grade category';
182
        $gradecategory->courseid = $course1->id;
183
        $gradecategory->insert();
184
        $gradecategoryid = $gradecategory->id;
185
 
186
        // This is a manual grade item.
187
        $gradeitem = new grade_item();
188
        $gradeitem->itemname = 'grade item one';
189
        $gradeitem->itemtype = 'manual';
190
        $gradeitem->categoryid = $gradecategoryid;
191
        $gradeitem->courseid = $course1->id;
192
        $gradeitem->idnumber = 'gi1';
193
        $gradeitem->insert();
194
 
195
        // Changing the category into a calculated grade category.
196
        $gradecategoryitem = grade_item::fetch(array('iteminstance' => $gradecategory->id));
197
        $gradecategoryitem->calculation = '=##gi' . $gradeitem->id . '##/2';
198
        $gradecategoryitem->update();
199
 
200
        // Setting a grade for the student.
201
        $grade = $gradeitem->get_grade($user->id, true);
202
        $grade->finalgrade = 50;
203
        $grade->update();
204
        // Creating all the grade_grade items.
205
        grade_regrade_final_grades($course1->id);
206
        // Updating the grade category to a new grade max and min.
207
        $gradecategoryitem->grademax = 50;
208
        $gradecategoryitem->grademin = 5;
209
        $gradecategoryitem->update();
210
 
211
        // Different manual grade item for course 2. We are creating a course with a calculated grade item that has a grade max of
212
        // 50. The grade_grade will have a rawgrademax of 100 regardless.
213
        $gradeitem = new grade_item();
214
        $gradeitem->itemname = 'grade item one';
215
        $gradeitem->itemtype = 'manual';
216
        $gradeitem->courseid = $course2->id;
217
        $gradeitem->idnumber = 'gi1';
218
        $gradeitem->grademax = 25;
219
        $gradeitem->insert();
220
 
221
        // Calculated grade item for course 2.
222
        $calculatedgradeitem = new grade_item();
223
        $calculatedgradeitem->itemname = 'calculated grade';
224
        $calculatedgradeitem->itemtype = 'manual';
225
        $calculatedgradeitem->courseid = $course2->id;
226
        $calculatedgradeitem->calculation = '=##gi' . $gradeitem->id . '##*2';
227
        $calculatedgradeitem->grademax = 50;
228
        $calculatedgradeitem->insert();
229
 
230
        // Assigning a grade for the user.
231
        $grade = $gradeitem->get_grade($user->id, true);
232
        $grade->finalgrade = 10;
233
        $grade->update();
234
 
235
        // Setting all of the grade_grade items.
236
        grade_regrade_final_grades($course2->id);
237
 
238
        // Different manual grade item for course 3. We are creating a course with a calculated grade item that has a grade max of
239
        // 50. The grade_grade will have a rawgrademax of 100 regardless.
240
        $gradeitem = new grade_item();
241
        $gradeitem->itemname = 'grade item one';
242
        $gradeitem->itemtype = 'manual';
243
        $gradeitem->courseid = $course3->id;
244
        $gradeitem->idnumber = 'gi1';
245
        $gradeitem->grademax = 25;
246
        $gradeitem->insert();
247
 
248
        // Calculated grade item for course 2.
249
        $calculatedgradeitem = new grade_item();
250
        $calculatedgradeitem->itemname = 'calculated grade';
251
        $calculatedgradeitem->itemtype = 'manual';
252
        $calculatedgradeitem->courseid = $course3->id;
253
        $calculatedgradeitem->calculation = '=##gi' . $gradeitem->id . '##*2';
254
        $calculatedgradeitem->grademax = 50;
255
        $calculatedgradeitem->insert();
256
 
257
        // Assigning a grade for the user.
258
        $grade = $gradeitem->get_grade($user->id, true);
259
        $grade->finalgrade = 10;
260
        $grade->update();
261
 
262
        // Setting all of the grade_grade items.
263
        grade_regrade_final_grades($course3->id);
264
        // Need to do this first before changing the other courses, otherwise they will be flagged too early.
265
        set_config('gradebook_calculations_freeze_' . $course3->id, null);
266
        upgrade_calculated_grade_items($course3->id);
267
        $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course3->id});
268
 
269
        // Change the setting back to null.
270
        set_config('gradebook_calculations_freeze_' . $course1->id, null);
271
        set_config('gradebook_calculations_freeze_' . $course2->id, null);
272
        // Run the upgrade.
273
        upgrade_calculated_grade_items();
274
        // The setting should be set again after the upgrade.
275
        $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course1->id});
276
        $this->assertEquals(20150627, $CFG->{'gradebook_calculations_freeze_' . $course2->id});
277
    }
278
 
279
    /**
280
     * Test the upgrade function for final grade after setting grade max for category and grade item.
281
     */
11 efrain 282
    public function test_upgrade_update_category_grademax_regrade_final_grades(): void {
1 efrain 283
        global $DB;
284
 
285
        $this->resetAfterTest();
286
 
287
        $generator = $this->getDataGenerator();
288
        $user = $generator->create_user();
289
 
290
        // Create a new course.
291
        $course = $generator->create_course();
292
 
293
        // Set the course aggregation to weighted mean of grades.
294
        $unitcategory = \grade_category::fetch_course_category($course->id);
295
        $unitcategory->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN;
296
        $unitcategory->update();
297
 
298
        // Set grade max for category.
299
        $gradecategoryitem = grade_item::fetch(array('iteminstance' => $unitcategory->id));
300
        $gradecategoryitem->grademax = 50;
301
        $gradecategoryitem->update();
302
 
303
        // Make new grade item.
304
        $gradeitem = new \grade_item($generator->create_grade_item([
305
            'itemname'        => 'Grade item',
306
            'idnumber'        => 'git1',
307
            'courseid'        => $course->id,
308
            'grademin'        => 0,
309
            'grademax'        => 50,
310
            'aggregationcoef' => 100.0,
311
        ]));
312
 
313
        // Set final grade.
314
        $grade = $gradeitem->get_grade($user->id, true);
315
        $grade->finalgrade = 20;
316
        $grade->update();
317
 
318
        $courseitem = \grade_item::fetch(['courseid' => $course->id, 'itemtype' => 'course']);
319
        $gradeitem->force_regrading();
320
 
321
        // Trigger regrade because the grade items needs to be updated.
322
        grade_regrade_final_grades($course->id);
323
 
324
        $coursegrade = new \grade_grade($courseitem->get_final($user->id), false);
325
        $this->assertEquals(20, $coursegrade->finalgrade);
326
    }
327
 
11 efrain 328
    function test_upgrade_calculated_grade_items_regrade(): void {
1 efrain 329
        global $DB, $CFG;
330
 
331
        $this->resetAfterTest();
332
 
333
        require_once($CFG->libdir . '/db/upgradelib.php');
334
 
335
        // Create a user.
336
        $user = $this->getDataGenerator()->create_user();
337
 
338
        // Create a course.
339
        $course = $this->getDataGenerator()->create_course();
340
 
341
        // Enrol the user in the course.
342
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
343
        $maninstance1 = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
344
        $manual = enrol_get_plugin('manual');
345
        $manual->enrol_user($maninstance1, $user->id, $studentrole->id);
346
 
347
        set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
348
 
349
        // Creating a category for a grade item.
350
        $gradecategory = new grade_category();
351
        $gradecategory->fullname = 'calculated grade category';
352
        $gradecategory->courseid = $course->id;
353
        $gradecategory->insert();
354
        $gradecategoryid = $gradecategory->id;
355
 
356
        // This is a manual grade item.
357
        $gradeitem = new grade_item();
358
        $gradeitem->itemname = 'grade item one';
359
        $gradeitem->itemtype = 'manual';
360
        $gradeitem->categoryid = $gradecategoryid;
361
        $gradeitem->courseid = $course->id;
362
        $gradeitem->idnumber = 'gi1';
363
        $gradeitem->insert();
364
 
365
        // Changing the category into a calculated grade category.
366
        $gradecategoryitem = grade_item::fetch(array('iteminstance' => $gradecategory->id));
367
        $gradecategoryitem->calculation = '=##gi' . $gradeitem->id . '##/2';
368
        $gradecategoryitem->grademax = 50;
369
        $gradecategoryitem->grademin = 15;
370
        $gradecategoryitem->update();
371
 
372
        // Setting a grade for the student.
373
        $grade = $gradeitem->get_grade($user->id, true);
374
        $grade->finalgrade = 50;
375
        $grade->update();
376
 
377
        grade_regrade_final_grades($course->id);
378
        $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id));
379
        $grade->rawgrademax = 100;
380
        $grade->rawgrademin = 0;
381
        $grade->update();
382
        $this->assertNotEquals($gradecategoryitem->grademax, $grade->rawgrademax);
383
        $this->assertNotEquals($gradecategoryitem->grademin, $grade->rawgrademin);
384
 
385
        // This is the function that we are testing. If we comment out this line, then the test fails because the grade items
386
        // are not flagged for regrading.
387
        upgrade_calculated_grade_items();
388
        grade_regrade_final_grades($course->id);
389
 
390
        $grade = grade_grade::fetch(array('itemid' => $gradecategoryitem->id, 'userid' => $user->id));
391
 
392
        $this->assertEquals($gradecategoryitem->grademax, $grade->rawgrademax);
393
        $this->assertEquals($gradecategoryitem->grademin, $grade->rawgrademin);
394
    }
395
 
396
    /**
397
     * Test that the upgrade script correctly flags courses to be frozen due to letter boundary problems.
398
     */
11 efrain 399
    public function test_upgrade_course_letter_boundary(): void {
1 efrain 400
        global $CFG, $DB;
401
        $this->resetAfterTest(true);
402
 
403
        require_once($CFG->libdir . '/db/upgradelib.php');
404
 
405
        // Create a user.
406
        $user = $this->getDataGenerator()->create_user();
407
 
408
        // Create some courses.
409
        $courses = array();
410
        $contexts = array();
411
        for ($i = 0; $i < 45; $i++) {
412
            $course = $this->getDataGenerator()->create_course();
413
            $context = context_course::instance($course->id);
414
            if (in_array($i, array(2, 5, 10, 13, 14, 19, 23, 25, 30, 34, 36))) {
415
                // Assign good letter boundaries.
416
                $this->assign_good_letter_boundary($context->id);
417
            }
418
            if (in_array($i, array(3, 6, 11, 15, 20, 24, 26, 31, 35))) {
419
                // Assign bad letter boundaries.
420
                $this->assign_bad_letter_boundary($context->id);
421
            }
422
 
423
            if (in_array($i, array(3, 9, 10, 11, 18, 19, 20, 29, 30, 31, 40))) {
424
                grade_set_setting($course->id, 'displaytype', '3');
425
            } else if (in_array($i, array(8, 17, 28))) {
426
                grade_set_setting($course->id, 'displaytype', '2');
427
            }
428
 
429
            if (in_array($i, array(37, 43))) {
430
                // Show.
431
                grade_set_setting($course->id, 'report_user_showlettergrade', '1');
432
            } else if (in_array($i, array(38, 42))) {
433
                // Hide.
434
                grade_set_setting($course->id, 'report_user_showlettergrade', '0');
435
            }
436
 
437
            $assignrow = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'name' => 'Test!'));
438
            $gi = grade_item::fetch(
439
                    array('itemtype' => 'mod',
440
                          'itemmodule' => 'assign',
441
                          'iteminstance' => $assignrow->id,
442
                          'courseid' => $course->id));
443
            if (in_array($i, array(6, 13, 14, 15, 23, 24, 34, 35, 36, 41))) {
444
                grade_item::set_properties($gi, array('display' => 3));
445
                $gi->update();
446
            } else if (in_array($i, array(12, 21, 32))) {
447
                grade_item::set_properties($gi, array('display' => 2));
448
                $gi->update();
449
            }
450
            $gradegrade = new grade_grade();
451
            $gradegrade->itemid = $gi->id;
452
            $gradegrade->userid = $user->id;
453
            $gradegrade->rawgrade = 55.5563;
454
            $gradegrade->finalgrade = 55.5563;
455
            $gradegrade->rawgrademax = 100;
456
            $gradegrade->rawgrademin = 0;
457
            $gradegrade->timecreated = time();
458
            $gradegrade->timemodified = time();
459
            $gradegrade->insert();
460
 
461
            $contexts[] = $context;
462
            $courses[] = $course;
463
        }
464
 
465
        upgrade_course_letter_boundary();
466
 
467
        // No system setting for grade letter boundaries.
468
        // [0] A course with no letter boundaries.
469
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[0]->id}));
470
        // [1] A course with letter boundaries which are default.
471
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[1]->id}));
472
        // [2] A course with letter boundaries which are custom but not affected.
473
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[2]->id}));
474
        // [3] A course with letter boundaries which are custom and will be affected.
475
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[3]->id});
476
        // [4] A course with no letter boundaries, but with a grade item with letter boundaries which are default.
477
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[4]->id}));
478
        // [5] A course with no letter boundaries, but with a grade item with letter boundaries which are not default, but not affected.
479
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[5]->id}));
480
        // [6] A course with no letter boundaries, but with a grade item with letter boundaries which are not default which will be affected.
481
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[6]->id});
482
 
483
        // System setting for grade letter boundaries (default).
484
        set_config('grade_displaytype', '3');
485
        for ($i = 0; $i < 45; $i++) {
486
            unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
487
        }
488
        upgrade_course_letter_boundary();
489
 
490
        // [7] A course with no grade display settings for the course or grade items.
491
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[7]->id}));
492
        // [8] A course with grade display settings, but for something that isn't letters.
493
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[8]->id}));
494
        // [9] A course with grade display settings of letters which are default.
495
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[9]->id}));
496
        // [10] A course with grade display settings of letters which are not default, but not affected.
497
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[10]->id}));
498
        // [11] A course with grade display settings of letters which are not default, which will be affected.
499
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[11]->id});
500
        // [12] A grade item with display settings that are not letters.
501
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[12]->id}));
502
        // [13] A grade item with display settings of letters which are default.
503
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[13]->id}));
504
        // [14] A grade item with display settings of letters which are not default, but not affected.
505
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[14]->id}));
506
        // [15] A grade item with display settings of letters which are not default, which will be affected.
507
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[15]->id});
508
 
509
        // System setting for grade letter boundaries (custom with problem).
510
        $systemcontext = context_system::instance();
511
        $this->assign_bad_letter_boundary($systemcontext->id);
512
        for ($i = 0; $i < 45; $i++) {
513
            unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
514
        }
515
        upgrade_course_letter_boundary();
516
 
517
        // [16] A course with no grade display settings for the course or grade items.
518
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[16]->id});
519
        // [17] A course with grade display settings, but for something that isn't letters.
520
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[17]->id}));
521
        // [18] A course with grade display settings of letters which are default.
522
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[18]->id});
523
        // [19] A course with grade display settings of letters which are not default, but not affected.
524
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[19]->id}));
525
        // [20] A course with grade display settings of letters which are not default, which will be affected.
526
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[20]->id});
527
        // [21] A grade item with display settings which are not letters. Grade total will be affected so should be frozen.
528
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[21]->id});
529
        // [22] A grade item with display settings of letters which are default.
530
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[22]->id});
531
        // [23] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting.
532
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[23]->id}));
533
        // [24] A grade item with display settings of letters which are not default, which will be affected.
534
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[24]->id});
535
        // [25] A course which is using the default grade display setting, but has updated the grade letter boundary (not 57) Should not be frozen.
536
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[25]->id}));
537
        // [26] A course that is using the default display setting (letters) and altered the letter boundary with 57. Should be frozen.
538
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[26]->id});
539
 
540
        // System setting not showing letters.
541
        set_config('grade_displaytype', '2');
542
        for ($i = 0; $i < 45; $i++) {
543
            unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
544
        }
545
        upgrade_course_letter_boundary();
546
 
547
        // [27] A course with no grade display settings for the course or grade items.
548
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[27]->id}));
549
        // [28] A course with grade display settings, but for something that isn't letters.
550
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[28]->id}));
551
        // [29] A course with grade display settings of letters which are default.
552
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[29]->id});
553
        // [30] A course with grade display settings of letters which are not default, but not affected.
554
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[30]->id}));
555
        // [31] A course with grade display settings of letters which are not default, which will be affected.
556
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[31]->id});
557
        // [32] A grade item with display settings which are not letters.
558
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[32]->id}));
559
        // [33] All system defaults.
560
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[33]->id}));
561
        // [34] A grade item with display settings of letters which are not default, but not affected. Course uses new letter boundary setting.
562
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[34]->id}));
563
        // [35] A grade item with display settings of letters which are not default, which will be affected.
564
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[35]->id});
565
        // [36] A course with grade display settings of letters with modified and good boundary (not 57) Should not be frozen.
566
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[36]->id}));
567
 
568
        // Previous site conditions still exist.
569
        for ($i = 0; $i < 45; $i++) {
570
            unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
571
        }
572
        upgrade_course_letter_boundary();
573
 
574
        // [37] Site setting for not showing the letter column and course setting set to show (frozen).
575
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[37]->id});
576
        // [38] Site setting for not showing the letter column and course setting set to hide.
577
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[38]->id}));
578
        // [39] Site setting for not showing the letter column and course setting set to default.
579
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[39]->id}));
580
        // [40] Site setting for not showing the letter column and course setting set to default. Course display set to letters (frozen).
581
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[40]->id});
582
        // [41] Site setting for not showing the letter column and course setting set to default. Grade item display set to letters (frozen).
583
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[41]->id});
584
 
585
        // Previous site conditions still exist.
586
        for ($i = 0; $i < 45; $i++) {
587
            unset_config('gradebook_calculations_freeze_' . $courses[$i]->id);
588
        }
589
        set_config('grade_report_user_showlettergrade', '1');
590
        upgrade_course_letter_boundary();
591
 
592
        // [42] Site setting for showing the letter column, but course setting set to hide.
593
        $this->assertTrue(empty($CFG->{'gradebook_calculations_freeze_' . $courses[42]->id}));
594
        // [43] Site setting for showing the letter column and course setting set to show (frozen).
595
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[43]->id});
596
        // [44] Site setting for showing the letter column and course setting set to default (frozen).
597
        $this->assertEquals(20160518, $CFG->{'gradebook_calculations_freeze_' . $courses[44]->id});
598
    }
599
 
600
    /**
601
     * Test upgrade_letter_boundary_needs_freeze function.
602
     */
11 efrain 603
    public function test_upgrade_letter_boundary_needs_freeze(): void {
1 efrain 604
        global $CFG;
605
 
606
        $this->resetAfterTest();
607
 
608
        require_once($CFG->libdir . '/db/upgradelib.php');
609
 
610
        $courses = array();
611
        $contexts = array();
612
        for ($i = 0; $i < 3; $i++) {
613
            $courses[] = $this->getDataGenerator()->create_course();
614
            $contexts[] = context_course::instance($courses[$i]->id);
615
        }
616
 
617
        // Course one is not using a letter boundary.
618
        $this->assertFalse(upgrade_letter_boundary_needs_freeze($contexts[0]));
619
 
620
        // Let's make course 2 use the bad boundary.
621
        $this->assign_bad_letter_boundary($contexts[1]->id);
622
        $this->assertTrue(upgrade_letter_boundary_needs_freeze($contexts[1]));
623
        // Course 3 has letter boundaries that are fine.
624
        $this->assign_good_letter_boundary($contexts[2]->id);
625
        $this->assertFalse(upgrade_letter_boundary_needs_freeze($contexts[2]));
626
        // Try the system context not using a letter boundary.
627
        $systemcontext = context_system::instance();
628
        $this->assertFalse(upgrade_letter_boundary_needs_freeze($systemcontext));
629
    }
630
 
631
    /**
632
     * Assigns letter boundaries with comparison problems.
633
     *
634
     * @param int $contextid Context ID.
635
     */
636
    private function assign_bad_letter_boundary($contextid) {
637
        global $DB;
638
        $newlettersscale = array(
639
                array('contextid' => $contextid, 'lowerboundary' => 90.00000, 'letter' => 'A'),
640
                array('contextid' => $contextid, 'lowerboundary' => 85.00000, 'letter' => 'A-'),
641
                array('contextid' => $contextid, 'lowerboundary' => 80.00000, 'letter' => 'B+'),
642
                array('contextid' => $contextid, 'lowerboundary' => 75.00000, 'letter' => 'B'),
643
                array('contextid' => $contextid, 'lowerboundary' => 70.00000, 'letter' => 'B-'),
644
                array('contextid' => $contextid, 'lowerboundary' => 65.00000, 'letter' => 'C+'),
645
                array('contextid' => $contextid, 'lowerboundary' => 57.00000, 'letter' => 'C'),
646
                array('contextid' => $contextid, 'lowerboundary' => 50.00000, 'letter' => 'C-'),
647
                array('contextid' => $contextid, 'lowerboundary' => 40.00000, 'letter' => 'D+'),
648
                array('contextid' => $contextid, 'lowerboundary' => 25.00000, 'letter' => 'D'),
649
                array('contextid' => $contextid, 'lowerboundary' => 0.00000, 'letter' => 'F'),
650
            );
651
 
652
        $DB->delete_records('grade_letters', array('contextid' => $contextid));
653
        foreach ($newlettersscale as $record) {
654
            // There is no API to do this, so we have to manually insert into the database.
655
            $DB->insert_record('grade_letters', $record);
656
        }
657
    }
658
 
659
    /**
660
     * Assigns letter boundaries with no comparison problems.
661
     *
662
     * @param int $contextid Context ID.
663
     */
664
    private function assign_good_letter_boundary($contextid) {
665
        global $DB;
666
        $newlettersscale = array(
667
                array('contextid' => $contextid, 'lowerboundary' => 90.00000, 'letter' => 'A'),
668
                array('contextid' => $contextid, 'lowerboundary' => 85.00000, 'letter' => 'A-'),
669
                array('contextid' => $contextid, 'lowerboundary' => 80.00000, 'letter' => 'B+'),
670
                array('contextid' => $contextid, 'lowerboundary' => 75.00000, 'letter' => 'B'),
671
                array('contextid' => $contextid, 'lowerboundary' => 70.00000, 'letter' => 'B-'),
672
                array('contextid' => $contextid, 'lowerboundary' => 65.00000, 'letter' => 'C+'),
673
                array('contextid' => $contextid, 'lowerboundary' => 54.00000, 'letter' => 'C'),
674
                array('contextid' => $contextid, 'lowerboundary' => 50.00000, 'letter' => 'C-'),
675
                array('contextid' => $contextid, 'lowerboundary' => 40.00000, 'letter' => 'D+'),
676
                array('contextid' => $contextid, 'lowerboundary' => 25.00000, 'letter' => 'D'),
677
                array('contextid' => $contextid, 'lowerboundary' => 0.00000, 'letter' => 'F'),
678
            );
679
 
680
        $DB->delete_records('grade_letters', array('contextid' => $contextid));
681
        foreach ($newlettersscale as $record) {
682
            // There is no API to do this, so we have to manually insert into the database.
683
            $DB->insert_record('grade_letters', $record);
684
        }
685
    }
686
 
687
    /**
688
     * Test libcurl custom check api.
689
     */
11 efrain 690
    public function test_check_libcurl_version(): void {
1 efrain 691
        $supportedversion = 0x071304;
692
        $curlinfo = curl_version();
693
        $currentversion = $curlinfo['version_number'];
694
 
695
        $result = new environment_results("custom_checks");
696
        if ($currentversion < $supportedversion) {
697
            $this->assertFalse(check_libcurl_version($result)->getStatus());
698
        } else {
699
            $this->assertNull(check_libcurl_version($result));
700
        }
701
    }
702
 
703
    /**
704
     * Create a collection of test themes to test determining parent themes.
705
     *
706
     * @return Url to the path containing the test themes
707
     */
708
    public function create_testthemes() {
709
        global $CFG;
710
 
711
        $themedircontent = [
712
            'testtheme' => [
713
                'config.php' => '<?php $THEME->name = "testtheme"; $THEME->parents = [""];',
714
            ],
715
            'childoftesttheme' => [
716
                'config.php' => '<?php $THEME->name = "childofboost"; $THEME->parents = ["testtheme"];',
717
            ],
718
            'infinite' => [
719
                'config.php' => '<?php $THEME->name = "infinite"; $THEME->parents = ["forever"];',
720
            ],
721
            'forever' => [
722
                'config.php' => '<?php $THEME->name = "forever"; $THEME->parents = ["infinite", "childoftesttheme"];',
723
            ],
724
            'orphantheme' => [
725
                'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = [];',
726
            ],
727
            'loop' => [
728
                'config.php' => '<?php $THEME->name = "loop"; $THEME->parents = ["around"];',
729
            ],
730
            'around' => [
731
                'config.php' => '<?php $THEME->name = "around"; $THEME->parents = ["loop"];',
732
            ],
733
            'themewithbrokenparent' => [
734
                'config.php' => '<?php $THEME->name = "orphantheme"; $THEME->parents = ["nonexistent", "testtheme"];',
735
            ],
736
        ];
737
        $vthemedir = \org\bovigo\vfs\vfsStream::setup('themes', null, $themedircontent);
738
 
739
        return \org\bovigo\vfs\vfsStream::url('themes');
740
    }
741
 
742
    /**
743
     * Data provider of serialized string.
744
     *
745
     * @return array
746
     */
747
    public function serialized_strings_dataprovider() {
748
        return [
749
            'A configuration that uses the old object' => [
750
                'O:6:"object":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}',
751
                true,
752
                'O:8:"stdClass":3:{s:4:"text";s:32:"Nothing that anyone cares about.";s:5:"title";s:16:"Really old block";s:6:"format";s:1:"1";}'
753
            ],
754
            'A configuration that uses stdClass' => [
755
                'O:8:"stdClass":5:{s:5:"title";s:4:"Tags";s:12:"numberoftags";s:2:"80";s:12:"showstandard";s:1:"0";s:3:"ctx";s:3:"289";s:3:"rec";s:1:"1";}',
756
                false,
757
                'O:8:"stdClass":5:{s:5:"title";s:4:"Tags";s:12:"numberoftags";s:2:"80";s:12:"showstandard";s:1:"0";s:3:"ctx";s:3:"289";s:3:"rec";s:1:"1";}'
758
            ],
759
            'A setting I saw when importing a course with blocks from 1.9' => [
760
                'N;',
761
                false,
762
                'N;'
763
            ],
764
            'An object in an object' => [
765
                'O:6:"object":2:{s:2:"id";i:5;s:5:"other";O:6:"object":1:{s:4:"text";s:13:"something new";}}',
766
                true,
767
                'O:8:"stdClass":2:{s:2:"id";i:5;s:5:"other";O:8:"stdClass":1:{s:4:"text";s:13:"something new";}}'
768
            ],
769
            'An array with an object in it' => [
770
                'a:3:{s:4:"name";s:4:"Test";s:10:"additional";O:6:"object":2:{s:2:"id";i:5;s:4:"info";s:18:"text in the object";}s:4:"type";i:1;}',
771
                true,
772
                'a:3:{s:4:"name";s:4:"Test";s:10:"additional";O:8:"stdClass":2:{s:2:"id";i:5;s:4:"info";s:18:"text in the object";}s:4:"type";i:1;}'
773
            ]
774
        ];
775
    }
776
 
777
    /**
778
     * Test that objects in serialized strings will be changed over to stdClass.
779
     *
780
     * @dataProvider serialized_strings_dataprovider
781
     * @param string $initialstring The initial serialized setting.
782
     * @param bool $expectededited If the string is expected to be edited.
783
     * @param string $expectedresult The expected serialized setting to be returned.
784
     */
11 efrain 785
    public function test_upgrade_fix_serialized_objects($initialstring, $expectededited, $expectedresult): void {
1 efrain 786
        list($edited, $resultstring) = upgrade_fix_serialized_objects($initialstring);
787
        $this->assertEquals($expectededited, $edited);
788
        $this->assertEquals($expectedresult, $resultstring);
789
    }
790
 
791
    /**
792
     * Data provider for base64_encoded block instance config data.
793
     */
794
    public function encoded_strings_dataprovider() {
795
        return [
796
            'Normal data using stdClass' => [
797
                'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30=',
798
                'Tzo4OiJzdGRDbGFzcyI6NTp7czo1OiJ0aXRsZSI7czo0OiJUYWdzIjtzOjEyOiJudW1iZXJvZnRhZ3MiO3M6MjoiODAiO3M6MTI6InNob3dzdGFuZGFyZCI7czoxOiIwIjtzOjM6ImN0eCI7czozOiIyODkiO3M6MzoicmVjIjtzOjE6IjEiO30='
799
            ],
800
            'No data at all' => [
801
                '',
802
                ''
803
            ],
804
            'Old data using object' => [
805
                'Tzo2OiJvYmplY3QiOjM6e3M6NDoidGV4dCI7czozMjoiTm90aGluZyB0aGF0IGFueW9uZSBjYXJlcyBhYm91dC4iO3M6NToidGl0bGUiO3M6MTY6IlJlYWxseSBvbGQgYmxvY2siO3M6NjoiZm9ybWF0IjtzOjE6IjEiO30=',
806
                'Tzo4OiJzdGRDbGFzcyI6Mzp7czo0OiJ0ZXh0IjtzOjMyOiJOb3RoaW5nIHRoYXQgYW55b25lIGNhcmVzIGFib3V0LiI7czo1OiJ0aXRsZSI7czoxNjoiUmVhbGx5IG9sZCBibG9jayI7czo2OiJmb3JtYXQiO3M6MToiMSI7fQ=='
807
            ]
808
        ];
809
    }
810
 
811
    /**
812
     * Check that orphaned files are deleted.
813
     */
11 efrain 814
    public function test_upgrade_delete_orphaned_file_records(): void {
1 efrain 815
        global $DB, $CFG;
816
        require_once($CFG->dirroot . '/repository/lib.php');
817
 
818
        $this->resetAfterTest();
819
        // Create user.
820
        $generator = $this->getDataGenerator();
821
        $user = $generator->create_user();
822
        $this->setUser($user);
823
        $usercontext = context_user::instance($user->id);
824
        $syscontext = context_system::instance();
825
 
826
        $fs = get_file_storage();
827
 
828
        $userrepository = array();
829
        $newstoredfile = array();
830
        $repositorypluginname = array('user', 'areafiles');
831
 
832
        // Create two repositories with one file in each.
833
        foreach ($repositorypluginname as $key => $value) {
834
            // Override repository permission.
835
            $capability = 'repository/' . $value . ':view';
836
            $guestroleid = $DB->get_field('role', 'id', array('shortname' => 'guest'));
837
            assign_capability($capability, CAP_ALLOW, $guestroleid, $syscontext->id, true);
838
 
839
            $args = array();
840
            $args['type'] = $value;
841
            $repos = repository::get_instances($args);
842
            $userrepository[$key] = reset($repos);
843
 
844
            $this->assertInstanceOf('repository', $userrepository[$key]);
845
 
846
            $component = 'user';
847
            $filearea  = 'private';
848
            $itemid    = $key;
849
            $filepath  = '/';
850
            $filename  = 'userfile.txt';
851
 
852
            $filerecord = array(
853
                'contextid' => $usercontext->id,
854
                'component' => $component,
855
                'filearea'  => $filearea,
856
                'itemid'    => $itemid,
857
                'filepath'  => $filepath,
858
                'filename'  => $filename,
859
            );
860
 
861
            $content = 'Test content';
862
            $originalfile = $fs->create_file_from_string($filerecord, $content);
863
            $this->assertInstanceOf('stored_file', $originalfile);
864
 
865
            $newfilerecord = array(
866
                'contextid' => $syscontext->id,
867
                'component' => 'core',
868
                'filearea'  => 'phpunit',
869
                'itemid'    => $key,
870
                'filepath'  => $filepath,
871
                'filename'  => $filename,
872
            );
873
            $ref = $fs->pack_reference($filerecord);
874
            $newstoredfile[$key] = $fs->create_file_from_reference($newfilerecord, $userrepository[$key]->id, $ref);
875
 
876
            // Look for references by repository ID.
877
            $files = $fs->get_external_files($userrepository[$key]->id);
878
            $file = reset($files);
879
            $this->assertEquals($file, $newstoredfile[$key]);
880
        }
881
 
882
        // Make one file orphaned by deleting first repository.
883
        $DB->delete_records('repository_instances', array('id' => $userrepository[0]->id));
884
        $DB->delete_records('repository_instance_config', array('instanceid' => $userrepository[0]->id));
885
 
886
        upgrade_delete_orphaned_file_records();
887
 
888
        $files = $fs->get_external_files($userrepository[0]->id);
889
        $file = reset($files);
890
        $this->assertFalse($file);
891
 
892
        $files = $fs->get_external_files($userrepository[1]->id);
893
        $file = reset($files);
894
        $this->assertEquals($file, $newstoredfile[1]);
895
    }
896
 
897
    /**
898
     * Test the functionality of {@link upgrade_core_licenses} function.
899
     */
11 efrain 900
    public function test_upgrade_core_licenses(): void {
1 efrain 901
        global $CFG, $DB;
902
 
903
        $this->resetAfterTest();
904
 
905
        // Emulate that upgrade is in process.
906
        $CFG->upgraderunning = time();
907
 
908
        $deletedcorelicenseshortname = 'unknown';
909
        $DB->delete_records('license', ['shortname' => $deletedcorelicenseshortname]);
910
 
911
        upgrade_core_licenses();
912
 
913
        $expectedshortnames = ['allrightsreserved', 'cc-4.0', 'cc-nc-4.0', 'cc-nc-nd-4.0', 'cc-nc-sa-4.0', 'cc-nd-4.0', 'cc-sa-4.0', 'public'];
914
        $licenses = $DB->get_records('license');
915
 
916
        foreach ($licenses as $license) {
917
            $this->assertContains($license->shortname, $expectedshortnames);
918
            $this->assertObjectHasProperty('custom', $license);
919
            $this->assertObjectHasProperty('sortorder', $license);
920
        }
921
        // A core license which was deleted prior to upgrade should not be reinstalled.
922
        $actualshortnames = $DB->get_records_menu('license', null, '', 'id, shortname');
923
        $this->assertNotContains($deletedcorelicenseshortname, $actualshortnames);
924
    }
925
 
926
    /**
927
     * Execute same problematic query from upgrade step.
928
     *
929
     * @return bool
930
     */
931
    public function run_upgrade_step_query() {
932
        global $DB;
933
 
934
        return $DB->execute("UPDATE {event} SET userid = 0 WHERE eventtype <> 'user' OR priority <> 0");
935
    }
936
 
937
    /**
938
     * Test the functionality of upgrade_calendar_events_status() function.
939
     */
11 efrain 940
    public function test_upgrade_calendar_events_status(): void {
1 efrain 941
 
942
        $this->resetAfterTest();
943
        $this->setAdminUser();
944
 
945
        $events = create_standard_events(5);
946
        $eventscount = count($events);
947
 
948
        // Run same DB query as the problematic upgrade step.
949
        $this->run_upgrade_step_query();
950
 
951
        // Get the events info.
952
        $status = upgrade_calendar_events_status(false);
953
 
954
        // Total events.
955
        $expected = [
956
            'total' => (object)[
957
                'count' => $eventscount,
958
                'bad' => $eventscount - 5, // Event count excluding user events.
959
            ],
960
            'standard' => (object)[
961
                'count' => $eventscount,
962
                'bad' => $eventscount - 5, // Event count excluding user events.
963
            ],
964
        ];
965
 
966
        $this->assertEquals($expected['standard']->count, $status['standard']->count);
967
        $this->assertEquals($expected['standard']->bad, $status['standard']->bad);
968
        $this->assertEquals($expected['total']->count, $status['total']->count);
969
        $this->assertEquals($expected['total']->bad, $status['total']->bad);
970
    }
971
 
972
    /**
973
     * Test the functionality of upgrade_calendar_events_get_teacherid() function.
974
     */
11 efrain 975
    public function test_upgrade_calendar_events_get_teacherid(): void {
1 efrain 976
        global $DB;
977
 
978
        $this->resetAfterTest();
979
 
980
        // Create a new course and enrol a user as editing teacher.
981
        $generator = $this->getDataGenerator();
982
        $course = $generator->create_course();
983
        $teacher = $generator->create_and_enrol($course, 'editingteacher');
984
 
985
        // There's a teacher enrolled in the course, return its user id.
986
        $userid = upgrade_calendar_events_get_teacherid($course->id);
987
 
988
        // It should return the enrolled teacher by default.
989
        $this->assertEquals($teacher->id, $userid);
990
 
991
        // Un-enrol teacher from course.
992
        $instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
993
        enrol_get_plugin('manual')->unenrol_user($instance, $teacher->id);
994
 
995
        // Since there are no teachers enrolled in the course, fallback to admin user id.
996
        $admin = get_admin();
997
        $userid = upgrade_calendar_events_get_teacherid($course->id);
998
        $this->assertEquals($admin->id, $userid);
999
    }
1000
 
1001
    /**
1002
     * Test the functionality of upgrade_calendar_standard_events_fix() function.
1003
     */
11 efrain 1004
    public function test_upgrade_calendar_standard_events_fix(): void {
1 efrain 1005
 
1006
        $this->resetAfterTest();
1007
        $this->setAdminUser();
1008
 
1009
        $events = create_standard_events(5);
1010
        $eventscount = count($events);
1011
 
1012
        // Get the events info.
1013
        $info = upgrade_calendar_events_status(false);
1014
 
1015
        // There should be no standard events to be fixed.
1016
        $this->assertEquals(0, $info['standard']->bad);
1017
 
1018
        // No events to be fixed, should return false.
1019
        $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1020
 
1021
        // Run same problematic DB query.
1022
        $this->run_upgrade_step_query();
1023
 
1024
        // Get the events info.
1025
        $info = upgrade_calendar_events_status(false);
1026
 
1027
        // There should be 20 events to be fixed (five from each type except user).
1028
        $this->assertEquals($eventscount - 5, $info['standard']->bad);
1029
 
1030
        // Test the function runtime, passing -1 as end time.
1031
        // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1032
        $result = upgrade_calendar_standard_events_fix($info['standard'], false, -1);
1033
        $this->assertNotFalse($result);
1034
 
1035
        // Call the function again, this time it will run until all events have been fixed.
1036
        $this->assertFalse(upgrade_calendar_standard_events_fix($info['standard'], false));
1037
 
1038
        // Get the events info again.
1039
        $info = upgrade_calendar_events_status(false);
1040
 
1041
        // All standard events should have been recovered.
1042
        // There should be no standard events flagged to be fixed.
1043
        $this->assertEquals(0, $info['standard']->bad);
1044
    }
1045
 
1046
    /**
1047
     * Test the functionality of upgrade_calendar_subscription_events_fix() function.
1048
     */
11 efrain 1049
    public function test_upgrade_calendar_subscription_events_fix(): void {
1 efrain 1050
        global $CFG, $DB;
1051
 
1052
        require_once($CFG->dirroot . '/calendar/lib.php');
1053
        require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
1054
 
1055
        $this->resetAfterTest();
1056
        $this->setAdminUser();
1057
 
1058
        // Create event subscription.
1059
        $subscription = new stdClass;
1060
        $subscription->name = 'Repeated events';
1061
        $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
1062
        $subscription->eventtype = 'site';
1063
        $id = calendar_add_subscription($subscription);
1064
 
1065
        // Get repeated events ICS file.
1066
        $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/repeated_events.ics');
1067
        $ical = new iCalendar();
1068
        $ical->unserialize($calendar);
1069
 
1070
        // Import subscription events.
1071
        calendar_import_events_from_ical($ical, $id);
1072
 
1073
        // Subscription should have added 18 events.
1074
        $eventscount = $DB->count_records('event');
1075
 
1076
        // Get the events info.
1077
        $info = upgrade_calendar_events_status(false);
1078
 
1079
        // There should be no subscription events to be fixed at this point.
1080
        $this->assertEquals(0, $info['subscription']->bad);
1081
 
1082
        // No events to be fixed, should return false.
1083
        $this->assertFalse(upgrade_calendar_subscription_events_fix($info['subscription'], false));
1084
 
1085
        // Run same problematic DB query.
1086
        $this->run_upgrade_step_query();
1087
 
1088
        // Get the events info and assert total number of events is correct.
1089
        $info = upgrade_calendar_events_status(false);
1090
        $subscriptioninfo = $info['subscription'];
1091
 
1092
        $this->assertEquals($eventscount, $subscriptioninfo->count);
1093
 
1094
        // Since we have added our subscription as site, all sub events have been affected.
1095
        $this->assertEquals($eventscount, $subscriptioninfo->bad);
1096
 
1097
        // Test the function runtime, passing -1 as end time.
1098
        // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1099
        $result = upgrade_calendar_subscription_events_fix($subscriptioninfo, false, -1);
1100
        $this->assertNotFalse($result);
1101
 
1102
        // Call the function again, this time it will run until all events have been fixed.
1103
        $this->assertFalse(upgrade_calendar_subscription_events_fix($subscriptioninfo, false));
1104
 
1105
        // Get the events info again.
1106
        $info = upgrade_calendar_events_status(false);
1107
 
1108
        // All standard events should have been recovered.
1109
        // There should be no standard events flagged to be fixed.
1110
        $this->assertEquals(0, $info['subscription']->bad);
1111
    }
1112
 
1113
    /**
1114
     * Test the functionality of upgrade_calendar_action_events_fix() function.
1115
     */
11 efrain 1116
    public function test_upgrade_calendar_action_events_fix(): void {
1 efrain 1117
        global $DB;
1118
 
1119
        $this->resetAfterTest();
1120
        $this->setAdminUser();
1121
 
1122
        // Create a new course and a choice activity.
1123
        $course = $this->getDataGenerator()->create_course();
1124
        $choice = $this->getDataGenerator()->create_module('choice', ['course' => $course->id]);
1125
 
1126
        // Create some action events.
1127
        create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
1128
            'eventtype' => CHOICE_EVENT_TYPE_OPEN]);
1129
        create_action_event(['courseid' => $course->id, 'modulename' => 'choice', 'instance' => $choice->id,
1130
            'eventtype' => CHOICE_EVENT_TYPE_CLOSE]);
1131
 
1132
        $eventscount = $DB->count_records('event');
1133
 
1134
        // Get the events info.
1135
        $info = upgrade_calendar_events_status(false);
1136
        $actioninfo = $info['action'];
1137
 
1138
        // There should be no standard events to be fixed.
1139
        $this->assertEquals(0, $actioninfo->bad);
1140
 
1141
        // No events to be fixed, should return false.
1142
        $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1143
 
1144
        // Run same problematic DB query.
1145
        $this->run_upgrade_step_query();
1146
 
1147
        // Get the events info.
1148
        $info = upgrade_calendar_events_status(false);
1149
        $actioninfo = $info['action'];
1150
 
1151
        // There should be 2 events to be fixed.
1152
        $this->assertEquals($eventscount, $actioninfo->bad);
1153
 
1154
        // Test the function runtime, passing -1 as end time.
1155
        // It should not be able to fix all events so fast, so some events should remain to be fixed in the next run.
1156
        $this->assertNotFalse(upgrade_calendar_action_events_fix($actioninfo, false, -1));
1157
 
1158
        // Call the function again, this time it will run until all events have been fixed.
1159
        $this->assertFalse(upgrade_calendar_action_events_fix($actioninfo, false));
1160
 
1161
        // Get the events info again.
1162
        $info = upgrade_calendar_events_status(false);
1163
 
1164
        // All standard events should have been recovered.
1165
        // There should be no standard events flagged to be fixed.
1166
        $this->assertEquals(0, $info['action']->bad);
1167
    }
1168
 
1169
    /**
1170
     * Test the user override part of upgrade_calendar_override_events_fix() function.
1171
     */
11 efrain 1172
    public function test_upgrade_calendar_user_override_events_fix(): void {
1 efrain 1173
        global $DB;
1174
 
1175
        $this->resetAfterTest();
1176
        $this->setAdminUser();
1177
 
1178
        $generator = $this->getDataGenerator();
1179
 
1180
        // Create a new course.
1181
        $course = $generator->create_course();
1182
 
1183
        // Create few users and enrol as students.
1184
        $student1 = $generator->create_and_enrol($course, 'student');
1185
        $student2 = $generator->create_and_enrol($course, 'student');
1186
        $student3 = $generator->create_and_enrol($course, 'student');
1187
 
1188
        // Create some activities and some override events.
1189
        foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1190
            $instance = $generator->create_module($modulename, ['course' => $course->id]);
1191
            create_user_override_event($modulename, $instance->id, $student1->id);
1192
            create_user_override_event($modulename, $instance->id, $student2->id);
1193
            create_user_override_event($modulename, $instance->id, $student3->id);
1194
        }
1195
 
1196
        // There should be 9 override events to be fixed (three from each module).
1197
        $eventscount = $DB->count_records('event');
1198
        $this->assertEquals(9, $eventscount);
1199
 
1200
        // Get the events info.
1201
        $info = upgrade_calendar_events_status(false);
1202
        $overrideinfo = $info['override'];
1203
 
1204
        // There should be no standard events to be fixed.
1205
        $this->assertEquals(0, $overrideinfo->bad);
1206
 
1207
        // No events to be fixed, should return false.
1208
        $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1209
 
1210
        // Run same problematic DB query.
1211
        $this->run_upgrade_step_query();
1212
 
1213
        // Get the events info.
1214
        $info = upgrade_calendar_events_status(false);
1215
        $overrideinfo = $info['override'];
1216
 
1217
        // There should be 9 events to be fixed (three from each module).
1218
        $this->assertEquals($eventscount, $overrideinfo->bad);
1219
 
1220
        // Call the function again, this time it will run until all events have been fixed.
1221
        $this->assertFalse(upgrade_calendar_override_events_fix($overrideinfo, false));
1222
 
1223
        // Get the events info again.
1224
        $info = upgrade_calendar_events_status(false);
1225
 
1226
        // All standard events should have been recovered.
1227
        // There should be no standard events flagged to be fixed.
1228
        $this->assertEquals(0, $info['override']->bad);
1229
    }
1230
 
1231
    /**
1232
     * Test the group override part of upgrade_calendar_override_events_fix() function.
1233
     */
11 efrain 1234
    public function test_upgrade_calendar_group_override_events_fix(): void {
1 efrain 1235
        global $DB;
1236
 
1237
        $this->resetAfterTest();
1238
        $this->setAdminUser();
1239
 
1240
        $generator = $this->getDataGenerator();
1241
 
1242
        // Create a new course and few groups.
1243
        $course = $generator->create_course();
1244
        $group1 = $generator->create_group(['courseid' => $course->id]);
1245
        $group2 = $generator->create_group(['courseid' => $course->id]);
1246
        $group3 = $generator->create_group(['courseid' => $course->id]);
1247
 
1248
        // Create some activities and some override events.
1249
        foreach (['assign', 'lesson', 'quiz'] as $modulename) {
1250
            $instance = $generator->create_module($modulename, ['course' => $course->id]);
1251
            create_group_override_event($modulename, $instance->id, $course->id, $group1->id);
1252
            create_group_override_event($modulename, $instance->id, $course->id, $group2->id);
1253
            create_group_override_event($modulename, $instance->id, $course->id, $group3->id);
1254
        }
1255
 
1256
        // There should be 9 override events to be fixed (three from each module).
1257
        $eventscount = $DB->count_records('event');
1258
        $this->assertEquals(9, $eventscount);
1259
 
1260
        // Get the events info.
1261
        $info = upgrade_calendar_events_status(false);
1262
 
1263
        // We classify group overrides as action events since they do not record the userid.
1264
        $groupoverrideinfo = $info['action'];
1265
 
1266
        // There should be no events to be fixed.
1267
        $this->assertEquals(0, $groupoverrideinfo->bad);
1268
 
1269
        // No events to be fixed, should return false.
1270
        $this->assertFalse(upgrade_calendar_action_events_fix($groupoverrideinfo, false));
1271
 
1272
        // Run same problematic DB query.
1273
        $this->run_upgrade_step_query();
1274
 
1275
        // Get the events info.
1276
        $info = upgrade_calendar_events_status(false);
1277
        $this->assertEquals(9, $info['action']->bad);
1278
 
1279
        // Call the function again, this time it will run until all events have been fixed.
1280
        $this->assertFalse(upgrade_calendar_action_events_fix($info['action'], false));
1281
 
1282
        // Since group override events do not set userid, these events should not be flagged to be fixed.
1283
        $this->assertEquals(0, $groupoverrideinfo->bad);
1284
    }
1285
 
1286
    /**
1287
     * Test the admin_dir_usage check with no admin setting specified.
1288
     */
1289
    public function test_admin_dir_usage_not_set(): void {
1290
        $result = new environment_results("custom_checks");
1291
 
1292
        $this->assertNull(check_admin_dir_usage($result));
1293
    }
1294
 
1295
    /**
1296
     * Test the admin_dir_usage check with the default admin setting specified.
1297
     */
1298
    public function test_admin_dir_usage_is_default(): void {
1299
        global $CFG;
1300
 
1301
        $CFG->admin = 'admin';
1302
 
1303
        $result = new environment_results("custom_checks");
1304
        $this->assertNull(check_admin_dir_usage($result));
1305
    }
1306
 
1307
    /**
1308
     * Test the admin_dir_usage check with a custom admin setting specified.
1309
     */
1310
    public function test_admin_dir_usage_non_standard(): void {
1311
        global $CFG;
1312
 
1313
        $this->resetAfterTest(true);
1314
        $CFG->admin = 'notadmin';
1315
 
1316
        $result = new environment_results("custom_checks");
1317
        $this->assertInstanceOf(environment_results::class, check_admin_dir_usage($result));
1318
        $this->assertEquals('admin_dir_usage', $result->getInfo());
1319
        $this->assertFalse($result->getStatus());
1320
    }
1321
 
1322
    /**
1323
     * Test the check_xmlrpc_usage check when the XML-RPC web service method is not set.
1324
     *
1325
     * @return void
1326
     */
1327
    public function test_check_xmlrpc_webservice_is_not_set(): void {
1328
        global $CFG;
1329
 
1330
        $this->resetAfterTest();
1331
 
1332
        $result = new environment_results('custom_checks');
1333
        $this->assertNull(check_xmlrpc_usage($result));
1334
 
1335
        $CFG->webserviceprotocols = 'rest';
1336
        $result = new environment_results('custom_checks');
1337
        $this->assertNull(check_xmlrpc_usage($result));
1338
    }
1339
 
1340
    /**
1341
     * Test the check_xmlrpc_usage check when the XML-RPC web service method is set.
1342
     *
1343
     * @return void
1344
     */
1345
    public function test_check_xmlrpc_webservice_is_set(): void {
1346
        global $CFG;
1347
 
1348
        $this->resetAfterTest();
1349
        $CFG->webserviceprotocols = 'xmlrpc,rest';
1350
 
1351
        $result = new environment_results('custom_checks');
1352
        $this->assertInstanceOf(environment_results::class, check_xmlrpc_usage($result));
1353
        $this->assertEquals('xmlrpc_webservice_usage', $result->getInfo());
1354
        $this->assertFalse($result->getStatus());
1355
    }
1356
 
1357
    /**
1358
     * Test the check_mod_assignment check if mod_assignment is still used.
1359
     *
1360
     * @covers ::check_mod_assignment
1361
     * @return void
1362
     */
1363
    public function test_check_mod_assignment_is_used(): void {
1364
        global $CFG, $DB;
1365
 
1366
        $this->resetAfterTest();
1367
        $result = new environment_results('custom_checks');
1368
 
1369
        if (file_exists("{$CFG->dirroot}/mod/assignment/version.php")) {
1370
            // This is for when the test is run on sites where mod_assignment is most likely reinstalled.
1371
            $this->assertNull(check_mod_assignment($result));
1372
        } else {
1373
            // This is for when the test is run on sites with mod_assignment now gone.
1374
            $this->assertFalse($DB->get_manager()->table_exists('assignment'));
1375
            $this->assertNull(check_mod_assignment($result));
1376
 
1377
            // Then we can simulate a scenario here where the assignment records are still present during the upgrade
1378
            // by recreating the assignment table and adding a record to it.
1379
            $dbman = $DB->get_manager();
1380
            $table = new xmldb_table('assignment');
1381
            $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
1382
            $table->add_field('name', XMLDB_TYPE_CHAR, '255');
1383
            $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
1384
            $dbman->create_table($table);
1385
            $DB->insert_record('assignment', (object)['name' => 'test_assign']);
1386
 
1387
            $this->assertNotNull(check_mod_assignment($result));
1388
            $this->assertEquals('Assignment 2.2 is in use', $result->getInfo());
1389
            $this->assertFalse($result->getStatus());
1390
        }
1391
    }
1392
 
1393
    /**
1394
     * Test the check_oracle_usage check when the Moodle instance is not using Oracle as a database architecture.
1395
     *
1396
     * @covers ::check_oracle_usage
1397
     */
1398
    public function test_check_oracle_usage_is_not_used(): void {
1399
        global $CFG;
1400
 
1401
        $this->resetAfterTest();
1402
        $CFG->dbtype = 'pgsql';
1403
 
1404
        $result = new environment_results('custom_checks');
1405
        $this->assertNull(check_oracle_usage($result));
1406
    }
1407
 
1408
    /**
1409
     * Test the check_oracle_usage check when the Moodle instance is using Oracle as a database architecture.
1410
     *
1411
     * @covers ::check_oracle_usage
1412
     */
1413
    public function test_check_oracle_usage_is_used(): void {
1414
        global $CFG;
1415
 
1416
        $this->resetAfterTest();
1417
        $CFG->dbtype = 'oci';
1418
 
1419
        $result = new environment_results('custom_checks');
1420
        $this->assertInstanceOf(environment_results::class, check_oracle_usage($result));
1421
        $this->assertEquals('oracle_database_usage', $result->getInfo());
1422
        $this->assertFalse($result->getStatus());
1423
    }
1424
 
1425
    /**
1426
     * Data provider of usermenu items.
1427
     *
1428
     * @return array
1429
     */
1430
    public function usermenu_items_dataprovider(): array {
1431
        return [
1432
            'Add new item to empty usermenu' => [
1433
                '',
1434
                'reports,core_reportbuilder|/reportbuilder/index.php',
1435
                'reports,core_reportbuilder|/reportbuilder/index.php',
1436
            ],
1437
            'Add new item to usermenu' => [
1438
                'profile,moodle|/user/profile.php
1439
grades,grades|/grade/report/mygrades.php',
1440
                'reports,core_reportbuilder|/reportbuilder/index.php',
1441
                'profile,moodle|/user/profile.php
1442
grades,grades|/grade/report/mygrades.php
1443
reports,core_reportbuilder|/reportbuilder/index.php',
1444
            ],
1445
            'Add existing item to usermenu' => [
1446
                'profile,moodle|/user/profile.php
1447
reports,core_reportbuilder|/reportbuilder/index.php
1448
calendar,core_calendar|/calendar/view.php?view=month',
1449
                'reports,core_reportbuilder|/reportbuilder/index.php',
1450
                'profile,moodle|/user/profile.php
1451
reports,core_reportbuilder|/reportbuilder/index.php
1452
calendar,core_calendar|/calendar/view.php?view=month',
1453
            ],
1454
        ];
1455
    }
1456
 
1457
    /**
1458
     * Test the functionality of the {@link upgrade_add_item_to_usermenu()} function.
1459
     *
1460
     * @covers ::upgrade_add_item_to_usermenu
1461
     * @dataProvider usermenu_items_dataprovider
1462
     */
11 efrain 1463
    public function test_upgrade_add_item_to_usermenu(string $initialmenu, string $newmenuitem, string $expectedmenu): void {
1 efrain 1464
        global $CFG;
1465
 
1466
        $this->resetAfterTest();
1467
        // Set the base user menu.
1468
        $CFG->customusermenuitems = $initialmenu;
1469
 
1470
        // Add the new item to the user menu.
1471
        upgrade_add_item_to_usermenu($newmenuitem);
1472
        $newcustomusermenu = $CFG->customusermenuitems;
1473
 
1474
        $this->assertEquals($expectedmenu, $newcustomusermenu);
1475
    }
1476
 
1477
    /**
1478
     * Test that file timestamps are corrected for copied files.
1479
     */
11 efrain 1480
    public function test_upgrade_fix_file_timestamps(): void {
1 efrain 1481
        global $DB;
1482
        $this->resetAfterTest();
1483
 
1484
        // Add 2 files for testing, one with edited old timestamps.
1485
        $origtime = time();
1486
        $new = [
1487
            'contextid' => 123,
1488
            'component' => 'mod_label',
1489
            'filearea' => 'intro',
1490
            'itemid' => 0,
1491
            'filepath' => '/',
1492
            'filename' => 'file.txt',
1493
        ];
1494
        $old = [
1495
            'contextid' => 321,
1496
            'component' => 'mod_label',
1497
            'filearea' => 'intro',
1498
            'itemid' => 0,
1499
            'filepath' => '/',
1500
            'filename' => 'file.txt',
1501
        ];
1502
 
1503
        // Create the file records. This will create a directory listing with the current time.
1504
        $fs = get_file_storage();
1505
        $newfile = $fs->create_file_from_string($new, 'new');
1506
        $oldfile = $fs->create_file_from_string($old, 'old');
1507
 
1508
        // Manually set the timestamps to use on files.
1509
        $DB->set_field('files', 'timecreated', $origtime, [
1510
            'contextid' => $newfile->get_contextid(),
1511
            'component' => $newfile->get_component(),
1512
            'filearea' => $newfile->get_filearea(),
1513
            'itemid' => $newfile->get_itemid(),
1514
        ]);
1515
        $DB->set_field('files', 'timemodified', $origtime, [
1516
            'contextid' => $newfile->get_contextid(),
1517
            'component' => $newfile->get_component(),
1518
            'filearea' => $newfile->get_filearea(),
1519
            'itemid' => $newfile->get_itemid(),
1520
        ]);
1521
 
1522
        $DB->set_field('files', 'timecreated', 1, ['id' => $oldfile->get_id()]);
1523
        $DB->set_field('files', 'timemodified', 1, ['id' => $oldfile->get_id()]);
1524
 
1525
        upgrade_fix_file_timestamps();
1526
 
1527
        // Check nothing changed on the new file.
1528
        $updatednew = $DB->get_record('files', ['id' => $newfile->get_id()]);
1529
        $this->assertEquals($origtime, $updatednew->timecreated);
1530
        $this->assertEquals($origtime, $updatednew->timemodified);
1531
 
1532
        // Confirm that the file with old timestamps has been fixed.
1533
        $updatedold = $DB->get_record('files', ['id' => $oldfile->get_id()]);
1534
        $this->assertNotEquals(1, $updatedold->timecreated);
1535
        $this->assertNotEquals(1, $updatedold->timemodified);
1536
        $this->assertTrue($updatedold->timecreated >= $origtime);
1537
        $this->assertTrue($updatedold->timemodified >= $origtime);
1538
    }
1539
 
1540
    /**
1541
     * Test the upgrade status check alongside the outageless flags.
1542
     *
1543
     * @covers ::moodle_needs_upgrading
1544
     */
11 efrain 1545
    public function test_moodle_upgrade_check_outageless(): void {
1 efrain 1546
        global $CFG;
1547
        $this->resetAfterTest();
1548
        // Get a baseline.
1549
        $this->assertFalse(moodle_needs_upgrading());
1550
 
1551
        // First lets check a plain upgrade ready.
1552
        $CFG->version = '';
1553
        $this->assertTrue(moodle_needs_upgrading());
1554
 
1555
        // Now set the locking config and confirm we shouldn't upgrade.
1556
        set_config('outagelessupgrade', true);
1557
        $this->assertFalse(moodle_needs_upgrading());
1558
 
1559
        // Test the ignorelock flag is functioning.
1560
        $this->assertTrue(moodle_needs_upgrading(false));
1561
    }
1562
 
1563
    /**
1564
     * Test the upgrade status check alongside the outageless flags.
1565
     *
1566
     * @covers ::upgrade_started
1567
     */
11 efrain 1568
    public function test_moodle_start_upgrade_outageless(): void {
1 efrain 1569
        global $CFG;
1570
        $this->resetAfterTest();
1571
        $this->assertObjectNotHasProperty('upgraderunning', $CFG);
1572
 
1573
        // Confirm that starting normally sets the upgraderunning flag.
1574
        upgrade_started();
1575
        $upgrade = get_config('core', 'upgraderunning');
1576
        $this->assertTrue($upgrade > (time() - 5));
1577
 
1578
        // Confirm that the config flag doesnt affect the internal upgrade processes.
1579
        unset($CFG->upgraderunning);
1580
        set_config('upgraderunning', null);
1581
        set_config('outagelessupgrade', true);
1582
        upgrade_started();
1583
        $upgrade = get_config('core', 'upgraderunning');
1584
        $this->assertTrue($upgrade > (time() - 5));
1585
    }
1586
 
1587
    /**
1588
     * Test the upgrade timeout setter alongside the outageless flags.
1589
     *
1590
     * @covers ::upgrade_set_timeout
1591
     */
11 efrain 1592
    public function test_moodle_set_upgrade_timeout_outageless(): void {
1 efrain 1593
        global $CFG;
1594
        $this->resetAfterTest();
1595
        $this->assertObjectNotHasProperty('upgraderunning', $CFG);
1596
 
1597
        // Confirm running normally sets the timeout.
1598
        upgrade_set_timeout(120);
1599
        $upgrade = get_config('core', 'upgraderunning');
1600
        $this->assertTrue($upgrade > (time() - 5));
1601
 
1602
        // Confirm that the config flag doesnt affect the internal upgrade processes.
1603
        unset($CFG->upgraderunning);
1604
        set_config('upgraderunning', null);
1605
        set_config('outagelessupgrade', true);
1606
        upgrade_set_timeout(120);
1607
        $upgrade = get_config('core', 'upgraderunning');
1608
        $this->assertTrue($upgrade > (time() - 5));
1609
    }
1610
 
1611
    /**
1612
     * Test the components of the upgrade process being run outageless.
1613
     *
1614
     * @covers ::moodle_needs_upgrading
1615
     * @covers ::upgrade_started
1616
     * @covers ::upgrade_set_timeout
1617
     */
11 efrain 1618
    public function test_upgrade_components_with_outageless(): void {
1 efrain 1619
        global $CFG;
1620
        $this->resetAfterTest();
1621
 
1622
        // We can now define the outageless constant for use in upgrade, and test the effects.
1623
        define('CLI_UPGRADE_RUNNING', true);
1624
 
1625
        // First test the upgrade check. Even when locked via config this should return true.
1626
        // This can happen when attempting to fix a broken upgrade, so needs to work.
1627
        set_config('outagelessupgrade', true);
1628
        $CFG->version = '';
1629
        $this->assertTrue(moodle_needs_upgrading());
1630
 
1631
        // Now confirm that starting upgrade with the constant will not set the upgraderunning flag.
1632
        set_config('upgraderunning', null);
1633
        upgrade_started();
1634
        $upgrade = get_config('core', 'upgraderunning');
1635
        $this->assertFalse($upgrade);
1636
 
1637
        // The same for timeouts, it should not be set if the constant is set.
1638
        set_config('upgraderunning', null);
1639
        upgrade_set_timeout(120);
1640
        $upgrade = get_config('core', 'upgraderunning');
1641
        $this->assertFalse($upgrade);
1642
    }
1643
}