MDL-37894 Amend tests with new openclose rule.
[moodle.git] / mod / quiz / accessrule / openclosedate / tests / rule_test.php
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/>.
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  */
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once($CFG->dirroot . '/mod/quiz/accessrule/openclosedate/rule.php');
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  */
40 class quizaccess_openclosedate_testcase extends basic_testcase {
41     public function test_no_dates() {
42         $quiz = new stdClass();
43         $quiz->timeopen = 0;
44         $quiz->timeclose = 0;
45         $quiz->overduehandling = 'autosubmit';
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;
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->end_time($attempt));
59         $this->assertFalse($rule->time_left_display($attempt, 10000));
60         $this->assertFalse($rule->time_left_display($attempt, 0));
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));
67         $this->assertFalse($rule->end_time($attempt));
68         $this->assertFalse($rule->time_left_display($attempt, 0));
69     }
71     public function test_start_date() {
72         $quiz = new stdClass();
73         $quiz->timeopen = 10000;
74         $quiz->timeclose = 0;
75         $quiz->overduehandling = 'autosubmit';
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;
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));
90         $this->assertFalse($rule->end_time($attempt));
91         $this->assertFalse($rule->time_left_display($attempt, 0));
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));
99         $this->assertFalse($rule->end_time($attempt));
100         $this->assertFalse($rule->time_left_display($attempt, 0));
101     }
103     public function test_close_date() {
104         $quiz = new stdClass();
105         $quiz->timeopen = 0;
106         $quiz->timeclose = 20000;
107         $quiz->overduehandling = 'autosubmit';
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;
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));
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);
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));
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);
140     }
142     public function test_both_dates() {
143         $quiz = new stdClass();
144         $quiz->timeopen = 10000;
145         $quiz->timeclose = 20000;
146         $quiz->overduehandling = 'autosubmit';
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;
154         $rule = new quizaccess_openclosedate($quizobj, 9999);
155         $this->assertEquals($rule->description(),
156             array(get_string('quiznotavailable', 'quizaccess_openclosedate', userdate(10000)),
157                     get_string('quizcloseson', 'quiz', userdate(20000))));
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));
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));
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));
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));
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);
192     }
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;
207         $rule = new quizaccess_openclosedate($quizobj, 20000);
208         $this->assertFalse($rule->prevent_access());
210         $rule = new quizaccess_openclosedate($quizobj, 20001);
211         $this->assertFalse($rule->prevent_access());
213         $rule = new quizaccess_openclosedate($quizobj, 21000);
214         $this->assertFalse($rule->prevent_access());
216         $rule = new quizaccess_openclosedate($quizobj, 21001);
217         $this->assertEquals($rule->prevent_access(),
218                 get_string('notavailable', 'quizaccess_openclosedate'));
219     }