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