Merge branch 'MDL-50704-master' of git://github.com/FMCorz/moodle
authorDan Poltawski <dan@moodle.com>
Tue, 10 May 2016 14:59:41 +0000 (15:59 +0100)
committerDan Poltawski <dan@moodle.com>
Tue, 10 May 2016 14:59:41 +0000 (15:59 +0100)
lib/classes/date.php
lib/classes/user.php
lib/tests/date_test.php
lib/tests/moodlelib_test.php
lib/tests/user_test.php
user/tests/userlib_test.php

index 3685809..a2b4a6b 100644 (file)
@@ -72,8 +72,12 @@ class core_date {
 
         if (is_numeric($currentvalue)) {
             // UTC offset.
-            $modifier = ($currentvalue > 0) ? '+' : '';
-            $a = 'UTC' . $modifier . number_format($currentvalue, 1);
+            if ($currentvalue == 0) {
+                $a = 'UTC';
+            } else {
+                $modifier = ($currentvalue > 0) ? '+' : '';
+                $a = 'UTC' . $modifier . number_format($currentvalue, 1);
+            }
             $timezones[$currentvalue] = get_string('timezoneinvalid', 'core_admin', $a);
         } else {
             // Some string we don't recognise.
@@ -508,7 +512,7 @@ class core_date {
         );
 
         // Legacy GMT fallback.
-        for ($i = -14; $i <= 13; $i++) {
+        for ($i = -12; $i <= 14; $i++) {
             $off = abs($i);
             if ($i < 0) {
                 $mapto = 'Etc/GMT+' . $off;
index c06a84c..a730cc0 100644 (file)
@@ -337,8 +337,8 @@ class core_user {
                 'choices' => array_merge(array('' => ''), \core_calendar\type_factory::get_list_of_calendar_types()));
         $fields['theme'] = array('type' => PARAM_THEME, 'null' => NULL_NOT_ALLOWED,
                 'default' => theme_config::DEFAULT_THEME, 'choices' => array_merge(array('' => ''), get_list_of_themes()));
-        $fields['timezone'] = array('type' => PARAM_TIMEZONE, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->timezone,
-                'choices' => core_date::get_list_of_timezones(null, true));
+        $fields['timezone'] = array('type' => PARAM_TIMEZONE, 'null' => NULL_NOT_ALLOWED,
+                'default' => core_date::get_server_timezone()); // Must not use choices here: timezones can come and go.
         $fields['firstaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
         $fields['lastaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
         $fields['lastlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
@@ -528,7 +528,7 @@ class core_user {
      * Get the choices of the property.
      *
      * This is a helper method to validate a value against a list of acceptable choices.
-     * For instance: country, timezone, language, themes and etc.
+     * For instance: country, language, themes and etc.
      *
      * @param string $property property name to be retrieved.
      * @throws coding_exception if the requested property name is invalid or if it does not has a list of choices.
index 3e4d908..2cd0d9f 100644 (file)
@@ -77,7 +77,7 @@ class core_date_testcase extends advanced_testcase {
         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-14));
         $this->assertSame('Etc/GMT-12', core_date::normalise_timezone(12));
         $this->assertSame('Etc/GMT-13', core_date::normalise_timezone(13));
-        $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(14));
+        $this->assertSame('Etc/GMT-14', core_date::normalise_timezone(14));
 
         $this->assertSame('Asia/Kabul', core_date::normalise_timezone(4.5));
         $this->assertSame('Asia/Kolkata', core_date::normalise_timezone(5.5));
@@ -107,7 +107,7 @@ class core_date_testcase extends advanced_testcase {
         $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(-14));
         $this->assertSame('Etc/GMT-12', core_date::normalise_timezone(12));
         $this->assertSame('Etc/GMT-13', core_date::normalise_timezone(13));
-        $this->assertSame('Pacific/Auckland', core_date::normalise_timezone(14));
+        $this->assertSame('Etc/GMT-14', core_date::normalise_timezone(14));
 
         $this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
         $tz = new DateTimeZone('Pacific/Auckland');
@@ -334,6 +334,76 @@ class core_date_testcase extends advanced_testcase {
         $this->assertSame('Etc/GMT-1', date_default_timezone_get());
     }
 
+    public function legacyUserTimezoneProvider() {
+        return [
+            ['', 'Australia/Perth'],            // Fallback on default timezone.
+            ['-13.0', 'Australia/Perth'],       // Fallback on default timezone.
+            ['-12.5', 'Etc/GMT+12'],
+            ['-12.0', 'Etc/GMT+12'],
+            ['-11.5', 'Etc/GMT+11'],
+            ['-11.0', 'Etc/GMT+11'],
+            ['-10.5', 'Etc/GMT+10'],
+            ['-10.0', 'Etc/GMT+10'],
+            ['-9.5', 'Etc/GMT+9'],
+            ['-9.0', 'Etc/GMT+9'],
+            ['-8.5', 'Etc/GMT+8'],
+            ['-8.0', 'Etc/GMT+8'],
+            ['-7.5', 'Etc/GMT+7'],
+            ['-7.0', 'Etc/GMT+7'],
+            ['-6.5', 'Etc/GMT+6'],
+            ['-6.0', 'Etc/GMT+6'],
+            ['-5.5', 'Etc/GMT+5'],
+            ['-5.0', 'Etc/GMT+5'],
+            ['-4.5', 'Etc/GMT+4'],
+            ['-4.0', 'Etc/GMT+4'],
+            ['-3.5', 'Etc/GMT+3'],
+            ['-3.0', 'Etc/GMT+3'],
+            ['-2.5', 'Etc/GMT+2'],
+            ['-2.0', 'Etc/GMT+2'],
+            ['-1.5', 'Etc/GMT+1'],
+            ['-1.0', 'Etc/GMT+1'],
+            ['-0.5', 'Etc/GMT'],
+            ['0', 'Etc/GMT'],
+            ['0.0', 'Etc/GMT'],
+            ['0.5', 'Etc/GMT'],
+            ['1.0', 'Etc/GMT-1'],
+            ['1.5', 'Etc/GMT-1'],
+            ['2.0', 'Etc/GMT-2'],
+            ['2.5', 'Etc/GMT-2'],
+            ['3.0', 'Etc/GMT-3'],
+            ['3.5', 'Etc/GMT-3'],
+            ['4.0', 'Etc/GMT-4'],
+            ['4.5', 'Asia/Kabul'],
+            ['5.0', 'Etc/GMT-5'],
+            ['5.5', 'Asia/Kolkata'],
+            ['6.0', 'Etc/GMT-6'],
+            ['6.5', 'Asia/Rangoon'],
+            ['7.0', 'Etc/GMT-7'],
+            ['7.5', 'Etc/GMT-7'],
+            ['8.0', 'Etc/GMT-8'],
+            ['8.5', 'Etc/GMT-8'],
+            ['9.0', 'Etc/GMT-9'],
+            ['9.5', 'Australia/Darwin'],
+            ['10.0', 'Etc/GMT-10'],
+            ['10.5', 'Etc/GMT-10'],
+            ['11.0', 'Etc/GMT-11'],
+            ['11.5', 'Etc/GMT-11'],
+            ['12.0', 'Etc/GMT-12'],
+            ['12.5', 'Etc/GMT-12'],
+            ['13.0', 'Etc/GMT-13'],
+        ];
+    }
+
+    /**
+     * @dataProvider legacyUserTimezoneProvider
+     * @param string $tz The legacy timezone.
+     * @param string $expected The expected converted timezone.
+     */
+    public function test_get_legacy_user_timezone($tz, $expected) {
+        $this->setTimezone('Australia/Perth', 'Australia/Perth');
+        $this->assertEquals($expected, core_date::get_user_timezone($tz));
+    }
+
     public function test_get_user_timezone() {
         global $CFG, $USER;
         $this->resetAfterTest();
index fb9d5ae..34e05bc 100644 (file)
@@ -1582,7 +1582,7 @@ class core_moodlelib_testcase extends advanced_testcase {
                 'expectedoutput' => '1309485600'
             ),
             array(
-                'usertimezone' => '14', // Server time.
+                'usertimezone' => '-14', // Server time.
                 'year' => '2011',
                 'month' => '7',
                 'day' => '1',
index cc64ba0..03192a5 100644 (file)
@@ -363,15 +363,6 @@ class core_user_testcase extends advanced_testcase {
         $this->assertArrayNotHasKey('unknowntheme', $choices);
         $this->assertArrayNotHasKey('wrongtheme', $choices);
 
-        // Test against timezone property choices.
-        $choices = core_user::get_property_choices('timezone');
-        $this->assertArrayHasKey('America/Sao_Paulo', $choices);
-        $this->assertArrayHasKey('Australia/Perth', $choices);
-        $this->assertArrayHasKey('99', $choices);
-        $this->assertArrayHasKey('UTC', $choices);
-        $this->assertArrayNotHasKey('North Korea', $choices);
-        $this->assertArrayNotHasKey('New york', $choices);
-
         // Try to fetch type of a non-existent properties.
         $nonexistingproperty = 'language';
         $this->setExpectedException('coding_exception', 'Invalid property requested: ' . $nonexistingproperty);
@@ -386,6 +377,7 @@ class core_user_testcase extends advanced_testcase {
      */
     public function test_get_property_default() {
         global $CFG;
+        $this->resetAfterTest();
 
         $country = core_user::get_property_default('country');
         $this->assertEquals($CFG->country, $country);
@@ -400,12 +392,14 @@ class core_user_testcase extends advanced_testcase {
         $lang = core_user::get_property_default('lang');
         $this->assertEquals($CFG->lang, $lang);
 
+        $this->setTimezone('Europe/London', 'Pacific/Auckland');
+        core_user::reset_caches();
         $timezone = core_user::get_property_default('timezone');
-        $this->assertEquals($CFG->timezone, $timezone);
-        set_config('timezone', 99);
+        $this->assertEquals('Europe/London', $timezone);
+        $this->setTimezone('99', 'Pacific/Auckland');
         core_user::reset_caches();
         $timezone = core_user::get_property_default('timezone');
-        $this->assertEquals(99, $timezone);
+        $this->assertEquals('Pacific/Auckland', $timezone);
 
         $this->setExpectedException('coding_exception', 'Invalid property requested, or the property does not has a default value.');
         core_user::get_property_default('firstname');
index 0a8bd77..72aebbb 100644 (file)
@@ -136,7 +136,7 @@ class core_userliblib_testcase extends advanced_testcase {
         $user->country = 'WW';
         $user->lang = 'xy';
         $user->theme = 'somewrongthemename';
-        $user->timezone = 'Paris';
+        $user->timezone = '30.5';
         $user->url = 'wwww.somewrong@#$url.com.aus';
         $debugmessages = $this->getDebuggingMessages();
         user_update_user($user, true, false);
@@ -219,7 +219,7 @@ class core_userliblib_testcase extends advanced_testcase {
         $user['country'] = 'WW';
         $user['lang'] = 'xy';
         $user['theme'] = 'somewrongthemename';
-        $user['timezone'] = 'Paris';
+        $user['timezone'] = '-30.5';
         $user['url'] = 'wwww.somewrong@#$url.com.aus';
         $debugmessages = $this->getDebuggingMessages();
         $user['id'] = user_create_user($user, true, false);
@@ -227,7 +227,7 @@ class core_userliblib_testcase extends advanced_testcase {
         $dbuser = $DB->get_record('user', array('id' => $user['id']));
         $this->assertEquals($dbuser->country, 0);
         $this->assertEquals($dbuser->lang, 'en');
-        $this->assertEquals($dbuser->timezone, 'Australia/Perth');
+        $this->assertEquals($dbuser->timezone, '');
 
         // Now, with valid user data.
         $user['username'] = 'johndoe321';