Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.6 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
30 | ||
31 | /** | |
32 | * Test class for CRM_Pledge_BAO_PledgePayment BAO | |
33 | * | |
34 | * @package CiviCRM | |
35 | */ | |
36 | class CRM_Pledge_BAO_PledgePaymentTest extends CiviUnitTestCase { | |
37 | ||
38 | /** | |
39 | * Sets up the fixture, for example, opens a network connection. | |
40 | * This method is called before a test is executed. | |
41 | */ | |
42 | protected function setUp() { | |
43 | parent::setUp(); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Tears down the fixture, for example, closes a network connection. | |
48 | * This method is called after a test is executed. | |
49 | */ | |
50 | protected function tearDown() { | |
51 | } | |
52 | ||
53 | /** | |
54 | * Test for Add/Update Pledge Payment. | |
55 | */ | |
56 | public function testAdd() { | |
57 | $pledge = CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_Pledge'); | |
58 | $params = array( | |
59 | 'pledge_id' => $pledge->id, | |
60 | 'scheduled_amount' => 100.55, | |
61 | 'currency' => 'USD', | |
62 | 'scheduled_date' => '20100512000000', | |
63 | 'reminder_date' => '20100520000000', | |
64 | 'reminder_count' => 5, | |
65 | 'status_id' => 1, | |
66 | ); | |
67 | ||
68 | //do test for normal add. | |
69 | $payment = CRM_Pledge_BAO_PledgePayment::add($params); | |
70 | foreach ($params as $param => $value) { | |
71 | $this->assertEquals($value, $payment->$param); | |
72 | } | |
73 | ||
74 | //do test for update mode. | |
75 | $params = array( | |
76 | 'id' => $payment->id, | |
77 | 'pledge_id' => $pledge->id, | |
78 | 'scheduled_amount' => 55.55, | |
79 | 'currency' => 'USD', | |
80 | 'scheduled_date' => '20100415000000', | |
81 | 'reminder_date' => '20100425000000', | |
82 | 'reminder_count' => 10, | |
83 | 'status_id' => 2, | |
84 | ); | |
85 | ||
86 | $payment = CRM_Pledge_BAO_PledgePayment::add($params); | |
87 | foreach ($params as $param => $value) { | |
88 | $this->assertEquals($value, $payment->$param); | |
89 | } | |
90 | $result = CRM_Pledge_BAO_Pledge::deletePledge($pledge->id); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Retrieve a payment based on a pledge id = 0 | |
95 | */ | |
96 | public function testRetrieveZeroPledeID() { | |
97 | $payment = CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_PledgePayment'); | |
98 | $params = array('pledge_id' => 0); | |
99 | $defaults = array(); | |
100 | $paymentid = CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults); | |
101 | ||
102 | $this->assertEquals(count($paymentid), 0, "Pledge Id must be greater than 0"); | |
103 | $result = CRM_Pledge_BAO_Pledge::deletePledge($payment->pledge_id); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Retrieve a payment based on a Null pledge id. | |
108 | */ | |
109 | public function testRetrieveStringPledgeID() { | |
110 | $payment = CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_PledgePayment'); | |
111 | $params = array('pledge_id' => 'Test'); | |
112 | $defaults = array(); | |
113 | $paymentid = CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults); | |
114 | ||
115 | $this->assertEquals(count($paymentid), 0, "Pledge Id cannot be a string"); | |
116 | $result = CRM_Pledge_BAO_Pledge::deletePledge($payment->pledge_id); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Test that payment retrieve wrks based on known pledge id. | |
121 | */ | |
122 | public function testRetrieveKnownPledgeID() { | |
123 | $payment = CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_PledgePayment'); | |
124 | $pledgeId = $payment->pledge_id; | |
125 | $params = array('pledge_id' => $pledgeId); | |
126 | $defaults = array(); | |
127 | $paymentid = CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults); | |
128 | ||
129 | $this->assertEquals(count($paymentid), 1, "Pledge was retrieved"); | |
130 | $result = CRM_Pledge_BAO_Pledge::deletePledge($pledgeId); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Delete Payments payments for one pledge. | |
135 | */ | |
136 | public function testDeletePledgePaymentsNormal() { | |
137 | $payment = CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_PledgePayment'); | |
138 | $paymentid = CRM_Pledge_BAO_PledgePayment::deletePayments($payment->pledge_id); | |
139 | $this->assertEquals(count($paymentid), 1, "Deleted one payment"); | |
140 | $result = CRM_Pledge_BAO_Pledge::deletePledge($payment->pledge_id); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Delete Multiple payments for one pledge. | |
145 | */ | |
146 | public function testDeletePledgePayments() { | |
147 | $contactId = $this->individualCreate(); | |
148 | $pledgeId = $this->pledgeCreate(array('contact_id' => $contactId)); | |
149 | CRM_Pledge_BAO_PledgePayment::deletePayments($pledgeId); | |
150 | ||
151 | // No payments should be retrieved | |
152 | $pledgePayment = CRM_Pledge_BAO_PledgePayment::getPledgePayments($pledgeId); | |
153 | $this->assertEquals(count($pledgePayment), 0, "Checking for empty array"); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Pass Null Id for a payment deletion for one pledge. | |
158 | */ | |
159 | public function testDeletePledgePaymentsNullId() { | |
160 | $payment = CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_PledgePayment'); | |
161 | $paymentid = CRM_Pledge_BAO_PledgePayment::deletePayments(NULL); | |
162 | $this->assertEquals(count($paymentid), 1, "No payments deleted"); | |
163 | $result = CRM_Pledge_BAO_Pledge::deletePledge($payment->pledge_id); | |
164 | } | |
165 | ||
166 | /** | |
167 | * Pass Zero Id for a payment deletion for one pledge. | |
168 | */ | |
169 | public function testDeletePaymentsZeroId() { | |
170 | $payment = CRM_Core_DAO::createTestObject('CRM_Pledge_BAO_PledgePayment'); | |
171 | $paymentid = CRM_Pledge_BAO_PledgePayment::deletePayments(0); | |
172 | $result = CRM_Pledge_BAO_Pledge::deletePledge($payment->pledge_id); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Test calculateBaseScheduleDate - should give 15th day of month | |
177 | */ | |
178 | public function testcalculateBaseScheduleDateMonth() { | |
179 | $params = array( | |
180 | 'scheduled_date' => '20110510', | |
181 | 'frequency_unit' => 'month', | |
182 | 'frequency_day' => 15, | |
183 | 'frequency_interval' => 2, | |
184 | ); | |
185 | ||
186 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
187 | $this->assertEquals('20110515000000', $date); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Test calculateBaseScheduleDate - should give original date | |
192 | */ | |
193 | public function testcalculateBaseScheduleDateDay() { | |
194 | $params = array( | |
195 | 'scheduled_date' => '20110510', | |
196 | 'frequency_unit' => 'day', | |
197 | 'frequency_day' => 15, | |
198 | 'frequency_interval' => 2, | |
199 | ); | |
200 | ||
201 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
202 | $this->assertEquals('20110510000000', $date); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Test calculateBaseScheduleDateWeek - should give the day in the week as indicated | |
207 | * testing each day as this is really the only unit that does anything | |
208 | */ | |
209 | public function testcalculateBaseScheduleDateWeek() { | |
210 | $params = array( | |
211 | 'scheduled_date' => '20110510', | |
212 | 'frequency_unit' => 'week', | |
213 | 'frequency_day' => 1, | |
214 | 'frequency_interval' => 2, | |
215 | ); | |
216 | ||
217 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
218 | $this->assertEquals('20110509000000', $date); | |
219 | $params['frequency_day'] = 2; | |
220 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
221 | $this->assertEquals('20110510000000', $date); | |
222 | $params['frequency_day'] = 3; | |
223 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
224 | $this->assertEquals('20110511000000', $date); | |
225 | $params['frequency_day'] = 4; | |
226 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
227 | $this->assertEquals('20110512000000', $date); | |
228 | $params['frequency_day'] = 5; | |
229 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
230 | $this->assertEquals('20110513000000', $date); | |
231 | $params['frequency_day'] = 6; | |
232 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
233 | $this->assertEquals('20110514000000', $date); | |
234 | $params['frequency_day'] = 7; | |
235 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
236 | $this->assertEquals('20110515000000', $date); | |
237 | } | |
238 | ||
239 | /** | |
240 | * Test calculateBaseScheduleDate - should give original date | |
241 | */ | |
242 | public function testcalculateBaseScheduleDateYear() { | |
243 | $params = array( | |
244 | 'scheduled_date' => '20110510', | |
245 | 'frequency_unit' => 'year', | |
246 | 'frequency_day' => 15, | |
247 | 'frequency_interval' => 2, | |
248 | ); | |
249 | ||
250 | $date = CRM_Pledge_BAO_PledgePayment::calculateBaseScheduleDate($params); | |
251 | $this->assertEquals('20110510000000', $date); | |
252 | } | |
253 | ||
254 | /** | |
255 | * Test calculateNextScheduledDate - no date provided | |
256 | */ | |
257 | public function testcalculateNextScheduledDateYear() { | |
258 | $params = array( | |
259 | 'scheduled_date' => '20110510', | |
260 | 'frequency_unit' => 'year', | |
261 | 'frequency_day' => 15, | |
262 | 'frequency_interval' => 2, | |
263 | ); | |
264 | ||
265 | $date = CRM_Pledge_BAO_PledgePayment::calculateNextScheduledDate($params, 1); | |
266 | $this->assertEquals('20130510000000', $date); | |
267 | } | |
268 | ||
269 | /** | |
270 | * Test calculateNextScheduledDate - no date provided | |
271 | */ | |
272 | public function testcalculateNextScheduledDateYearDateProvided() { | |
273 | $params = array( | |
274 | 'scheduled_date' => '20110510', | |
275 | 'frequency_unit' => 'year', | |
276 | 'frequency_day' => 15, | |
277 | 'frequency_interval' => 2, | |
278 | ); | |
279 | ||
280 | $date = CRM_Pledge_BAO_PledgePayment::calculateNextScheduledDate($params, 3, '20080510'); | |
281 | $this->assertEquals('20140510000000', $date); | |
282 | } | |
283 | ||
284 | } |