Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core_grades\output;
18
 
19
use advanced_testcase;
20
use grade_grade;
21
 
22
/**
23
 * Test class for penalty_indicator
24
 *
25
 * @package   core_grades
26
 * @copyright 2024 Catalyst IT Australia Pty Ltd
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core_grades\output\penalty_indicator
29
 */
30
final class penalty_indicator_test extends advanced_testcase {
31
    /**
32
     * Data provider for test_export_for_template.
33
     *
34
     * @return array
35
     */
36
    public static function export_for_template_provider(): array {
37
        return [
38
            // Default icon, with final grade and max grade.
39
            [
40
                'expectedexport' => [
41
                    'penalty' => '10.00',
42
                    'finalgrade' => '90.00',
43
                    'icon' => [
44
                        'name' => 'i/risk_xss',
45
                        'component' => 'core',
46
                    ],
47
                    'info' => 'Late penalty applied -10.00 marks',
48
                    'grademax' => '100.00',
49
                ],
50
                'icon' => [],
51
                'penalty' => 10,
52
                'finalgrade' => 90,
53
                'grademax' => 100,
54
                'showfinalgrade' => true,
55
                'showgrademax' => true,
56
            ],
57
            // Custom icon, without max grade.
58
            [
59
                'expectedexport' => [
60
                    'penalty' => '10.00',
61
                    'finalgrade' => '90.00',
62
                    'icon' => [
63
                        'name' => 'i/flagged',
64
                        'component' => 'core',
65
                    ],
66
                    'info' => 'Late penalty applied -10.00 marks',
67
                    'grademax' => null,
68
                ],
69
                'icon' => ['name' => 'i/flagged', 'component' => 'core'],
70
                'penalty' => 10,
71
                'finalgrade' => 90,
72
                'grademax' => 100,
73
                'showfinalgrade' => true,
74
                'showgrademax' => false,
75
            ],
76
            // Icon only.
77
            [
78
                'expectedexport' => [
79
                    'penalty' => '10.00',
80
                    'icon' => [
81
                        'name' => 'i/risk_xss',
82
                        'component' => 'core',
83
                    ],
84
                    'info' => 'Late penalty applied -10.00 marks',
85
                    'grademax' => null,
86
                    'finalgrade' => null,
87
                ],
88
                'icon' => [],
89
                'penalty' => 10,
90
                'finalgrade' => 90,
91
                'grademax' => 100,
92
                'showfinalgrade' => false,
93
                'showgrademax' => false,
94
            ],
95
 
96
        ];
97
    }
98
 
99
    /**
100
     * Test penalty_indicator
101
     *
102
     * @dataProvider export_for_template_provider
103
     *
104
     * @param array $expectedexport The expected export data
105
     * @param array $icon icon to display before the penalty
106
     * @param float $penalty The penalty
107
     * @param float $finalgrade The final grade
108
     * @param float $grademax The max grade
109
     * @param bool $showfinalgrade Whether to show the final grade
110
     * @param bool $showgrademax Whether to show the max grade
111
     */
112
    public function test_export_for_template(
113
        array $expectedexport,
114
        array $icon,
115
        float $penalty,
116
        float $finalgrade,
117
        float $grademax,
118
        bool $showfinalgrade,
119
        bool $showgrademax
120
    ): void {
121
        global $PAGE, $DB;
122
 
123
        $this->resetAfterTest();
124
 
125
        // Create a course.
126
        $course = $this->getDataGenerator()->create_course();
127
 
128
        // Create a user and enrol them in the course.
129
        $user = $this->getDataGenerator()->create_and_enrol($course);
130
 
131
        // Create an assignment.
132
        $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
133
 
134
        // Create a grade item.
135
        $gradeitem = \grade_item::fetch(['iteminstance' => $assign->id, 'itemtype' => 'mod', 'itemmodule' => 'assign']);
136
 
137
        $DB->set_field('grade_items', 'grademax', $grademax, ['id' => $gradeitem->id]);
138
 
139
        // Create a grade.
140
        $grade = new grade_grade();
141
        $grade->itemid = $gradeitem->id;
142
        $grade->timemodified = time();
143
        $grade->userid = $user->id;
144
        $grade->finalgrade = $finalgrade;
145
        $grade->deductedmark = $penalty;
146
        $grade->insert();
147
 
148
        $indicator = new \core_grades\output\penalty_indicator(2, $grade, $showfinalgrade, $showgrademax, $icon);
149
        $renderer = $PAGE->get_renderer('core_grades');
150
        $data = $indicator->export_for_template($renderer);
151
 
152
        $this->assertEquals($expectedexport, $data);
153
    }
154
}