Commit | Line | Data |
---|---|---|
123fe8fe | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
123fe8fe | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
123fe8fe | 7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Include parent class definition | |
30 | */ | |
31 | ||
32 | /** | |
33 | * Test Contribution Search form filters | |
34 | * | |
35 | * @package CiviCRM | |
36 | */ | |
08a0a69b | 37 | class CRM_Contribute_Form_SearchTest extends CiviUnitTestCase { |
123fe8fe | 38 | |
39 | protected $_individual; | |
66c25e4c | 40 | protected $_tablesToTruncate = ['civicrm_contribution', 'civicrm_line_item']; |
123fe8fe | 41 | |
42 | public function setUp() { | |
123fe8fe | 43 | parent::setUp(); |
6b77350a | 44 | $this->_individual = $this->individualCreate(); |
123fe8fe | 45 | } |
46 | ||
47 | public function tearDown() { | |
48 | } | |
49 | ||
50 | /** | |
cbd44e94 | 51 | * CRM-19325: Test CRM_Contribute_Form_Search batch filters |
123fe8fe | 52 | */ |
53 | public function testBatchFilter() { | |
54 | $this->quickCleanup($this->_tablesToTruncate); | |
66c25e4c | 55 | $contactID1 = $this->individualCreate([], 1); |
56 | $contactID2 = $this->individualCreate([], 2); | |
123fe8fe | 57 | $batchTitle = CRM_Batch_BAO_Batch::generateBatchName(); |
58 | ||
59 | // create batch | |
66c25e4c | 60 | $batch = $this->callAPISuccess('Batch', 'create', [ |
123fe8fe | 61 | 'created_id' => $this->_individual, |
62 | 'created_date' => CRM_Utils_Date::processDate(date("Y-m-d"), date("H:i:s")), | |
6b77350a | 63 | 'status_id' => CRM_Core_Pseudoconstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Data Entry'), |
123fe8fe | 64 | 'title' => $batchTitle, |
65 | 'item_count' => 2, | |
66 | 'total' => 100, | |
67 | 'type_id' => array_search('Contribution', CRM_Batch_BAO_Batch::buildOptions('type_id')), | |
66c25e4c | 68 | ]); |
123fe8fe | 69 | $batchID = $batch['id']; |
70 | ||
66c25e4c | 71 | $batchEntry = [ |
72 | 'primary_profiles' => [1 => NULL, 2 => NULL, 3 => NULL], | |
73 | 'primary_contact_id' => [ | |
123fe8fe | 74 | 1 => $contactID1, |
75 | 2 => $contactID2, | |
66c25e4c | 76 | ], |
77 | 'field' => [ | |
78 | 1 => [ | |
123fe8fe | 79 | 'financial_type' => 1, |
80 | 'total_amount' => 70, | |
6b77350a | 81 | 'receive_date' => '2013-07-24', |
123fe8fe | 82 | 'receive_date_time' => NULL, |
83 | 'payment_instrument' => 1, | |
84 | 'check_number' => NULL, | |
85 | 'contribution_status_id' => 1, | |
66c25e4c | 86 | ], |
87 | 2 => [ | |
123fe8fe | 88 | 'financial_type' => 1, |
89 | 'total_amount' => 30, | |
6b77350a | 90 | 'receive_date' => '2014-07-24', |
123fe8fe | 91 | 'receive_date_time' => NULL, |
92 | 'payment_instrument' => 1, | |
93 | 'check_number' => NULL, | |
94 | 'contribution_status_id' => 1, | |
66c25e4c | 95 | ], |
96 | ], | |
123fe8fe | 97 | 'actualBatchTotal' => 100, |
66c25e4c | 98 | ]; |
123fe8fe | 99 | |
100 | // create random contribution to check IS NULL filter more precisely | |
66c25e4c | 101 | $nonBatchContri = $this->callAPISuccess('Contribution', 'create', [ |
123fe8fe | 102 | 'financial_type_id' => 1, |
103 | 'total_amount' => 123, | |
6b77350a | 104 | 'receive_date' => '2014-07-24', |
123fe8fe | 105 | 'receive_date_time' => NULL, |
106 | 'payment_instrument' => 1, | |
107 | 'check_number' => NULL, | |
108 | 'contribution_status_id' => 1, | |
109 | 'contact_id' => $this->_individual, | |
66c25e4c | 110 | ]); |
cbd44e94 | 111 | $nonBatchContriID = $nonBatchContri['id']; |
123fe8fe | 112 | |
113 | // process batch entries | |
114 | $form = new CRM_Batch_Form_Entry(); | |
115 | $form->setBatchID($batchID); | |
116 | $form->testProcessContribution($batchEntry); | |
117 | ||
cbd44e94 | 118 | // fetch created contributions |
5cd3eaeb | 119 | $entities = $this->callAPISuccess('EntityBatch', 'get', array('batch_id' => $batchID)); |
66c25e4c | 120 | $ids = []; |
cbd44e94 | 121 | foreach ($entities['values'] as $value) { |
122 | $ids[] = $value['entity_id']; | |
123 | } | |
124 | list($batchContriID1, $batchContriID2) = $ids; | |
125 | ||
66c25e4c | 126 | $useCases = [ |
123fe8fe | 127 | // Case 1: Search for ONLY those contributions which are created from batch |
66c25e4c | 128 | [ |
123fe8fe | 129 | 'form_value' => array('contribution_batch_id' => 'IS NOT NULL'), |
130 | 'expected_count' => 2, | |
cbd44e94 | 131 | 'expected_contribution' => array($batchContriID1, $batchContriID2), |
123fe8fe | 132 | 'expected_qill' => 'Batch Name Not Null', |
66c25e4c | 133 | ], |
123fe8fe | 134 | // Case 2: Search for ONLY those contributions which are NOT created from batch |
66c25e4c | 135 | [ |
123fe8fe | 136 | 'form_value' => array('contribution_batch_id' => 'IS NULL'), |
137 | 'expected_count' => 1, | |
cbd44e94 | 138 | 'expected_contribution' => array($nonBatchContriID), |
123fe8fe | 139 | 'expected_qill' => 'Batch Name Is Null', |
66c25e4c | 140 | ], |
123fe8fe | 141 | // Case 3: Search for ONLY those contributions which are created from batch ID - $batchID |
66c25e4c | 142 | [ |
123fe8fe | 143 | 'form_value' => array('contribution_batch_id' => $batchID), |
144 | 'expected_count' => 2, | |
cbd44e94 | 145 | 'expected_contribution' => array($batchContriID1, $batchContriID2), |
123fe8fe | 146 | 'expected_qill' => 'Batch Name = ' . $batchTitle, |
66c25e4c | 147 | ], |
148 | ]; | |
123fe8fe | 149 | foreach ($useCases as $case) { |
150 | $fv = $case['form_value']; | |
151 | CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('contribution_batch_id')); | |
152 | $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv)); | |
66c25e4c | 153 | list($select, $from, $where) = $query->query(); |
123fe8fe | 154 | |
155 | // get and assert contribution count | |
cbd44e94 | 156 | $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL', $from, $where))->fetchAll(); |
157 | foreach ($contributions as $key => $value) { | |
158 | $contributions[$key] = $value['id']; | |
159 | } | |
160 | // assert the contribution count | |
161 | $this->assertEquals($case['expected_count'], count($contributions)); | |
162 | // assert the contribution IDs | |
163 | $this->checkArrayEquals($case['expected_contribution'], $contributions); | |
123fe8fe | 164 | // get and assert qill string |
165 | $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill()))); | |
166 | $this->assertEquals($case['expected_qill'], $qill); | |
167 | } | |
168 | } | |
169 | ||
898bfdcd PN |
170 | /** |
171 | * CRM-20286: Test CRM_Contribute_Form_Search Card type filters | |
172 | */ | |
173 | public function testCardTypeFilter() { | |
174 | $this->quickCleanup($this->_tablesToTruncate); | |
175 | $contactID1 = $this->individualCreate(array(), 1); | |
176 | $contactID2 = $this->individualCreate(array(), 2); | |
177 | $Contribution1 = $this->callAPISuccess('Contribution', 'create', array( | |
178 | 'financial_type_id' => 1, | |
179 | 'total_amount' => 100, | |
180 | 'receive_date' => date('Ymd'), | |
181 | 'receive_date_time' => NULL, | |
182 | 'payment_instrument' => 1, | |
183 | 'contribution_status_id' => 1, | |
184 | 'contact_id' => $contactID1, | |
185 | )); | |
186 | $params = array( | |
187 | 'to_financial_account_id' => 1, | |
188 | 'status_id' => 1, | |
189 | 'contribution_id' => $Contribution1['id'], | |
190 | 'payment_instrument_id' => 1, | |
6b77350a | 191 | 'card_type_id' => 1, |
898bfdcd PN |
192 | 'total_amount' => 100, |
193 | ); | |
194 | CRM_Core_BAO_FinancialTrxn::create($params); | |
66c25e4c | 195 | $this->callAPISuccess('Contribution', 'create', array( |
898bfdcd PN |
196 | 'financial_type_id' => 1, |
197 | 'total_amount' => 150, | |
198 | 'receive_date' => date('Ymd'), | |
199 | 'receive_date_time' => NULL, | |
200 | 'payment_instrument' => 1, | |
201 | 'contribution_status_id' => 1, | |
202 | 'contact_id' => $contactID1, | |
203 | )); | |
5cd3eaeb | 204 | $Contribution3 = $this->callAPISuccess('Contribution', 'create', array( |
898bfdcd PN |
205 | 'financial_type_id' => 1, |
206 | 'total_amount' => 200, | |
207 | 'receive_date' => date('Ymd'), | |
208 | 'receive_date_time' => NULL, | |
209 | 'payment_instrument' => 1, | |
210 | 'contribution_status_id' => 1, | |
211 | 'contact_id' => $contactID2, | |
212 | )); | |
213 | $params = array( | |
214 | 'to_financial_account_id' => 1, | |
215 | 'status_id' => 1, | |
216 | 'contribution_id' => $Contribution3['id'], | |
217 | 'payment_instrument_id' => 1, | |
6b77350a | 218 | 'card_type_id' => 2, |
898bfdcd PN |
219 | 'total_amount' => 200, |
220 | ); | |
221 | CRM_Core_BAO_FinancialTrxn::create($params); | |
222 | ||
223 | $useCases = array( | |
224 | // Case 1: Search for ONLY those contributions which have card type | |
225 | array( | |
6b77350a | 226 | 'form_value' => array('financial_trxn_card_type_id' => 'IS NOT NULL'), |
898bfdcd PN |
227 | 'expected_count' => 2, |
228 | 'expected_contribution' => array($Contribution1['id'], $Contribution3['id']), | |
229 | 'expected_qill' => 'Card Type Not Null', | |
230 | ), | |
231 | // Case 2: Search for ONLY those contributions which have Card Type as Visa | |
232 | array( | |
6b77350a | 233 | 'form_value' => array('financial_trxn_card_type_id' => array(1)), |
898bfdcd PN |
234 | 'expected_count' => 1, |
235 | 'expected_contribution' => array($Contribution1['id']), | |
236 | 'expected_qill' => 'Card Type In Visa', | |
237 | ), | |
238 | // Case 3: Search for ONLY those contributions which have Card Type as Amex | |
239 | array( | |
6b77350a | 240 | 'form_value' => array('financial_trxn_card_type_id' => array(3)), |
898bfdcd PN |
241 | 'expected_count' => 0, |
242 | 'expected_contribution' => array(), | |
243 | 'expected_qill' => 'Card Type In Amex', | |
244 | ), | |
245 | // Case 4: Search for ONLY those contributions which have Card Type as Visa or MasterCard | |
246 | array( | |
6b77350a | 247 | 'form_value' => array('financial_trxn_card_type_id' => array(1, 2)), |
898bfdcd PN |
248 | 'expected_count' => 2, |
249 | 'expected_contribution' => array($Contribution1['id'], $Contribution3['id']), | |
250 | 'expected_qill' => 'Card Type In Visa, MasterCard', | |
251 | ), | |
252 | ); | |
253 | ||
254 | foreach ($useCases as $case) { | |
255 | $fv = $case['form_value']; | |
6b77350a | 256 | CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('financial_trxn_card_type_id')); |
898bfdcd | 257 | $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv)); |
66c25e4c | 258 | list($select, $from, $where) = $query->query(); |
898bfdcd PN |
259 | |
260 | // get and assert contribution count | |
261 | $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL', $from, $where))->fetchAll(); | |
262 | foreach ($contributions as $key => $value) { | |
263 | $contributions[$key] = $value['id']; | |
264 | } | |
265 | // assert the contribution count | |
6b77350a | 266 | //$this->assertEquals($case['expected_count'], count($contributions)); |
898bfdcd PN |
267 | // assert the contribution IDs |
268 | $this->checkArrayEquals($case['expected_contribution'], $contributions); | |
269 | // get and assert qill string | |
270 | $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill()))); | |
271 | $this->assertEquals($case['expected_qill'], $qill); | |
272 | } | |
273 | } | |
274 | ||
c6ba3619 E |
275 | /** |
276 | * CRM-20391: Test CRM_Contribute_Form_Search Card Number filters | |
277 | */ | |
278 | public function testCardNumberFilter() { | |
279 | $this->quickCleanup($this->_tablesToTruncate); | |
280 | $contactID1 = $this->individualCreate(array(), 1); | |
281 | $contactID2 = $this->individualCreate(array(), 2); | |
282 | $Contribution1 = $this->callAPISuccess('Contribution', 'create', array( | |
283 | 'financial_type_id' => 1, | |
284 | 'total_amount' => 100, | |
285 | 'receive_date' => date('Ymd'), | |
286 | 'receive_date_time' => NULL, | |
287 | 'payment_instrument' => 1, | |
288 | 'contribution_status_id' => 1, | |
289 | 'contact_id' => $contactID1, | |
290 | )); | |
291 | $params = array( | |
292 | 'to_financial_account_id' => 1, | |
293 | 'status_id' => 1, | |
294 | 'contribution_id' => $Contribution1['id'], | |
295 | 'payment_instrument_id' => 1, | |
6b77350a | 296 | 'card_type_id' => 1, |
c6ba3619 E |
297 | 'total_amount' => 100, |
298 | 'pan_truncation' => 1234, | |
299 | ); | |
300 | CRM_Core_BAO_FinancialTrxn::create($params); | |
66c25e4c | 301 | $this->callAPISuccess('Contribution', 'create', array( |
c6ba3619 E |
302 | 'financial_type_id' => 1, |
303 | 'total_amount' => 150, | |
304 | 'receive_date' => date('Ymd'), | |
305 | 'receive_date_time' => NULL, | |
306 | 'payment_instrument' => 1, | |
307 | 'contribution_status_id' => 1, | |
308 | 'contact_id' => $contactID1, | |
309 | )); | |
5cd3eaeb | 310 | $Contribution3 = $this->callAPISuccess('Contribution', 'create', array( |
c6ba3619 E |
311 | 'financial_type_id' => 1, |
312 | 'total_amount' => 200, | |
313 | 'receive_date' => date('Ymd'), | |
314 | 'receive_date_time' => NULL, | |
315 | 'payment_instrument' => 1, | |
316 | 'contribution_status_id' => 1, | |
317 | 'contact_id' => $contactID2, | |
318 | )); | |
319 | $params = array( | |
320 | 'to_financial_account_id' => 1, | |
321 | 'status_id' => 1, | |
322 | 'contribution_id' => $Contribution3['id'], | |
323 | 'payment_instrument_id' => 1, | |
6b77350a | 324 | 'card_type_id' => 2, |
c6ba3619 E |
325 | 'total_amount' => 200, |
326 | 'pan_truncation' => 5678, | |
327 | ); | |
328 | CRM_Core_BAO_FinancialTrxn::create($params); | |
329 | ||
330 | $useCases = array( | |
331 | // Case 1: Search for ONLY those contributions which have card number | |
332 | array( | |
333 | 'form_value' => array('financial_trxn_pan_truncation' => 'IS NOT NULL'), | |
334 | 'expected_count' => 2, | |
335 | 'expected_contribution' => array($Contribution1['id'], $Contribution3['id']), | |
336 | 'expected_qill' => 'Card Number Not Null', | |
337 | ), | |
338 | // Case 2: Search for ONLY those contributions which have Card Number as 1234 | |
339 | array( | |
340 | 'form_value' => array('financial_trxn_pan_truncation' => 1234), | |
341 | 'expected_count' => 1, | |
342 | 'expected_contribution' => array($Contribution1['id']), | |
343 | 'expected_qill' => 'Card Number Like %1234%', | |
344 | ), | |
345 | // Case 3: Search for ONLY those contributions which have Card Number as 8888 | |
346 | array( | |
347 | 'form_value' => array('financial_trxn_pan_truncation' => 8888), | |
348 | 'expected_count' => 0, | |
349 | 'expected_contribution' => array(), | |
350 | 'expected_qill' => 'Card Number Like %8888%', | |
351 | ), | |
352 | ); | |
353 | ||
354 | foreach ($useCases as $case) { | |
355 | $fv = $case['form_value']; | |
356 | CRM_Contact_BAO_Query::processSpecialFormValue($fv, array('financial_trxn_pan_truncation')); | |
357 | $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv)); | |
66c25e4c | 358 | list($select, $from, $where) = $query->query(); |
c6ba3619 E |
359 | |
360 | // get and assert contribution count | |
361 | $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL', $from, $where))->fetchAll(); | |
362 | foreach ($contributions as $key => $value) { | |
363 | $contributions[$key] = $value['id']; | |
364 | } | |
365 | // assert the contribution count | |
366 | $this->assertEquals($case['expected_count'], count($contributions)); | |
367 | // assert the contribution IDs | |
368 | $this->checkArrayEquals($case['expected_contribution'], $contributions); | |
369 | // get and assert qill string | |
370 | $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill()))); | |
371 | $this->assertEquals($case['expected_qill'], $qill); | |
372 | } | |
373 | } | |
374 | ||
5af1389d VH |
375 | /** |
376 | * Test CRM_Contribute_Form_Search Recurring Contribution Status Id filters | |
377 | */ | |
378 | public function testContributionRecurStatusFilter() { | |
379 | $this->quickCleanup($this->_tablesToTruncate); | |
66c25e4c | 380 | $contactID1 = $this->individualCreate([], 1); |
381 | $contactID2 = $this->individualCreate([], 2); | |
5af1389d | 382 | // "In Progress" recurring contribution for contactID1 |
66c25e4c | 383 | $ContributionRecur1 = $this->callAPISuccess('ContributionRecur', 'create', [ |
5af1389d VH |
384 | 'sequential' => 1, |
385 | 'contact_id' => $contactID1, | |
386 | 'frequency_interval' => 1, | |
387 | 'frequency_unit' => "month", | |
388 | 'amount' => 11, | |
389 | 'currency' => "CAD", | |
390 | 'payment_instrument_id' => 1, | |
391 | 'contribution_status_id' => 5, | |
392 | 'financial_type_id' => "Donation", | |
66c25e4c | 393 | ]); |
394 | $Contribution1 = $this->callAPISuccess('Contribution', 'create', [ | |
395 | 'financial_type_id' => 'Donation', | |
5af1389d VH |
396 | 'total_amount' => 11, |
397 | 'receive_date' => date('Ymd'), | |
398 | 'receive_date_time' => NULL, | |
399 | 'payment_instrument_id' => 1, | |
400 | 'contribution_status_id' => 1, | |
401 | 'contact_id' => $contactID1, | |
402 | 'contribution_recur_id' => $ContributionRecur1['id'], | |
66c25e4c | 403 | ]); |
404 | $params = [ | |
5af1389d VH |
405 | 'to_financial_account_id' => 1, |
406 | 'status_id' => 1, | |
407 | 'contribution_id' => $Contribution1['id'], | |
408 | 'payment_instrument_id' => 1, | |
409 | 'card_type_id' => 1, | |
410 | 'total_amount' => 11, | |
66c25e4c | 411 | ]; |
5af1389d VH |
412 | CRM_Core_BAO_FinancialTrxn::create($params); |
413 | // "Completed" recurring contribution for contactID2 | |
66c25e4c | 414 | $ContributionRecur2 = $this->callAPISuccess('ContributionRecur', 'create', [ |
5af1389d VH |
415 | 'sequential' => 1, |
416 | 'contact_id' => $contactID2, | |
417 | 'frequency_interval' => 1, | |
418 | 'frequency_unit' => "month", | |
419 | 'amount' => 22, | |
420 | 'currency' => "CAD", | |
421 | 'payment_instrument_id' => 1, | |
422 | 'contribution_status_id' => 1, | |
423 | 'financial_type_id' => "Donation", | |
66c25e4c | 424 | ]); |
425 | $Contribution2 = $this->callAPISuccess('Contribution', 'create', [ | |
426 | 'financial_type_id' => 'Donation', | |
5af1389d VH |
427 | 'total_amount' => 22, |
428 | 'receive_date' => date('Ymd'), | |
429 | 'receive_date_time' => NULL, | |
430 | 'payment_instrument' => 1, | |
431 | 'contribution_status_id' => 1, | |
432 | 'contact_id' => $contactID2, | |
433 | 'contribution_recur_id' => $ContributionRecur2['id'], | |
66c25e4c | 434 | ]); |
435 | $params = [ | |
5af1389d VH |
436 | 'to_financial_account_id' => 1, |
437 | 'status_id' => 1, | |
438 | 'contribution_id' => $Contribution2['id'], | |
439 | 'payment_instrument_id' => 1, | |
440 | 'card_type_id' => 1, | |
441 | 'total_amount' => 22, | |
66c25e4c | 442 | ]; |
5af1389d VH |
443 | CRM_Core_BAO_FinancialTrxn::create($params); |
444 | ||
66c25e4c | 445 | $useCases = [ |
5af1389d | 446 | // Case 1: Search for ONLY those recurring contributions with status "In Progress" |
66c25e4c | 447 | [ |
448 | 'form_value' => ['contribution_recur_contribution_status_id' => 5], | |
5af1389d | 449 | 'expected_count' => 1, |
66c25e4c | 450 | 'expected_contact' => [$contactID1], |
5af1389d | 451 | 'expected_qill' => "Recurring Contribution Status = 'In Progress'", |
66c25e4c | 452 | ], |
5af1389d | 453 | // Case 2: Search for ONLY those recurring contributions with status "Completed" |
66c25e4c | 454 | [ |
455 | 'form_value' => ['contribution_recur_contribution_status_id' => 1], | |
5af1389d | 456 | 'expected_count' => 1, |
66c25e4c | 457 | 'expected_contact' => [$contactID2], |
5af1389d | 458 | 'expected_qill' => "Recurring Contribution Status = 'Completed'", |
66c25e4c | 459 | ], |
5af1389d | 460 | // Case 3: Search for ONLY those recurring contributions with status "Cancelled" |
66c25e4c | 461 | [ |
462 | 'form_value' => ['contribution_recur_contribution_status_id' => 3], | |
5af1389d | 463 | 'expected_count' => 0, |
66c25e4c | 464 | 'expected_contact' => [], |
5af1389d | 465 | 'expected_qill' => "Recurring Contribution Status = 'Cancelled'", |
66c25e4c | 466 | ], |
467 | ]; | |
5af1389d VH |
468 | |
469 | foreach ($useCases as $case) { | |
470 | $fv = $case['form_value']; | |
471 | $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv)); | |
472 | list($select, $from, $where, $having) = $query->query(); | |
473 | ||
474 | // get and assert contribution count | |
475 | $contacts = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT contact_a.id %s %s AND contact_a.id IS NOT NULL', $from, $where))->fetchAll(); | |
476 | foreach ($contacts as $key => $value) { | |
477 | $contacts[$key] = $value['id']; | |
478 | } | |
479 | // assert the contribution count | |
480 | $this->assertEquals($case['expected_count'], count($contacts)); | |
481 | // assert the contribution IDs | |
482 | $this->checkArrayEquals($case['expected_contact'], $contacts); | |
483 | // get and assert qill string | |
484 | $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill()))); | |
485 | $this->assertEquals($case['expected_qill'], $qill); | |
486 | } | |
487 | } | |
10f25097 LS |
488 | |
489 | /** | |
70c907d4 LS |
490 | * CRM-21343: Test CRM_Contribute_Form_Search Cancelled filters |
491 | */ | |
492 | public function testCancelledFilter() { | |
493 | $this->quickCleanup($this->_tablesToTruncate); | |
66c25e4c | 494 | $contactID1 = $this->individualCreate([], 1); |
495 | $contactID2 = $this->individualCreate([], 2); | |
496 | $Contribution1 = $this->callAPISuccess('Contribution', 'create', [ | |
70c907d4 LS |
497 | 'financial_type_id' => 1, |
498 | 'total_amount' => 100, | |
499 | 'receive_date' => date('Ymd'), | |
500 | 'receive_date_time' => NULL, | |
501 | 'payment_instrument' => 1, | |
502 | 'contribution_status_id' => 3, | |
503 | 'cancel_date' => date('Ymd'), | |
504 | 'cancel_reason' => 'Insufficient funds', | |
505 | 'contact_id' => $contactID1, | |
66c25e4c | 506 | ]); |
507 | $this->callAPISuccess('Contribution', 'create', [ | |
70c907d4 LS |
508 | 'financial_type_id' => 1, |
509 | 'total_amount' => 150, | |
510 | 'receive_date' => date('Ymd', strtotime(date('Y-m-d') . ' - 1 days')), | |
511 | 'receive_date_time' => NULL, | |
512 | 'payment_instrument' => 1, | |
513 | 'contribution_status_id' => 3, | |
514 | 'cancel_date' => date('Ymd', strtotime(date('Y-m-d') . ' - 1 days')), | |
515 | 'cancel_reason' => 'Insufficient funds', | |
516 | 'contact_id' => $contactID2, | |
66c25e4c | 517 | ]); |
518 | $Contribution3 = $this->callAPISuccess('Contribution', 'create', [ | |
70c907d4 LS |
519 | 'financial_type_id' => 1, |
520 | 'total_amount' => 200, | |
521 | 'receive_date' => date('Ymd'), | |
522 | 'receive_date_time' => NULL, | |
523 | 'payment_instrument' => 1, | |
524 | 'contribution_status_id' => 3, | |
525 | 'cancel_date' => date('Ymd'), | |
526 | 'cancel_reason' => 'Invalid Credit Card Number', | |
527 | 'contact_id' => $contactID1, | |
66c25e4c | 528 | ]); |
70c907d4 | 529 | |
66c25e4c | 530 | $useCases = [ |
70c907d4 | 531 | // Case 1: Search for Cancelled Date |
66c25e4c | 532 | [ |
533 | 'form_value' => ['cancel_date' => date('Y-m-d')], | |
70c907d4 | 534 | 'expected_count' => 2, |
66c25e4c | 535 | 'expected_contribution' => [$Contribution1['id'], $Contribution3['id']], |
70c907d4 | 536 | 'expected_qill' => "Cancel Date Like '%" . date('Y-m-d') . "%'", |
66c25e4c | 537 | ], |
70c907d4 | 538 | // Case 2: Search for Cancelled Reason |
66c25e4c | 539 | [ |
540 | 'form_value' => ['cancel_reason' => 'Invalid Credit Card Number'], | |
70c907d4 | 541 | 'expected_count' => 1, |
66c25e4c | 542 | 'expected_contribution' => [$Contribution3['id']], |
927898c5 | 543 | 'expected_qill' => "Cancellation / Refund Reason Like '%Invalid Credit Card Number%'", |
66c25e4c | 544 | ], |
70c907d4 | 545 | // Case 3: Search for Cancelled Date and Cancelled Reason |
66c25e4c | 546 | [ |
547 | 'form_value' => ['cancel_date' => date('Y-m-d'), 'cancel_reason' => 'Insufficient funds'], | |
70c907d4 | 548 | 'expected_count' => 1, |
66c25e4c | 549 | 'expected_contribution' => [$Contribution1['id']], |
927898c5 | 550 | 'expected_qill' => "Cancel Date Like '%" . date('Y-m-d') . "%'ANDCancellation / Refund Reason Like '%Insufficient funds%'", |
66c25e4c | 551 | ], |
552 | ]; | |
70c907d4 LS |
553 | |
554 | foreach ($useCases as $case) { | |
555 | $fv = $case['form_value']; | |
66c25e4c | 556 | CRM_Contact_BAO_Query::processSpecialFormValue($fv, ['cancel_date', 'cancel_reason']); |
70c907d4 | 557 | $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($fv)); |
66c25e4c | 558 | list($select, $from, $where) = $query->query(); |
70c907d4 LS |
559 | |
560 | // get and assert contribution count | |
561 | $contributions = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT civicrm_contribution.id %s %s AND civicrm_contribution.id IS NOT NULL AND civicrm_contribution.contribution_status_id = 3', $from, $where))->fetchAll(); | |
562 | foreach ($contributions as $key => $value) { | |
563 | $contributions[$key] = $value['id']; | |
564 | } | |
565 | // assert the contribution count | |
566 | $this->assertEquals($case['expected_count'], count($contributions)); | |
567 | // assert the contribution IDs | |
568 | $this->checkArrayEquals($case['expected_contribution'], $contributions); | |
569 | // get and assert qill string | |
570 | $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill()))); | |
571 | $this->assertEquals($case['expected_qill'], $qill); | |
572 | } | |
573 | } | |
10f25097 | 574 | |
123fe8fe | 575 | } |