Skip to content

Commit eeac823

Browse files
author
David GABISON
committed
test: add test for ListDonations custom filter
1 parent 3cc566b commit eeac823

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/Unit/Donations/Endpoints/TestListDonations.php

+57
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,63 @@ public function testShouldReturnFilteredListByDonorId()
100100
$this->assertSame($expectedItems, $response->data['items']);
101101
}
102102

103+
/**
104+
* @unreleased
105+
*
106+
* @return void
107+
* @throws Exception
108+
*/
109+
public function testShouldAllowAddingFilters()
110+
{
111+
$donations = Donation::factory()->count(5)->create();
112+
113+
$expectedItems = array_slice($donations, 0, 2);
114+
foreach ($expectedItems as $item) {
115+
give_update_payment_meta($item->id, 'my_key', 'on');
116+
}
117+
$expectedItems = $this->getMockColumns($expectedItems);
118+
119+
add_filter('give_list-donation_api_args', function ($args) {
120+
$args['my_param'] = [
121+
'type' => 'string',
122+
'required' => false,
123+
'sanitize_callback' => 'sanitize_text_field',
124+
];
125+
return $args;
126+
});
127+
128+
add_filter('give_list-donation_where_conditions', function ($value, $endpoint) {
129+
list($query, $dependencies) = $value;
130+
131+
$paramValue = $endpoint->request->get_param('my_param');
132+
if (!empty($paramValue)) {
133+
$query->attachMeta(
134+
'give_donationmeta',
135+
'ID',
136+
'donation_id',
137+
['my_key', 'myKey']
138+
);
139+
$query->where('give_donationmeta_attach_meta_myKey.meta_value', $paramValue);
140+
}
141+
142+
return [$query, $dependencies];
143+
}, 10, 2);
144+
145+
$mockRequest = $this->getMockRequest();
146+
// set_params
147+
$mockRequest->set_param('page', 1);
148+
$mockRequest->set_param('perPage', 30);
149+
$mockRequest->set_param('locale', 'us-US');
150+
$mockRequest->set_param('testMode', give_is_test_mode());
151+
$mockRequest->set_param('my_param', 'on');
152+
153+
$listDonations = give(ListDonations::class);
154+
155+
$response = $listDonations->handleRequest($mockRequest);
156+
157+
$this->assertSame($expectedItems, $response->data['items']);
158+
}
159+
103160
/**
104161
*
105162
* @since 2.25.0

0 commit comments

Comments
 (0)