PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/calendar/tests/calendartype_test.php

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