[ 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 * This file contains the class that handles testing the calendar type system. 19 * 20 * @package core_calendar 21 * @copyright 2013 Mark Nelson <[email protected]> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 29 // The test calendar type. 30 require_once($CFG->dirroot . '/calendar/tests/calendartype_test_example.php'); 31 32 // Used to test the dateselector elements. 33 require_once($CFG->libdir . '/form/dateselector.php'); 34 require_once($CFG->libdir . '/form/datetimeselector.php'); 35 36 // Used to test the calendar/lib.php functions. 37 require_once($CFG->dirroot . '/calendar/lib.php'); 38 39 // Used to test the user datetime profile field. 40 require_once($CFG->dirroot . '/user/profile/lib.php'); 41 require_once($CFG->dirroot . '/user/profile/definelib.php'); 42 require_once($CFG->dirroot . '/user/profile/index_field_form.php'); 43 44 /** 45 * Unit tests for the calendar type system. 46 * 47 * @package core_calendar 48 * @copyright 2013 Mark Nelson <[email protected]> 49 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 50 * @since Moodle 2.6 51 */ 52 class core_calendar_type_testcase extends advanced_testcase { 53 54 /** 55 * The test user. 56 */ 57 private $user; 58 59 /** 60 * Test set up. 61 */ 62 protected function setUp() { 63 // The user we are going to test this on. 64 $this->user = self::getDataGenerator()->create_user(); 65 self::setUser($this->user); 66 } 67 68 /** 69 * Test that setting the calendar type works. 70 */ 71 public function test_calendar_type_set() { 72 // We want to reset the test data after this run. 73 $this->resetAfterTest(); 74 75 // Test setting it as the 'Test' calendar type. 76 $this->set_calendar_type('test_example'); 77 $this->assertEquals('test_example', \core_calendar\type_factory::get_calendar_type()); 78 79 // Test setting it as the 'Gregorian' calendar type. 80 $this->set_calendar_type('gregorian'); 81 $this->assertEquals('gregorian', \core_calendar\type_factory::get_calendar_type()); 82 } 83 84 /** 85 * Test that calling core Moodle functions responsible for displaying the date 86 * have the same results as directly calling the same function in the calendar type. 87 */ 88 public function test_calendar_type_core_functions() { 89 // We want to reset the test data after this run. 90 $this->resetAfterTest(); 91 92 // Test that the core functions reproduce the same results as the Gregorian calendar. 93 $this->core_functions_test('gregorian'); 94 95 // Test that the core functions reproduce the same results as the test calendar. 96 $this->core_functions_test('test_example'); 97 } 98 99 /** 100 * Test that dates selected using the date selector elements are being saved as unixtime, and that the 101 * unixtime is being converted back to a valid date to display in the date selector elements for 102 * different calendar types. 103 */ 104 public function test_calendar_type_dateselector_elements() { 105 // We want to reset the test data after this run. 106 $this->resetAfterTest(); 107 108 // Check converting dates to Gregorian when submitting a date selector element works. Note: the test 109 // calendar is 2 years, 2 months, 2 days, 2 hours and 2 minutes ahead of the Gregorian calendar. 110 $date1 = array(); 111 $date1['day'] = 4; 112 $date1['month'] = 7; 113 $date1['year'] = 2013; 114 $date1['hour'] = 0; 115 $date1['minute'] = 0; 116 $date1['timestamp'] = 1372896000; 117 $this->convert_dateselector_to_unixtime_test('dateselector', 'gregorian', $date1); 118 119 $date2 = array(); 120 $date2['day'] = 7; 121 $date2['month'] = 9; 122 $date2['year'] = 2015; 123 $date2['hour'] = 0; // The dateselector element does not have hours. 124 $date2['minute'] = 0; // The dateselector element does not have minutes. 125 $date2['timestamp'] = 1372896000; 126 $this->convert_dateselector_to_unixtime_test('dateselector', 'test_example', $date2); 127 128 $date3 = array(); 129 $date3['day'] = 4; 130 $date3['month'] = 7; 131 $date3['year'] = 2013; 132 $date3['hour'] = 23; 133 $date3['minute'] = 15; 134 $date3['timestamp'] = 1372979700; 135 $this->convert_dateselector_to_unixtime_test('datetimeselector', 'gregorian', $date3); 136 137 $date4 = array(); 138 $date4['day'] = 7; 139 $date4['month'] = 9; 140 $date4['year'] = 2015; 141 $date4['hour'] = 1; 142 $date4['minute'] = 17; 143 $date4['timestamp'] = 1372979700; 144 $this->convert_dateselector_to_unixtime_test('datetimeselector', 'test_example', $date4); 145 146 // The date selector element values are set by using the function usergetdate, here we want to check that 147 // the unixtime passed is being successfully converted to the correct values for the calendar type. 148 $this->convert_unixtime_to_dateselector_test('gregorian', $date3); 149 $this->convert_unixtime_to_dateselector_test('test_example', $date4); 150 } 151 152 /** 153 * Test that the user profile field datetime minimum and maximum year settings are saved as the 154 * equivalent Gregorian years. 155 */ 156 public function test_calendar_type_datetime_field_submission() { 157 // We want to reset the test data after this run. 158 $this->resetAfterTest(); 159 160 // Create an array with the input values and expected values once submitted. 161 $date = array(); 162 $date['inputminyear'] = '1970'; 163 $date['inputmaxyear'] = '2013'; 164 $date['expectedminyear'] = '1970'; 165 $date['expectedmaxyear'] = '2013'; 166 $this->datetime_field_submission_test('gregorian', $date); 167 168 // The test calendar is 2 years, 2 months, 2 days in the future, so when the year 1970 is submitted, 169 // the year 1967 should be saved in the DB, as 1/1/1970 converts to 30/10/1967 in Gregorian. 170 $date['expectedminyear'] = '1967'; 171 $date['expectedmaxyear'] = '2010'; 172 $this->datetime_field_submission_test('test_example', $date); 173 } 174 175 /** 176 * Test all the core functions that use the calendar type system. 177 * 178 * @param string $type the calendar type we want to test 179 */ 180 private function core_functions_test($type) { 181 $this->set_calendar_type($type); 182 183 // Get the calendar. 184 $calendar = \core_calendar\type_factory::get_calendar_instance(); 185 186 // Test the userdate function. 187 $this->assertEquals($calendar->timestamp_to_date_string($this->user->timecreated, '', 99, true, true), 188 userdate($this->user->timecreated)); 189 190 // Test the calendar/lib.php functions. 191 $this->assertEquals($calendar->get_weekdays(), calendar_get_days()); 192 $this->assertEquals($calendar->get_starting_weekday(), calendar_get_starting_weekday()); 193 $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), calendar_days_in_month('9', '1986')); 194 $this->assertEquals($calendar->get_next_month('1986', '9'), calendar_add_month('9', '1986')); 195 $this->assertEquals($calendar->get_prev_month('1986', '9'), calendar_sub_month('9', '1986')); 196 197 // Test the lib/moodle.php functions. 198 $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), days_in_month('9', '1986')); 199 $this->assertEquals($calendar->get_weekday('1986', '9', '16'), dayofweek('16', '9', '1986')); 200 } 201 202 /** 203 * Simulates submitting a form with a date selector element and tests that the chosen dates 204 * are converted into unixtime before being saved in DB. 205 * 206 * @param string $element the form element we are testing 207 * @param string $type the calendar type we want to test 208 * @param array $date the date variables 209 */ 210 private function convert_dateselector_to_unixtime_test($element, $type, $date) { 211 $this->set_calendar_type($type); 212 213 if ($element == 'dateselector') { 214 $el = new MoodleQuickForm_date_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1)); 215 } else { 216 $el = new MoodleQuickForm_date_time_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1)); 217 } 218 $el->_createElements(); 219 $submitvalues = array('dateselector' => $date); 220 221 $this->assertSame($el->exportValue($submitvalues), array('dateselector' => $date['timestamp'])); 222 } 223 224 /** 225 * Test converting dates from unixtime to a date for the calendar type specified. 226 * 227 * @param string $type the calendar type we want to test 228 * @param array $date the date variables 229 */ 230 private function convert_unixtime_to_dateselector_test($type, $date) { 231 $this->set_calendar_type($type); 232 233 // Get the calendar. 234 $calendar = \core_calendar\type_factory::get_calendar_instance(); 235 236 $usergetdate = $calendar->timestamp_to_date_array($date['timestamp'], 0.0); 237 $comparedate = array( 238 'minute' => $usergetdate['minutes'], 239 'hour' => $usergetdate['hours'], 240 'day' => $usergetdate['mday'], 241 'month' => $usergetdate['mon'], 242 'year' => $usergetdate['year'], 243 'timestamp' => $date['timestamp'] 244 ); 245 246 $this->assertEquals($comparedate, $date); 247 } 248 249 /** 250 * Test saving the minimum and max year settings for the user datetime field. 251 * 252 * @param string $type the calendar type we want to test 253 * @param array $date the date variables 254 */ 255 private function datetime_field_submission_test($type, $date) { 256 $this->set_calendar_type($type); 257 258 // Get the data we are submitting for the form. 259 $formdata = array(); 260 $formdata['id'] = 0; 261 $formdata['shortname'] = 'Shortname'; 262 $formdata['name'] = 'Name'; 263 $formdata['param1'] = $date['inputminyear']; 264 $formdata['param2'] = $date['inputmaxyear']; 265 266 // Mock submitting this. 267 field_form::mock_submit($formdata); 268 269 // Create the user datetime form. 270 $form = new field_form(null, 'datetime'); 271 272 // Get the data from the submission. 273 $submissiondata = $form->get_data(); 274 // On the user profile field page after get_data, the function define_save is called 275 // in the field base class, which then calls the field's function define_save_preprocess. 276 $field = new profile_define_datetime(); 277 $submissiondata = $field->define_save_preprocess($submissiondata); 278 279 // Create an array we want to compare with the date passed. 280 $comparedate = $date; 281 $comparedate['expectedminyear'] = $submissiondata->param1; 282 $comparedate['expectedmaxyear'] = $submissiondata->param2; 283 284 $this->assertEquals($comparedate, $date); 285 } 286 287 /** 288 * Set the calendar type for this user. 289 * 290 * @param string $type the calendar type we want to set 291 */ 292 private function set_calendar_type($type) { 293 $this->user->calendartype = $type; 294 \core\session\manager::set_user($this->user); 295 } 296 }
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 |