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 relativedate condition.
|
|
|
19 |
*
|
|
|
20 |
* @package availability_relativedate
|
|
|
21 |
* @copyright 2022 eWallah.net
|
|
|
22 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
namespace availability_relativedate;
|
|
|
26 |
|
|
|
27 |
use availability_relativedate\condition;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Unit tests for the relativedate condition.
|
|
|
31 |
*
|
|
|
32 |
* @package availability_relativedate
|
|
|
33 |
* @copyright 2022 eWallah.net
|
|
|
34 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
* @coversDefaultClass \availability_relativedate\condition
|
|
|
37 |
*/
|
|
|
38 |
final class simple_test extends \advanced_testcase {
|
|
|
39 |
/**
|
|
|
40 |
* Tests the constructor including error conditions.
|
|
|
41 |
* @covers \availability_relativedate\condition
|
|
|
42 |
*/
|
|
|
43 |
public function test_constructor(): void {
|
|
|
44 |
$structure = (object)['type' => 'relativedate'];
|
|
|
45 |
$cond = new condition($structure);
|
|
|
46 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
47 |
$structure->n = 1;
|
|
|
48 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
49 |
$cond = new condition($structure);
|
|
|
50 |
$structure->d = 1;
|
|
|
51 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
52 |
$cond = new condition($structure);
|
|
|
53 |
$structure->d = '2';
|
|
|
54 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
55 |
$cond = new condition($structure);
|
|
|
56 |
$structure->n = 'a';
|
|
|
57 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
58 |
$cond = new condition($structure);
|
|
|
59 |
$structure->e = 'a';
|
|
|
60 |
$cond = new condition($structure);
|
|
|
61 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
62 |
$structure->c = 'a';
|
|
|
63 |
$cond = new condition($structure);
|
|
|
64 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
65 |
$structure->n = 9;
|
|
|
66 |
$structure->c = 1111;
|
|
|
67 |
$cond = new condition($structure);
|
|
|
68 |
$this->assertNotEqualsCanonicalizing($structure, $cond->save());
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Tests the save() function.
|
|
|
73 |
* @covers \availability_relativedate\condition
|
|
|
74 |
*/
|
|
|
75 |
public function test_save(): void {
|
|
|
76 |
$structure = (object)['n' => 1, 'd' => 2, 's' => 1, 'm' => 1];
|
|
|
77 |
$cond = new condition($structure);
|
|
|
78 |
$structure->type = 'relativedate';
|
|
|
79 |
$this->assertEquals($structure, $cond->save());
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Tests static methods.
|
|
|
84 |
* @covers \availability_relativedate\condition
|
|
|
85 |
*/
|
|
|
86 |
public function test_static(): void {
|
|
|
87 |
$this->assertCount(5, condition::options_dwm());
|
|
|
88 |
$this->assertEquals('minute', condition::option_dwm(0));
|
|
|
89 |
$this->assertEquals('hour', condition::option_dwm(1));
|
|
|
90 |
$this->assertEquals('day', condition::option_dwm(2));
|
|
|
91 |
$this->assertEquals('week', condition::option_dwm(3));
|
|
|
92 |
$this->assertEquals('month', condition::option_dwm(4));
|
|
|
93 |
$this->assertEquals('', condition::option_dwm(5));
|
|
|
94 |
$this->assertEquals('after course start date', condition::options_start(1));
|
|
|
95 |
$this->assertEquals('before course end date', condition::options_start(2));
|
|
|
96 |
$this->assertEquals('after user enrolment date', condition::options_start(3));
|
|
|
97 |
$this->assertEquals('after enrolment method end date', condition::options_start(4));
|
|
|
98 |
$this->assertEquals('after course end date', condition::options_start(5));
|
|
|
99 |
$this->assertEquals('before course start date', condition::options_start(6));
|
|
|
100 |
$this->assertEquals('after completion of activity', condition::options_start(7));
|
|
|
101 |
$this->assertEquals('', condition::options_start(8));
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Test debug string.
|
|
|
106 |
*
|
|
|
107 |
* @dataProvider debug_provider
|
|
|
108 |
* @param array $cond
|
|
|
109 |
* @param string $result
|
|
|
110 |
* @covers \availability_relativedate\condition
|
|
|
111 |
*/
|
|
|
112 |
public function test_debug($cond, $result): void {
|
|
|
113 |
$name = 'availability_relativedate\condition';
|
|
|
114 |
$condition = new condition((object)$cond);
|
|
|
115 |
$callresult = \phpunit_util::call_internal_method($condition, 'get_debug_string', [], $name);
|
|
|
116 |
$this->assertEquals($result, $callresult);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Relative dates debug provider.
|
|
|
121 |
*/
|
|
|
122 |
public static function debug_provider(): array {
|
|
|
123 |
$daybefore = ' 1 ' . get_string('day', 'availability_relativedate') . ' ';
|
|
|
124 |
return [
|
|
|
125 |
'After start course' => [
|
|
|
126 |
['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 1, 'm' => 999999],
|
|
|
127 |
$daybefore . get_string('datestart', 'availability_relativedate'), ],
|
|
|
128 |
'Before end course' => [
|
|
|
129 |
['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 2, 'm' => 999999],
|
|
|
130 |
$daybefore . get_string('dateend', 'availability_relativedate'), ],
|
|
|
131 |
'After end enrol' => [
|
|
|
132 |
['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 3, 'm' => 999999],
|
|
|
133 |
$daybefore . get_string('dateenrol', 'availability_relativedate'), ],
|
|
|
134 |
'After end method' => [
|
|
|
135 |
['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 4, 'm' => 999999],
|
|
|
136 |
$daybefore . get_string('dateendenrol', 'availability_relativedate'), ],
|
|
|
137 |
'After end course' => [
|
|
|
138 |
['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 5, 'm' => 999999],
|
|
|
139 |
$daybefore . get_string('dateendafter', 'availability_relativedate'), ],
|
|
|
140 |
'Before start course' => [
|
|
|
141 |
['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 6, 'm' => 999999],
|
|
|
142 |
$daybefore . get_string('datestartbefore', 'availability_relativedate'), ],
|
|
|
143 |
'After invalid module' => [
|
|
|
144 |
['type' => 'relativedate', 'n' => 1, 'd' => 2, 's' => 999, 'm' => 999999],
|
|
|
145 |
$daybefore, ],
|
|
|
146 |
];
|
|
|
147 |
}
|
|
|
148 |
}
|