diff options
author | Stefan Giehl <stefan@matomo.org> | 2020-12-24 08:21:02 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-24 08:21:02 +0300 |
commit | 04ac70e0cbf8b7a5ff3d5de66fc0825ace4b0b71 (patch) | |
tree | 29f57e261ffe1d82971e5b1a10da335dae6593f6 | |
parent | ddfa42e16f6648a1f54d2912fcc897e0ae07b75e (diff) |
Fix report showing data of wrong date (#17008)
* Use timezone in period factory only for magic keywords
* add test
* use better timezone
* add non-timezone date
Co-authored-by: diosmosis <diosmosis@users.noreply.github.com>
-rw-r--r-- | core/Period/Factory.php | 6 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Period/FactoryTest.php | 29 |
2 files changed, 33 insertions, 2 deletions
diff --git a/core/Period/Factory.php b/core/Period/Factory.php index 75b4e35214..b316bebefa 100644 --- a/core/Period/Factory.php +++ b/core/Period/Factory.php @@ -167,7 +167,11 @@ abstract class Factory $oPeriod = new Range('range', $date, $timezone, Date::factory('today', $timezone)); } else { if (!($date instanceof Date)) { - $date = Date::factory($date, $timezone); + if (preg_match('/^(now|today|yesterday|yesterdaySameTime|last[ -]?(?:week|month|year))$/i', $date)) { + $date = Date::factory($date, $timezone); + } else { + $date = Date::factory($date); + } } $oPeriod = Factory::build($period, $date); } diff --git a/tests/PHPUnit/Integration/Period/FactoryTest.php b/tests/PHPUnit/Integration/Period/FactoryTest.php index 9ff978a646..ba1ee78164 100644 --- a/tests/PHPUnit/Integration/Period/FactoryTest.php +++ b/tests/PHPUnit/Integration/Period/FactoryTest.php @@ -9,6 +9,7 @@ namespace Piwik\Tests\Integration\Period; use Piwik\Config; +use Piwik\Date; use Piwik\Period; use Piwik\Period\Day; use Piwik\Period\Month; @@ -63,10 +64,36 @@ class MockPluginManager extends \Piwik\Plugin\Manager class FactoryTest extends IntegrationTestCase { /** + * @dataProvider getTestDataForMakePeriodFromQueryParams + */ + public function test_makePeriodFromQueryParams_appliesTimezoneProperly($period, $date, $expectedLabel, $expectedRange) + { + Date::$now = strtotime('2020-12-24 03:37:00'); + + $factory = Period\Factory::makePeriodFromQueryParams('America/Chicago', $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'], + ]; + } + + /** * @dataProvider getBuildTestData */ public function test_build_CreatesCorrectPeriodInstances($strPeriod, $date, $timezone, $expectedPeriodClass, - $expectedRangeString) + $expectedRangeString) { $period = Period\Factory::build($strPeriod, $date, $timezone); $this->assertInstanceOf($expectedPeriodClass, $period); |