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