Merge pull request #21374 from ufundo/event-custom-tokens
[civicrm-core.git] / tests / phpunit / CRM / Batch / BAO / BatchTest.php
1 <?php
2 /**
3 * File for the BatchTest class
4 *
5 * (PHP 5)
6 *
7 * @package CiviCRM
8 *
9 * This file is part of CiviCRM
10 *
11 * CiviCRM is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Affero General Public License
13 * as published by the Free Software Foundation; either version 3 of
14 * the License, or (at your option) any later version.
15 *
16 * CiviCRM is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public
22 * License along with this program. If not, see
23 * <http://www.gnu.org/licenses/>.
24 */
25
26 /**
27 * Test CRM/Batch/BAO/Batch.php getBatchFinancialItems
28 *
29 * @package CiviCRM
30 * @group headless
31 */
32 class CRM_Batch_BAO_BatchTest extends CiviUnitTestCase {
33
34 /**
35 * Cleanup after test.
36 *
37 */
38 public function tearDown(): void {
39 $this->quickCleanup(['civicrm_batch', 'civicrm_file', 'civicrm_entity_file']);
40 parent::tearDown();
41 }
42
43 /**
44 * This test checks that a batch search
45 * by payment method works.
46 * This function could later be expanded to include
47 * checks that other types of searches are also
48 * working.
49 *
50 * It creates two contributions, one with payment method credit
51 * card and one with payment method check. After performing a
52 * search by payment method for checks, it makes sure that the
53 * results are only contributions made by check.
54 *
55 * @throws \CRM_Core_Exception
56 */
57 public function testGetBatchFinancialItems(): void {
58
59 // create two contributions: one check and one credit card
60
61 $contactId = $this->individualCreate(['first_name' => 'John', 'last_name' => 'Doe']);
62 $this->contributionCreate([
63 'contact_id' => $contactId,
64 'total_amount' => 1,
65 'payment_instrument_id' => 'Check',
66 'financial_type_id' => 'Donation',
67 'contribution_status_id' => 'Completed',
68 'receive_date' => '20080522000000',
69 'receipt_date' => '20080522000000',
70 'trxn_id' => '22ereerwww322323',
71 'id' => NULL,
72 'fee_amount' => 0,
73 'net_amount' => 1,
74 'currency' => 'USD',
75 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
76 'skipCleanMoney' => TRUE,
77 ]);
78 $this->contributionCreate([
79 'contact_id' => $contactId,
80 'total_amount' => 1,
81 'payment_instrument_id' => 'Credit Card',
82 'financial_type_id' => 'Member Dues',
83 'contribution_status_id' => 'Completed',
84 'receive_date' => '20080523000000',
85 'receipt_date' => '20080523000000',
86 'trxn_id' => '22ereerwww323323',
87 'id' => NULL,
88 'fee_amount' => 0,
89 'net_amount' => 1,
90 'currency' => 'USD',
91 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da71',
92 'skipCleanMoney' => TRUE,
93 ]);
94
95 //create an empty batch to use for the search, and run the search
96
97 $batchParams = ['title' => 'Test Batch'];
98 $batchParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Open');
99 $batch = CRM_Batch_BAO_Batch::create($batchParams);
100 $entityId = $batch->id;
101 $returnvalues = [
102 'civicrm_financial_trxn.payment_instrument_id as payment_method',
103 ];
104 $notPresent = TRUE;
105 $params['contribution_payment_instrument_id']
106 = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
107 $result = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityId, $returnvalues, $notPresent, $params, TRUE)->fetchAll();
108 $this->assertEquals(count($result), 1, 'In line' . __LINE__);
109 $this->assertEquals($result[0]['payment_method'], CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'), 'In line' . __LINE__);
110 $params['financial_type_id'] = implode(',', [
111 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
112 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Member Dues'),
113 ]);
114 $result = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityId, $returnvalues, $notPresent, $params, TRUE)->fetchAll();
115 $this->assertEquals(count($result), 1, 'In line' . __LINE__);
116 }
117
118 /**
119 * Test testExportFinancialBatch.
120 *
121 * @throws \CRM_Core_Exception
122 */
123 public function testExportFinancialBatch(): void {
124 $this->createLoggedInUser();
125 $batchParams = ['title' => 'Test Batch'];
126 $batchParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Exported');
127 $batch = $this->callAPISuccess('Batch', 'create', $batchParams);
128 CRM_Batch_BAO_Batch::exportFinancialBatch([$batch['id']], 'CSV', NULL);
129 }
130
131 }