forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetimeselector_test.php
204 lines (187 loc) · 6.38 KB
/
datetimeselector_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for datetimeselector form element
*
* This file contains unit test related to datetimeselector form element
*
* @package core_form
* @category phpunit
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/form/datetimeselector.php');
require_once($CFG->libdir.'/formslib.php');
/**
* Unit tests for MoodleQuickForm_date_time_selector
*
* Contains test cases for testing MoodleQuickForm_date_time_selector
*
* @package core_form
* @category phpunit
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_form_datetimeselector_testcase extends advanced_testcase {
/** @var MoodleQuickForm Keeps reference of dummy form object */
private $mform;
/** @var array test fixtures */
private $testvals;
/**
* Initalize test wide variable, it is called in start of the testcase
*/
protected function setUp() {
global $CFG;
parent::setUp();
$this->resetAfterTest();
$this->setAdminUser();
$this->setTimezone('Australia/Perth');
// Get form data.
$form = new temp_form_datetime();
$this->mform = $form->getform();
// Set test values.
$this->testvals = array(
array (
'minute' => 0,
'hour' => 0,
'day' => 1,
'month' => 7,
'year' => 2011,
'usertimezone' => 'America/Moncton',
'timezone' => 'America/Moncton',
'timestamp' => 1309489200
),
array (
'minute' => 0,
'hour' => 0,
'day' => 1,
'month' => 7,
'year' => 2011,
'usertimezone' => 'America/Moncton',
'timezone' => 99,
'timestamp' => 1309489200
),
array (
'minute' => 0,
'hour' => 23,
'day' => 30,
'month' => 6,
'year' => 2011,
'usertimezone' => 'America/Moncton',
'timezone' => -4,
'timestamp' => 1309489200
),
array (
'minute' => 0,
'hour' => 23,
'day' => 30,
'month' => 6,
'year' => 2011,
'usertimezone' => -4,
'timezone' => 99,
'timestamp' => 1309489200
),
array (
'minute' => 0,
'hour' => 0,
'day' => 1,
'month' => 7,
'year' => 2011,
'usertimezone' => 0.0,
'timezone' => 0.0,
'timestamp' => 1309478400 // 6am at UTC+0
),
array (
'minute' => 0,
'hour' => 0,
'day' => 1,
'month' => 7,
'year' => 2011,
'usertimezone' => 0.0,
'timezone' => 99,
'timestamp' => 1309478400 // 6am at UTC+0
)
);
}
/**
* Testcase to check exportvalue
*/
public function test_exportvalue() {
global $USER;
$testvals = $this->testvals;
foreach ($testvals as $vals) {
// Set user timezone to test value.
$USER->timezone = $vals['usertimezone'];
// Create dateselector element with different timezones.
$elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
$el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams);
$el->_createElements();
$submitvalues = array('dateselector' => $vals);
$this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues),
"Please check if timezones are updated (Site adminstration -> location -> update timezone)");
}
}
/**
* Testcase to check onQuickformEvent
*/
public function test_onquickformevent() {
global $USER;
$testvals = $this->testvals;
// Get dummy form for data.
$mform = $this->mform;
foreach ($testvals as $vals) {
// Set user timezone to test value.
$USER->timezone = $vals['usertimezone'];
// Create dateselector element with different timezones.
$elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
$el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams);
$el->_createElements();
$expectedvalues = array(
'day' => array($vals['day']),
'month' => array($vals['month']),
'year' => array($vals['year']),
'hour' => array($vals['hour']),
'minute' => array($vals['minute'])
);
$mform->_submitValues = array('dateselector' => $vals['timestamp']);
$el->onQuickFormEvent('updateValue', null, $mform);
$this->assertSame($expectedvalues, $el->getValue());
}
}
}
/**
* Form object to be used in test case
*/
class temp_form_datetime extends moodleform {
/**
* Form definition.
*/
public function definition() {
// No definition required.
}
/**
* Returns form reference.
* @return MoodleQuickForm
*/
public function getform() {
$mform = $this->_form;
// set submitted flag, to simulate submission
$mform->_flagSubmitted = true;
return $mform;
}
}