Merge pull request #4252 from civicrm/4.4
[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 require_once 'CiviTest/CiviUnitTestCase.php';
28
29
30 /**
31 * Test CRM/Member/BAO Membership Log add , delete functions
32 *
33 * @package CiviCRM
34 */
35 class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
36
37 /**
38 * Membership type name used in test function
39 * @var String
40 */
41 protected $_membershipTypeName = NULL;
42
43 /**
44 * Membership type id used in test function
45 * @var String
46 */
47 protected $_membershipTypeID = NULL;
48
49 /**
50 * Contact id used in test function
51 * @var String
52 */
53 protected $_contactID = NULL;
54 /**
55 * Contact id used in test function
56 * @var String
57 */
58 protected $_contactID2 = NULL;
59
60 /**
61 * Contact id used in test function
62 * @var String
63 */
64 protected $_contactID3 = NULL;
65
66 /**
67 * Contact id used in test function
68 * @var String
69 */
70 protected $_contactID4 = NULL;
71
72 /**
73 * Describe test class
74 * @return array
75 */
76 function get_info() {
77 return array(
78 'name' => 'MembershipParserTest',
79 'description' => 'Test import parser function',
80 'group' => 'CiviCRM BAO Tests',
81 );
82 }
83
84 function setUp() {
85 parent::setUp();
86
87 $params = array(
88 'contact_type_a' => 'Individual',
89 'contact_type_b' => 'Organization',
90 'name_a_b' => 'Test Employee of',
91 'name_b_a' => 'Test Employer of',
92 );
93 $this->_relationshipTypeId = $this->relationshipTypeCreate($params);
94 $this->_orgContactID = $this->organizationCreate();
95 $this->_financialTypeId = 1;
96 $this->_membershipTypeName = 'Mickey Mouse Club Member';
97 $params = array(
98 'name' => $this->_membershipTypeName,
99 'description' => NULL,
100 'minimum_fee' => 10,
101 'duration_unit' => 'year',
102 'member_of_contact_id' => $this->_orgContactID,
103 'period_type' => 'fixed',
104 'duration_interval' => 1,
105 'financial_type_id' => $this->_financialTypeId,
106 'relationship_type_id' => $this->_relationshipTypeId,
107 'visibility' => 'Public',
108 'is_active' => 1,
109 'fixed_period_start_day' => 101,
110 'fixed_period_rollover_day' => 1231,
111 'domain_id' => CRM_Core_Config::domainID(),
112 );
113 $membershipType = $this->callAPISuccess('membership_type', 'create', $params);
114 $this->_membershipTypeID = $membershipType['id'];
115
116 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
117 $this->_contactID = $this->individualCreate();
118 $contact2Params = array(
119 'first_name' => 'Anthonita',
120 'middle_name' => 'J.',
121 'last_name' => 'Anderson',
122 'prefix_id' => 3,
123 'suffix_id' => 3,
124 'email' => 'b@c.com',
125 'contact_type' => 'Individual',
126 );
127 $this->_contactID2 = $this->individualCreate($contact2Params);
128 $this->_contactID3 = $this->individualCreate(array('first_name' => 'bobby', 'email' => 'c@d.com'));
129 $this->_contactID4 = $this->individualCreate(array('first_name' => 'bobbynita', 'email' => 'c@de.com'));
130
131 $session = CRM_Core_Session::singleton();
132 $session->set('dateTypes', 1);
133 $this->_sethtmlGlobals();
134
135 }
136
137 /**
138 * Tears down the fixture, for example, closes a network connection.
139 * This method is called after a test is executed.
140 *
141 */
142 function tearDown()
143 {
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 $this->membershipStatusDelete($this->_membershipStatusID);
150 $this->contactDelete($this->_contactID);
151 $this->contactDelete($this->_contactID2);
152 $this->contactDelete($this->_orgContactID);
153 }
154
155 /**
156 * Test Import
157 */
158 function testProcessMembership() {
159 $form = new CRM_Batch_Form_Entry();
160 $params = $this->getMembershipData();
161 $this->assertTrue($form->testProcessMembership($params));
162 $result = $this->callAPISuccess('membership', 'get', array());
163 $this->assertEquals(3, $result['count']);
164 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
165 $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']);
166 $this->assertEquals('2013-02-03', $result['values'][2]['start_date']);
167
168 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
169 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][1]['end_date']);
170 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][2]['end_date']);
171 $this->assertEquals('2013-12-01', $result['values'][3]['end_date']);
172
173 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
174 $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $result['values'][1]['join_date']);
175 $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']);
176 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['values'][3]['join_date']);
177 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount'));
178 $this->assertEquals(3, $result['count']);
179 foreach($result['values'] as $contribution) {
180 $this-> assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
181 'contribution_id' => $contribution['id'],
182 'return' => 'line_total',
183
184 )), $contribution['total_amount']);
185 }
186 }
187
188 /**
189 * Test Contribution Import
190 */
191 function testProcessContribution() {
192 $this->offsetDefaultPriceSet();
193 $form = new CRM_Batch_Form_Entry();
194 $params = $this->getContributionData();
195 $this->assertTrue($form->testProcessContribution($params));
196 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount'));
197 $this->assertEquals(2, $result['count']);
198 foreach($result['values'] as $contribution) {
199 $this-> assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
200 'contribution_id' => $contribution['id'],
201 'return' => 'line_total',
202
203 )), $contribution['total_amount']);
204 }
205 }
206 /**
207 * data provider for test process membership
208 * @return array
209 */
210 function getMembershipData() {
211
212 return array(
213 'batch_id' => 4,
214 'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL),
215 'primary_contact_id' => Array (
216 1 => $this->_contactID,
217 2 => $this->_contactID2,
218 3 => $this->_contactID3,
219 ),
220 'field' => array(
221 1 => array(
222 'membership_type' => Array (0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
223 'join_date' => '07/22/2013',
224 'membership_start_date' => NULL,
225 'membership_end_date' => NULL,
226 'membership_source' => NULL,
227 'financial_type' => 2,
228 'total_amount' => 1,
229 'receive_date' => '07/24/2013',
230 'receive_date_time' => NULL,
231 'payment_instrument' => 1,
232 'check_number' => NULL,
233 'contribution_status_id' => 1,
234 ),
235 2 => array(
236 'membership_type' => Array (0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
237 'join_date' => '07/03/2013',
238 'membership_start_date' => '02/03/2013',
239 'membership_end_date' => NULL,
240 'membership_source' => NULL,
241 'financial_type' => 2,
242 'total_amount' => 1,
243 'receive_date' => '07/17/2013',
244 'receive_date_time' => NULL,
245 'payment_instrument' => NULL,
246 'check_number' => NULL,
247 'contribution_status_id' => 1,
248 ),
249 // no join date, coded end date
250 3 => array(
251 'membership_type' => Array (0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
252 'join_date' => NULL,
253 'membership_start_date' => NULL,
254 'membership_end_date' => '2013-12-01',
255 'membership_source' => NULL,
256 'financial_type' => 2,
257 'total_amount' => 1,
258 'receive_date' => '07/17/2013',
259 'receive_date_time' => NULL,
260 'payment_instrument' => NULL,
261 'check_number' => NULL,
262 'contribution_status_id' => 1,
263 ),
264
265 ),
266 'actualBatchTotal' => 0,
267
268 );
269 }
270
271 /**
272 * @return array
273 */
274 function getContributionData() {
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 'financial_type' => 1,
286 'total_amount' => 15,
287 'receive_date' => '07/24/2013',
288 'receive_date_time' => NULL,
289 'payment_instrument' => 1,
290 'check_number' => NULL,
291 'contribution_status_id' => 1,
292 ),
293 2 => array(
294 'financial_type' => 1,
295 'total_amount' => 15,
296 'receive_date' => '07/24/2013',
297 'receive_date_time' => NULL,
298 'payment_instrument' => 1,
299 'check_number' => NULL,
300 'contribution_status_id' => 1,
301 ),
302 ),
303 'actualBatchTotal' => 30,
304
305 );
306 }
307 }
308