a2f78a3bc94bcecc2cab26684f977a64f337b4b9
[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->_membershipStatusID = $this->membershipStatusCreate('test status');
103 $this->_contactID = $this->individualCreate();
104 $contact2Params = array(
105 'first_name' => 'Anthonita',
106 'middle_name' => 'J.',
107 'last_name' => 'Anderson',
108 'prefix_id' => 3,
109 'suffix_id' => 3,
110 'email' => 'b@c.com',
111 'contact_type' => 'Individual',
112 );
113 $this->_contactID2 = $this->individualCreate($contact2Params);
114 $this->_contactID3 = $this->individualCreate(array('first_name' => 'bobby', 'email' => 'c@d.com'));
115 $this->_contactID4 = $this->individualCreate(array('first_name' => 'bobbynita', 'email' => 'c@de.com'));
116
117 $session = CRM_Core_Session::singleton();
118 $session->set('dateTypes', 1);
119 $this->_sethtmlGlobals();
120
121 }
122
123 /**
124 * Tears down the fixture, for example, closes a network connection.
125 * This method is called after a test is executed.
126 */
127 public function tearDown() {
128 $this->quickCleanUpFinancialEntities();
129 $this->relationshipTypeDelete($this->_relationshipTypeId);
130 if ($this->callAPISuccessGetCount('membership', array('id' => $this->_membershipTypeID))) {
131 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
132 }
133 $this->membershipStatusDelete($this->_membershipStatusID);
134 $this->contactDelete($this->_contactID);
135 $this->contactDelete($this->_contactID2);
136 $this->contactDelete($this->_orgContactID);
137 }
138
139 /**
140 * Test Import.
141 */
142 public function testProcessMembership() {
143 $form = new CRM_Batch_Form_Entry();
144 $params = $this->getMembershipData();
145 $this->assertTrue($form->testProcessMembership($params));
146 $result = $this->callAPISuccess('membership', 'get', array());
147 $this->assertEquals(3, $result['count']);
148 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
149 $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']);
150 $this->assertEquals('2013-02-03', $result['values'][2]['start_date']);
151
152 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
153 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][1]['end_date']);
154 $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][2]['end_date']);
155 $this->assertEquals('2013-12-01', $result['values'][3]['end_date']);
156
157 //check start dates #1 should default to 1 Jan this year, #2 should be as entered
158 $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $result['values'][1]['join_date']);
159 $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']);
160 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['values'][3]['join_date']);
161 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount'));
162 $this->assertEquals(3, $result['count']);
163 foreach ($result['values'] as $contribution) {
164 $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
165 'contribution_id' => $contribution['id'],
166 'return' => 'line_total',
167
168 )), $contribution['total_amount']);
169 }
170 }
171
172 /**
173 * Test Contribution Import.
174 */
175 public function testProcessContribution() {
176 $this->offsetDefaultPriceSet();
177 $form = new CRM_Batch_Form_Entry();
178 $params = $this->getContributionData();
179 $this->assertTrue($form->testProcessContribution($params));
180 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount'));
181 $this->assertEquals(2, $result['count']);
182 foreach ($result['values'] as $contribution) {
183 $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
184 'contribution_id' => $contribution['id'],
185 'return' => 'line_total',
186
187 )), $contribution['total_amount']);
188 }
189 }
190
191 /**
192 * Data provider for test process membership.
193 * @return array
194 */
195 public function getMembershipData() {
196
197 return array(
198 'batch_id' => 4,
199 'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL),
200 'primary_contact_id' => array(
201 1 => $this->_contactID,
202 2 => $this->_contactID2,
203 3 => $this->_contactID3,
204 ),
205 'field' => array(
206 1 => array(
207 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
208 'join_date' => '07/22/2013',
209 'membership_start_date' => NULL,
210 'membership_end_date' => NULL,
211 'membership_source' => NULL,
212 'financial_type' => 2,
213 'total_amount' => 1,
214 'receive_date' => '07/24/2013',
215 'receive_date_time' => NULL,
216 'payment_instrument' => 1,
217 'check_number' => NULL,
218 'contribution_status_id' => 1,
219 ),
220 2 => array(
221 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
222 'join_date' => '07/03/2013',
223 'membership_start_date' => '02/03/2013',
224 'membership_end_date' => NULL,
225 'membership_source' => NULL,
226 'financial_type' => 2,
227 'total_amount' => 1,
228 'receive_date' => '07/17/2013',
229 'receive_date_time' => NULL,
230 'payment_instrument' => NULL,
231 'check_number' => NULL,
232 'contribution_status_id' => 1,
233 ),
234 // no join date, coded end date
235 3 => array(
236 'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
237 'join_date' => NULL,
238 'membership_start_date' => NULL,
239 'membership_end_date' => '2013-12-01',
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
250 ),
251 'actualBatchTotal' => 0,
252
253 );
254 }
255
256 /**
257 * @return array
258 */
259 public function getContributionData() {
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 'financial_type' => 1,
271 'total_amount' => 15,
272 'receive_date' => '07/24/2013',
273 'receive_date_time' => NULL,
274 'payment_instrument' => 1,
275 'check_number' => NULL,
276 'contribution_status_id' => 1,
277 ),
278 2 => array(
279 'financial_type' => 1,
280 'total_amount' => 15,
281 'receive_date' => '07/24/2013',
282 'receive_date_time' => NULL,
283 'payment_instrument' => 1,
284 'check_number' => NULL,
285 'contribution_status_id' => 1,
286 ),
287 ),
288 'actualBatchTotal' => 30,
289
290 );
291 }
292
293 }