Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/date validation #7689

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Donations/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function validateInt($value)
public function validateDate($param, $request, $key)
{
// Check that date is valid, and formatted YYYY-MM-DD
list($year, $month, $day) = explode('-', $param);
if (substr_count($param, '-') !== 2) return false;
list($year, $month, $day) = array_map('intval', explode('-', $param));
$valid = checkdate($month, $day, $year);

// If checking end date, check that it is after start date
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Donations/Endpoints/TestListDonations.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,44 @@ public function testShouldReturnListWithSameData()
$this->assertSame($expectedItems, $response->data['items']);
}

/**
* @unreleased
*
* @return void
* @throws Exception
*/
public function testShouldFilterInvalidDateArgument_missingMonthDay()
{
$listDonation = give(ListDonations::class);
$key = 'start';
$value = '2020';

$mockRequest = $this->getMockRequest();
$mockRequest->set_param($key, $value);

$response = $listDonation->validateDate($value, $mockRequest, $key);
$this->assertFalse($response);
}

/**
* @unreleased
*
* @return void
* @throws Exception
*/
public function testShouldFilterInvalidDateArgument_InvalidChar()
{
$listDonation = give(ListDonations::class);
$key = 'start';
$value = '2020-mar-02';

$mockRequest = $this->getMockRequest();
$mockRequest->set_param($key, $value);

$response = $listDonation->validateDate($value, $mockRequest, $key);
$this->assertFalse($response);
}

/**
*
* @since 2.25.0
Expand Down
Loading