4 * File for the EntryTest class
10 * This file is part of CiviCRM
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.
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.
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/>.
27 use Civi\Api4\Campaign
;
30 * Test CRM/Member/BAO Membership Log add , delete functions
35 class CRM_Batch_Form_EntryTest
extends CiviUnitTestCase
{
38 * Membership type name used in test function.
42 protected $_membershipTypeName = NULL;
45 * Membership type id used in test function.
49 protected $_membershipTypeID = NULL;
52 * Contact id used in test function.
56 protected $_contactID = NULL;
58 * Contact id used in test function.
62 protected $_contactID2 = NULL;
65 * Contact id used in test function.
69 protected $_contactID3 = NULL;
72 * Contact id used in test function.
76 protected $_contactID4 = NULL;
78 public function setUp() {
82 'contact_type_a' => 'Individual',
83 'contact_type_b' => 'Organization',
84 'name_a_b' => 'Test Employee of',
85 'name_b_a' => 'Test Employer of',
87 $this->_relationshipTypeId
= $this->relationshipTypeCreate($params);
88 $this->_orgContactID
= $this->organizationCreate();
89 $this->_financialTypeId
= 1;
90 $this->_membershipTypeName
= 'Mickey Mouse Club Member';
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',
103 'fixed_period_start_day' => 101,
104 'fixed_period_rollover_day' => 1231,
105 'domain_id' => CRM_Core_Config
::domainID(),
107 $membershipType = $this->callAPISuccess('membership_type', 'create', $params);
108 $this->_membershipTypeID
= $membershipType['id'];
110 $this->_orgContactID2
= $this->organizationCreate();
113 'duration_unit' => 'year',
114 'duration_interval' => 1,
115 'period_type' => 'rolling',
116 'member_of_contact_id' => $this->_orgContactID2
,
118 'financial_type_id' => 1,
121 'visibility' => 'Public',
123 $membershipType2 = $this->callAPISuccess('membership_type', 'create', $params);
124 $this->_membershipTypeID2
= $membershipType2['id'];
126 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
127 $this->_contactID
= $this->individualCreate();
129 'first_name' => 'Anthonita',
130 'middle_name' => 'J.',
131 'last_name' => 'Anderson',
134 'email' => 'b@c.com',
135 'contact_type' => 'Individual',
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']);
141 $session = CRM_Core_Session
::singleton();
142 $session->set('dateTypes', 1);
143 $this->_sethtmlGlobals();
148 * Tears down the fixture, for example, closes a network connection.
149 * This method is called after a test is executed.
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
]);
157 if ($this->callAPISuccessGetCount('MembershipStatus', ['id' => $this->_membershipStatusID
])) {
158 $this->membershipStatusDelete($this->_membershipStatusID
);
160 $this->contactDelete($this->_contactID
);
161 $this->contactDelete($this->_contactID2
);
162 $this->contactDelete($this->_orgContactID
);
168 * @param string $thousandSeparator
170 * @dataProvider getThousandSeparators
172 public function testProcessMembership($thousandSeparator) {
173 $this->setCurrencySeparators($thousandSeparator);
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
);
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']);
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']);
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',
203 ]), $contribution['total_amount']);
204 $this->assertEquals(1500, $contribution['total_amount']);
205 $this->assertEquals($params['field'][$key]['trxn_id'], $contribution['trxn_id']);
210 * Test Contribution Import.
212 * @param $thousandSeparator
214 * @dataProvider getThousandSeparators
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',
229 ]), $contribution['total_amount']);
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']);
239 * CRM-18000 - Test start_date, end_date after renewal
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",
252 $this->contactMembershipCreate($membershipParams);
255 $params = $this->getMembershipData();
256 //ensure membership renewal
257 $params['member_option'] = [
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;
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";
271 $this->assertEquals(3.0, $form->testProcessMembership($params));
272 $result = $this->callAPISuccess('membership', 'get')['values'];
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']);
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']));
287 * Data provider for test process membership.
290 public function getMembershipData() {
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
,
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,
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,
331 // no join date, coded end date
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,
349 'actualBatchTotal' => 0,
355 * @param $thousandSeparator
359 public function getContributionData($thousandSeparator = '.') {
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
,
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,
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,
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,
397 'actualBatchTotal' => $this->formatMoneyInput(4500.45),