c3fa203b0fd0b9500492c3d3171e83ae405d9e83
[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 * @throws \CRM_Core_Exception
38 */
39 public function tearDown(): void {
40 parent::tearDown();
41 $this->quickCleanup(['civicrm_batch']);
42 }
43
44 /**
45 * This test checks that a batch search
46 * by payment method works.
47 * This function could later be expanded to include
48 * checks that other types of searches are also
49 * working.
50 *
51 * It creates two contributions, one with payment method credit
52 * card and one with payment method check. After performing a
53 * search by payment method for checks, it makes sure that the
54 * results are only contributions made by check.
55 *
56 * @throws \CRM_Core_Exception
57 */
58 public function testGetBatchFinancialItems() {
59
60 // create two contributions: one check and one credit card
61
62 $contactId = $this->individualCreate(['first_name' => 'John', 'last_name' => 'Doe']);
63 $this->contributionCreate([
64 'contact_id' => $contactId,
65 'total_amount' => 1,
66 'payment_instrument_id' => 'Check',
67 'financial_type_id' => 'Donation',
68 'contribution_status_id' => 'Completed',
69 'receive_date' => '20080522000000',
70 'receipt_date' => '20080522000000',
71 'trxn_id' => '22ereerwww322323',
72 'id' => NULL,
73 'fee_amount' => 0,
74 'net_amount' => 1,
75 'currency' => 'USD',
76 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
77 'skipCleanMoney' => TRUE,
78 ]);
79 $this->contributionCreate([
80 'contact_id' => $contactId,
81 'total_amount' => 1,
82 'payment_instrument_id' => 'Credit Card',
83 'financial_type_id' => 'Member Dues',
84 'contribution_status_id' => 'Completed',
85 'receive_date' => '20080523000000',
86 'receipt_date' => '20080523000000',
87 'trxn_id' => '22ereerwww323323',
88 'id' => NULL,
89 'fee_amount' => 0,
90 'net_amount' => 1,
91 'currency' => 'USD',
92 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da71',
93 'skipCleanMoney' => TRUE,
94 ]);
95
96 //create an empty batch to use for the search, and run the search
97
98 $batchParams = ['title' => 'Test Batch'];
99 $batchParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Open');
100 $batch = CRM_Batch_BAO_Batch::create($batchParams);
101 $entityId = $batch->id;
102 $returnvalues = [
103 'civicrm_financial_trxn.payment_instrument_id as payment_method',
104 ];
105 $notPresent = TRUE;
106 $params['contribution_payment_instrument_id']
107 = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
108 $result = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityId, $returnvalues, $notPresent, $params, TRUE)->fetchAll();
109 $this->assertEquals(count($result), 1, 'In line' . __LINE__);
110 $this->assertEquals($result[0]['payment_method'], CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'), 'In line' . __LINE__);
111 $params['financial_type_id'] = implode(',', [
112 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
113 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Member Dues'),
114 ]);
115 $result = CRM_Batch_BAO_Batch::getBatchFinancialItems($entityId, $returnvalues, $notPresent, $params, TRUE)->fetchAll();
116 $this->assertEquals(count($result), 1, 'In line' . __LINE__);
117 }
118
119 /**
120 * Test testExportFinancialBatch.
121 */
122 public function testExportFinancialBatch() {
123 $this->createLoggedInUser();
124 $batchParams = ['title' => 'Test Batch'];
125 $batchParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Exported');
126 $batch = $this->callAPISuccess('Batch', 'create', $batchParams);
127 CRM_Batch_BAO_Batch::exportFinancialBatch([$batch['id']], 'CSV', NULL);
128 }
129
130 }