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;
18
 
19
use advanced_testcase;
20
use grade_item;
21
 
22
/**
23
 * Unit tests for penalty_manager class.
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\penalty_manager
29
 */
30
final class penalty_manager_test extends advanced_testcase {
31
    /**
32
     * Test is_penalty_enabled_for_module method.
33
     */
34
    public function test_is_penalty_enabled_for_module(): void {
35
        $this->resetAfterTest();
36
        $this->setAdminUser();
37
 
38
        // No modules are enabled by default.
39
        $this->assertEmpty(penalty_manager::get_enabled_modules());
40
 
41
        // Enable a module.
42
        penalty_manager::enable_module('assign');
43
        $this->assertCount(1, penalty_manager::get_enabled_modules());
44
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('assign'));
45
 
46
        // Enable multiple modules.
47
        penalty_manager::enable_modules(['quiz', 'forum', 'page']);
48
        $this->assertCount(4, penalty_manager::get_enabled_modules());
49
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('assign'));
50
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('quiz'));
51
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('forum'));
52
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('page'));
53
 
54
        // Disable a module.
55
        penalty_manager::disable_module('assign');
56
        $this->assertCount(3, penalty_manager::get_enabled_modules());
57
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('quiz'));
58
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('forum'));
59
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('page'));
60
 
61
        // Disable multiple modules.
62
        penalty_manager::disable_modules(['quiz', 'forum']);
63
        $this->assertCount(1, penalty_manager::get_enabled_modules());
64
        $this->assertTrue(penalty_manager::is_penalty_enabled_for_module('page'));
65
    }
66
 
67
    /**
68
     * Test apply_grade_penalty_to_user method.
69
     */
70
    public function test_apply_grade_penalty_to_user(): void {
71
        $this->resetAfterTest();
72
        $this->setAdminUser();
73
        // Create user, course and assignment.
74
        $user = $this->getDataGenerator()->create_user();
75
        $course = $this->getDataGenerator()->create_course();
76
        $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
77
 
78
        // Get grade item.
79
        $gradeitemparams = [
80
            'courseid' => $course->id,
81
            'itemtype' => 'mod',
82
            'itemmodule' => 'assign',
83
            'iteminstance' => $assign->id,
84
            'itemnumber' => 0,
85
        ];
86
 
87
        $gradeitem = grade_item::fetch($gradeitemparams);
88
 
89
        grade_update(
90
            'mod/assign',
91
            $course->id,
92
            'mod',
93
            'assign',
94
            $assign->id,
95
            0,
96
           ['userid' => $user->id, 'rawgrade' => 90],
97
        );
98
 
99
        $submissiondate = time();
100
        $duedate = time();
101
        $container = penalty_manager::apply_grade_penalty_to_user($user->id, $gradeitem, $submissiondate, $duedate);
102
 
103
        // No penalty by default.
104
        $this->assertEquals(90, $container->get_grade_after_penalties());
105
    }
106
}