Skip to content
Snippets Groups Projects
Commit 1f8e6e78 authored by Thomas Müller's avatar Thomas Müller
Browse files

Merge pull request #15424 from owncloud/failing-master-tests

Fix failing tests related to timezones on master
parents 95e55aa4 0fcd2737
No related branches found
No related tags found
No related merge requests found
...@@ -110,7 +110,7 @@ class ApiControllerTest extends TestCase { ...@@ -110,7 +110,7 @@ class ApiControllerTest extends TestCase {
[ [
'id' => null, 'id' => null,
'parentId' => null, 'parentId' => null,
'date' => 'January 1, 1970 at 12:00:55 AM GMT+0', 'date' => \OCP\Util::formatDate(55),
'mtime' => 55000, 'mtime' => 55000,
'icon' => \OCA\Files\Helper::determineIcon($fileInfo), 'icon' => \OCA\Files\Helper::determineIcon($fileInfo),
'name' => 'root.txt', 'name' => 'root.txt',
...@@ -175,7 +175,7 @@ class ApiControllerTest extends TestCase { ...@@ -175,7 +175,7 @@ class ApiControllerTest extends TestCase {
[ [
'id' => null, 'id' => null,
'parentId' => null, 'parentId' => null,
'date' => 'January 1, 1970 at 12:00:55 AM GMT+0', 'date' => \OCP\Util::formatDate(55),
'mtime' => 55000, 'mtime' => 55000,
'icon' => \OCA\Files\Helper::determineIcon($fileInfo1), 'icon' => \OCA\Files\Helper::determineIcon($fileInfo1),
'name' => 'root.txt', 'name' => 'root.txt',
...@@ -194,7 +194,7 @@ class ApiControllerTest extends TestCase { ...@@ -194,7 +194,7 @@ class ApiControllerTest extends TestCase {
[ [
'id' => null, 'id' => null,
'parentId' => null, 'parentId' => null,
'date' => 'January 1, 1970 at 12:16:39 AM GMT+0', 'date' => \OCP\Util::formatDate(999),
'mtime' => 999000, 'mtime' => 999000,
'icon' => \OCA\Files\Helper::determineIcon($fileInfo2), 'icon' => \OCA\Files\Helper::determineIcon($fileInfo2),
'name' => 'root.txt', 'name' => 'root.txt',
......
...@@ -48,13 +48,14 @@ class DateTimeZone implements IDateTimeZone { ...@@ -48,13 +48,14 @@ class DateTimeZone implements IDateTimeZone {
/** /**
* Get the timezone of the current user, based on his session information and config data * Get the timezone of the current user, based on his session information and config data
* *
* @param bool|int $timestamp
* @return \DateTimeZone * @return \DateTimeZone
*/ */
public function getTimeZone() { public function getTimeZone($timestamp = false) {
$timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null); $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null);
if ($timeZone === null) { if ($timeZone === null) {
if ($this->session->exists('timezone')) { if ($this->session->exists('timezone')) {
return $this->guessTimeZoneFromOffset($this->session->get('timezone')); return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp);
} }
$timeZone = $this->getDefaultTimeZone(); $timeZone = $this->getDefaultTimeZone();
} }
...@@ -74,9 +75,10 @@ class DateTimeZone implements IDateTimeZone { ...@@ -74,9 +75,10 @@ class DateTimeZone implements IDateTimeZone {
* we try to find it manually, before falling back to UTC. * we try to find it manually, before falling back to UTC.
* *
* @param mixed $offset * @param mixed $offset
* @param bool|int $timestamp
* @return \DateTimeZone * @return \DateTimeZone
*/ */
protected function guessTimeZoneFromOffset($offset) { protected function guessTimeZoneFromOffset($offset, $timestamp) {
try { try {
// Note: the timeZone name is the inverse to the offset, // Note: the timeZone name is the inverse to the offset,
// so a positive offset means negative timeZone // so a positive offset means negative timeZone
...@@ -93,7 +95,13 @@ class DateTimeZone implements IDateTimeZone { ...@@ -93,7 +95,13 @@ class DateTimeZone implements IDateTimeZone {
// we try to guess one timezone that has the same offset // we try to guess one timezone that has the same offset
foreach (\DateTimeZone::listIdentifiers() as $timeZone) { foreach (\DateTimeZone::listIdentifiers() as $timeZone) {
$dtz = new \DateTimeZone($timeZone); $dtz = new \DateTimeZone($timeZone);
$dtOffset = $dtz->getOffset(new \DateTime()); $dateTime = new \DateTime();
if ($timestamp !== false) {
$dateTime->setTimestamp($timestamp);
}
$dtOffset = $dtz->getOffset($dateTime);
if ($dtOffset == 3600 * $offset) { if ($dtOffset == 3600 * $offset) {
return $dtz; return $dtz;
} }
......
...@@ -25,7 +25,8 @@ namespace OCP; ...@@ -25,7 +25,8 @@ namespace OCP;
interface IDateTimeZone { interface IDateTimeZone {
/** /**
* @param bool|int $timestamp
* @return \DateTimeZone * @return \DateTimeZone
*/ */
public function getTimeZone(); public function getTimeZone($timestamp = false);
} }
...@@ -54,24 +54,27 @@ class Test_Util extends \Test\TestCase { ...@@ -54,24 +54,27 @@ class Test_Util extends \Test\TestCase {
public function formatDateWithTZFromSessionData() { public function formatDateWithTZFromSessionData() {
return array( return array(
array(3, 'October 13, 2012 at 2:53:25 PM GMT+3'), array(3, 'October 13, 2012 at 2:53:25 PM GMT+3', 'Etc/GMT-3'),
array(15, 'October 13, 2012 at 11:53:25 AM GMT+0'), array(15, 'October 13, 2012 at 11:53:25 AM GMT+0', 'UTC'),
array(-13, 'October 13, 2012 at 11:53:25 AM GMT+0'), array(-13, 'October 13, 2012 at 11:53:25 AM GMT+0', 'UTC'),
array(9.5, 'October 13, 2012 at 9:23:25 PM GMT+9:30'), array(9.5, 'October 13, 2012 at 9:23:25 PM GMT+9:30', 'Australia/Darwin'),
array(-4.5, 'October 13, 2012 at 7:23:25 AM GMT-4:30'), array(-4.5, 'October 13, 2012 at 7:23:25 AM GMT-4:30', 'America/Caracas'),
array(15.5, 'October 13, 2012 at 11:53:25 AM GMT+0'), array(15.5, 'October 13, 2012 at 11:53:25 AM GMT+0', 'UTC'),
); );
} }
/** /**
* @dataProvider formatDateWithTZFromSessionData * @dataProvider formatDateWithTZFromSessionData
*/ */
function testFormatDateWithTZFromSession($offset, $expected) { function testFormatDateWithTZFromSession($offset, $expected, $expectedTimeZone) {
date_default_timezone_set("UTC"); date_default_timezone_set("UTC");
$oldDateTimeFormatter = \OC::$server->query('DateTimeFormatter'); $oldDateTimeFormatter = \OC::$server->query('DateTimeFormatter');
\OC::$server->getSession()->set('timezone', $offset); \OC::$server->getSession()->set('timezone', $offset);
$newDateTimeFormatter = new \OC\DateTimeFormatter(\OC::$server->getDateTimeZone()->getTimeZone(), new \OC_L10N('lib', 'en'));
$selectedTimeZone = \OC::$server->getDateTimeZone()->getTimeZone(1350129205);
$this->assertEquals($expectedTimeZone, $selectedTimeZone->getName());
$newDateTimeFormatter = new \OC\DateTimeFormatter($selectedTimeZone, new \OC_L10N('lib', 'en'));
$this->setDateFormatter($newDateTimeFormatter); $this->setDateFormatter($newDateTimeFormatter);
$result = OC_Util::formatDate(1350129205, false); $result = OC_Util::formatDate(1350129205, false);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment