Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Unit tests for datetimeselector form element
  19   *
  20   * This file contains unit test related to datetimeselector form element
  21   *
  22   * @package    core_form
  23   * @category   phpunit
  24   * @copyright  2012 Rajesh Taneja
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->libdir . '/form/datetimeselector.php');
  32  require_once($CFG->libdir.'/formslib.php');
  33  
  34  /**
  35   * Unit tests for MoodleQuickForm_date_time_selector
  36   *
  37   * Contains test cases for testing MoodleQuickForm_date_time_selector
  38   *
  39   * @package    core_form
  40   * @category   phpunit
  41   * @copyright  2012 Rajesh Taneja
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class core_form_datetimeselector_testcase extends advanced_testcase {
  45      /** @var MoodleQuickForm Keeps reference of dummy form object */
  46      private $mform;
  47      /** @var array test fixtures */
  48      private $testvals;
  49  
  50      /**
  51       * Initalize test wide variable, it is called in start of the testcase
  52       */
  53      protected function setUp(): void {
  54          global $CFG;
  55          parent::setUp();
  56  
  57          $this->resetAfterTest();
  58          $this->setAdminUser();
  59  
  60          $this->setTimezone('Australia/Perth');
  61  
  62          // Get form data.
  63          $form = new temp_form_datetime();
  64          $this->mform = $form->getform();
  65  
  66          // Set test values.
  67          $this->testvals = array(
  68              array (
  69                  'minute' => 0,
  70                  'hour' => 0,
  71                  'day' => 1,
  72                  'month' => 7,
  73                  'year' => 2011,
  74                  'usertimezone' => 'America/Moncton',
  75                  'timezone' => 'America/Moncton',
  76                  'timestamp' => 1309489200
  77              ),
  78              array (
  79                  'minute' => 0,
  80                  'hour' => 0,
  81                  'day' => 1,
  82                  'month' => 7,
  83                  'year' => 2011,
  84                  'usertimezone' => 'America/Moncton',
  85                  'timezone' => 99,
  86                  'timestamp' => 1309489200
  87              ),
  88              array (
  89                  'minute' => 0,
  90                  'hour' => 23,
  91                  'day' => 30,
  92                  'month' => 6,
  93                  'year' => 2011,
  94                  'usertimezone' => 'America/Moncton',
  95                  'timezone' => -4,
  96                  'timestamp' => 1309489200
  97              ),
  98              array (
  99                  'minute' => 0,
 100                  'hour' => 23,
 101                  'day' => 30,
 102                  'month' => 6,
 103                  'year' => 2011,
 104                  'usertimezone' => -4,
 105                  'timezone' => 99,
 106                  'timestamp' => 1309489200
 107              ),
 108              array (
 109                  'minute' => 0,
 110                  'hour' => 0,
 111                  'day' => 1,
 112                  'month' => 7,
 113                  'year' => 2011,
 114                  'usertimezone' => 0.0,
 115                  'timezone' => 0.0,
 116                  'timestamp' => 1309478400 // 6am at UTC+0
 117              ),
 118              array (
 119                  'minute' => 0,
 120                  'hour' => 0,
 121                  'day' => 1,
 122                  'month' => 7,
 123                  'year' => 2011,
 124                  'usertimezone' => 0.0,
 125                  'timezone' => 99,
 126                  'timestamp' => 1309478400 // 6am at UTC+0
 127              )
 128          );
 129      }
 130  
 131      /**
 132       * Testcase to check exportvalue
 133       */
 134      public function test_exportvalue() {
 135          global $USER;
 136          $testvals = $this->testvals;
 137  
 138          foreach ($testvals as $vals) {
 139              // Set user timezone to test value.
 140              $USER->timezone = $vals['usertimezone'];
 141  
 142              // Create dateselector element with different timezones.
 143              $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
 144              $el = $this->mform->addElement('date_time_selector', 'dateselector', null, $elparams);
 145              $this->assertTrue($el instanceof MoodleQuickForm_date_time_selector);
 146              $submitvalues = array('dateselector' => $vals);
 147  
 148              $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues, true),
 149                      "Please check if timezones are updated (Site adminstration -> location -> update timezone)");
 150          }
 151      }
 152  
 153      /**
 154       * Testcase to check onQuickformEvent
 155       */
 156      public function test_onquickformevent() {
 157          global $USER;
 158          $testvals = $this->testvals;
 159          // Get dummy form for data.
 160          $mform = $this->mform;
 161  
 162          foreach ($testvals as $vals) {
 163              // Set user timezone to test value.
 164              $USER->timezone = $vals['usertimezone'];
 165  
 166              // Create dateselector element with different timezones.
 167              $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
 168              $el = $this->mform->addElement('date_time_selector', 'dateselector', null, $elparams);
 169              $this->assertTrue($el instanceof MoodleQuickForm_date_time_selector);
 170              $expectedvalues = array(
 171                  'day' => array($vals['day']),
 172                  'month' => array($vals['month']),
 173                  'year' => array($vals['year']),
 174                  'hour' => array($vals['hour']),
 175                  'minute' => array($vals['minute'])
 176                  );
 177              $mform->_submitValues = array('dateselector' => $vals['timestamp']);
 178              $el->onQuickFormEvent('updateValue', null, $mform);
 179              $this->assertSame($expectedvalues, $el->getValue());
 180          }
 181      }
 182  }
 183  
 184  /**
 185   * Form object to be used in test case
 186   */
 187  class temp_form_datetime extends moodleform {
 188      /**
 189       * Form definition.
 190       */
 191      public function definition() {
 192          // No definition required.
 193      }
 194      /**
 195       * Returns form reference.
 196       * @return MoodleQuickForm
 197       */
 198      public function getform() {
 199          $mform = $this->_form;
 200          // set submitted flag, to simulate submission
 201          $mform->_flagSubmitted = true;
 202          return $mform;
 203      }
 204  }