Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Giehl <stefan@matomo.org>2020-12-10 05:18:07 +0300
committerGitHub <noreply@github.com>2020-12-10 05:18:07 +0300
commit7cde69f763f2cbf2b9a27c8a7dcf7bcaa950c3bd (patch)
treeda719a778d96e167ee4ba55c89a529c1c3bb0dce
parent06918eea4d2ca01298200348b40f57817433b931 (diff)
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
-rw-r--r--core/Date.php59
-rw-r--r--core/Period/Factory.php11
-rw-r--r--core/Period/Range.php4
-rw-r--r--plugins/CoreHome/angularjs/common/services/periods.js20
-rw-r--r--tests/PHPUnit/Fixtures/VisitsInCurrentYear.php70
-rw-r--r--tests/PHPUnit/Framework/TestRequest/Collection.php3
-rw-r--r--tests/PHPUnit/System/SpecialDateParamsTest.php78
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__Actions.getPageTitles_day.xml27
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_lastmonth__VisitsSummary.getVisits_day.xml2
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_lastweek__Actions.getPageTitles_day.xml27
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_lastweek__VisitsSummary.getVisits_day.xml2
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_lastyear__Actions.getPageTitles_day.xml27
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_lastyear__VisitsSummary.getVisits_day.xml2
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_range__Actions.getPageTitles_range.xml75
-rw-r--r--tests/PHPUnit/System/expected/test_specialDateParams_range__VisitsSummary.getVisits_range.xml2
-rw-r--r--tests/PHPUnit/Unit/DateTest.php119
16 files changed, 471 insertions, 57 deletions
diff --git a/core/Date.php b/core/Date.php
index 4b67eeb9cb..328467ab67 100644
--- a/core/Date.php
+++ b/core/Date.php
@@ -135,6 +135,12 @@ class Date
$date = self::yesterday();
} elseif ($dateString === 'yesterdaySameTime') {
$date = self::yesterdaySameTime();
+ } else if (preg_match('/last[ -]?week/i', urldecode($dateString))) {
+ $date = self::lastWeek();
+ } else if (preg_match('/last[ -]?month/i', urldecode($dateString))) {
+ $date = self::lastMonth();
+ } else if (preg_match('/last[ -]?year/i', urldecode($dateString))) {
+ $date = self::lastYear();
} elseif (!is_int($dateString)
&& (
// strtotime returns the timestamp for April 1st for a date like 2011-04-01,today
@@ -187,6 +193,12 @@ class Date
return self::yesterdayInTimezone($timezone);
} else if ($dateString === 'yesterdaySameTime') {
return self::yesterdaySameTimeInTimezone($timezone);
+ } else if (preg_match('/last[ -]?week/i', urldecode($dateString))) {
+ return self::lastWeekInTimezone($timezone);
+ } else if (preg_match('/last[ -]?month/i', urldecode($dateString))) {
+ return self::lastMonthInTimezone($timezone);
+ } else if (preg_match('/last[ -]?year/i', urldecode($dateString))) {
+ return self::lastYearInTimezone($timezone);
} else {
throw new \Exception("Date::factoryInTimezone() should not be used with $dateString.");
}
@@ -214,6 +226,21 @@ class Date
return self::nowInTimezone($timezone)->subDay(1);
}
+ private static function lastWeekInTimezone($timezone)
+ {
+ return new Date(strtotime('-1week', self::todayInTimezone($timezone)->getTimestamp()));
+ }
+
+ private static function lastMonthInTimezone($timezone)
+ {
+ return new Date(strtotime('-1month', self::todayInTimezone($timezone)->getTimestamp()));
+ }
+
+ private static function lastYearInTimezone($timezone)
+ {
+ return new Date(strtotime('-1year', self::todayInTimezone($timezone)->getTimestamp()));
+ }
+
/**
* Returns the current timestamp as a string with the following format: `'YYYY-MM-DD HH:MM:SS'`.
*
@@ -586,7 +613,37 @@ class Date
*/
public static function yesterdaySameTime()
{
- return new Date(strtotime("yesterday " . date('H:i:s'), self::getNowTimestamp()));
+ return new Date(strtotime("yesterday " . date('H:i:s', self::getNowTimestamp()), self::getNowTimestamp()));
+ }
+
+ /**
+ * Returns a date object set to the day a week ago at midnight in UTC.
+ *
+ * @return \Piwik\Date
+ */
+ public static function lastWeek()
+ {
+ return new Date(strtotime("-1week 00:00:00", self::getNowTimestamp()));
+ }
+
+ /**
+ * Returns a date object set to the day a month ago at midnight in UTC.
+ *
+ * @return \Piwik\Date
+ */
+ public static function lastMonth()
+ {
+ return new Date(strtotime("-1month 00:00:00", self::getNowTimestamp()));
+ }
+
+ /**
+ * Returns a date object set to the day a year ago at midnight in UTC.
+ *
+ * @return \Piwik\Date
+ */
+ public static function lastYear()
+ {
+ return new Date(strtotime("-1year 00:00:00", self::getNowTimestamp()));
}
/**
diff --git a/core/Period/Factory.php b/core/Period/Factory.php
index 052e7c0dfb..75b4e35214 100644
--- a/core/Period/Factory.php
+++ b/core/Period/Factory.php
@@ -74,7 +74,7 @@ abstract class Factory
self::checkPeriodIsEnabled($period);
if (is_string($date)) {
- list($period, $date) = self::convertRangeToDateIfNeeded($period, $date);
+ [$period, $date] = self::convertRangeToDateIfNeeded($period, $date);
if (Period::isMultiplePeriod($date, $period)
|| $period == 'range'
) {
@@ -160,19 +160,14 @@ abstract class Factory
$timezone = 'UTC';
}
- list($period, $date) = self::convertRangeToDateIfNeeded($period, $date);
+ [$period, $date] = self::convertRangeToDateIfNeeded($period, $date);
if ($period == 'range') {
self::checkPeriodIsEnabled('range');
$oPeriod = new Range('range', $date, $timezone, Date::factory('today', $timezone));
} else {
if (!($date instanceof Date)) {
- if ($date == 'now' || $date == 'today') {
- $date = date('Y-m-d', Date::factory('now', $timezone)->getTimestamp());
- } elseif ($date == 'yesterday' || $date == 'yesterdaySameTime') {
- $date = date('Y-m-d', Date::factory('now', $timezone)->subDay(1)->getTimestamp());
- }
- $date = Date::factory($date);
+ $date = Date::factory($date, $timezone);
}
$oPeriod = Factory::build($period, $date);
}
diff --git a/core/Period/Range.php b/core/Period/Range.php
index 0f466912a6..1a4e381dc6 100644
--- a/core/Period/Range.php
+++ b/core/Period/Range.php
@@ -220,7 +220,7 @@ class Range extends Period
parent::generate();
- if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) {
+ if (preg_match('/^(last|previous)([0-9]*)$/', $this->strDate, $regs)) {
$lastN = $regs[2];
$lastOrPrevious = $regs[1];
if (!is_null($this->defaultEndDate)) {
@@ -290,7 +290,7 @@ class Range extends Period
*/
public static function parseDateRange($dateString)
{
- $matched = preg_match('/^([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}),(([0-9]{4}-[0-9]{1,2}-[0-9]{1,2})|today|now|yesterday)$/D', trim($dateString), $regs);
+ $matched = preg_match('/^((?:[0-9]{4}-[0-9]{1,2}-[0-9]{1,2})|last[ -]?(?:week|month|year)),((?:[0-9]{4}-[0-9]{1,2}-[0-9]{1,2})|today|now|yesterday|last[ -]?(?:week|month|year))$/Di', trim($dateString), $regs);
if (empty($matched)) {
return false;
diff --git a/plugins/CoreHome/angularjs/common/services/periods.js b/plugins/CoreHome/angularjs/common/services/periods.js
index 8b7283cda3..a6d3896b03 100644
--- a/plugins/CoreHome/angularjs/common/services/periods.js
+++ b/plugins/CoreHome/angularjs/common/services/periods.js
@@ -321,6 +321,8 @@
return strDate;
}
+ strDate = decodeURIComponent(strDate);
+
if (strDate === 'today'
|| strDate === 'now'
) {
@@ -336,6 +338,24 @@
return yesterday;
}
+ if (strDate.match(/last[ -]?week/i)) {
+ var lastWeek = getToday();
+ lastWeek.setDate(lastWeek.getDate() - 7);
+ return lastWeek;
+ }
+
+ if (strDate.match(/last[ -]?month/i)) {
+ var lastMonth = getToday();
+ lastMonth.setMonth(lastMonth.getMonth() - 1);
+ return lastMonth;
+ }
+
+ if (strDate.match(/last[ -]?year/i)) {
+ var lastYear = getToday();
+ lastYear.setFullYear(lastYear.getFullYear() - 1);
+ return lastYear;
+ }
+
try {
return $.datepicker.parseDate('yy-mm-dd', strDate);
} catch (err) {
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 @@
+<?php
+/**
+ * Matomo - free/libre analytics platform
+ *
+ * @link https://matomo.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+namespace Piwik\Tests\Fixtures;
+
+use Piwik\Common;
+use Piwik\Date;
+use Piwik\Db;
+use Piwik\Plugins\Goals\API as APIGoals;
+use Piwik\Plugins\SitesManager\API as APISitesManager;
+use Piwik\Tests\Framework\Fixture;
+use Piwik\Tracker\Cache;
+
+/**
+ * This fixture adds one website and tracks two visits by one visitor.
+ */
+class VisitsInCurrentYear extends Fixture
+{
+ public $idSite = 1;
+
+ public function setUp(): void
+ {
+ $this->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&parameter=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&parameter=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 @@
+<?php
+/**
+ * Matomo - free/libre analytics platform
+ *
+ * @link https://matomo.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+
+namespace Piwik\Tests\System;
+
+use Piwik\Tests\Fixtures\VisitsInCurrentYear;
+use Piwik\Tests\Framework\TestCase\SystemTestCase;
+
+/**
+ * @group SpecialDateParams
+ * @group Core
+ */
+class SpecialDateParamsTest extends SystemTestCase
+{
+ public static $fixture = null; // initialized below class definition
+
+ /**
+ * @dataProvider getApiForTesting
+ */
+ public function testApi($api, $params)
+ {
+ $this->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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>
+ <row>
+ <label> Checkout/Purchasing...</label>
+ <nb_visits>1</nb_visits>
+ <nb_uniq_visitors>1</nb_uniq_visitors>
+ <nb_hits>1</nb_hits>
+ <sum_time_spent>0</sum_time_spent>
+ <sum_bandwidth>0</sum_bandwidth>
+ <nb_hits_with_bandwidth>0</nb_hits_with_bandwidth>
+ <min_bandwidth />
+ <max_bandwidth />
+ <entry_nb_uniq_visitors>1</entry_nb_uniq_visitors>
+ <entry_nb_visits>1</entry_nb_visits>
+ <entry_nb_actions>1</entry_nb_actions>
+ <entry_sum_visit_length>0</entry_sum_visit_length>
+ <entry_bounce_count>1</entry_bounce_count>
+ <exit_nb_uniq_visitors>1</exit_nb_uniq_visitors>
+ <exit_nb_visits>1</exit_nb_visits>
+ <avg_bandwidth>0</avg_bandwidth>
+ <avg_page_load_time>0</avg_page_load_time>
+ <avg_time_on_page>0</avg_time_on_page>
+ <bounce_rate>100%</bounce_rate>
+ <exit_rate>100%</exit_rate>
+ <segment>pageTitle==Checkout%252FPurchasing...</segment>
+ </row>
+</result> \ 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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>1</result> \ 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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>
+ <row>
+ <label> incredible!</label>
+ <nb_visits>1</nb_visits>
+ <nb_uniq_visitors>1</nb_uniq_visitors>
+ <nb_hits>1</nb_hits>
+ <sum_time_spent>0</sum_time_spent>
+ <sum_bandwidth>0</sum_bandwidth>
+ <nb_hits_with_bandwidth>0</nb_hits_with_bandwidth>
+ <min_bandwidth />
+ <max_bandwidth />
+ <entry_nb_uniq_visitors>1</entry_nb_uniq_visitors>
+ <entry_nb_visits>1</entry_nb_visits>
+ <entry_nb_actions>1</entry_nb_actions>
+ <entry_sum_visit_length>0</entry_sum_visit_length>
+ <entry_bounce_count>1</entry_bounce_count>
+ <exit_nb_uniq_visitors>1</exit_nb_uniq_visitors>
+ <exit_nb_visits>1</exit_nb_visits>
+ <avg_bandwidth>0</avg_bandwidth>
+ <avg_page_load_time>0</avg_page_load_time>
+ <avg_time_on_page>0</avg_time_on_page>
+ <bounce_rate>100%</bounce_rate>
+ <exit_rate>100%</exit_rate>
+ <segment>pageTitle==incredible%2521</segment>
+ </row>
+</result> \ 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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>1</result> \ 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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>
+ <row>
+ <label> Visit product</label>
+ <nb_visits>1</nb_visits>
+ <nb_uniq_visitors>1</nb_uniq_visitors>
+ <nb_hits>1</nb_hits>
+ <sum_time_spent>0</sum_time_spent>
+ <sum_bandwidth>0</sum_bandwidth>
+ <nb_hits_with_bandwidth>0</nb_hits_with_bandwidth>
+ <min_bandwidth />
+ <max_bandwidth />
+ <entry_nb_uniq_visitors>1</entry_nb_uniq_visitors>
+ <entry_nb_visits>1</entry_nb_visits>
+ <entry_nb_actions>1</entry_nb_actions>
+ <entry_sum_visit_length>0</entry_sum_visit_length>
+ <entry_bounce_count>1</entry_bounce_count>
+ <exit_nb_uniq_visitors>1</exit_nb_uniq_visitors>
+ <exit_nb_visits>1</exit_nb_visits>
+ <avg_bandwidth>0</avg_bandwidth>
+ <avg_page_load_time>0</avg_page_load_time>
+ <avg_time_on_page>0</avg_time_on_page>
+ <bounce_rate>100%</bounce_rate>
+ <exit_rate>100%</exit_rate>
+ <segment>pageTitle==Visit%2Bproduct</segment>
+ </row>
+</result> \ 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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>1</result> \ 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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>
+ <row>
+ <label> Checkout/Purchasing...</label>
+ <nb_visits>1</nb_visits>
+ <nb_hits>1</nb_hits>
+ <sum_time_spent>0</sum_time_spent>
+ <sum_bandwidth>0</sum_bandwidth>
+ <nb_hits_with_bandwidth>0</nb_hits_with_bandwidth>
+ <min_bandwidth />
+ <max_bandwidth />
+ <entry_nb_visits>1</entry_nb_visits>
+ <entry_nb_actions>1</entry_nb_actions>
+ <entry_sum_visit_length>0</entry_sum_visit_length>
+ <entry_bounce_count>1</entry_bounce_count>
+ <exit_nb_visits>1</exit_nb_visits>
+ <sum_daily_nb_uniq_visitors>1</sum_daily_nb_uniq_visitors>
+ <sum_daily_entry_nb_uniq_visitors>1</sum_daily_entry_nb_uniq_visitors>
+ <sum_daily_exit_nb_uniq_visitors>1</sum_daily_exit_nb_uniq_visitors>
+ <avg_bandwidth>0</avg_bandwidth>
+ <avg_page_load_time>0</avg_page_load_time>
+ <avg_time_on_page>0</avg_time_on_page>
+ <bounce_rate>100%</bounce_rate>
+ <exit_rate>100%</exit_rate>
+ <segment>pageTitle==Checkout%252FPurchasing...</segment>
+ </row>
+ <row>
+ <label> incredible!</label>
+ <nb_visits>1</nb_visits>
+ <nb_hits>1</nb_hits>
+ <sum_time_spent>0</sum_time_spent>
+ <sum_bandwidth>0</sum_bandwidth>
+ <nb_hits_with_bandwidth>0</nb_hits_with_bandwidth>
+ <min_bandwidth />
+ <max_bandwidth />
+ <entry_nb_visits>1</entry_nb_visits>
+ <entry_nb_actions>1</entry_nb_actions>
+ <entry_sum_visit_length>0</entry_sum_visit_length>
+ <entry_bounce_count>1</entry_bounce_count>
+ <exit_nb_visits>1</exit_nb_visits>
+ <sum_daily_nb_uniq_visitors>1</sum_daily_nb_uniq_visitors>
+ <sum_daily_entry_nb_uniq_visitors>1</sum_daily_entry_nb_uniq_visitors>
+ <sum_daily_exit_nb_uniq_visitors>1</sum_daily_exit_nb_uniq_visitors>
+ <avg_bandwidth>0</avg_bandwidth>
+ <avg_page_load_time>0</avg_page_load_time>
+ <avg_time_on_page>0</avg_time_on_page>
+ <bounce_rate>100%</bounce_rate>
+ <exit_rate>100%</exit_rate>
+ <segment>pageTitle==incredible%2521</segment>
+ </row>
+ <row>
+ <label> Visit product</label>
+ <nb_visits>1</nb_visits>
+ <nb_hits>1</nb_hits>
+ <sum_time_spent>0</sum_time_spent>
+ <sum_bandwidth>0</sum_bandwidth>
+ <nb_hits_with_bandwidth>0</nb_hits_with_bandwidth>
+ <min_bandwidth />
+ <max_bandwidth />
+ <entry_nb_visits>1</entry_nb_visits>
+ <entry_nb_actions>1</entry_nb_actions>
+ <entry_sum_visit_length>0</entry_sum_visit_length>
+ <entry_bounce_count>1</entry_bounce_count>
+ <exit_nb_visits>1</exit_nb_visits>
+ <sum_daily_nb_uniq_visitors>1</sum_daily_nb_uniq_visitors>
+ <sum_daily_entry_nb_uniq_visitors>1</sum_daily_entry_nb_uniq_visitors>
+ <sum_daily_exit_nb_uniq_visitors>1</sum_daily_exit_nb_uniq_visitors>
+ <avg_bandwidth>0</avg_bandwidth>
+ <avg_page_load_time>0</avg_page_load_time>
+ <avg_time_on_page>0</avg_time_on_page>
+ <bounce_rate>100%</bounce_rate>
+ <exit_rate>100%</exit_rate>
+ <segment>pageTitle==Visit%2Bproduct</segment>
+ </row>
+</result> \ 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 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>3</result> \ 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'],
];
}