Fix token subscriber to format the display of the custom tokens
[civicrm-core.git] / tests / phpunit / CRM / Batch / Form / EntryTest.php
1 <?php
2
3 /**
4 * File for the EntryTest class
5 *
6 * (PHP 5)
7 *
8 * @package CiviCRM
9 *
10 * This file is part of CiviCRM
11 *
12 * CiviCRM is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * CiviCRM is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public
23 * License along with this program. If not, see
24 * <http://www.gnu.org/licenses/>.
25 */
26
27 use Civi\Api4\Campaign;
28
29 /**
30 * Test CRM/Member/BAO Membership Log add , delete functions
31 *
32 * @package CiviCRM
33 * @group headless
34 */
35 class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
36
37 /**
38 * Membership type name used in test function.
39 *
40 * @var string
41 */
42 protected $_membershipTypeName = NULL;
43
44 /**
45 * Membership type id used in test function.
46 *
47 * @var string
48 */
49 protected $_membershipTypeID = NULL;
50
51 /**
52 * Contact id used in test function.
53 *
54 * @var string
55 */
56 protected $_contactID = NULL;
57 /**
58 * Contact id used in test function.
59 *
60 * @var string
61 */
62 protected $_contactID2 = NULL;
63
64 /**
65 * Contact id used in test function.
66 *
67 * @var string
68 */
69 protected $_contactID3 = NULL;
70
71 /**
72 * Contact id used in test function.
73 *
74 * @var string
75 */
76 protected $_contactID4 = NULL;
77
78 public function setUp() {
79 parent::setUp();
80
81 $params = [
82 'contact_type_a' => 'Individual',
83 'contact_type_b' => 'Organization',
84 'name_a_b' => 'Test Employee of',
85 'name_b_a' => 'Test Employer of',
86 ];
87 $this->_relationshipTypeId = $this->relationshipTypeCreate($params);
88 $this->_orgContactID = $this->organizationCreate();
89 $this->_financialTypeId = 1;
90 $this->_membershipTypeName = 'Mickey Mouse Club Member';
91 $params = [
92 'name' => $this->_membershipTypeName,
93 'description' => NULL,
94 'minimum_fee' => 1500,
95 'duration_unit' => 'year',
96 'member_of_contact_id' => $this->_orgContactID,
97 'period_type' => 'fixed',
98 'duration_interval' => 1,
99 'financial_type_id' => $this->_financialTypeId,
100 'relationship_type_id' => $this->_relationshipTypeId,
101 'visibility' => 'Public',
102 'is_active' => 1,
103 'fixed_period_start_day' => 101,
104 'fixed_period_rollover_day' => 1231,
105 'domain_id' => CRM_Core_Config::domainID(),
106 ];
107 $membershipType = $this->callAPISuccess('membership_type', 'create', $params);
108 $this->_membershipTypeID = $membershipType['id'];
109
110 $this->_orgContactID2 = $this->organizationCreate();
111 $params = [
112 'name' => 'General',
113 'duration_unit' => 'year',
114 'duration_interval' => 1,
115 'period_type' => 'rolling',
116 'member_of_contact_id' => $this->_orgContactID2,
117 'domain_id' => 1,
118 'financial_type_id' => 1,
119 'is_active' => 1,
120 'sequential' => 1,
121 'visibility' => 'Public',
122 ];
123 $membershipType2 = $this->callAPISuccess('membership_type', 'create', $params);
124 $this->_membershipTypeID2 = $membershipType2['id'];
125
126 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
127 $this->_contactID = $this->individualCreate();
128 $contact2Params = [
129 'first_name' => 'Anthonita',
130 'middle_name' => 'J.',
131 'last_name' => 'Anderson',
132 'prefix_id' => 3,
133 'suffix_id' => 3,
134 'email' => 'b@c.com',
135 'contact_type' => 'Individual',
136 ];
137 $this->_contactID2 = $this->individualCreate($contact2Params);
138 $this->_contactID3 = $this->individualCreate(['first_name' => 'bobby', 'email' => 'c@d.com']);
139 $this->_contactID4 = $this->individualCreate(['first_name' => 'bobbynita', 'email' => 'c@de.com']);
140
141 $session = CRM_Core_Session::singleton();
142 $session->set('dateTypes', 1);
143 $this->_sethtmlGlobals();
144
145 }
146
147 /**
148 * Tears down the fixture, for example, closes a network connection.
149 * This method is called after a test is executed.
150 */
151 public function tearDown() {
152 $this->quickCleanUpFinancialEntities();
153 $this->relationshipTypeDelete($this->_relationshipTypeId);
154 if ($this->callAPISuccessGetCount('membership', ['id' => $this->_membershipTypeID])) {
155 $this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
156 }
157 if ($this->callAPISuccessGetCount('MembershipStatus', ['id' => $this->_membershipStatusID])) {
158 $this->membershipStatusDelete($this->_membershipStatusID);
159 }
160 $this->contactDelete($this->_contactID);
161 $this->contactDelete($this->_contactID2);
162 $this->contactDelete($this->_orgContactID);
163 }
164
165 /**
166 * Test Import.
167 *
168 * @param string $thousandSeparator
169 *
170 * @dataProvider getThousandSeparators
171 */
172 public function testProcessMembership($thousandSeparator) {
173 $this->setCurrencySeparators($thousandSeparator);
174
175 $form = new CRM_Batch_Form_Entry();
176 $profileID = (int) $this->callAPISuccessGetValue('UFGroup', ['return' => 'id', 'name' => 'membership_batch_entry']);
177 $form->_fields = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::VIEW);
178
179 $params = $this->getMembershipData();
180 $this->assertEquals(4500.0, $form->testProcessMembership($params));
181 $result = $this->callAPISuccess('membership', 'get');
182 $this->assertEquals(3, $result['count']);
183 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
184 $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']);
185 $this->assertEquals('2013-02-03', $result['values'][2]['start_date']);
186
187 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
188 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][1]['end_date']);
189 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][2]['end_date']);
190 $this->assertEquals('2013-12-01', $result['values'][3]['end_date']);
191
192 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
193 $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $result['values'][1]['join_date']);
194 $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']);
195 $this->assertEquals(date('Y-m-d'), $result['values'][3]['join_date']);
196 $result = $this->callAPISuccess('contribution', 'get', ['return' => ['total_amount', 'trxn_id']]);
197 $this->assertEquals(3, $result['count']);
198 foreach ($result['values'] as $key => $contribution) {
199 $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', [
200 'contribution_id' => $contribution['id'],
201 'return' => 'line_total',
202
203 ]), $contribution['total_amount']);
204 $this->assertEquals(1500, $contribution['total_amount']);
205 $this->assertEquals($params['field'][$key]['trxn_id'], $contribution['trxn_id']);
206 }
207 }
208
209 /**
210 * Test Contribution Import.
211 *
212 * @param $thousandSeparator
213 *
214 * @dataProvider getThousandSeparators
215 */
216 public function testProcessContribution($thousandSeparator) {
217 $this->setCurrencySeparators($thousandSeparator);
218 $this->offsetDefaultPriceSet();
219 $form = new CRM_Batch_Form_Entry();
220 $params = $this->getContributionData();
221 $this->assertTrue($form->testProcessContribution($params));
222 $result = $this->callAPISuccess('contribution', 'get', ['return' => 'total_amount']);
223 $this->assertEquals(3, $result['count']);
224 foreach ($result['values'] as $contribution) {
225 $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', [
226 'contribution_id' => $contribution['id'],
227 'return' => 'line_total',
228
229 ]), $contribution['total_amount']);
230 }
231 $checkResult = $this->callAPISuccess('Contribution', 'get', ['check_number' => ['IS NOT NULL' => 1]]);
232 $this->assertEquals(1, $checkResult['count']);
233 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['entity_table' => 'civicrm_contribution', 'entity_id' => $checkResult['id']]);
234 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'get', ['id' => $entityFinancialTrxn['values'][$entityFinancialTrxn['id']]['financial_trxn_id']]);
235 $this->assertEquals('1234', $financialTrxn['values'][$financialTrxn['id']]['check_number']);
236 }
237
238 /**
239 * CRM-18000 - Test start_date, end_date after renewal
240 */
241 public function testMembershipRenewalDates() {
242 $form = new CRM_Batch_Form_Entry();
243 $campaignID = Campaign::create()->setValues(['name' => 'blah', 'title' => 'blah'])->execute()->first()['id'];
244 foreach ([$this->_contactID, $this->_contactID2] as $contactID) {
245 $membershipParams = [
246 'membership_type_id' => $this->_membershipTypeID2,
247 'contact_id' => $contactID,
248 'start_date' => "01/01/2015",
249 'join_date' => "01/01/2010",
250 'end_date' => "12/31/2015",
251 ];
252 $this->contactMembershipCreate($membershipParams);
253 }
254
255 $params = $this->getMembershipData();
256 //ensure membership renewal
257 $params['member_option'] = [
258 1 => 2,
259 2 => 2,
260 ];
261 $params['field'][1]['membership_type'] = [0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2];
262 $params['field'][1]['receive_date'] = date('Y-m-d');
263 $params['field'][1]['member_campaign_id'] = $campaignID;
264
265 // explicitly specify start and end dates
266 $params['field'][2]['membership_type'] = [0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2];
267 $params['field'][2]['membership_start_date'] = "2016-04-01";
268 $params['field'][2]['membership_end_date'] = "2017-03-31";
269 $params['field'][2]['receive_date'] = "2016-04-01";
270
271 $this->assertEquals(3.0, $form->testProcessMembership($params));
272 $result = $this->callAPISuccess('membership', 'get')['values'];
273
274 // renewal dates should be from current if start_date and end_date is passed as NULL
275 $this->assertEquals(date('Y-m-d'), $result[1]['start_date']);
276 $endDate = date("Y-m-d", strtotime(date("Y-m-d") . " +1 year -1 day"));
277 $this->assertEquals($endDate, $result[1]['end_date']);
278 $this->assertEquals(1, $result[1]['campaign_id']);
279
280 // verify if the modified dates asserts with the dates passed above
281 $this->assertEquals('2016-04-01', $result[2]['start_date']);
282 $this->assertEquals('2017-03-31', $result[2]['end_date']);
283 $this->assertTrue(empty($result[2]['campaign_id']));
284 }
285
286 /**
287 * Data provider for test process membership.
288 * @return array
289 */
290 public function getMembershipData() {
291
292 return [
293 'batch_id' => 4,
294 'primary_profiles' => [1 => NULL, 2 => NULL, 3 => NULL],
295 'primary_contact_id' => [
296 1 => $this->_contactID,
297 2 => $this->_contactID2,
298 3 => $this->_contactID3,
299 ],
300 'field' => [
301 1 => [
302 'membership_type' => [0 => $this->_orgContactID, 1 => $this->_membershipTypeID],
303 'membership_join_date' => '2013-07-22',
304 'membership_start_date' => NULL,
305 'membership_end_date' => NULL,
306 'membership_source' => NULL,
307 'financial_type' => 2,
308 'total_amount' => $this->formatMoneyInput(1500),
309 'receive_date' => '2013-07-24',
310 'receive_date_time' => NULL,
311 'payment_instrument' => 1,
312 'trxn_id' => 'TX101',
313 'check_number' => NULL,
314 'contribution_status_id' => 1,
315 ],
316 2 => [
317 'membership_type' => [0 => $this->_orgContactID, 1 => $this->_membershipTypeID],
318 'membership_join_date' => '2013-07-03',
319 'membership_start_date' => '2013-02-03',
320 'membership_end_date' => NULL,
321 'membership_source' => NULL,
322 'financial_type' => 2,
323 'total_amount' => $this->formatMoneyInput(1500),
324 'receive_date' => '2013-07-17',
325 'receive_date_time' => NULL,
326 'payment_instrument' => NULL,
327 'trxn_id' => 'TX102',
328 'check_number' => NULL,
329 'contribution_status_id' => 1,
330 ],
331 // no join date, coded end date
332 3 => [
333 'membership_type' => [0 => $this->_orgContactID, 1 => $this->_membershipTypeID],
334 'membership_join_date' => NULL,
335 'membership_start_date' => NULL,
336 'membership_end_date' => '2013-12-01',
337 'membership_source' => NULL,
338 'financial_type' => 2,
339 'total_amount' => $this->formatMoneyInput(1500),
340 'receive_date' => '2013-07-17',
341 'receive_date_time' => NULL,
342 'payment_instrument' => NULL,
343 'trxn_id' => 'TX103',
344 'check_number' => NULL,
345 'contribution_status_id' => 1,
346 ],
347
348 ],
349 'actualBatchTotal' => 0,
350
351 ];
352 }
353
354 /**
355 * @param $thousandSeparator
356 *
357 * @return array
358 */
359 public function getContributionData($thousandSeparator = '.') {
360 return [
361 //'batch_id' => 4,
362 'primary_profiles' => [1 => NULL, 2 => NULL, 3 => NULL],
363 'primary_contact_id' => [
364 1 => $this->_contactID,
365 2 => $this->_contactID2,
366 3 => $this->_contactID3,
367 ],
368 'field' => [
369 1 => [
370 'financial_type' => 1,
371 'total_amount' => $this->formatMoneyInput(1500.15),
372 'receive_date' => '2013-07-24',
373 'receive_date_time' => NULL,
374 'payment_instrument' => 1,
375 'check_number' => NULL,
376 'contribution_status_id' => 1,
377 ],
378 2 => [
379 'financial_type' => 1,
380 'total_amount' => $this->formatMoneyInput(1500.15),
381 'receive_date' => '2013-07-24',
382 'receive_date_time' => NULL,
383 'payment_instrument' => 1,
384 'check_number' => NULL,
385 'contribution_status_id' => 1,
386 ],
387 3 => [
388 'financial_type' => 1,
389 'total_amount' => $this->formatMoneyInput(1500.15),
390 'receive_date' => '2013-07-24',
391 'receive_date_time' => NULL,
392 'payment_instrument' => 4,
393 'contribution_check_number' => '1234',
394 'contribution_status_id' => 1,
395 ],
396 ],
397 'actualBatchTotal' => $this->formatMoneyInput(4500.45),
398
399 ];
400 }
401
402 }