Commit a13ea218 authored by Karamel's avatar Karamel
Browse files

Prevent from requesting archive with date boundaries in the future.

parent 5e856e88
......@@ -112,14 +112,15 @@ class ArchiveAPI implements API
* @param mixed $stop The representation of the stop date, compatible with
* DateUtils::readDate.
* @throws InvalidFieldException, with constraint CSTR_INVALID_DATE when
* $start or $stop could not be read as a date; with constraint
* CSTR_INVALID_DATERANGE when stop is anterior to start or when the time
* interval is greater than one year + one day.
* $start or $stop could not be read as a date or one is in the future;
* with constraint CSTR_INVALID_DATERANGE when stop is anterior to start
* or when the time interval is greater than one year + one day.
*/
public function addRequest($start, $stop) {
// Defensive checks
$startDate = DateUtils::readDate($start);
$stopDate = DateUtils::readDate($stop);
// Valid date check
if ($startDate === false || $startDate === null) {
throw new InvalidFieldException(
InvalidFieldException::CSTR_INVALID_DATE,
......@@ -134,6 +135,23 @@ class ArchiveAPI implements API
['startDate' => $start, 'stopDate' => $stop],
$stop);
}
// Past dates check
$now = new \DateTime();
if ($now->getTimestamp() < $startDate->getTimestamp()) {
throw new InvalidFieldException(
InvalidFieldException::CSTR_INVALID_DATE,
ArchiveRequest::class, 'startDate',
['startDate' => $start, 'stopDate' => $stop],
$start);
}
if ($now->getTimestamp() < $stopDate->getTimestamp()) {
throw new InvalidFieldException(
InvalidFieldException::CSTR_INVALID_DATE,
ArchiveRequest::class, 'stopDate',
['startDate' => $start, 'stopDate' => $stop],
$stop);
}
// Less than 1 year interval check
$interval = $startDate->diff($stopDate);
if ($interval->invert === 1) {
throw new InvalidFieldException(
......
......@@ -132,6 +132,39 @@ class ArchiveAPIRequestTest extends PastequeTestCase
}
}
public function testRequestFutureDate() {
$now = new \DateTime();
$start = $now->add(new \DateInterval('P1D'));
$now = new \DateTime();
$stop = $now->add(new \DateInterval('P3D'));
$exceptionThrown = false;
try {
$request = $this->api->addRequest($start, $stop);
} catch (InvalidFieldException $e) {
$exceptionThrown = true;
$this->assertInvalidFieldException(
InvalidFieldException::CSTR_INVALID_DATE,
ArchiveRequest::class, 'startDate',
['startDate' => $start, 'stopDate' => $stop],
$start, $e);
}
$this->assertTrue($exceptionThrown);
$now = new \DateTime();
$start = $now->sub(new \DateInterval('P14D'));
$exceptionThrown = false;
try {
$request = $this->api->addRequest($start, $stop);
} catch (InvalidFieldException $e) {
$exceptionThrown = true;
$this->assertInvalidFieldException(
InvalidFieldException::CSTR_INVALID_DATE,
ArchiveRequest::class, 'stopDate',
['startDate' => $start, 'stopDate' => $stop],
$stop, $e);
}
$this->assertTrue($exceptionThrown);
}
public function testRequestArchiveOneYearAndSo() {
$start = new \DateTime('2017-01-01 3:00:00');
$stop = new \DateTime('2018-01-01 6:00:00');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment