Skip to content

Commit

Permalink
Merge branch '5.x-dev' into PG-3814-check-license-status
Browse files Browse the repository at this point in the history
  • Loading branch information
AltamashShaikh authored Sep 25, 2024
2 parents e5f2014 + ec8cf14 commit 7245f86
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
21 changes: 21 additions & 0 deletions core/Period/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ protected function generate()
if (strpos($strDateEnd, '-') === false) {
$timezone = $this->timezone;
}

$endDate = Date::factory($strDateEnd, $timezone)->setTime("00:00:00");
$maxAllowedEndDate = Date::factory(self::getMaxAllowedEndTimestamp());

if ($endDate->isLater($maxAllowedEndDate)) {
$endDate = $maxAllowedEndDate;
}
} else {
throw new Exception($this->translator->translate('General_ExceptionInvalidDateRange', array($this->strDate, ' \'lastN\', \'previousN\', \'YYYY-MM-DD,YYYY-MM-DD\'')));
}
Expand Down Expand Up @@ -587,4 +593,19 @@ public function getParentPeriodLabel()
{
return null;
}

/**
* Returns the max allowed end timestamp for a range. If an enddate after this timestamp is provided, Matomo will
* automatically lower the end date to the date returned by this method.
* The max supported timestamp is always set to end of the current year plus 10 years.
*
* @return int
* @api
*/
public static function getMaxAllowedEndTimestamp(): int
{
return strtotime(
date('Y-12-31 00:00:00', strtotime('+10 year', Date::getNowTimestamp()))
);
}
}
17 changes: 8 additions & 9 deletions tests/PHPUnit/Unit/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,14 @@ public function testInvalidDateThrowsException($valueToTest)
Date::factory($valueToTest);
}

public function getInvalidDates(): array
{
return [
['0001-01-01'],
['randomString'],
[null],
[''],
[['arrayValue']],
];
public function getInvalidDates(): iterable
{
yield 'valid format, earliest possible date' => ['0001-01-01'];
yield 'valid format, day before first website creation' => ['1991-08-05'];
yield 'ivalid string value' => ['randomString'];
yield 'empty string value' => [''];
yield 'null value' => [null];
yield 'array value' => [['arrayValue']];
}

public function getTimezoneOffsets()
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPUnit/Unit/Period/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,17 @@ public function testRangeMonthcomma1()
$this->assertEquals('2006-12-01,2007-01-31', $range->getRangeString());
}

// test range date1,date2
public function testRangeMonthcommaAfterMaxAllowedDate()
{
Date::$now = strtotime('2024-07-09');
$range = new Range('month', '2024-01-01,2100-01-03');

// range should be limited to 2034, so includes 11 years
$this->assertEquals(11 * 12, $range->getNumberOfSubperiods());
$this->assertEquals('2024-01-01,2034-12-31', $range->getRangeString());
}

// test range WEEK
public function testRangeWeek()
{
Expand Down Expand Up @@ -1215,6 +1226,28 @@ public function testCustomRangeBeforeIsAfterYearRight()
$range->getPrettyString();
}

/**
* @dataProvider getAbnormalDateRanges
*/
public function testCustomRangeWithOutOfRangeDate($dateStr)
{
self::expectException(Exception::class);

$range = new Range('range', $dateStr);
$range->getDateStart();
}

public function getAbnormalDateRanges(): iterable
{
yield 'range starts before first website creation' => [
'1900-01-01,2021-01-01',
];

yield 'range starts after it ends' => [
'2024-01-01,2020-12-16',
];
}

public function testCustomRangeLastN()
{
$range = new Range('range', 'last4');
Expand Down

0 comments on commit 7245f86

Please sign in to comment.