MDL-37894 Amend tests with new openclose rule.
[moodle.git] / mod / quiz / accessrule / openclosedate / tests / rule_test.php
CommitLineData
7fe8aac1
PS
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 quizaccess_openclosedate plugin.
19 *
20 * @package quizaccess
21 * @subpackage openclosedate
22 * @category phpunit
23 * @copyright 2008 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->dirroot . '/mod/quiz/accessrule/openclosedate/rule.php');
32
33
34/**
35 * Unit tests for the quizaccess_openclosedate plugin.
36 *
37 * @copyright 2008 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class quizaccess_openclosedate_testcase extends basic_testcase {
41 public function test_no_dates() {
42 $quiz = new stdClass();
43 $quiz->timeopen = 0;
44 $quiz->timeclose = 0;
b640e681 45 $quiz->overduehandling = 'autosubmit';
7fe8aac1
PS
46 $quiz->questions = '';
47 $cm = new stdClass();
48 $cm->id = 0;
49 $quizobj = new quiz($quiz, $cm, null);
50 $attempt = new stdClass();
51 $attempt->preview = 0;
52
53 $rule = new quizaccess_openclosedate($quizobj, 10000);
54 $this->assertEmpty($rule->description());
55 $this->assertFalse($rule->prevent_access());
56 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
57 $this->assertFalse($rule->is_finished(0, $attempt));
30ef7963
MP
58 $this->assertFalse($rule->end_time($attempt));
59 $this->assertFalse($rule->time_left_display($attempt, 10000));
60 $this->assertFalse($rule->time_left_display($attempt, 0));
7fe8aac1
PS
61
62 $rule = new quizaccess_openclosedate($quizobj, 0);
63 $this->assertEmpty($rule->description());
64 $this->assertFalse($rule->prevent_access());
65 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
66 $this->assertFalse($rule->is_finished(0, $attempt));
30ef7963
MP
67 $this->assertFalse($rule->end_time($attempt));
68 $this->assertFalse($rule->time_left_display($attempt, 0));
7fe8aac1
PS
69 }
70
71 public function test_start_date() {
72 $quiz = new stdClass();
73 $quiz->timeopen = 10000;
74 $quiz->timeclose = 0;
b640e681 75 $quiz->overduehandling = 'autosubmit';
7fe8aac1
PS
76 $quiz->questions = '';
77 $cm = new stdClass();
78 $cm->id = 0;
79 $quizobj = new quiz($quiz, $cm, null);
80 $attempt = new stdClass();
81 $attempt->preview = 0;
82
83 $rule = new quizaccess_openclosedate($quizobj, 9999);
84 $this->assertEquals($rule->description(),
85 array(get_string('quiznotavailable', 'quizaccess_openclosedate', userdate(10000))));
86 $this->assertEquals($rule->prevent_access(),
87 get_string('notavailable', 'quizaccess_openclosedate'));
88 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
89 $this->assertFalse($rule->is_finished(0, $attempt));
30ef7963
MP
90 $this->assertFalse($rule->end_time($attempt));
91 $this->assertFalse($rule->time_left_display($attempt, 0));
7fe8aac1
PS
92
93 $rule = new quizaccess_openclosedate($quizobj, 10000);
94 $this->assertEquals($rule->description(),
95 array(get_string('quizopenedon', 'quiz', userdate(10000))));
96 $this->assertFalse($rule->prevent_access());
97 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
98 $this->assertFalse($rule->is_finished(0, $attempt));
30ef7963
MP
99 $this->assertFalse($rule->end_time($attempt));
100 $this->assertFalse($rule->time_left_display($attempt, 0));
7fe8aac1
PS
101 }
102
103 public function test_close_date() {
104 $quiz = new stdClass();
105 $quiz->timeopen = 0;
106 $quiz->timeclose = 20000;
b640e681 107 $quiz->overduehandling = 'autosubmit';
7fe8aac1
PS
108 $quiz->questions = '';
109 $cm = new stdClass();
110 $cm->id = 0;
111 $quizobj = new quiz($quiz, $cm, null);
112 $attempt = new stdClass();
113 $attempt->preview = 0;
114
115 $rule = new quizaccess_openclosedate($quizobj, 20000);
116 $this->assertEquals($rule->description(),
117 array(get_string('quizcloseson', 'quiz', userdate(20000))));
118 $this->assertFalse($rule->prevent_access());
119 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
120 $this->assertFalse($rule->is_finished(0, $attempt));
30ef7963
MP
121
122 $this->assertEquals($rule->end_time($attempt), 20000);
123 $this->assertFalse($rule->time_left_display($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
124 $this->assertEquals($rule->time_left_display($attempt, 19900), 100);
125 $this->assertEquals($rule->time_left_display($attempt, 20000), 0);
126 $this->assertEquals($rule->time_left_display($attempt, 20100), -100);
7fe8aac1
PS
127
128 $rule = new quizaccess_openclosedate($quizobj, 20001);
129 $this->assertEquals($rule->description(),
130 array(get_string('quizclosed', 'quiz', userdate(20000))));
131 $this->assertEquals($rule->prevent_access(),
132 get_string('notavailable', 'quizaccess_openclosedate'));
133 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
134 $this->assertTrue($rule->is_finished(0, $attempt));
30ef7963
MP
135 $this->assertEquals($rule->end_time($attempt), 20000);
136 $this->assertFalse($rule->time_left_display($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
137 $this->assertEquals($rule->time_left_display($attempt, 19900), 100);
138 $this->assertEquals($rule->time_left_display($attempt, 20000), 0);
139 $this->assertEquals($rule->time_left_display($attempt, 20100), -100);
7fe8aac1
PS
140 }
141
142 public function test_both_dates() {
143 $quiz = new stdClass();
144 $quiz->timeopen = 10000;
145 $quiz->timeclose = 20000;
b640e681 146 $quiz->overduehandling = 'autosubmit';
7fe8aac1
PS
147 $quiz->questions = '';
148 $cm = new stdClass();
149 $cm->id = 0;
150 $quizobj = new quiz($quiz, $cm, null);
151 $attempt = new stdClass();
152 $attempt->preview = 0;
153
154 $rule = new quizaccess_openclosedate($quizobj, 9999);
155 $this->assertEquals($rule->description(),
feb84363
EL
156 array(get_string('quiznotavailable', 'quizaccess_openclosedate', userdate(10000)),
157 get_string('quizcloseson', 'quiz', userdate(20000))));
7fe8aac1
PS
158 $this->assertEquals($rule->prevent_access(),
159 get_string('notavailable', 'quizaccess_openclosedate'));
160 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
161 $this->assertFalse($rule->is_finished(0, $attempt));
162
163 $rule = new quizaccess_openclosedate($quizobj, 10000);
164 $this->assertEquals($rule->description(),
165 array(get_string('quizopenedon', 'quiz', userdate(10000)),
166 get_string('quizcloseson', 'quiz', userdate(20000))));
167 $this->assertFalse($rule->prevent_access());
168 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
169 $this->assertFalse($rule->is_finished(0, $attempt));
170
171 $rule = new quizaccess_openclosedate($quizobj, 20000);
172 $this->assertEquals($rule->description(),
173 array(get_string('quizopenedon', 'quiz', userdate(10000)),
174 get_string('quizcloseson', 'quiz', userdate(20000))));
175 $this->assertFalse($rule->prevent_access());
176 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
177 $this->assertFalse($rule->is_finished(0, $attempt));
178
179 $rule = new quizaccess_openclosedate($quizobj, 20001);
180 $this->assertEquals($rule->description(),
181 array(get_string('quizclosed', 'quiz', userdate(20000))));
182 $this->assertEquals($rule->prevent_access(),
183 get_string('notavailable', 'quizaccess_openclosedate'));
184 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
185 $this->assertTrue($rule->is_finished(0, $attempt));
186
30ef7963
MP
187 $this->assertEquals($rule->end_time($attempt), 20000);
188 $this->assertFalse($rule->time_left_display($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
189 $this->assertEquals($rule->time_left_display($attempt, 19900), 100);
190 $this->assertEquals($rule->time_left_display($attempt, 20000), 0);
191 $this->assertEquals($rule->time_left_display($attempt, 20100), -100);
7fe8aac1 192 }
b640e681
TH
193
194 public function test_close_date_with_overdue() {
195 $quiz = new stdClass();
196 $quiz->timeopen = 0;
197 $quiz->timeclose = 20000;
198 $quiz->overduehandling = 'graceperiod';
199 $quiz->graceperiod = 1000;
200 $quiz->questions = '';
201 $cm = new stdClass();
202 $cm->id = 0;
203 $quizobj = new quiz($quiz, $cm, null);
204 $attempt = new stdClass();
205 $attempt->preview = 0;
fdf26a0e 206
b640e681
TH
207 $rule = new quizaccess_openclosedate($quizobj, 20000);
208 $this->assertFalse($rule->prevent_access());
209
210 $rule = new quizaccess_openclosedate($quizobj, 20001);
211 $this->assertFalse($rule->prevent_access());
212
213 $rule = new quizaccess_openclosedate($quizobj, 21000);
214 $this->assertFalse($rule->prevent_access());
215
216 $rule = new quizaccess_openclosedate($quizobj, 21001);
217 $this->assertEquals($rule->prevent_access(),
218 get_string('notavailable', 'quizaccess_openclosedate'));
219 }
7fe8aac1 220}