Merge pull request #11552 from seamuslee001/activity_unit_test_failures_4_7_30
[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' => 10,
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 public function testProcessMembership() {
161 $form = new CRM_Batch_Form_Entry();
162 $params = $this->getMembershipData();
163 $this->assertTrue($form->testProcessMembership($params));
164 $result = $this->callAPISuccess('membership', 'get', array());
165 $this->assertEquals(3, $result['count']);
166 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
167 $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']);
168 $this->assertEquals('2013-02-03', $result['values'][2]['start_date']);
169
170 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
171 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][1]['end_date']);
172 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][2]['end_date']);
173 $this->assertEquals('2013-12-01', $result['values'][3]['end_date']);
174
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('07/22/2013')), $result['values'][1]['join_date']);
177 $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']);
178 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['values'][3]['join_date']);
179 $result = $this->callAPISuccess('contribution', 'get', array('return' => array('total_amount', 'trxn_id')));
180 $this->assertEquals(3, $result['count']);
181 foreach ($result['values'] as $key => $contribution) {
182 $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
183 'contribution_id' => $contribution['id'],
184 'return' => 'line_total',
185
186 )), $contribution['total_amount']);
187 $this->assertEquals($params['field'][$key]['trxn_id'], $contribution['trxn_id']);
188 }
189 }
190
191 /**
192 * Test Contribution Import.
193 */
194 public function testProcessContribution() {
195 $this->offsetDefaultPriceSet();
196 $form = new CRM_Batch_Form_Entry();
197 $params = $this->getContributionData();
198 $this->assertTrue($form->testProcessContribution($params));
199 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount'));
200 $this->assertEquals(2, $result['count']);
201 foreach ($result['values'] as $contribution) {
202 $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
203 'contribution_id' => $contribution['id'],
204 'return' => 'line_total',
205
206 )), $contribution['total_amount']);
207 }
208 }
209
210 /**
211 * CRM-18000 - Test start_date, end_date after renewal
212 */
213 public function testMembershipRenewalDates() {
214 $form = new CRM_Batch_Form_Entry();
215 foreach (array($this->_contactID, $this->_contactID2) as $contactID) {
216 $membershipParams = array(
217 'membership_type_id' => $this->_membershipTypeID2,
218 'contact_id' => $contactID,
219 'start_date' => "01/01/2015",
220 'join_date' => "01/01/2010",
221 'end_date' => "12/31/2015",
222 );
223 $this->contactMembershipCreate($membershipParams);
224 }
225
226 $params = $this->getMembershipData();
227 //ensure membership renewal
228 $params['member_option'] = array(
229 1 => 2,
230 2 => 2,
231 );
232 $params['field'][1]['membership_type'] = array(0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2);
233 $params['field'][1]['receive_date'] = date('Y-m-d');
234
235 // explicitly specify start and end dates
236 $params['field'][2]['membership_type'] = array(0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2);
237 $params['field'][2]['membership_start_date'] = "2016-04-01";
238 $params['field'][2]['membership_end_date'] = "2017-03-31";
239 $params['field'][2]['receive_date'] = "2016-04-01";
240
241 $this->assertTrue($form->testProcessMembership($params));
242 $result = $this->callAPISuccess('membership', 'get', array());
243
244 // renewal dates should be from current if start_date and end_date is passed as NULL
245 $this->assertEquals(date('Y-m-d'), $result['values'][1]['start_date']);
246 $endDate = date("Y-m-d", strtotime(date("Y-m-d") . " +1 year -1 day"));
247 $this->assertEquals($endDate, $result['values'][1]['end_date']);
248
249 // verify if the modified dates asserts with the dates passed above
250 $this->assertEquals('2016-04-01', $result['values'][2]['start_date']);
251 $this->assertEquals('2017-03-31', $result['values'][2]['end_date']);
252 }
253
254 /**
255 * Data provider for test process membership.
256 * @return array
257 */
258 public function getMembershipData() {
259
260 return array(
261 'batch_id' => 4,
262 'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL),
263 'primary_contact_id' => array(
264 1 => $this->_contactID,
265 2 => $this->_contactID2,
266 3 => $this->_contactID3,
267 ),
268 'field' => array(
269 1 => array(
270 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
271 'join_date' => '2013-07-22',
272 'membership_start_date' => NULL,
273 'membership_end_date' => NULL,
274 'membership_source' => NULL,
275 'financial_type' => 2,
276 'total_amount' => 1,
277 'receive_date' => '2013-07-24',
278 'receive_date_time' => NULL,
279 'payment_instrument' => 1,
280 'trxn_id' => 'TX101',
281 'check_number' => NULL,
282 'contribution_status_id' => 1,
283 ),
284 2 => array(
285 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
286 'join_date' => '2013-07-03',
287 'membership_start_date' => '2013-02-03',
288 'membership_end_date' => NULL,
289 'membership_source' => NULL,
290 'financial_type' => 2,
291 'total_amount' => 1,
292 'receive_date' => '2013-07-17',
293 'receive_date_time' => NULL,
294 'payment_instrument' => NULL,
295 'trxn_id' => 'TX102',
296 'check_number' => NULL,
297 'contribution_status_id' => 1,
298 ),
299 // no join date, coded end date
300 3 => array(
301 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
302 'join_date' => NULL,
303 'membership_start_date' => NULL,
304 'membership_end_date' => '2013-12-01',
305 'membership_source' => NULL,
306 'financial_type' => 2,
307 'total_amount' => 1,
308 'receive_date' => '2013-07-17',
309 'receive_date_time' => NULL,
310 'payment_instrument' => NULL,
311 'trxn_id' => 'TX103',
312 'check_number' => NULL,
313 'contribution_status_id' => 1,
314 ),
315
316 ),
317 'actualBatchTotal' => 0,
318
319 );
320 }
321
322 /**
323 * @return array
324 */
325 public function getContributionData() {
326 return array(
327 //'batch_id' => 4,
328 'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL),
329 'primary_contact_id' => array(
330 1 => $this->_contactID,
331 2 => $this->_contactID2,
332 3 => $this->_contactID3,
333 ),
334 'field' => array(
335 1 => array(
336 'financial_type' => 1,
337 'total_amount' => 15,
338 'receive_date' => '2013-07-24',
339 'receive_date_time' => NULL,
340 'payment_instrument' => 1,
341 'check_number' => NULL,
342 'contribution_status_id' => 1,
343 ),
344 2 => array(
345 'financial_type' => 1,
346 'total_amount' => 15,
347 'receive_date' => '2013-07-24',
348 'receive_date_time' => NULL,
349 'payment_instrument' => 1,
350 'check_number' => NULL,
351 'contribution_status_id' => 1,
352 ),
353 ),
354 'actualBatchTotal' => 30,
355
356 );
357 }
358
359 }