[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/form/tests/ -> dateselector_test.php (source)

   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 basic_testcase {
  45      /** @var MoodleQuickForm Keeps reference of dummy form object */
  46      private $mform;
  47      /** @var stdClass saves current user data */
  48      private $olduser;
  49      /** @var int|float|string saves forcetimezone config variable */
  50      private $cfgforcetimezone;
  51      /** @var int|float|string saves current user timezone */
  52      private $userstimezone;
  53      /** @var string saves system locale */
  54      private $oldlocale;
  55      /** @var string saves system timezone */
  56      private $systemdefaulttimezone;
  57      /** @var array test fixtures */
  58      private $testvals;
  59  
  60      /**
  61       * Initalize test wide variable, it is called in start of the testcase
  62       */
  63      protected function setUp() {
  64          parent::setUp();
  65          // Get form data.
  66          $form = new temp_form_date();
  67          $this->mform = $form->getform();
  68  
  69          // Set test values.
  70          $this->testvals = array(
  71              array (
  72                  'day' => 1,
  73                  'month' => 7,
  74                  'year' => 2011,
  75                  'usertimezone' => 'America/Moncton',
  76                  'timezone' => 'America/Moncton',
  77                  'timestamp' => 1309489200
  78              ),
  79              array (
  80                  'day' => 1,
  81                  'month' => 7,
  82                  'year' => 2011,
  83                  'usertimezone' => 'America/Moncton',
  84                  'timezone' => 99,
  85                  'timestamp' => 1309489200
  86              ),
  87              array (
  88                  'day' => 30,
  89                  'month' => 6,
  90                  'year' => 2011,
  91                  'usertimezone' => 'America/Moncton',
  92                  'timezone' => -4,
  93                  'timestamp' => 1309406400
  94              ),
  95              array (
  96                  'day' => 30,
  97                  'month' => 6,
  98                  'year' => 2011,
  99                  'usertimezone' => -4,
 100                  'timezone' => 99,
 101                  'timestamp' => 1309406400
 102              ),
 103              array (
 104                  'day' => 1,
 105                  'month' => 7,
 106                  'year' => 2011,
 107                  'usertimezone' => 0.0,
 108                  'timezone' => 0.0,
 109                  'timestamp' => 1309478400 // 6am at UTC+0
 110              ),
 111              array (
 112                  'day' => 1,
 113                  'month' => 7,
 114                  'year' => 2011,
 115                  'usertimezone' => 0.0,
 116                  'timezone' => 99,
 117                  'timestamp' => 1309478400 // 6am at UTC+0
 118              )
 119          );
 120      }
 121  
 122      /**
 123       * Clears the data set in the setUp() method call.
 124       * @see dateselector_form_element_testcase::setUp()
 125       */
 126      protected function tearDown() {
 127          unset($this->testvals);
 128          parent::tearDown();
 129      }
 130  
 131      /**
 132       * Testcase to check exportvalue
 133       */
 134      public function test_exportvalue() {
 135          global $USER;
 136          $testvals = $this->testvals;
 137  
 138          // Set timezone to Australia/Perth for testing.
 139          $this->settimezone();
 140  
 141          foreach ($testvals as $vals) {
 142              // Set user timezone to test value.
 143              $USER->timezone = $vals['usertimezone'];
 144  
 145              // Create dateselector element with different timezones.
 146              $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
 147              $el = new MoodleQuickForm_date_selector('dateselector', null, $elparams);
 148              $el->_createElements();
 149              $submitvalues = array('dateselector' => $vals);
 150  
 151              $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues),
 152                      "Please check if timezones are updated (Site adminstration -> location -> update timezone)");
 153          }
 154  
 155          // Restore user original timezone.
 156          $this->restoretimezone();
 157      }
 158  
 159      /**
 160       * Testcase to check onQuickformEvent
 161       */
 162      public function test_onquickformevent() {
 163          global $USER;
 164          $testvals = $this->testvals;
 165          // Get dummy form for data.
 166          $mform = $this->mform;
 167          // Set timezone to Australia/Perth for testing.
 168          $this->settimezone();
 169          foreach ($testvals as $vals) {
 170              // Set user timezone to test value.
 171              $USER->timezone = $vals['usertimezone'];
 172  
 173              // Create dateselector element with different timezones.
 174              $elparams = array('optional'=>false, 'timezone' => $vals['timezone']);
 175              $el = new MoodleQuickForm_date_selector('dateselector', null, $elparams);
 176              $el->_createElements();
 177              $expectedvalues = array(
 178                  'day' => array($vals['day']),
 179                  'month' => array($vals['month']),
 180                  'year' => array($vals['year'])
 181                  );
 182              $mform->_submitValues = array('dateselector' => $vals['timestamp']);
 183              $el->onQuickFormEvent('updateValue', null, $mform);
 184              $this->assertSame($expectedvalues, $el->getValue());
 185          }
 186  
 187          // Restore user original timezone.
 188          $this->restoretimezone();
 189      }
 190  
 191      /**
 192       * Set user timezone to Australia/Perth for testing.
 193       */
 194      private function settimezone() {
 195          global $USER, $CFG, $DB;
 196          $this->olduser = $USER;
 197          $USER = $DB->get_record('user', array('id'=>2)); // Admin.
 198  
 199          // Check if forcetimezone is set then save it and set it to use user timezone.
 200          $this->cfgforcetimezone = null;
 201          if (isset($CFG->forcetimezone)) {
 202              $this->cfgforcetimezone = $CFG->forcetimezone;
 203              $CFG->forcetimezone = 99; //get user default timezone.
 204          }
 205  
 206          // Store user default timezone to restore later.
 207          $this->userstimezone = $USER->timezone;
 208  
 209          // The string version of date comes from server locale setting and does
 210          // not respect user language, so it is necessary to reset that.
 211          $this->oldlocale = setlocale(LC_TIME, '0');
 212          setlocale(LC_TIME, 'en_AU.UTF-8');
 213  
 214          // Set default timezone to Australia/Perth, else time calculated
 215          // will not match expected values. Before that save system defaults.
 216          $this->systemdefaulttimezone = date_default_timezone_get();
 217          date_default_timezone_set('Australia/Perth');
 218      }
 219  
 220      /**
 221       * Restore user timezone to original state
 222       */
 223      private function restoretimezone() {
 224          global $USER, $CFG;
 225          // Restore user timezone back to what it was.
 226          $USER->timezone = $this->userstimezone;
 227  
 228          // Restore forcetimezone.
 229          if (!is_null($this->cfgforcetimezone)) {
 230              $CFG->forcetimezone = $this->cfgforcetimezone;
 231          }
 232  
 233          // Restore system default values.
 234          date_default_timezone_set($this->systemdefaulttimezone);
 235          setlocale(LC_TIME, $this->oldlocale);
 236  
 237          $USER = $this->olduser;
 238      }
 239  }
 240  
 241  /**
 242   * Form object to be used in test case.
 243   */
 244  class temp_form_date extends moodleform {
 245      /**
 246       * Form definition.
 247       */
 248      public function definition() {
 249          // No definition required.
 250      }
 251      /**
 252       * Returns form reference
 253       * @return MoodleQuickForm
 254       */
 255      public function getform() {
 256          $mform = $this->_form;
 257          // set submitted flag, to simulate submission
 258          $mform->_flagSubmitted = true;
 259          return $mform;
 260      }
 261  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1