Merge branch 'MDL-75012-39-5' of https://github.com/andrewnicols/moodle into MOODLE_3...
[moodle.git] / lib / form / tests / dateselector_test.php
blob12934141c6894e7eaa5df4fba87ee8ddcc8a83d5
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.
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 dateselector form element
20 * This file contains unit test related to dateselector form element
22 * @package core_form
23 * @category phpunit
24 * @copyright 2012 Rajesh Taneja
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once($CFG->libdir . '/form/dateselector.php');
32 require_once($CFG->libdir.'/formslib.php');
34 /**
35 * Unit tests for MoodleQuickForm_date_selector
37 * Contains test cases for testing MoodleQuickForm_date_selector
39 * @package core_form
40 * @category phpunit
41 * @copyright 2012 Rajesh Taneja
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class core_form_dateselector_testcase extends advanced_testcase {
45 /** @var MoodleQuickForm Keeps reference of dummy form object */
46 private $mform;
47 /** @var array test fixtures */
48 private $testvals;
50 /**
51 * Initalize test wide variable, it is called in start of the testcase
53 protected function setUp() {
54 global $CFG;
55 parent::setUp();
57 $this->resetAfterTest();
58 $this->setAdminUser();
60 $this->setTimezone('Australia/Perth');
62 // Get form data.
63 $form = new temp_form_date();
64 $this->mform = $form->getform();
66 // Set test values.
67 $this->testvals = array(
68 array (
69 'day' => 1,
70 'month' => 7,
71 'year' => 2011,
72 'usertimezone' => 'America/Moncton',
73 'timezone' => 'America/Moncton',
74 'timestamp' => 1309489200
76 array (
77 'day' => 1,
78 'month' => 7,
79 'year' => 2011,
80 'usertimezone' => 'America/Moncton',
81 'timezone' => 99,
82 'timestamp' => 1309489200
84 array (
85 'day' => 30,
86 'month' => 6,
87 'year' => 2011,
88 'usertimezone' => 'America/Moncton',
89 'timezone' => -4,
90 'timestamp' => 1309406400
92 array (
93 'day' => 30,
94 'month' => 6,
95 'year' => 2011,
96 'usertimezone' => -4,
97 'timezone' => 99,
98 'timestamp' => 1309406400
100 array (
101 'day' => 1,
102 'month' => 7,
103 'year' => 2011,
104 'usertimezone' => 0.0,
105 'timezone' => 0.0,
106 'timestamp' => 1309478400 // 6am at UTC+0
108 array (
109 'day' => 1,
110 'month' => 7,
111 'year' => 2011,
112 'usertimezone' => 0.0,
113 'timezone' => 99,
114 'timestamp' => 1309478400 // 6am at UTC+0
120 * Testcase to check exportvalue
122 public function test_exportvalue() {
123 global $USER;
124 $testvals = $this->testvals;
126 foreach ($testvals as $vals) {
127 // Set user timezone to test value.
128 $USER->timezone = $vals['usertimezone'];
130 // Create dateselector element with different timezones.
131 $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
132 $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
133 $this->assertTrue($el instanceof MoodleQuickForm_date_selector);
134 $submitvalues = array('dateselector' => $vals);
136 $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues, true),
137 "Please check if timezones are updated (Site adminstration -> location -> update timezone)");
142 * Testcase to check onQuickformEvent
144 public function test_onquickformevent() {
145 global $USER;
146 $testvals = $this->testvals;
147 // Get dummy form for data.
148 $mform = $this->mform;
150 foreach ($testvals as $vals) {
151 // Set user timezone to test value.
152 $USER->timezone = $vals['usertimezone'];
154 // Create dateselector element with different timezones.
155 $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
156 $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
157 $this->assertTrue($el instanceof MoodleQuickForm_date_selector);
158 $expectedvalues = array(
159 'day' => array($vals['day']),
160 'month' => array($vals['month']),
161 'year' => array($vals['year'])
163 $mform->_submitValues = array('dateselector' => $vals['timestamp']);
164 $el->onQuickFormEvent('updateValue', null, $mform);
165 $this->assertSame($expectedvalues, $el->getValue());
171 * Form object to be used in test case.
173 class temp_form_date extends moodleform {
175 * Form definition.
177 public function definition() {
178 // No definition required.
181 * Returns form reference
182 * @return MoodleQuickForm
184 public function getform() {
185 $mform = $this->_form;
186 // set submitted flag, to simulate submission
187 $mform->_flagSubmitted = true;
188 return $mform;