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