Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timezone issue when using magic date keywords #17144

Merged
merged 3 commits into from Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions core/Period/Factory.php
Expand Up @@ -168,10 +168,9 @@ public static function makePeriodFromQueryParams($timezone, $period, $date)
} else {
if (!($date instanceof Date)) {
if (preg_match('/^(now|today|yesterday|yesterdaySameTime|last[ -]?(?:week|month|year))$/i', $date)) {
$date = Date::factory($date, $timezone);
} else {
$date = Date::factory($date);
$date = Date::factoryInTimezone($date, $timezone);
}
$date = Date::factory($date);
sgiehl marked this conversation as resolved.
Show resolved Hide resolved
}
$oPeriod = Factory::build($period, $date);
}
Expand Down
35 changes: 22 additions & 13 deletions tests/PHPUnit/Integration/Period/FactoryTest.php
Expand Up @@ -16,7 +16,7 @@
use Piwik\Period\Range;
use Piwik\Period\Week;
use Piwik\Period\Year;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\TestCase\UnitTestCase;

class TestPeriod
{
Expand Down Expand Up @@ -61,31 +61,40 @@ public function findComponents($componentName, $expectedSubclass)
}
}

class FactoryTest extends IntegrationTestCase
/**
* @group PeriodFactoryTest
*/
class FactoryTest extends UnitTestCase
{
/**
* @dataProvider getTestDataForMakePeriodFromQueryParams
*/
public function test_makePeriodFromQueryParams_appliesTimezoneProperly($period, $date, $expectedLabel, $expectedRange)
public function test_makePeriodFromQueryParams_appliesTimezoneProperly($now, $timezone, $period, $date, $expectedLabel, $expectedRange)
{
Date::$now = strtotime('2020-12-24 03:37:00');
Date::$now = strtotime($now);

$factory = Period\Factory::makePeriodFromQueryParams('America/Chicago', $period, $date);
$factory = Period\Factory::makePeriodFromQueryParams($timezone, $period, $date);
$this->assertEquals($expectedLabel, $factory->getLabel());
$this->assertEquals($expectedRange, $factory->getRangeString());
}

public function getTestDataForMakePeriodFromQueryParams()
{
return [
['day', 'now', 'day', '2020-12-23,2020-12-23'],
['day', 'today', 'day', '2020-12-23,2020-12-23'],
['day', 'yesterday', 'day', '2020-12-22,2020-12-22'],
['day', 'yesterdaySameTime', 'day', '2020-12-22,2020-12-22'],
['day', 'last-week', 'day', '2020-12-16,2020-12-16'],
['day', 'last-month', 'day', '2020-11-23,2020-11-23'],
['day', 'last-year', 'day', '2019-12-23,2019-12-23'],
['day', '2020-12-23', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'now', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'today', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 16:37:00', 'America/Chicago', 'day', 'today', 'day', '2020-12-24,2020-12-24'],
['2020-12-24 22:37:00', 'UTC+5', 'day', 'today', 'day', '2020-12-25,2020-12-25'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'yesterday', 'day', '2020-12-22,2020-12-22'],
['2020-12-24 03:37:00', 'UTC+5', 'day', 'yesterday', 'day', '2020-12-23,2020-12-23'],
['2020-12-24 16:37:00', 'UTC+12', 'day', 'yesterday', 'day', '2020-12-24,2020-12-24'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'yesterdaySameTime', 'day', '2020-12-22,2020-12-22'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-week', 'day', '2020-12-16,2020-12-16'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-month', 'day', '2020-11-23,2020-11-23'],
['2020-12-24 03:37:00', 'UTC', 'week', 'last-month', 'week', '2020-11-23,2020-11-29'],
['2020-12-23 03:37:00', 'America/Chicago', 'week', 'last-month', 'week', '2020-11-16,2020-11-22'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-year', 'day', '2019-12-23,2019-12-23'],
['2020-12-24 03:37:00', 'America/Chicago', 'day', '2020-12-23', 'day', '2020-12-23,2020-12-23'],
];
}

Expand Down