Commit | Line | Data |
---|---|---|
29cd38f0 JF |
1 | <?php |
2 | /** | |
3 | * @file | |
4 | * File for the CRM_Contribute_Import_Parser_ContributionTest class. | |
5 | */ | |
6 | /** | |
7 | * Test Contribution import parser. | |
8 | * | |
9 | * @package CiviCRM | |
10 | * @group headless | |
11 | */ | |
12 | class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { | |
9099cab3 | 13 | protected $_tablesToTruncate = []; |
35aca6d6 | 14 | use CRMTraits_Custom_CustomDataTrait; |
15 | ||
16 | /** | |
17 | * Default entity for class. | |
18 | * | |
19 | * @var string | |
20 | */ | |
21 | protected $entity = 'Contribution'; | |
39b959db | 22 | |
29cd38f0 JF |
23 | /** |
24 | * Setup function. | |
25 | */ | |
8e2ac367 | 26 | public function tearDown() { |
27 | $this->quickCleanUpFinancialEntities(); | |
28 | parent::tearDown(); | |
29cd38f0 | 29 | } |
39b959db | 30 | |
29cd38f0 JF |
31 | /** |
32 | * Test import parser will add contribution and soft contribution each for different contact. | |
33 | * | |
34 | * In this case primary contact and secondary contact both are identified by external identifier. | |
35 | * | |
9d8db541 | 36 | * @dataProvider getThousandSeparators |
37 | * | |
38 | * @param string $thousandSeparator | |
39 | * | |
29cd38f0 JF |
40 | * @throws \Exception |
41 | */ | |
9d8db541 | 42 | public function testImportParserWithSoftCreditsByExternalIdentifier($thousandSeparator) { |
43 | $this->setCurrencySeparators($thousandSeparator); | |
9099cab3 | 44 | $contact1Params = [ |
29cd38f0 JF |
45 | 'first_name' => 'Contact', |
46 | 'last_name' => 'One', | |
47 | 'external_identifier' => 'ext-1', | |
48 | 'contact_type' => 'Individual', | |
9099cab3 CW |
49 | ]; |
50 | $contact2Params = [ | |
29cd38f0 JF |
51 | 'first_name' => 'Contact', |
52 | 'last_name' => 'Two', | |
53 | 'external_identifier' => 'ext-2', | |
54 | 'contact_type' => 'Individual', | |
9099cab3 | 55 | ]; |
29cd38f0 JF |
56 | $contact1Id = $this->individualCreate($contact1Params); |
57 | $contact2Id = $this->individualCreate($contact2Params); | |
9099cab3 | 58 | $values = [ |
9d8db541 | 59 | "total_amount" => $this->formatMoneyInput(1230.99), |
29cd38f0 JF |
60 | "financial_type" => "Donation", |
61 | "external_identifier" => "ext-1", | |
62 | "soft_credit" => "ext-2", | |
9099cab3 CW |
63 | ]; |
64 | $mapperSoftCredit = [NULL, NULL, NULL, "external_identifier"]; | |
65 | $mapperSoftCreditType = [NULL, NULL, NULL, "1"]; | |
29cd38f0 | 66 | $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Contribute_Import_Parser::SOFT_CREDIT, $mapperSoftCredit, NULL, $mapperSoftCreditType); |
8e2ac367 | 67 | $params = ['contact_id' => $contact1Id]; |
9099cab3 | 68 | $values = []; |
29cd38f0 | 69 | $contributionsOfMainContact = CRM_Contribute_BAO_Contribution::retrieve($params, $values, $values); |
9d8db541 | 70 | $this->assertEquals(1230.99, $contributionsOfMainContact->total_amount); |
71 | $this->assertEquals(1230.99, $contributionsOfMainContact->net_amount); | |
72 | $this->assertEquals(0, $contributionsOfMainContact->fee_amount); | |
73 | ||
29cd38f0 JF |
74 | $params["contact_id"] = $contact2Id; |
75 | $contributionsOfSoftContact = CRM_Contribute_BAO_ContributionSoft::retrieve($params, $values); | |
58f89319 SL |
76 | $this->assertEquals(1, $contributionsOfMainContact->N, 'Contribution not added for primary contact'); |
77 | $this->assertEquals(1, $contributionsOfSoftContact->N, 'Soft Contribution not added for secondary contact'); | |
78 | $this->callAPISuccess('ContributionSoft', 'Delete', ['id' => $contributionsOfSoftContact->id]); | |
79 | $this->callAPISuccess('Contribution', 'Delete', ['id' => $contributionsOfMainContact->id]); | |
29cd38f0 | 80 | } |
1004b689 | 81 | |
82 | /** | |
83 | * Test dates are parsed | |
84 | */ | |
85 | public function testParsedDates() { | |
86 | $mapperKeys = []; | |
87 | $form = new CRM_Contribute_Import_Parser_Contribution($mapperKeys); | |
88 | $params = ['receive_date' => '20/10/2019']; | |
89 | CRM_Core_Session::singleton()->set('dateTypes', 32); | |
90 | $form->formatDateFields($params); | |
91 | $this->assertEquals('20191020', $params['receive_date']); | |
92 | ||
93 | $params = ['receive_date' => '20/10/2019']; | |
94 | CRM_Core_Session::singleton()->set('dateTypes', 32); | |
95 | $form->formatInput($params); | |
96 | $this->assertEquals('20191020', $params['receive_date']); | |
97 | } | |
98 | ||
315a6e2a | 99 | /** |
100 | * Test payment types are passed. | |
101 | * | |
102 | * @throws \CRM_Core_Exception | |
103 | */ | |
104 | public function testPaymentTypeLabel() { | |
105 | $contactID = $this->individualCreate(); | |
106 | $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check']; | |
107 | // Note that the expected result should logically be CRM_Import_Parser::valid but writing test to reflect not fix here | |
108 | $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); | |
109 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID]); | |
110 | $this->assertEquals('Check', $contribution['payment_instrument']); | |
111 | ||
112 | $this->callAPISuccess('OptionValue', 'create', [ | |
113 | 'option_group_id' => 'payment_instrument', | |
114 | 'value' => 777, | |
115 | 'name' => 'random', | |
116 | 'label' => 'not at all random', | |
117 | ]); | |
118 | $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'not at all random']; | |
119 | $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); | |
8e2ac367 | 120 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'payment_instrument_id' => 'random']); |
315a6e2a | 121 | $this->assertEquals('not at all random', $contribution['payment_instrument']); |
122 | } | |
123 | ||
8e2ac367 | 124 | /** |
125 | * Test handling of contribution statuses. | |
126 | * | |
127 | * @throws \CRM_Core_Exception | |
128 | */ | |
129 | public function testContributionStatusLabel() { | |
130 | $contactID = $this->individualCreate(); | |
131 | $values = ['contribution_contact_id' => $contactID, 'total_amount' => 10, 'financial_type' => 'Donation', 'payment_instrument' => 'Check', 'contribution_status_id' => 'Pending']; | |
132 | // Note that the expected result should logically be CRM_Import_Parser::valid but writing test to reflect not fix here | |
133 | $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); | |
134 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID]); | |
135 | $this->assertEquals('Pending', $contribution['contribution_status']); | |
136 | ||
137 | $this->callAPISuccess('OptionValue', 'create', [ | |
138 | 'option_group_id' => 'contribution_status', | |
139 | 'value' => 777, | |
140 | 'name' => 'random', | |
141 | 'label' => 'not at all random', | |
142 | ]); | |
143 | $values['contribution_status_id'] = 'not at all random'; | |
144 | $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); | |
145 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $contactID, 'contribution_status_id' => 'random']); | |
146 | $this->assertEquals('not at all random', $contribution['contribution_status']); | |
147 | ||
148 | $values['contribution_status_id'] = 'just say no'; | |
149 | $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::ERROR); | |
150 | $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2); | |
9ae10cd7 | 151 | |
152 | // Per https://lab.civicrm.org/dev/core/issues/1285 it's a bit arguable but Ok we can support id... | |
153 | $values['contribution_status_id'] = 3; | |
154 | $this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL); | |
155 | $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID, 'contribution_status_id' => 3], 1); | |
156 | ||
8e2ac367 | 157 | } |
158 | ||
35aca6d6 | 159 | /** |
160 | * Test dates are parsed | |
161 | * | |
162 | * @throws \CRM_Core_Exception | |
163 | */ | |
164 | public function testParsedCustomDates() { | |
165 | $this->createCustomGroupWithFieldOfType([], 'date'); | |
166 | $mapperKeys = []; | |
167 | $form = new CRM_Contribute_Import_Parser_Contribution($mapperKeys); | |
168 | $params = [$this->getCustomFieldName('date') => '20/10/2019']; | |
169 | CRM_Core_Session::singleton()->set('dateTypes', 32); | |
170 | $formatted = []; | |
171 | $form->formatInput($params, $formatted); | |
172 | // @todo I feel like we should work towards this actually parsing $params here - | |
173 | // & dropping formatting but | |
174 | // per https://github.com/civicrm/civicrm-core/pull/14986 for now $formatted is parsing | |
175 | // The issue I hit was that when I tried to extend to checking they were correctly imported | |
176 | // I was not actually sure what correct behaviour was for what dates were accepted since | |
177 | // on one hand the custom fields have a date format & on the other there is an input format & | |
178 | // it seems to ignore the latter in favour of the former - which seems wrong. | |
179 | $this->assertEquals('20191020000000', $formatted[$this->getCustomFieldName('date')]); | |
180 | } | |
181 | ||
29cd38f0 JF |
182 | /** |
183 | * Run the import parser. | |
184 | * | |
185 | * @param array $originalValues | |
186 | * | |
187 | * @param int $onDuplicateAction | |
188 | * @param int $expectedResult | |
189 | * @param array|null $mapperSoftCredit | |
190 | * @param array|null $mapperPhoneType | |
191 | * @param array|null $mapperSoftCreditType | |
192 | * @param array|null $fields | |
193 | * Array of field names. Will be calculated from $originalValues if not passed in. | |
194 | */ | |
c0c7b727 | 195 | protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperSoftCredit = NULL, $mapperPhoneType = NULL, $mapperSoftCreditType = NULL, $fields = NULL) { |
29cd38f0 JF |
196 | if (!$fields) { |
197 | $fields = array_keys($originalValues); | |
198 | } | |
199 | $values = array_values($originalValues); | |
200 | $parser = new CRM_Contribute_Import_Parser_Contribution($fields, $mapperSoftCredit, $mapperPhoneType, $mapperSoftCreditType); | |
201 | $parser->_contactType = 'Individual'; | |
202 | $parser->init(); | |
203 | $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected'); | |
204 | } | |
205 | ||
206 | } |