From 7cde69f763f2cbf2b9a27c8a7dcf7bcaa950c3bd Mon Sep 17 00:00:00 2001 From: Stefan Giehl Date: Thu, 10 Dec 2020 03:18:07 +0100 Subject: Allow using last (week|month|year) as date param (#16830) * Allow using last (week|month|year) as date param * Adds some unit tests * improve tests * Make it possible to use last (week|month|year) for api requests * improve test --- tests/PHPUnit/Fixtures/VisitsInCurrentYear.php | 70 ++++++++++++ tests/PHPUnit/Framework/TestRequest/Collection.php | 3 +- tests/PHPUnit/System/SpecialDateParamsTest.php | 78 ++++++++++++++ ...Params_lastmonth__Actions.getPageTitles_day.xml | 27 +++++ ...rams_lastmonth__VisitsSummary.getVisits_day.xml | 2 + ...eParams_lastweek__Actions.getPageTitles_day.xml | 27 +++++ ...arams_lastweek__VisitsSummary.getVisits_day.xml | 2 + ...eParams_lastyear__Actions.getPageTitles_day.xml | 27 +++++ ...arams_lastyear__VisitsSummary.getVisits_day.xml | 2 + ...teParams_range__Actions.getPageTitles_range.xml | 75 +++++++++++++ ...Params_range__VisitsSummary.getVisits_range.xml | 2 + tests/PHPUnit/Unit/DateTest.php | 119 +++++++++++++-------- 12 files changed, 388 insertions(+), 46 deletions(-) create mode 100644 tests/PHPUnit/Fixtures/VisitsInCurrentYear.php create mode 100644 tests/PHPUnit/System/SpecialDateParamsTest.php create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__Actions.getPageTitles_day.xml create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__VisitsSummary.getVisits_day.xml create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_lastweek__Actions.getPageTitles_day.xml create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_lastweek__VisitsSummary.getVisits_day.xml create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_lastyear__Actions.getPageTitles_day.xml create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_lastyear__VisitsSummary.getVisits_day.xml create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_range__Actions.getPageTitles_range.xml create mode 100644 tests/PHPUnit/System/expected/test_specialDateParams_range__VisitsSummary.getVisits_range.xml (limited to 'tests') diff --git a/tests/PHPUnit/Fixtures/VisitsInCurrentYear.php b/tests/PHPUnit/Fixtures/VisitsInCurrentYear.php new file mode 100644 index 0000000000..b77c361dbb --- /dev/null +++ b/tests/PHPUnit/Fixtures/VisitsInCurrentYear.php @@ -0,0 +1,70 @@ +setUpWebsite(); + $this->trackVisits(); + } + + public function tearDown(): void + { + // empty + } + + private function setUpWebsite() + { + if (!self::siteCreated($idSite = 1)) { + self::createWebsite('2018-01-01 15:00:00'); + } + } + + private function trackVisits() + { + $idSite = $this->idSite; + + // Record 1st visit today + $t = self::getTracker($idSite, date('Y-m-d H:i:s'), $defaultInit = true); + $t->setUrl('http://example.org/index.htm?excluded_Parameter=SHOULD_NOT_DISPLAY¶meter=Should display'); + $t->setUrlReferrer('http://referrer.com/page.htm?param=valuewith some spaces'); + self::checkResponse($t->doTrackPageView('incredible title!')); + + // Record 2nd visit 7 days ago + $t = self::getTracker($idSite, date('Y-m-d H:i:s', strtotime('-7days')), $defaultInit = true); + $t->setUrl('http://example.org/index.htm?excluded_Parameter=SHOULD_NOT_DISPLAY¶meter=Should display'); + $t->setUrlReferrer('http://referrer.com/page.htm'); + self::checkResponse($t->doTrackPageView('incredible!')); + + // Record 3rd visit 1 month ago + $t = self::getTracker($idSite, date('Y-m-d H:i:s', strtotime('-1month')), $defaultInit = true); + $t->setUrl('http://example.org/store/purchase.htm'); + $t->setUrlReferrer('http://search.yahoo.com/search?p=purchase'); + self::checkResponse($t->doTrackPageView('Checkout/Purchasing...')); + + // Record 4th visit 1 year ago + $t = self::getTracker($idSite, date('Y-m-d H:i:s', strtotime('-1year')), $defaultInit = true); + $t->setUrl('http://example.org/shop/product.htm'); + self::checkResponse($t->doTrackPageView('Visit product')); + } +} \ No newline at end of file diff --git a/tests/PHPUnit/Framework/TestRequest/Collection.php b/tests/PHPUnit/Framework/TestRequest/Collection.php index 0510b7df4b..1a7310f78b 100644 --- a/tests/PHPUnit/Framework/TestRequest/Collection.php +++ b/tests/PHPUnit/Framework/TestRequest/Collection.php @@ -11,6 +11,7 @@ namespace Piwik\Tests\Framework\TestRequest; use Piwik\API\DocumentationGenerator; use Piwik\API\Proxy; use Piwik\API\Request; +use Piwik\Date; use Piwik\Tests\Framework\TestCase\SystemTestCase; use Piwik\Url; use Piwik\UrlHelper; @@ -109,7 +110,7 @@ class Collection { $parametersToSet = array( 'idSite' => $this->testConfig->idSite, - 'date' => ($this->testConfig->periods == array('range') || strpos($this->testConfig->date, ',') !== false) ? + 'date' => ($this->testConfig->periods == array('range') || strpos($this->testConfig->date, ',') !== false || preg_match('/last[ -]?(week|month|year)/i', $this->testConfig->date)) ? $this->testConfig->date : date('Y-m-d', strtotime($this->testConfig->date)), 'expanded' => '1', 'piwikUrl' => 'http://example.org/piwik/', diff --git a/tests/PHPUnit/System/SpecialDateParamsTest.php b/tests/PHPUnit/System/SpecialDateParamsTest.php new file mode 100644 index 0000000000..f3508c2223 --- /dev/null +++ b/tests/PHPUnit/System/SpecialDateParamsTest.php @@ -0,0 +1,78 @@ +runApiTests($api, $params); + } + + public function getApiForTesting() + { + return [ + [ + ['VisitsSummary.getVisits', 'Actions.getPageTitles'], + [ + 'idSite' => self::$fixture->idSite, + 'date' => 'last week', + 'periods' => ['day'], + 'testSuffix' => '_lastweek', + ], + ], + [ + ['VisitsSummary.getVisits', 'Actions.getPageTitles'], + [ + 'idSite' => self::$fixture->idSite, + 'date' => 'last month', + 'periods' => ['day'], + 'testSuffix' => '_lastmonth', + ], + ], + [ + ['VisitsSummary.getVisits', 'Actions.getPageTitles'], + [ + 'idSite' => self::$fixture->idSite, + 'date' => 'lastyear', + 'periods' => ['day'], + 'testSuffix' => '_lastyear', + ], + ], + [ + ['VisitsSummary.getVisits', 'Actions.getPageTitles'], + [ + 'idSite' => self::$fixture->idSite, + 'date' => 'last-year,lastWeek', + 'periods' => ['range'], + 'testSuffix' => '_range', + ], + ], + ]; + } + + public static function getOutputPrefix() + { + return 'specialDateParams'; + } +} + +SpecialDateParamsTest::$fixture = new VisitsInCurrentYear(); \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__Actions.getPageTitles_day.xml b/tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__Actions.getPageTitles_day.xml new file mode 100644 index 0000000000..f5bc869511 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__Actions.getPageTitles_day.xml @@ -0,0 +1,27 @@ + + + + + 1 + 1 + 1 + 0 + 0 + 0 + + + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 0 + 0 + 0 + 100% + 100% + pageTitle==Checkout%252FPurchasing... + + \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__VisitsSummary.getVisits_day.xml b/tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__VisitsSummary.getVisits_day.xml new file mode 100644 index 0000000000..606fbb5241 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__VisitsSummary.getVisits_day.xml @@ -0,0 +1,2 @@ + +1 \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_lastweek__Actions.getPageTitles_day.xml b/tests/PHPUnit/System/expected/test_specialDateParams_lastweek__Actions.getPageTitles_day.xml new file mode 100644 index 0000000000..0cbcf088c9 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_lastweek__Actions.getPageTitles_day.xml @@ -0,0 +1,27 @@ + + + + + 1 + 1 + 1 + 0 + 0 + 0 + + + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 0 + 0 + 0 + 100% + 100% + pageTitle==incredible%2521 + + \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_lastweek__VisitsSummary.getVisits_day.xml b/tests/PHPUnit/System/expected/test_specialDateParams_lastweek__VisitsSummary.getVisits_day.xml new file mode 100644 index 0000000000..606fbb5241 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_lastweek__VisitsSummary.getVisits_day.xml @@ -0,0 +1,2 @@ + +1 \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_lastyear__Actions.getPageTitles_day.xml b/tests/PHPUnit/System/expected/test_specialDateParams_lastyear__Actions.getPageTitles_day.xml new file mode 100644 index 0000000000..2a3ee0717b --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_lastyear__Actions.getPageTitles_day.xml @@ -0,0 +1,27 @@ + + + + + 1 + 1 + 1 + 0 + 0 + 0 + + + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 0 + 0 + 0 + 100% + 100% + pageTitle==Visit%2Bproduct + + \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_lastyear__VisitsSummary.getVisits_day.xml b/tests/PHPUnit/System/expected/test_specialDateParams_lastyear__VisitsSummary.getVisits_day.xml new file mode 100644 index 0000000000..606fbb5241 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_lastyear__VisitsSummary.getVisits_day.xml @@ -0,0 +1,2 @@ + +1 \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_range__Actions.getPageTitles_range.xml b/tests/PHPUnit/System/expected/test_specialDateParams_range__Actions.getPageTitles_range.xml new file mode 100644 index 0000000000..759d5c3392 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_range__Actions.getPageTitles_range.xml @@ -0,0 +1,75 @@ + + + + + 1 + 1 + 0 + 0 + 0 + + + 1 + 1 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 0 + 0 + 100% + 100% + pageTitle==Checkout%252FPurchasing... + + + + 1 + 1 + 0 + 0 + 0 + + + 1 + 1 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 0 + 0 + 100% + 100% + pageTitle==incredible%2521 + + + + 1 + 1 + 0 + 0 + 0 + + + 1 + 1 + 0 + 1 + 1 + 1 + 1 + 1 + 0 + 0 + 0 + 100% + 100% + pageTitle==Visit%2Bproduct + + \ No newline at end of file diff --git a/tests/PHPUnit/System/expected/test_specialDateParams_range__VisitsSummary.getVisits_range.xml b/tests/PHPUnit/System/expected/test_specialDateParams_range__VisitsSummary.getVisits_range.xml new file mode 100644 index 0000000000..15ef03fb49 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_specialDateParams_range__VisitsSummary.getVisits_range.xml @@ -0,0 +1,2 @@ + +3 \ No newline at end of file diff --git a/tests/PHPUnit/Unit/DateTest.php b/tests/PHPUnit/Unit/DateTest.php index 2a328b6717..bf6da99863 100644 --- a/tests/PHPUnit/Unit/DateTest.php +++ b/tests/PHPUnit/Unit/DateTest.php @@ -15,6 +15,8 @@ use Piwik\SettingsServer; use Piwik\Tests\Framework\Fixture; /** + * @group Core + * @group DateTest */ class DateTest extends \PHPUnit\Framework\TestCase { @@ -36,8 +38,6 @@ class DateTest extends \PHPUnit\Framework\TestCase /** * create today object check that timestamp is correct (midnight) - * - * @group Core */ public function testToday() { @@ -52,29 +52,70 @@ class DateTest extends \PHPUnit\Framework\TestCase /** * create tomorrow object check that timestamp is correct (midnight) - * - * @group Core */ public function testTomorrow() { + Date::$now = strtotime('2020-05-05 17:00:00'); // 1588698000 $date = Date::tomorrow(); - $this->assertEquals(strtotime(date("Y-m-d ", strtotime('+1day')) . " 00:00:00"), $date->getTimestamp()); + $this->assertEquals("2020-05-06 00:00:00", $date->getDatetime()); + $this->assertEquals(1588723200, $date->getTimestamp()); } /** * create today object check that timestamp is correct (midnight) - * - * @group Core */ public function testYesterday() { + Date::$now = strtotime('2020-05-05 17:00:00'); // 1588698000 $date = Date::yesterday(); - $this->assertEquals(strtotime(date("Y-m-d", strtotime('-1day')) . " 00:00:00"), $date->getTimestamp()); + $this->assertEquals("2020-05-04 00:00:00", $date->getDatetime()); + $this->assertEquals(1588550400, $date->getTimestamp()); } /** - * @group Core + * create today object check that timestamp is correct (same time) */ + public function testYesterdaySameTime() + { + Date::$now = strtotime('2020-05-05 17:00:00'); // 1588698000 + $date = Date::yesterdaySameTime(); + $this->assertEquals("2020-05-04 17:00:00", $date->getDatetime()); + $this->assertEquals(1588611600, $date->getTimestamp()); + } + + /** + * create last week object check that timestamp is correct (midnight) + */ + public function testLastWeek() + { + Date::$now = strtotime('2020-05-05 17:00:00'); // 1588698000 + $date = Date::lastWeek(); + $this->assertEquals("2020-04-28 00:00:00", $date->getDatetime()); + $this->assertEquals(1588032000, $date->getTimestamp()); + } + + /** + * create last month object check that timestamp is correct (midnight) + */ + public function testLastMonth() + { + Date::$now = strtotime('2020-05-05 17:00:00'); // 1588698000 + $date = Date::lastMonth(); + $this->assertEquals("2020-04-05 00:00:00", $date->getDatetime()); + $this->assertEquals(1586044800, $date->getTimestamp()); + } + + /** + * create last year object check that timestamp is correct (midnight) + */ + public function testLastYear() + { + Date::$now = strtotime('2020-05-05 17:00:00'); // 1588698000 + $date = Date::lastYear(); + $this->assertEquals("2019-05-05 00:00:00", $date->getDatetime()); + $this->assertEquals(1557014400, $date->getTimestamp()); + } + public function testInvalidDateThrowsException() { $this->expectException(Exception::class); @@ -94,8 +135,6 @@ class DateTest extends \PHPUnit\Framework\TestCase } /** - * @group Core - * @group DateTest * @dataProvider getTimezoneOffsets */ public function testGetUtcOffset($timezone, $expectedOffset) @@ -104,9 +143,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertEquals($expectedOffset, $offset); } - /** - * @group Core - */ public function testFactoryTimezone() { // now in UTC converted to UTC+10 means adding 10 hours @@ -158,9 +194,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertSame('19', $hour); } - /** - * @group Core - */ public function testSetTimezoneDayInUTC() { $date = Date::factory('2010-01-01'); @@ -208,9 +241,6 @@ class DateTest extends \PHPUnit\Framework\TestCase } } - /** - * @group Core - */ public function testModifyDateWithTimezone() { $date = Date::factory('2010-01-01'); @@ -229,9 +259,6 @@ class DateTest extends \PHPUnit\Framework\TestCase } } - /** - * @group Core - */ public function testGetDateStartUTCEndDuringDstTimezone() { if (SettingsServer::isTimezoneSupportEnabled()) { @@ -246,9 +273,6 @@ class DateTest extends \PHPUnit\Framework\TestCase } } - /** - * @group Core - */ public function testAddHour() { // add partial hours less than 1 @@ -270,9 +294,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertEquals($dayExpected, $date->getDatetime()); } - /** - * @group Core - */ public function testAddHourLongHours() { $dateTime = '2010-01-03 11:22:33'; @@ -281,9 +302,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertEquals($dateTime, Date::factory($dateTime)->addHour(48.1)->subHour(48.1)->getDatetime()); } - /** - * @group Core - */ public function testAddPeriod() { $date = Date::factory('2010-01-01'); @@ -297,9 +315,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertEquals($dateExpected->getTimestamp(), $date->getTimestamp()); } - /** - * @group Core - */ public function testSubPeriod() { $date = Date::factory('2010-03-01'); @@ -313,9 +328,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertEquals($dateExpected->getTimestamp(), $date->getTimestamp()); } - /** - * @group Core - */ public function testSubSeconds() { $date = Date::factory('2010-03-01 00:01:25'); @@ -330,9 +342,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertSame($dateExpected->getTimestamp(), $date->getTimestamp()); } - /** - * @group Core - */ public function testAddPeriodMonthRespectsMaxDaysInMonth() { $date = Date::factory('2014-07-31'); @@ -352,9 +361,6 @@ class DateTest extends \PHPUnit\Framework\TestCase $this->assertEquals($dateExpected->toString(), $dateActual->toString()); } - /** - * @group Core - */ public function testIsLeapYear() { $date = Date::factory('2011-03-01'); @@ -395,7 +401,6 @@ class DateTest extends \PHPUnit\Framework\TestCase } /** - * @group Core * @dataProvider getLocalizedLongStrings */ public function testGetLocalizedTimeFormats($language, $use12HourClock, $time, $shouldBe) @@ -441,6 +446,18 @@ class DateTest extends \PHPUnit\Framework\TestCase ['yesterdaySameTime', 'UTC-5', '2012-12-30 20:00:00', '2013-01-01 01:00:00'], ['yesterdaySameTime', 'UTC-5', '2012-12-31 01:00:00', '2013-01-01 06:00:00'], ['yesterdaySameTime', 'America/Toronto', '2012-12-31 01:00:00', '2013-01-01 06:00:00'], + ['lastWeek', 'America/Toronto', '2012-12-24 00:00:00', '2013-01-01 01:00:00'], + ['lastweek', 'UTC-5', '2012-12-24 00:00:00', '2013-01-01 01:00:00'], + ['last week', 'UTC-5', '2012-12-25 00:00:00', '2013-01-01 06:00:00'], + ['last-week', 'America/Toronto', '2012-12-25 00:00:00', '2013-01-01 06:00:00'], + ['lastMonth', 'America/Toronto', '2012-12-01 00:00:00', '2013-01-01 01:00:00'], + ['lastmonth', 'UTC-5', '2012-12-01 00:00:00', '2013-01-01 01:00:00'], + ['last month', 'UTC-5', '2012-12-01 00:00:00', '2013-01-01 06:00:00'], + ['last-month', 'America/Toronto', '2012-12-01 00:00:00', '2013-01-01 06:00:00'], + ['lastYear', 'America/Toronto', '2011-12-31 00:00:00', '2013-01-01 01:00:00'], + ['lastyear', 'UTC-5', '2011-12-31 00:00:00', '2013-01-01 01:00:00'], + ['last year', 'UTC-5', '2012-01-01 00:00:00', '2013-01-01 06:00:00'], + ['last-year', 'America/Toronto', '2012-01-01 00:00:00', '2013-01-01 06:00:00'], // UTC+5 ['now', 'Antarctica/Mawson', '2012-12-31 19:00:00', '2012-12-31 14:00:00'], @@ -459,6 +476,18 @@ class DateTest extends \PHPUnit\Framework\TestCase ['yesterdaySameTime', 'UTC+5', '2012-12-30 19:00:00', '2012-12-31 14:00:00'], ['yesterdaySameTime', 'UTC+5', '2012-12-31 01:00:00', '2012-12-31 20:00:00'], ['yesterdaySameTime', 'Antarctica/Mawson', '2012-12-31 01:00:00', '2012-12-31 20:00:00'], + ['lastWeek', 'Antarctica/Mawson', '2012-12-24 00:00:00', '2012-12-31 14:00:00'], + ['lastweek', 'UTC+5', '2012-12-24 00:00:00', '2012-12-31 14:00:00'], + ['last week', 'UTC+5', '2012-12-25 00:00:00', '2012-12-31 19:00:00'], + ['last-week', 'Antarctica/Mawson', '2012-12-25 00:00:00', '2012-12-31 19:00:00'], + ['lastMonth', 'Antarctica/Mawson', '2012-12-01 00:00:00', '2012-12-31 14:00:00'], + ['lastmonth', 'UTC+5', '2012-12-01 00:00:00', '2012-12-31 14:00:00'], + ['last month', 'UTC+5', '2012-12-01 00:00:00', '2012-12-31 19:00:00'], + ['last-month', 'Antarctica/Mawson', '2012-12-01 00:00:00', '2012-12-31 19:00:00'], + ['lastYear', 'Antarctica/Mawson', '2011-12-31 00:00:00', '2012-12-31 14:00:00'], + ['lastyear', 'UTC+5', '2011-12-31 00:00:00', '2012-12-31 14:00:00'], + ['last year', 'UTC+5', '2012-01-01 00:00:00', '2012-12-31 19:00:00'], + ['last-year', 'Antarctica/Mawson', '2012-01-01 00:00:00', '2012-12-31 19:00:00'], ]; } -- cgit v1.2.3