Skip to content

Commit

Permalink
test: add test for ListDonations custom filter
Browse files Browse the repository at this point in the history
  • Loading branch information
David GABISON committed Aug 9, 2024
1 parent 3cc566b commit eeac823
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/Unit/Donations/Endpoints/TestListDonations.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,63 @@ public function testShouldReturnFilteredListByDonorId()
$this->assertSame($expectedItems, $response->data['items']);
}

/**
* @unreleased
*
* @return void
* @throws Exception
*/
public function testShouldAllowAddingFilters()
{
$donations = Donation::factory()->count(5)->create();

$expectedItems = array_slice($donations, 0, 2);
foreach ($expectedItems as $item) {
give_update_payment_meta($item->id, 'my_key', 'on');
}
$expectedItems = $this->getMockColumns($expectedItems);

add_filter('give_list-donation_api_args', function ($args) {
$args['my_param'] = [
'type' => 'string',
'required' => false,
'sanitize_callback' => 'sanitize_text_field',
];
return $args;
});

add_filter('give_list-donation_where_conditions', function ($value, $endpoint) {
list($query, $dependencies) = $value;

$paramValue = $endpoint->request->get_param('my_param');
if (!empty($paramValue)) {
$query->attachMeta(
'give_donationmeta',
'ID',
'donation_id',
['my_key', 'myKey']
);
$query->where('give_donationmeta_attach_meta_myKey.meta_value', $paramValue);
}

return [$query, $dependencies];
}, 10, 2);

$mockRequest = $this->getMockRequest();
// set_params
$mockRequest->set_param('page', 1);
$mockRequest->set_param('perPage', 30);
$mockRequest->set_param('locale', 'us-US');
$mockRequest->set_param('testMode', give_is_test_mode());
$mockRequest->set_param('my_param', 'on');

$listDonations = give(ListDonations::class);

$response = $listDonations->handleRequest($mockRequest);

$this->assertSame($expectedItems, $response->data['items']);
}

/**
*
* @since 2.25.0
Expand Down

0 comments on commit eeac823

Please sign in to comment.