[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 datetimeselector form element 19 * 20 * This file contains unit test related to datetimeselector form element 21 * 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 26 */ 27 28 defined('MOODLE_INTERNAL') || die(); 29 30 global $CFG; 31 require_once($CFG->libdir . '/form/datetimeselector.php'); 32 require_once($CFG->libdir.'/formslib.php'); 33 34 /** 35 * Unit tests for MoodleQuickForm_date_time_selector 36 * 37 * Contains test cases for testing MoodleQuickForm_date_time_selector 38 * 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 43 */ 44 class core_form_datetimeselector_testcase extends basic_testcase { 45 /** @var MoodleQuickForm Keeps reference of dummy form object */ 46 private $mform; 47 /** @var stdClass saves current user data */ 48 private $olduser; 49 /** @var int|float|string saves forcetimezone config variable */ 50 private $cfgforcetimezone; 51 /** @var int|float|string saves current user timezone */ 52 private $userstimezone; 53 /** @var string saves system locale */ 54 private $oldlocale; 55 /** @var string saves system timezone */ 56 private $systemdefaulttimezone; 57 /** @var array test fixtures */ 58 private $testvals; 59 60 /** 61 * Initalize test wide variable, it is called in start of the testcase 62 */ 63 protected function setUp() { 64 parent::setUp(); 65 // Get form data. 66 $form = new temp_form_datetime(); 67 $this->mform = $form->getform(); 68 69 // Set test values. 70 $this->testvals = array( 71 array ( 72 'minute' => 0, 73 'hour' => 0, 74 'day' => 1, 75 'month' => 7, 76 'year' => 2011, 77 'usertimezone' => 'America/Moncton', 78 'timezone' => 'America/Moncton', 79 'timestamp' => 1309489200 80 ), 81 array ( 82 'minute' => 0, 83 'hour' => 0, 84 'day' => 1, 85 'month' => 7, 86 'year' => 2011, 87 'usertimezone' => 'America/Moncton', 88 'timezone' => 99, 89 'timestamp' => 1309489200 90 ), 91 array ( 92 'minute' => 0, 93 'hour' => 23, 94 'day' => 30, 95 'month' => 6, 96 'year' => 2011, 97 'usertimezone' => 'America/Moncton', 98 'timezone' => -4, 99 'timestamp' => 1309489200 100 ), 101 array ( 102 'minute' => 0, 103 'hour' => 23, 104 'day' => 30, 105 'month' => 6, 106 'year' => 2011, 107 'usertimezone' => -4, 108 'timezone' => 99, 109 'timestamp' => 1309489200 110 ), 111 array ( 112 'minute' => 0, 113 'hour' => 0, 114 'day' => 1, 115 'month' => 7, 116 'year' => 2011, 117 'usertimezone' => 0.0, 118 'timezone' => 0.0, 119 'timestamp' => 1309478400 // 6am at UTC+0 120 ), 121 array ( 122 'minute' => 0, 123 'hour' => 0, 124 'day' => 1, 125 'month' => 7, 126 'year' => 2011, 127 'usertimezone' => 0.0, 128 'timezone' => 99, 129 'timestamp' => 1309478400 // 6am at UTC+0 130 ) 131 ); 132 } 133 134 /** 135 * Clears the data set in the setUp() method call. 136 * @see datetimeselector_form_element_testcase::setUp() 137 */ 138 protected function tearDown() { 139 unset($this->testvals); 140 parent::tearDown(); 141 } 142 143 /** 144 * Testcase to check exportvalue 145 */ 146 public function test_exportvalue() { 147 global $USER; 148 $testvals = $this->testvals; 149 150 // Set timezone to Australia/Perth for testing. 151 $this->settimezone(); 152 153 foreach ($testvals as $vals) { 154 // Set user timezone to test value. 155 $USER->timezone = $vals['usertimezone']; 156 157 // Create dateselector element with different timezones. 158 $elparams = array('optional'=>false, 'timezone' => $vals['timezone']); 159 $el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams); 160 $el->_createElements(); 161 $submitvalues = array('dateselector' => $vals); 162 163 $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues), 164 "Please check if timezones are updated (Site adminstration -> location -> update timezone)"); 165 } 166 167 // Restore user original timezone. 168 $this->restoretimezone(); 169 } 170 171 /** 172 * Testcase to check onQuickformEvent 173 */ 174 public function test_onquickformevent() { 175 global $USER; 176 $testvals = $this->testvals; 177 // Get dummy form for data. 178 $mform = $this->mform; 179 // Set timezone to Australia/Perth for testing. 180 $this->settimezone(); 181 foreach ($testvals as $vals) { 182 // Set user timezone to test value. 183 $USER->timezone = $vals['usertimezone']; 184 185 // Create dateselector element with different timezones. 186 $elparams = array('optional'=>false, 'timezone' => $vals['timezone']); 187 $el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams); 188 $el->_createElements(); 189 $expectedvalues = array( 190 'day' => array($vals['day']), 191 'month' => array($vals['month']), 192 'year' => array($vals['year']), 193 'hour' => array($vals['hour']), 194 'minute' => array($vals['minute']) 195 ); 196 $mform->_submitValues = array('dateselector' => $vals['timestamp']); 197 $el->onQuickFormEvent('updateValue', null, $mform); 198 $this->assertSame($expectedvalues, $el->getValue()); 199 } 200 201 // Restore user original timezone. 202 $this->restoretimezone(); 203 } 204 205 /** 206 * Set user timezone to Australia/Perth for testing 207 */ 208 private function settimezone() { 209 global $USER, $CFG, $DB; 210 $this->olduser = $USER; 211 $USER = $DB->get_record('user', array('id'=>2)); //admin 212 213 // Check if forcetimezone is set then save it and set it to use user timezone. 214 $this->cfgforcetimezone = null; 215 if (isset($CFG->forcetimezone)) { 216 $this->cfgforcetimezone = $CFG->forcetimezone; 217 $CFG->forcetimezone = 99; //get user default timezone. 218 } 219 220 // Store user default timezone to restore later. 221 $this->userstimezone = $USER->timezone; 222 223 // The string version of date comes from server locale setting and does 224 // not respect user language, so it is necessary to reset that. 225 $this->oldlocale = setlocale(LC_TIME, '0'); 226 setlocale(LC_TIME, 'en_AU.UTF-8'); 227 228 // Set default timezone to Australia/Perth, else time calculated 229 // will not match expected values. Before that save system defaults. 230 $this->systemdefaulttimezone = date_default_timezone_get(); 231 date_default_timezone_set('Australia/Perth'); 232 } 233 234 /** 235 * Restore user timezone to original state 236 */ 237 private function restoretimezone() { 238 global $USER, $CFG; 239 // Restore user timezone back to what it was. 240 $USER->timezone = $this->userstimezone; 241 242 // Restore forcetimezone. 243 if (!is_null($this->cfgforcetimezone)) { 244 $CFG->forcetimezone = $this->cfgforcetimezone; 245 } 246 247 // Restore system default values. 248 date_default_timezone_set($this->systemdefaulttimezone); 249 setlocale(LC_TIME, $this->oldlocale); 250 251 $USER = $this->olduser; 252 } 253 } 254 255 /** 256 * Form object to be used in test case 257 */ 258 class temp_form_datetime extends moodleform { 259 /** 260 * Form definition. 261 */ 262 public function definition() { 263 // No definition required. 264 } 265 /** 266 * Returns form reference. 267 * @return MoodleQuickForm 268 */ 269 public function getform() { 270 $mform = $this->_form; 271 // set submitted flag, to simulate submission 272 $mform->_flagSubmitted = true; 273 return $mform; 274 } 275 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |