Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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