weekly release 2.3.3+
[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));
58 $this->assertFalse($rule->time_left($attempt, 10000));
59 $this->assertFalse($rule->time_left($attempt, 0));
60
61 $rule = new quizaccess_openclosedate($quizobj, 0);
62 $this->assertEmpty($rule->description());
63 $this->assertFalse($rule->prevent_access());
64 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
65 $this->assertFalse($rule->is_finished(0, $attempt));
66 $this->assertFalse($rule->time_left($attempt, 0));
67 }
68
69 public function test_start_date() {
70 $quiz = new stdClass();
71 $quiz->timeopen = 10000;
72 $quiz->timeclose = 0;
b640e681 73 $quiz->overduehandling = 'autosubmit';
7fe8aac1
PS
74 $quiz->questions = '';
75 $cm = new stdClass();
76 $cm->id = 0;
77 $quizobj = new quiz($quiz, $cm, null);
78 $attempt = new stdClass();
79 $attempt->preview = 0;
80
81 $rule = new quizaccess_openclosedate($quizobj, 9999);
82 $this->assertEquals($rule->description(),
83 array(get_string('quiznotavailable', 'quizaccess_openclosedate', userdate(10000))));
84 $this->assertEquals($rule->prevent_access(),
85 get_string('notavailable', 'quizaccess_openclosedate'));
86 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
87 $this->assertFalse($rule->is_finished(0, $attempt));
88 $this->assertFalse($rule->time_left($attempt, 0));
89
90 $rule = new quizaccess_openclosedate($quizobj, 10000);
91 $this->assertEquals($rule->description(),
92 array(get_string('quizopenedon', 'quiz', userdate(10000))));
93 $this->assertFalse($rule->prevent_access());
94 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
95 $this->assertFalse($rule->is_finished(0, $attempt));
96 $this->assertFalse($rule->time_left($attempt, 0));
97 }
98
99 public function test_close_date() {
100 $quiz = new stdClass();
101 $quiz->timeopen = 0;
102 $quiz->timeclose = 20000;
b640e681 103 $quiz->overduehandling = 'autosubmit';
7fe8aac1
PS
104 $quiz->questions = '';
105 $cm = new stdClass();
106 $cm->id = 0;
107 $quizobj = new quiz($quiz, $cm, null);
108 $attempt = new stdClass();
109 $attempt->preview = 0;
110
111 $rule = new quizaccess_openclosedate($quizobj, 20000);
112 $this->assertEquals($rule->description(),
113 array(get_string('quizcloseson', 'quiz', userdate(20000))));
114 $this->assertFalse($rule->prevent_access());
115 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
116 $this->assertFalse($rule->is_finished(0, $attempt));
117 $this->assertFalse($rule->time_left($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
118 $this->assertEquals($rule->time_left($attempt, 19900), 100);
119 $this->assertEquals($rule->time_left($attempt, 20000), 0);
120 $this->assertEquals($rule->time_left($attempt, 20100), -100);
121
122 $rule = new quizaccess_openclosedate($quizobj, 20001);
123 $this->assertEquals($rule->description(),
124 array(get_string('quizclosed', 'quiz', userdate(20000))));
125 $this->assertEquals($rule->prevent_access(),
126 get_string('notavailable', 'quizaccess_openclosedate'));
127 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
128 $this->assertTrue($rule->is_finished(0, $attempt));
129 $this->assertFalse($rule->time_left($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
130 $this->assertEquals($rule->time_left($attempt, 19900), 100);
131 $this->assertEquals($rule->time_left($attempt, 20000), 0);
132 $this->assertEquals($rule->time_left($attempt, 20100), -100);
133 }
134
135 public function test_both_dates() {
136 $quiz = new stdClass();
137 $quiz->timeopen = 10000;
138 $quiz->timeclose = 20000;
b640e681 139 $quiz->overduehandling = 'autosubmit';
7fe8aac1
PS
140 $quiz->questions = '';
141 $cm = new stdClass();
142 $cm->id = 0;
143 $quizobj = new quiz($quiz, $cm, null);
144 $attempt = new stdClass();
145 $attempt->preview = 0;
146
147 $rule = new quizaccess_openclosedate($quizobj, 9999);
148 $this->assertEquals($rule->description(),
149 array(get_string('quiznotavailable', 'quizaccess_openclosedate', userdate(10000))));
150 $this->assertEquals($rule->prevent_access(),
151 get_string('notavailable', 'quizaccess_openclosedate'));
152 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
153 $this->assertFalse($rule->is_finished(0, $attempt));
154
155 $rule = new quizaccess_openclosedate($quizobj, 10000);
156 $this->assertEquals($rule->description(),
157 array(get_string('quizopenedon', 'quiz', userdate(10000)),
158 get_string('quizcloseson', 'quiz', userdate(20000))));
159 $this->assertFalse($rule->prevent_access());
160 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
161 $this->assertFalse($rule->is_finished(0, $attempt));
162
163 $rule = new quizaccess_openclosedate($quizobj, 20000);
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, 20001);
172 $this->assertEquals($rule->description(),
173 array(get_string('quizclosed', 'quiz', userdate(20000))));
174 $this->assertEquals($rule->prevent_access(),
175 get_string('notavailable', 'quizaccess_openclosedate'));
176 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
177 $this->assertTrue($rule->is_finished(0, $attempt));
178
179 $this->assertFalse($rule->time_left($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
180 $this->assertEquals($rule->time_left($attempt, 19900), 100);
181 $this->assertEquals($rule->time_left($attempt, 20000), 0);
182 $this->assertEquals($rule->time_left($attempt, 20100), -100);
183 }
b640e681
TH
184
185 public function test_close_date_with_overdue() {
186 $quiz = new stdClass();
187 $quiz->timeopen = 0;
188 $quiz->timeclose = 20000;
189 $quiz->overduehandling = 'graceperiod';
190 $quiz->graceperiod = 1000;
191 $quiz->questions = '';
192 $cm = new stdClass();
193 $cm->id = 0;
194 $quizobj = new quiz($quiz, $cm, null);
195 $attempt = new stdClass();
196 $attempt->preview = 0;
fdf26a0e 197
b640e681
TH
198 $rule = new quizaccess_openclosedate($quizobj, 20000);
199 $this->assertFalse($rule->prevent_access());
200
201 $rule = new quizaccess_openclosedate($quizobj, 20001);
202 $this->assertFalse($rule->prevent_access());
203
204 $rule = new quizaccess_openclosedate($quizobj, 21000);
205 $this->assertFalse($rule->prevent_access());
206
207 $rule = new quizaccess_openclosedate($quizobj, 21001);
208 $this->assertEquals($rule->prevent_access(),
209 get_string('notavailable', 'quizaccess_openclosedate'));
210 }
7fe8aac1 211}