MDL-32149 add quiz unit tests
[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;
45 $quiz->questions = '';
46 $cm = new stdClass();
47 $cm->id = 0;
48 $quizobj = new quiz($quiz, $cm, null);
49 $attempt = new stdClass();
50 $attempt->preview = 0;
51
52 $rule = new quizaccess_openclosedate($quizobj, 10000);
53 $this->assertEmpty($rule->description());
54 $this->assertFalse($rule->prevent_access());
55 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
56 $this->assertFalse($rule->is_finished(0, $attempt));
57 $this->assertFalse($rule->time_left($attempt, 10000));
58 $this->assertFalse($rule->time_left($attempt, 0));
59
60 $rule = new quizaccess_openclosedate($quizobj, 0);
61 $this->assertEmpty($rule->description());
62 $this->assertFalse($rule->prevent_access());
63 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
64 $this->assertFalse($rule->is_finished(0, $attempt));
65 $this->assertFalse($rule->time_left($attempt, 0));
66 }
67
68 public function test_start_date() {
69 $quiz = new stdClass();
70 $quiz->timeopen = 10000;
71 $quiz->timeclose = 0;
72 $quiz->questions = '';
73 $cm = new stdClass();
74 $cm->id = 0;
75 $quizobj = new quiz($quiz, $cm, null);
76 $attempt = new stdClass();
77 $attempt->preview = 0;
78
79 $rule = new quizaccess_openclosedate($quizobj, 9999);
80 $this->assertEquals($rule->description(),
81 array(get_string('quiznotavailable', 'quizaccess_openclosedate', userdate(10000))));
82 $this->assertEquals($rule->prevent_access(),
83 get_string('notavailable', 'quizaccess_openclosedate'));
84 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
85 $this->assertFalse($rule->is_finished(0, $attempt));
86 $this->assertFalse($rule->time_left($attempt, 0));
87
88 $rule = new quizaccess_openclosedate($quizobj, 10000);
89 $this->assertEquals($rule->description(),
90 array(get_string('quizopenedon', 'quiz', userdate(10000))));
91 $this->assertFalse($rule->prevent_access());
92 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
93 $this->assertFalse($rule->is_finished(0, $attempt));
94 $this->assertFalse($rule->time_left($attempt, 0));
95 }
96
97 public function test_close_date() {
98 $quiz = new stdClass();
99 $quiz->timeopen = 0;
100 $quiz->timeclose = 20000;
101 $quiz->questions = '';
102 $cm = new stdClass();
103 $cm->id = 0;
104 $quizobj = new quiz($quiz, $cm, null);
105 $attempt = new stdClass();
106 $attempt->preview = 0;
107
108 $rule = new quizaccess_openclosedate($quizobj, 20000);
109 $this->assertEquals($rule->description(),
110 array(get_string('quizcloseson', 'quiz', userdate(20000))));
111 $this->assertFalse($rule->prevent_access());
112 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
113 $this->assertFalse($rule->is_finished(0, $attempt));
114 $this->assertFalse($rule->time_left($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
115 $this->assertEquals($rule->time_left($attempt, 19900), 100);
116 $this->assertEquals($rule->time_left($attempt, 20000), 0);
117 $this->assertEquals($rule->time_left($attempt, 20100), -100);
118
119 $rule = new quizaccess_openclosedate($quizobj, 20001);
120 $this->assertEquals($rule->description(),
121 array(get_string('quizclosed', 'quiz', userdate(20000))));
122 $this->assertEquals($rule->prevent_access(),
123 get_string('notavailable', 'quizaccess_openclosedate'));
124 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
125 $this->assertTrue($rule->is_finished(0, $attempt));
126 $this->assertFalse($rule->time_left($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
127 $this->assertEquals($rule->time_left($attempt, 19900), 100);
128 $this->assertEquals($rule->time_left($attempt, 20000), 0);
129 $this->assertEquals($rule->time_left($attempt, 20100), -100);
130 }
131
132 public function test_both_dates() {
133 $quiz = new stdClass();
134 $quiz->timeopen = 10000;
135 $quiz->timeclose = 20000;
136 $quiz->questions = '';
137 $cm = new stdClass();
138 $cm->id = 0;
139 $quizobj = new quiz($quiz, $cm, null);
140 $attempt = new stdClass();
141 $attempt->preview = 0;
142
143 $rule = new quizaccess_openclosedate($quizobj, 9999);
144 $this->assertEquals($rule->description(),
145 array(get_string('quiznotavailable', 'quizaccess_openclosedate', userdate(10000))));
146 $this->assertEquals($rule->prevent_access(),
147 get_string('notavailable', 'quizaccess_openclosedate'));
148 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
149 $this->assertFalse($rule->is_finished(0, $attempt));
150
151 $rule = new quizaccess_openclosedate($quizobj, 10000);
152 $this->assertEquals($rule->description(),
153 array(get_string('quizopenedon', 'quiz', userdate(10000)),
154 get_string('quizcloseson', 'quiz', userdate(20000))));
155 $this->assertFalse($rule->prevent_access());
156 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
157 $this->assertFalse($rule->is_finished(0, $attempt));
158
159 $rule = new quizaccess_openclosedate($quizobj, 20000);
160 $this->assertEquals($rule->description(),
161 array(get_string('quizopenedon', 'quiz', userdate(10000)),
162 get_string('quizcloseson', 'quiz', userdate(20000))));
163 $this->assertFalse($rule->prevent_access());
164 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
165 $this->assertFalse($rule->is_finished(0, $attempt));
166
167 $rule = new quizaccess_openclosedate($quizobj, 20001);
168 $this->assertEquals($rule->description(),
169 array(get_string('quizclosed', 'quiz', userdate(20000))));
170 $this->assertEquals($rule->prevent_access(),
171 get_string('notavailable', 'quizaccess_openclosedate'));
172 $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
173 $this->assertTrue($rule->is_finished(0, $attempt));
174
175 $this->assertFalse($rule->time_left($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
176 $this->assertEquals($rule->time_left($attempt, 19900), 100);
177 $this->assertEquals($rule->time_left($attempt, 20000), 0);
178 $this->assertEquals($rule->time_left($attempt, 20100), -100);
179 }
180}