Proyectos de Subversion Moodle

Rev

| 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
namespace gradereport_overview;
18
 
19
/**
20
 * Overview grade report lib functions unit tests
21
 *
22
 * @package gradereport_overview
23
 * @copyright 2023 The Open University
24
 * @covers \grade_report_overview
25
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class lib_test extends \advanced_testcase {
28
 
29
    /**
30
     * Require the library file we're about to test, and other requirements.
31
     */
32
    protected function setUp(): void {
33
        global $CFG;
34
        require_once($CFG->dirroot . '/grade/report/overview/lib.php');
35
        require_once($CFG->dirroot . '/grade/querylib.php');
36
    }
37
 
38
    /**
39
     * Data provider for true or false value.
40
     *
41
     * @return array Two options, one with true and one with false
42
     */
43
    public function true_or_false(): array {
44
        return [
45
            [true],
46
            [false]
47
        ];
48
    }
49
 
50
    /**
51
     * Tests the regrade_all_courses_if_needed function, which is supposed to regrade all the
52
     * courses that the current user is enrolled on (if they need update).
53
     *
54
     * This is tested with both frontend and backend - the frontend option should not actually
55
     * do the progress bar/continue button (which can't be tested from here because it calls exit)
56
     * because these courses are small.
57
     *
58
     * @dataProvider true_or_false
59
     * @param bool $frontend True to use the front-end parameter to the function under test
60
     */
61
    public function test_regrade_all_courses_if_needed(bool $frontend): void {
62
        global $DB;
63
        $this->resetAfterTest(true);
64
 
65
        $generator = $this->getDataGenerator();
66
 
67
        // Create 3 courses and a test user. The test user belongs to 2 of them, while another user
68
        // belongs to the other course.
69
        $user = $generator->create_user();
70
        $otheruser = $generator->create_user();
71
        $course1 = $generator->create_course();
72
        $generator->enrol_user($user->id, $course1->id, 'student');
73
        $course2 = $generator->create_course();
74
        $generator->enrol_user($otheruser->id, $course2->id, 'student');
75
        $course3 = $generator->create_course();
76
        $generator->enrol_user($user->id, $course3->id, 'student');
77
 
78
        // We need permission to create grades (even though it's a data generator).
79
        $this->setAdminUser();
80
 
81
        // Set up each course grade.
82
        foreach ([$course1, $course2, $course3] as $course) {
83
            // Create an assignment and get its grade item.
84
            $assign = $generator->create_module('assign', ['course' => $course->id]);
85
            $modinfo = get_fast_modinfo($course->id);
86
            $cm = $modinfo->get_cm($assign->cmid);
87
            $items = grade_get_grade_items_for_activity($cm, true);
88
            $item = reset($items);
89
 
90
            // Set a grade in the assignment, either for the normal test user or the other user
91
            // depending on course.
92
            if ($course === $course2) {
93
                $userid = $otheruser->id;
94
            } else {
95
                $userid = $user->id;
96
            }
97
            $generator->create_grade_grade([
98
                'itemid' => $item->id,
99
                'userid' => $userid,
100
                'teamsubmission' => false,
101
                'attemptnumber' => 0,
102
                'grade' => 50
103
            ]);
104
 
105
            // Bodge the final grade so that it needs regrading and is set wrong.
106
            $course->gradeitemid = $DB->get_field('grade_items', 'id',
107
                    ['courseid' => $course->id, 'itemtype' => 'course'], MUST_EXIST);
108
            $DB->set_field('grade_items', 'needsupdate', 1, ['id' => $course->gradeitemid]);
109
            $DB->set_field('grade_grades', 'finalgrade', 25.0, ['itemid' => $course->gradeitemid]);
110
        }
111
 
112
        // Construct the overview report and call regrade_all_courses_if_needed.
113
        $gpr = new \stdClass();
114
        $report = new \grade_report_overview($user->id, $gpr, '');
115
        $report->regrade_all_courses_if_needed($frontend);
116
 
117
        // This should have regraded courses 1 and 3, but not 2 (because the user doesn't belong).
118
        $this->assertEqualsWithDelta(50.0, $DB->get_field('grade_grades', 'finalgrade',
119
                ['itemid' => $course1->gradeitemid]), 1.0);
120
        $this->assertEqualsWithDelta(25.0, $DB->get_field('grade_grades', 'finalgrade',
121
                ['itemid' => $course2->gradeitemid]), 1.0);
122
        $this->assertEqualsWithDelta(50.0, $DB->get_field('grade_grades', 'finalgrade',
123
                ['itemid' => $course3->gradeitemid]), 1.0);
124
    }
125
}