MDL-71051 core_user: create user profile field form is now modal form
[moodle.git] / calendar / tests / calendartype_test.php
blob4e6e5dfc08c18fc7dbe2256d9a06ddb2bd052dc2
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 * This file contains the class that handles testing the calendar type system.
20 * @package core_calendar
21 * @copyright 2013 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 // The test calendar type.
30 require_once($CFG->dirroot . '/calendar/tests/calendartype_test_example.php');
32 // Used to test the dateselector elements.
33 require_once($CFG->libdir . '/form/dateselector.php');
34 require_once($CFG->libdir . '/form/datetimeselector.php');
36 // Used to test the user datetime profile field.
37 require_once($CFG->dirroot . '/user/profile/lib.php');
38 require_once($CFG->dirroot . '/user/profile/definelib.php');
40 /**
41 * Unit tests for the calendar type system.
43 * @package core_calendar
44 * @copyright 2013 Mark Nelson <markn@moodle.com>
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 * @since Moodle 2.6
48 class core_calendar_type_testcase extends advanced_testcase {
49 /** @var MoodleQuickForm Keeps reference of dummy form object */
50 private $mform;
52 /**
53 * The test user.
55 private $user;
57 /**
58 * Test set up.
60 protected function setUp(): void {
61 // The user we are going to test this on.
62 $this->user = self::getDataGenerator()->create_user();
63 self::setUser($this->user);
65 // Get form data.
66 $form = new temp_form_calendartype();
67 $this->mform = $form->getform();
70 /**
71 * Test that setting the calendar type works.
73 public function test_calendar_type_set() {
74 // We want to reset the test data after this run.
75 $this->resetAfterTest();
77 // Test setting it as the 'Test' calendar type.
78 $this->set_calendar_type('test_example');
79 $this->assertEquals('test_example', \core_calendar\type_factory::get_calendar_type());
81 // Test setting it as the 'Gregorian' calendar type.
82 $this->set_calendar_type('gregorian');
83 $this->assertEquals('gregorian', \core_calendar\type_factory::get_calendar_type());
86 /**
87 * Test that calling core Moodle functions responsible for displaying the date
88 * have the same results as directly calling the same function in the calendar type.
90 public function test_calendar_type_core_functions() {
91 // We want to reset the test data after this run.
92 $this->resetAfterTest();
94 // Test that the core functions reproduce the same results as the Gregorian calendar.
95 $this->core_functions_test('gregorian');
97 // Test that the core functions reproduce the same results as the test calendar.
98 $this->core_functions_test('test_example');
102 * Test that dates selected using the date selector elements are being saved as unixtime, and that the
103 * unixtime is being converted back to a valid date to display in the date selector elements for
104 * different calendar types.
106 public function test_calendar_type_dateselector_elements() {
107 // We want to reset the test data after this run.
108 $this->resetAfterTest();
110 $this->setTimezone('UTC');
112 // Note: this test is pretty useless because it does not test current user timezones.
114 // Check converting dates to Gregorian when submitting a date selector element works. Note: the test
115 // calendar is 2 years, 2 months, 2 days, 2 hours and 2 minutes ahead of the Gregorian calendar.
116 $date1 = array();
117 $date1['day'] = 4;
118 $date1['month'] = 7;
119 $date1['year'] = 2013;
120 $date1['hour'] = 0;
121 $date1['minute'] = 0;
122 $date1['timestamp'] = 1372896000;
123 $this->convert_dateselector_to_unixtime_test('dateselector', 'gregorian', $date1);
125 $date2 = array();
126 $date2['day'] = 7;
127 $date2['month'] = 9;
128 $date2['year'] = 2015;
129 $date2['hour'] = 0; // The dateselector element does not have hours.
130 $date2['minute'] = 0; // The dateselector element does not have minutes.
131 $date2['timestamp'] = 1372896000;
132 $this->convert_dateselector_to_unixtime_test('dateselector', 'test_example', $date2);
134 $date3 = array();
135 $date3['day'] = 4;
136 $date3['month'] = 7;
137 $date3['year'] = 2013;
138 $date3['hour'] = 23;
139 $date3['minute'] = 15;
140 $date3['timestamp'] = 1372979700;
141 $this->convert_dateselector_to_unixtime_test('datetimeselector', 'gregorian', $date3);
143 $date4 = array();
144 $date4['day'] = 7;
145 $date4['month'] = 9;
146 $date4['year'] = 2015;
147 $date4['hour'] = 1;
148 $date4['minute'] = 17;
149 $date4['timestamp'] = 1372979700;
150 $this->convert_dateselector_to_unixtime_test('datetimeselector', 'test_example', $date4);
152 // The date selector element values are set by using the function usergetdate, here we want to check that
153 // the unixtime passed is being successfully converted to the correct values for the calendar type.
154 $this->convert_unixtime_to_dateselector_test('gregorian', $date3);
155 $this->convert_unixtime_to_dateselector_test('test_example', $date4);
159 * Test that the user profile field datetime minimum and maximum year settings are saved as the
160 * equivalent Gregorian years.
162 public function test_calendar_type_datetime_field_submission() {
163 // We want to reset the test data after this run.
164 $this->resetAfterTest();
166 // Create an array with the input values and expected values once submitted.
167 $date = array();
168 $date['inputminyear'] = '1970';
169 $date['inputmaxyear'] = '2013';
170 $date['expectedminyear'] = '1970';
171 $date['expectedmaxyear'] = '2013';
172 $this->datetime_field_submission_test('gregorian', $date);
174 // The test calendar is 2 years, 2 months, 2 days in the future, so when the year 1970 is submitted,
175 // the year 1967 should be saved in the DB, as 1/1/1970 converts to 30/10/1967 in Gregorian.
176 $date['expectedminyear'] = '1967';
177 $date['expectedmaxyear'] = '2010';
178 $this->datetime_field_submission_test('test_example', $date);
182 * Test all the core functions that use the calendar type system.
184 * @param string $type the calendar type we want to test
186 private function core_functions_test($type) {
187 $this->set_calendar_type($type);
189 // Get the calendar.
190 $calendar = \core_calendar\type_factory::get_calendar_instance();
192 // Test the userdate function.
193 $this->assertEquals($calendar->timestamp_to_date_string($this->user->timecreated, '', 99, true, true),
194 userdate($this->user->timecreated));
196 // Test the calendar/lib.php functions.
197 $this->assertEquals($calendar->get_weekdays(), calendar_get_days());
198 $this->assertEquals($calendar->get_starting_weekday(), calendar_get_starting_weekday());
199 $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), calendar_days_in_month('9', '1986'));
200 $this->assertEquals($calendar->get_next_month('1986', '9'), calendar_add_month('9', '1986'));
201 $this->assertEquals($calendar->get_prev_month('1986', '9'), calendar_sub_month('9', '1986'));
203 // Test the lib/moodle.php functions.
204 $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), days_in_month('9', '1986'));
205 $this->assertEquals($calendar->get_weekday('1986', '9', '16'), dayofweek('16', '9', '1986'));
209 * Simulates submitting a form with a date selector element and tests that the chosen dates
210 * are converted into unixtime before being saved in DB.
212 * @param string $element the form element we are testing
213 * @param string $type the calendar type we want to test
214 * @param array $date the date variables
216 private function convert_dateselector_to_unixtime_test($element, $type, $date) {
217 $this->set_calendar_type($type);
219 static $counter = 0;
220 $counter++;
222 if ($element == 'dateselector') {
223 $el = $this->mform->addElement('date_selector',
224 'dateselector' . $counter, null, array('timezone' => 0.0));
225 } else {
226 $el = $this->mform->addElement('date_time_selector',
227 'dateselector' . $counter, null, array('timezone' => 0.0, 'optional' => false));
229 $submitvalues = array('dateselector' . $counter => $date);
231 $this->assertSame(array('dateselector' . $counter => $date['timestamp']), $el->exportValue($submitvalues, true));
235 * Test converting dates from unixtime to a date for the calendar type specified.
237 * @param string $type the calendar type we want to test
238 * @param array $date the date variables
240 private function convert_unixtime_to_dateselector_test($type, $date) {
241 $this->set_calendar_type($type);
243 // Get the calendar.
244 $calendar = \core_calendar\type_factory::get_calendar_instance();
246 $usergetdate = $calendar->timestamp_to_date_array($date['timestamp'], 0.0);
247 $comparedate = array(
248 'minute' => $usergetdate['minutes'],
249 'hour' => $usergetdate['hours'],
250 'day' => $usergetdate['mday'],
251 'month' => $usergetdate['mon'],
252 'year' => $usergetdate['year'],
253 'timestamp' => $date['timestamp']
256 $this->assertEquals($comparedate, $date);
260 * Test saving the minimum and max year settings for the user datetime field.
262 * @param string $type the calendar type we want to test
263 * @param array $date the date variables
265 private function datetime_field_submission_test($type, $date) {
266 $this->set_calendar_type($type);
268 // Get the data we are submitting for the form.
269 $formdata = array();
270 $formdata['id'] = 0;
271 $formdata['shortname'] = 'Shortname';
272 $formdata['name'] = 'Name';
273 $formdata['param1'] = $date['inputminyear'];
274 $formdata['param2'] = $date['inputmaxyear'];
275 $formdata['datatype'] = 'datetime';
277 // Mock submitting this.
278 \core_user\form\profile_field_form::mock_submit($formdata);
280 // Create the user datetime form.
281 $form = new \core_user\form\profile_field_form();
283 // Get the data from the submission.
284 $submissiondata = $form->get_data();
285 // On the user profile field page after get_data, the function define_save is called
286 // in the field base class, which then calls the field's function define_save_preprocess.
287 $field = new profile_define_datetime();
288 $submissiondata = $field->define_save_preprocess($submissiondata);
290 // Create an array we want to compare with the date passed.
291 $comparedate = $date;
292 $comparedate['expectedminyear'] = $submissiondata->param1;
293 $comparedate['expectedmaxyear'] = $submissiondata->param2;
295 $this->assertEquals($comparedate, $date);
299 * Set the calendar type for this user.
301 * @param string $type the calendar type we want to set
303 private function set_calendar_type($type) {
304 $this->user->calendartype = $type;
305 \core\session\manager::set_user($this->user);
310 * Form object to be used in test case.
312 class temp_form_calendartype extends moodleform {
314 * Form definition.
316 public function definition() {
317 // No definition required.
320 * Returns form reference
321 * @return MoodleQuickForm
323 public function getform() {
324 $mform = $this->_form;
325 // Set submitted flag, to simulate submission.
326 $mform->_flagSubmitted = true;
327 return $mform;