diff options
Diffstat (limited to 'modules/user')
-rw-r--r-- | modules/user/user.install | 72 | ||||
-rw-r--r-- | modules/user/user.js | 8 | ||||
-rw-r--r-- | modules/user/user.module | 19 | ||||
-rw-r--r-- | modules/user/user.test | 66 |
4 files changed, 133 insertions, 32 deletions
diff --git a/modules/user/user.install b/modules/user/user.install index 0220e8589..0583d3cd1 100644 --- a/modules/user/user.install +++ b/modules/user/user.install @@ -158,9 +158,9 @@ function user_schema() { ), 'timezone' => array( 'type' => 'varchar', - 'length' => 8, + 'length' => 32, 'not null' => FALSE, - 'description' => "User's timezone.", + 'description' => "User's time zone.", ), 'language' => array( 'type' => 'varchar', @@ -292,6 +292,74 @@ function user_update_7001() { } /** + * Convert user time zones from time zone offsets to time zone names. + */ +function user_update_7002(&$sandbox) { + $ret = array('#finished' => 0); + + // Multi-part update. + if (!isset($sandbox['user_from'])) { + db_change_field($ret, 'users', 'timezone', 'timezone', array('type' => 'varchar', 'length' => 32, 'not null' => FALSE)); + $sandbox['user_from'] = 0; + $sandbox['user_count'] = db_result(db_query("SELECT COUNT(uid) FROM {users}")); + $sandbox['user_not_migrated'] = 0; + } + else { + $timezones = system_time_zones(); + // Update this many per page load. + $count = 10000; + $contributed_date_module = db_column_exists('users', 'timezone_name'); + $contributed_event_module = db_column_exists('users', 'timezone_id'); + + $results = db_query_range("SELECT uid FROM {users} ORDER BY uid", array(), $sandbox['user_from'], $count); + foreach ($results as $account) { + $timezone = NULL; + // If the contributed Date module has created a users.timezone_name + // column, use this data to set each user's time zone. + if ($contributed_date_module) { + $date_timezone = db_query("SELECT timezone_name FROM {users} WHERE uid = :uid", array(':uid' => $account->uid))->fetchField(); + if (isset($timezones[$date_timezone])) { + $timezone = $date_timezone; + } + } + // If the contributed Event module has stored user time zone information + // use that information to update the user accounts. + if (!$timezone && $contributed_event_module) { + try { + $event_timezone = db_query("SELECT t.name FROM {users} u LEFT JOIN {event_timezones} t ON u.timezone_id = t.timezone WHERE u.uid = :uid", array(':uid' => $account->uid))->fetchField(); + $event_timezone = str_replace(' ', '_', $event_timezone); + if (isset($timezones[$event_timezone])) { + $timezone = $event_timezone; + } + } + catch (PDOException $e) { + // Ignore error if event_timezones table does not exist or unexpected + // schema found. + } + } + if ($timezone) { + db_query("UPDATE {users} SET timezone = :timezone WHERE uid = :uid", array(':timezone' => $timezone, ':uid' => $account->uid)); + } + else { + $sandbox['user_not_migrated']++; + db_query("UPDATE {users} SET timezone = NULL WHERE uid = :uid", array(':uid' => $account->uid)); + } + $sandbox['user_from']++; + } + + $ret['#finished'] = $sandbox['user_from'] / $sandbox['user_count']; + if ($sandbox['user_from'] == $sandbox['user_count']) { + $ret[] = array('success' => TRUE, 'query' => "Migrate user time zones."); + if ($sandbox['user_not_migrated'] > 0) { + variable_set('empty_timezone_message', 1); + drupal_set_message('Some user time zones have been emptied and need to be set to the correct values. Use the new ' . l('time zone options', 'admin/settings/date-time') . ' to choose whether to remind users at login to set the correct time zone.', 'warning'); + } + } + } + return $ret; +} + +/** * @} End of "defgroup user-updates-6.x-to-7.x" * The next series of updates should start at 8000. */ diff --git a/modules/user/user.js b/modules/user/user.js index 173a4879b..926a92137 100644 --- a/modules/user/user.js +++ b/modules/user/user.js @@ -160,14 +160,6 @@ Drupal.evaluatePasswordStrength = function (password) { }; /** - * Set the client's system timezone as default values of form fields. - */ -Drupal.setDefaultTimezone = function() { - var offset = new Date().getTimezoneOffset() * -60; - $("#edit-date-default-timezone, #edit-user-register-timezone").val(offset); -}; - -/** * On the admin/user/settings page, conditionally show all of the * picture-related form elements depending on the current value of the * "Picture support" radio buttons. diff --git a/modules/user/user.module b/modules/user/user.module index 130c0f07e..6e8b83a80 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -2390,25 +2390,6 @@ function user_register() { $form = array_merge($form, $extra); } - if (variable_get('configurable_timezones', 1)) { - // Override field ID, so we only change timezone on user registration, - // and never touch it on user edit pages. - $form['timezone'] = array( - '#type' => 'hidden', - '#default_value' => variable_get('date_default_timezone', NULL), - '#id' => 'edit-user-register-timezone', - ); - - // Add the JavaScript callback to automatically set the timezone. - drupal_add_js(' -// Global Killswitch -if (Drupal.jsEnabled) { - $(document).ready(function() { - Drupal.setDefaultTimezone(); - }); -}', 'inline'); - } - $form['submit'] = array('#type' => 'submit', '#value' => t('Create new account'), '#weight' => 30); $form['#validate'][] = 'user_register_validate'; diff --git a/modules/user/user.test b/modules/user/user.test index 91e1df32e..bfe12858f 100644 --- a/modules/user/user.test +++ b/modules/user/user.test @@ -23,9 +23,9 @@ class UserRegistrationTestCase extends DrupalWebTestCase { // Set user registration to "Visitors can create accounts and no administrator approval is required." variable_set('user_register', 1); - // Enable user configurable timezone, and set the default timezone to +1 hour (or +3600 seconds). + // Enable user-configurable time zones, and set the default time zone to Brussels time. variable_set('configurable_timezones', 1); - variable_set('date_default_timezone', 3600); + variable_set('date_default_timezone', 'Europe/Brussels'); $edit = array(); $edit['name'] = $name = $this->randomName(); @@ -45,7 +45,7 @@ class UserRegistrationTestCase extends DrupalWebTestCase { $this->assertEqual($user->signature, '', t('Correct signature field.')); $this->assertTrue(($user->created > REQUEST_TIME - 20 ), t('Correct creation time.')); $this->assertEqual($user->status, variable_get('user_register', 1) == 1 ? 1 : 0, t('Correct status field.')); - $this->assertEqual($user->timezone, variable_get('date_default_timezone', NULL), t('Correct timezone field.')); + $this->assertEqual($user->timezone, variable_get('date_default_timezone', NULL), t('Correct time zone field.')); $this->assertEqual($user->language, '', t('Correct language field.')); $this->assertEqual($user->picture, '', t('Correct picture field.')); $this->assertEqual($user->init, $mail, t('Correct init field.')); @@ -525,6 +525,66 @@ class UserAdminTestCase extends DrupalWebTestCase { } /** + * Tests for user-configurable time zones. + */ +class UserTimeZoneFunctionalTest extends DrupalWebTestCase { + function getInfo() { + return array( + 'name' => t('User time zones'), + 'description' => t('Set a user time zone and verify that dates are displayed in local time.'), + 'group' => t('User'), + ); + } + + /** + * Tests the display of dates and time when user-configurable time zones are set. + */ + function testUserTimeZone() { + // Setup date/time settings for Los Angeles time. + variable_set('date_default_timezone', 'America/Los_Angeles'); + variable_set('configurable_timezones', 1); + variable_set('date_format_medium', 'Y-m-d H:i T'); + + // Create a user account and login. + $web_user = $this->drupalCreateUser(); + $this->drupalLogin($web_user); + + // Create some nodes with different authored-on dates. + // Two dates in PST (winter time): + $date1 = '2007-03-09 21:00:00 -0800'; + $date2 = '2007-03-11 01:00:00 -0800'; + // One date in PDT (summer time): + $date3 = '2007-03-20 21:00:00 -0700'; + $node1 = $this->drupalCreateNode(array('created' => strtotime($date1), 'type' => 'article')); + $node2 = $this->drupalCreateNode(array('created' => strtotime($date2), 'type' => 'article')); + $node3 = $this->drupalCreateNode(array('created' => strtotime($date3), 'type' => 'article')); + + // Confirm date format and time zone. + $this->drupalGet("node/$node1->nid"); + $this->assertText('2007-03-09 21:00 PST', t('Date should be PST.')); + $this->drupalGet("node/$node2->nid"); + $this->assertText('2007-03-11 01:00 PST', t('Date should be PST.')); + $this->drupalGet("node/$node3->nid"); + $this->assertText('2007-03-20 21:00 PDT', t('Date should be PDT.')); + + // Change user time zone to Santiago time. + $edit = array(); + $edit['mail'] = $web_user->mail; + $edit['timezone'] = 'America/Santiago'; + $this->drupalPost("user/$web_user->uid/edit", $edit, t('Save')); + $this->assertText(t('The changes have been saved.'), t('Time zone changed to Santiago time.')); + + // Confirm date format and time zone. + $this->drupalGet("node/$node1->nid"); + $this->assertText('2007-03-10 02:00 CLST', t('Date should be Chile summer time; five hours ahead of PST.')); + $this->drupalGet("node/$node2->nid"); + $this->assertText('2007-03-11 05:00 CLT', t('Date should be Chile time; four hours ahead of PST')); + $this->drupalGet("node/$node3->nid"); + $this->assertText('2007-03-21 00:00 CLT', t('Date should be Chile time; three hours ahead of PDT.')); + } +} + +/** * Test user autocompletion. */ class UserAutocompleteTestCase extends DrupalWebTestCase { |