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 gradepenalty_duedate;
|
|
|
18 |
|
|
|
19 |
use context_course;
|
|
|
20 |
use context_system;
|
|
|
21 |
use gradepenalty_duedate\tests\penalty_testcase;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Test penalty rule persistent.
|
|
|
25 |
*
|
|
|
26 |
* @package gradepenalty_duedate
|
|
|
27 |
* @copyright 2024 Catalyst IT Australia Pty Ltd
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @covers \gradepenalty_duedate\penalty_rule
|
|
|
30 |
*/
|
|
|
31 |
final class penalty_rule_persistent_test extends penalty_testcase {
|
|
|
32 |
/**
|
|
|
33 |
* Test get rules.
|
|
|
34 |
*/
|
|
|
35 |
public function test_get_rules(): void {
|
|
|
36 |
$this->resetAfterTest();
|
|
|
37 |
$this->create_sample_rules();
|
|
|
38 |
|
|
|
39 |
$course = $this->getDataGenerator()->create_course();
|
|
|
40 |
|
|
|
41 |
$systemcontextid = context_system::instance()->id;
|
|
|
42 |
$coursecontextid = context_course::instance($course->id)->id;
|
|
|
43 |
|
|
|
44 |
// Check system context penalty rules.
|
|
|
45 |
$rules = penalty_rule::get_rules($systemcontextid);
|
|
|
46 |
$this->assertCount(5, $rules);
|
|
|
47 |
$this->assertEquals(10, $rules[0]->get('penalty'));
|
|
|
48 |
$this->assertEquals(20, $rules[1]->get('penalty'));
|
|
|
49 |
$this->assertEquals(30, $rules[2]->get('penalty'));
|
|
|
50 |
$this->assertEquals(40, $rules[3]->get('penalty'));
|
|
|
51 |
$this->assertEquals(50, $rules[4]->get('penalty'));
|
|
|
52 |
|
|
|
53 |
// Check course context penalty rules.
|
|
|
54 |
$rules = penalty_rule::get_records(['contextid' => $coursecontextid]);
|
|
|
55 |
$this->assertCount(0, $rules);
|
|
|
56 |
|
|
|
57 |
// Verify the rules are inherited.
|
|
|
58 |
$rules = penalty_rule::get_rules($coursecontextid);
|
|
|
59 |
$this->assertCount(5, $rules);
|
|
|
60 |
$this->assertEquals(10, $rules[0]->get('penalty'));
|
|
|
61 |
$this->assertEquals(20, $rules[1]->get('penalty'));
|
|
|
62 |
$this->assertEquals(30, $rules[2]->get('penalty'));
|
|
|
63 |
$this->assertEquals(40, $rules[3]->get('penalty'));
|
|
|
64 |
$this->assertEquals(50, $rules[4]->get('penalty'));
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Test reset rules.
|
|
|
69 |
*/
|
|
|
70 |
public function test_reset_rules(): void {
|
|
|
71 |
$this->resetAfterTest();
|
|
|
72 |
$this->create_sample_rules();
|
|
|
73 |
$systemcontextid = context_system::instance()->id;
|
|
|
74 |
penalty_rule::reset_rules($systemcontextid);
|
|
|
75 |
$rules = penalty_rule::get_rules($systemcontextid);
|
|
|
76 |
// Default 0% rule.
|
|
|
77 |
$this->assertCount(1, $rules);
|
|
|
78 |
$this->assertEquals(0, $rules[0]->get('penalty'));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Test check if rules are overridden.
|
|
|
83 |
*/
|
|
|
84 |
public function test_is_overridden(): void {
|
|
|
85 |
$this->resetAfterTest();
|
|
|
86 |
// System context penalty rules are never considered to be overridden.
|
|
|
87 |
$systemcontextid = context_system::instance()->id;
|
|
|
88 |
$this->create_sample_rules();
|
|
|
89 |
$this->assertFalse(penalty_rule::is_overridden($systemcontextid));
|
|
|
90 |
|
|
|
91 |
$course = $this->getDataGenerator()->create_course();
|
|
|
92 |
$coursecontextid = context_course::instance($course->id)->id;
|
|
|
93 |
|
|
|
94 |
// Verify the course with no rules is not considered overridden.
|
|
|
95 |
$this->assertFalse(penalty_rule::is_overridden($coursecontextid));
|
|
|
96 |
|
|
|
97 |
// Add penalty rules to the course context and verify they are considered overridden.
|
|
|
98 |
$this->create_sample_rules($coursecontextid);
|
|
|
99 |
$this->assertTrue(penalty_rule::is_overridden($coursecontextid));
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Test check if rules are inherited.
|
|
|
104 |
*/
|
|
|
105 |
public function test_is_inherited(): void {
|
|
|
106 |
$this->resetAfterTest();
|
|
|
107 |
// System context.
|
|
|
108 |
$systemcontextid = context_system::instance()->id;
|
|
|
109 |
$this->create_sample_rules();
|
|
|
110 |
$this->assertFalse(penalty_rule::is_inherited($systemcontextid));
|
|
|
111 |
|
|
|
112 |
// Create a course.
|
|
|
113 |
$course = $this->getDataGenerator()->create_course();
|
|
|
114 |
$coursecontextid = context_course::instance($course->id)->id;
|
|
|
115 |
|
|
|
116 |
// There is no rules created at course context, so they are inherited rules.
|
|
|
117 |
$this->assertTrue(penalty_rule::is_inherited($coursecontextid));
|
|
|
118 |
|
|
|
119 |
// Create sample rules at course context, they are not considered inherited.
|
|
|
120 |
$this->create_sample_rules($coursecontextid);
|
|
|
121 |
$this->assertFalse(penalty_rule::is_inherited($coursecontextid));
|
|
|
122 |
|
|
|
123 |
// Remove the rules from the parent context.
|
|
|
124 |
penalty_rule::reset_rules($systemcontextid);
|
|
|
125 |
$this->assertFalse(penalty_rule::is_inherited($coursecontextid));
|
|
|
126 |
}
|
|
|
127 |
}
|