MDL-38999 timezones: add some TZ/stamps know to be problematic to tests.
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 8 Apr 2013 14:09:41 +0000 (16:09 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 8 Apr 2013 14:09:41 +0000 (16:09 +0200)
lib/tests/moodlelib_test.php

index b5807b3..d04a90c 100644 (file)
@@ -1539,6 +1539,59 @@ class moodlelib_testcase extends advanced_testcase {
         $USER = $olduser;
     }
 
+    /**
+     * Test some critical TZ/DST.
+     *
+     * This method tests some special TZ/DST combinations that were fixed
+     * by MDL-38999. The tests are done by comparing the results of the
+     * output using Moodle TZ/DST support and PHP native one.
+     *
+     * Note: If you don't trust PHP TZ/DST support, can verify the
+     * harcoded expectations below with:
+     * http://www.tools4noobs.com/online_tools/unix_timestamp_to_datetime/
+     */
+    public function test_some_moodle_special_dst() {
+        $stamp = 1365386400; // 2013/04/08 02:00:00 GMT/UTC.
+
+        // In Europe/Tallinn it was 2013/04/08 05:00:00.
+        $expectation = '2013/04/08 05:00:00';
+        $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
+        $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn'));
+        $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
+        $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result.
+        $this->assertSame($expectation, $phpres);
+        $this->assertSame($expectation, $moodleres);
+
+        // In St. Johns it was 2013/04/07 23:30:00.
+        $expectation = '2013/04/07 23:30:00';
+        $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
+        $phpdt->setTimezone(new DateTimeZone('America/St_Johns'));
+        $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
+        $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result.
+        $this->assertSame($expectation, $phpres);
+        $this->assertSame($expectation, $moodleres);
+
+        $stamp = 1383876000; // 2013/11/08 02:00:00 GMT/UTC.
+
+        // In Europe/Tallinn it was 2013/11/08 04:00:00.
+        $expectation = '2013/11/08 04:00:00';
+        $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
+        $phpdt->setTimezone(new DateTimeZone('Europe/Tallinn'));
+        $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
+        $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'Europe/Tallinn', false); // Moodle result.
+        $this->assertSame($expectation, $phpres);
+        $this->assertSame($expectation, $moodleres);
+
+        // In St. Johns it was 2013/11/07 22:30:00.
+        $expectation = '2013/11/07 22:30:00';
+        $phpdt = DateTime::createFromFormat('U', $stamp, new DateTimeZone('UTC'));
+        $phpdt->setTimezone(new DateTimeZone('America/St_Johns'));
+        $phpres = $phpdt->format('Y/m/d H:i:s'); // PHP result.
+        $moodleres = userdate($stamp, '%Y/%m/%d %H:%M:%S', 'America/St_Johns', false); // Moodle result.
+        $this->assertSame($expectation, $phpres);
+        $this->assertSame($expectation, $moodleres);
+    }
+
     public function test_userdate() {
         global $USER, $CFG, $DB;