Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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