Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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 dateselector form element
  19   *
  20   * This file contains unit test related to dateselector 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/dateselector.php');
  32  require_once($CFG->libdir.'/formslib.php');
  33  
  34  /**
  35   * Unit tests for MoodleQuickForm_date_selector
  36   *
  37   * Contains test cases for testing MoodleQuickForm_date_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_dateselector_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() {
  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_date();
  64          $this->mform = $form->getform();
  65  
  66          // Set test values.
  67          $this->testvals = array(
  68              array (
  69                  'day' => 1,
  70                  'month' => 7,
  71                  'year' => 2011,
  72                  'usertimezone' => 'America/Moncton',
  73                  'timezone' => 'America/Moncton',
  74                  'timestamp' => 1309489200
  75              ),
  76              array (
  77                  'day' => 1,
  78                  'month' => 7,
  79                  'year' => 2011,
  80                  'usertimezone' => 'America/Moncton',
  81                  'timezone' => 99,
  82                  'timestamp' => 1309489200
  83              ),
  84              array (
  85                  'day' => 30,
  86                  'month' => 6,
  87                  'year' => 2011,
  88                  'usertimezone' => 'America/Moncton',
  89                  'timezone' => -4,
  90                  'timestamp' => 1309406400
  91              ),
  92              array (
  93                  'day' => 30,
  94                  'month' => 6,
  95                  'year' => 2011,
  96                  'usertimezone' => -4,
  97                  'timezone' => 99,
  98                  'timestamp' => 1309406400
  99              ),
 100              array (
 101                  'day' => 1,
 102                  'month' => 7,
 103                  'year' => 2011,
 104                  'usertimezone' => 0.0,
 105                  'timezone' => 0.0,
 106                  'timestamp' => 1309478400 // 6am at UTC+0
 107              ),
 108              array (
 109                  'day' => 1,
 110                  'month' => 7,
 111                  'year' => 2011,
 112                  'usertimezone' => 0.0,
 113                  'timezone' => 99,
 114                  'timestamp' => 1309478400 // 6am at UTC+0
 115              )
 116          );
 117      }
 118  
 119      /**
 120       * Testcase to check exportvalue
 121       */
 122      public function test_exportvalue() {
 123          global $USER;
 124          $testvals = $this->testvals;
 125  
 126          foreach ($testvals as $vals) {
 127              // Set user timezone to test value.
 128              $USER->timezone = $vals['usertimezone'];
 129  
 130              // Create dateselector element with different timezones.
 131              $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
 132              $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
 133              $this->assertTrue($el instanceof MoodleQuickForm_date_selector);
 134              $submitvalues = array('dateselector' => $vals);
 135  
 136              $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues, true),
 137                      "Please check if timezones are updated (Site adminstration -> location -> update timezone)");
 138          }
 139      }
 140  
 141      /**
 142       * Testcase to check onQuickformEvent
 143       */
 144      public function test_onquickformevent() {
 145          global $USER;
 146          $testvals = $this->testvals;
 147          // Get dummy form for data.
 148          $mform = $this->mform;
 149  
 150          foreach ($testvals as $vals) {
 151              // Set user timezone to test value.
 152              $USER->timezone = $vals['usertimezone'];
 153  
 154              // Create dateselector element with different timezones.
 155              $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
 156              $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams);
 157              $this->assertTrue($el instanceof MoodleQuickForm_date_selector);
 158              $expectedvalues = array(
 159                  'day' => array($vals['day']),
 160                  'month' => array($vals['month']),
 161                  'year' => array($vals['year'])
 162                  );
 163              $mform->_submitValues = array('dateselector' => $vals['timestamp']);
 164              $el->onQuickFormEvent('updateValue', null, $mform);
 165              $this->assertSame($expectedvalues, $el->getValue());
 166          }
 167      }
 168  }
 169  
 170  /**
 171   * Form object to be used in test case.
 172   */
 173  class temp_form_date extends moodleform {
 174      /**
 175       * Form definition.
 176       */
 177      public function definition() {
 178          // No definition required.
 179      }
 180      /**
 181       * Returns form reference
 182       * @return MoodleQuickForm
 183       */
 184      public function getform() {
 185          $mform = $this->_form;
 186          // set submitted flag, to simulate submission
 187          $mform->_flagSubmitted = true;
 188          return $mform;
 189      }
 190  }