| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Test class for Pledge API - civicrm_pledge_* |
| 14 | * |
| 15 | * @package CiviCRM_APIv3 |
| 16 | * @group headless |
| 17 | */ |
| 18 | class api_v3_PledgePaymentTest extends CiviUnitTestCase { |
| 19 | |
| 20 | protected $_individualId; |
| 21 | protected $_pledgeID; |
| 22 | protected $_contributionID; |
| 23 | protected $_financialTypeId = 1; |
| 24 | protected $_entity = 'PledgePayment'; |
| 25 | |
| 26 | /** |
| 27 | * Setup for tests. |
| 28 | * |
| 29 | * @throws \CRM_Core_Exception |
| 30 | */ |
| 31 | public function setUp() { |
| 32 | parent::setUp(); |
| 33 | $this->_individualId = $this->individualCreate(); |
| 34 | $this->_pledgeID = $this->pledgeCreate(['contact_id' => $this->_individualId]); |
| 35 | $this->_contributionID = $this->contributionCreate(['contact_id' => $this->_individualId]); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Clean up after function. |
| 40 | * @throws \CRM_Core_Exception |
| 41 | */ |
| 42 | public function tearDown() { |
| 43 | $tablesToTruncate = [ |
| 44 | 'civicrm_contribution', |
| 45 | 'civicrm_contact', |
| 46 | 'civicrm_pledge', |
| 47 | 'civicrm_pledge_payment', |
| 48 | 'civicrm_line_item', |
| 49 | ]; |
| 50 | |
| 51 | $this->quickCleanup($tablesToTruncate); |
| 52 | parent::tearDown(); |
| 53 | } |
| 54 | |
| 55 | public function testGetPledgePayment() { |
| 56 | $params = []; |
| 57 | $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__, __FILE__); |
| 58 | $this->assertEquals(5, $result['count'], " in line " . __LINE__); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Test that passing in a single variable works. |
| 63 | */ |
| 64 | public function testGetSinglePledgePayment() { |
| 65 | $createparams = [ |
| 66 | 'contact_id' => $this->_individualId, |
| 67 | 'pledge_id' => $this->_pledgeID, |
| 68 | 'contribution_id' => $this->_contributionID, |
| 69 | 'status_id' => 1, |
| 70 | ]; |
| 71 | $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams); |
| 72 | $params = [ |
| 73 | 'contribution_id' => $this->_contributionID, |
| 74 | ]; |
| 75 | $result = $this->callAPISuccess('pledge_payment', 'get', $params); |
| 76 | $this->assertEquals(1, $result['count'], " in line " . __LINE__); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Test process_pledge job log. |
| 81 | */ |
| 82 | public function testProcessPledgeJob() { |
| 83 | $pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status', |
| 84 | FALSE, FALSE, FALSE, NULL, 'name' |
| 85 | ); |
| 86 | //Make first payment. |
| 87 | $paymentParams = [ |
| 88 | 'contact_id' => $this->_individualId, |
| 89 | 'pledge_id' => $this->_pledgeID, |
| 90 | 'contribution_id' => $this->_contributionID, |
| 91 | 'scheduled_date' => date('Ymd', strtotime("-1 days")), |
| 92 | 'status_id' => array_search('Pending', $pledgeStatuses), |
| 93 | ]; |
| 94 | $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams); |
| 95 | //Status should be 'Pending' after first incomplete payment. |
| 96 | $checkStatus = $this->callAPISuccess('pledge', 'getsingle', [ |
| 97 | 'id' => $this->_pledgeID, |
| 98 | 'return' => 'pledge_status', |
| 99 | ]); |
| 100 | $this->assertEquals('Pending Label**', $checkStatus['pledge_status']); |
| 101 | |
| 102 | //Execute process_pledge job log. |
| 103 | $result = $this->callAPISuccess('Job', 'process_pledge', []); |
| 104 | $this->assertEquals("Checking if status update is needed for Pledge Id: {$this->_pledgeID} (current status is Pending)\n\r- status updated to: Overdue\n\r1 records updated.", $result['values']); |
| 105 | |
| 106 | //Status should be 'Overdue' after processing. |
| 107 | $statusAfterProcessing = $this->callAPISuccess('pledge', 'getsingle', [ |
| 108 | 'id' => $this->_pledgeID, |
| 109 | 'return' => 'pledge_status', |
| 110 | ]); |
| 111 | $this->assertEquals('Overdue', $statusAfterProcessing['pledge_status']); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Test status of pledge on payments and cancellation. |
| 116 | */ |
| 117 | public function testPledgeStatus() { |
| 118 | //Status should initially be Pending. |
| 119 | $checkStatus = $this->callAPISuccess('pledge', 'getsingle', [ |
| 120 | 'id' => $this->_pledgeID, |
| 121 | 'return' => 'pledge_status', |
| 122 | ]); |
| 123 | $this->assertEquals('Pending Label**', $checkStatus['pledge_status']); |
| 124 | |
| 125 | //Make first payment. |
| 126 | $paymentParams = [ |
| 127 | 'contact_id' => $this->_individualId, |
| 128 | 'pledge_id' => $this->_pledgeID, |
| 129 | 'contribution_id' => $this->_contributionID, |
| 130 | 'status_id' => 1, |
| 131 | ]; |
| 132 | $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams); |
| 133 | |
| 134 | //Status should be 'In Progress' after first payment. |
| 135 | $checkStatus = $this->callAPISuccess('pledge', 'getsingle', [ |
| 136 | 'id' => $this->_pledgeID, |
| 137 | 'return' => 'pledge_status', |
| 138 | ]); |
| 139 | $this->assertEquals('In Progress', $checkStatus['pledge_status']); |
| 140 | |
| 141 | //Cancel the Pledge. |
| 142 | $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
| 143 | $updateParams = [ |
| 144 | 'id' => $this->_pledgeID, |
| 145 | 'status_id' => array_search('Cancelled', $paymentStatusTypes), |
| 146 | ]; |
| 147 | $this->callAPISuccess('pledge', 'create', $updateParams); |
| 148 | |
| 149 | //Status should be calculated as Cancelled. |
| 150 | $pledgeStatus = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($this->_pledgeID); |
| 151 | $this->assertEquals('Cancelled', $paymentStatusTypes[$pledgeStatus]); |
| 152 | |
| 153 | //Already completed payments should not be cancelled. |
| 154 | $checkPaymentStatus = $this->callAPISuccess('pledge_payment', 'getsingle', [ |
| 155 | 'id' => $firstPayment['id'], |
| 156 | 'return' => 'status_id', |
| 157 | ]); |
| 158 | $this->assertEquals(array_search('Completed', $paymentStatusTypes), $checkPaymentStatus['status_id']); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Test that passing in a single variable works:: status_id |
| 163 | */ |
| 164 | public function testGetSinglePledgePaymentByStatusID() { |
| 165 | $createparams = [ |
| 166 | 'contact_id' => $this->_individualId, |
| 167 | 'pledge_id' => $this->_pledgeID, |
| 168 | 'contribution_id' => $this->_contributionID, |
| 169 | 'status_id' => 1, |
| 170 | ]; |
| 171 | $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams); |
| 172 | $params = [ |
| 173 | 'status_id' => 1, |
| 174 | ]; |
| 175 | |
| 176 | $result = $this->callAPISuccess('pledge_payment', 'get', $params); |
| 177 | $this->assertEquals(1, $result['count'], " in line " . __LINE__); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Test that creating a payment will add the contribution ID. |
| 182 | */ |
| 183 | public function testCreatePledgePayment() { |
| 184 | //check that 5 pledge payments exist at the start |
| 185 | $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', []); |
| 186 | $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__); |
| 187 | |
| 188 | //test the pledge_payment_create function |
| 189 | $params = [ |
| 190 | 'contact_id' => $this->_individualId, |
| 191 | 'pledge_id' => $this->_pledgeID, |
| 192 | 'contribution_id' => $this->_contributionID, |
| 193 | 'status_id' => 1, |
| 194 | 'actual_amount' => 20, |
| 195 | ]; |
| 196 | $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__, __FILE__); |
| 197 | |
| 198 | //check existing updated not new one created - 'create' means add contribution_id in this context |
| 199 | $afterAdd = $this->callAPISuccess('pledge_payment', 'get', []); |
| 200 | $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__); |
| 201 | |
| 202 | //get the created payment & check it out |
| 203 | $getParams['id'] = $result['id']; |
| 204 | $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams); |
| 205 | $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__); |
| 206 | $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__); |
| 207 | |
| 208 | //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded |
| 209 | $contributionParams = [ |
| 210 | 'total_amount' => 20, |
| 211 | 'contact_id' => $this->_individualId, |
| 212 | 'financial_type_id' => $this->_financialTypeId, |
| 213 | ]; |
| 214 | $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams); |
| 215 | $params['contribution_id'] = $contribution['id']; |
| 216 | |
| 217 | $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params); |
| 218 | //make sure original is untouched & has not been updated |
| 219 | $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__); |
| 220 | $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams); |
| 221 | $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__); |
| 222 | $this->assertEquals($this->_contributionID, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Test checks behaviour when more payments are created than should be possible. |
| 227 | */ |
| 228 | public function testCreatePledgePaymentAllCreated() { |
| 229 | $params = [ |
| 230 | 'pledge_id' => $this->_pledgeID, |
| 231 | 'status_id' => 1, |
| 232 | ]; |
| 233 | // create one more pledge than there are spaces for |
| 234 | $i = 0; |
| 235 | while ($i <= 5) { |
| 236 | $contributionParams = [ |
| 237 | 'total_amount' => 20, |
| 238 | 'contact_id' => $this->_individualId, |
| 239 | 'financial_type_id' => $this->_financialTypeId, |
| 240 | ]; |
| 241 | $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams); |
| 242 | |
| 243 | $params['contribution_id'] = $contribution['id']; |
| 244 | |
| 245 | $resultCont2 = civicrm_api('pledge_payment', 'create', $params + ['version' => $this->_apiversion]); |
| 246 | $i++; |
| 247 | } |
| 248 | // check that only 5 exist & we got an error setting the 6th |
| 249 | $result = $this->callAPISuccess('PledgePayment', 'Get', [ |
| 250 | 'pledge_id' => $this->_pledgeID, |
| 251 | ]); |
| 252 | // the last one above should result in an error |
| 253 | $this->assertEquals("There are no unmatched payment on this pledge. Pass in the pledge_payment id to specify one or 'option.create_new' to create one", $resultCont2['error_message']); |
| 254 | $this->assertEquals(5, $result['count']); |
| 255 | |
| 256 | $params['option.create_new'] = 1; |
| 257 | $params['scheduled_amount'] = 20; |
| 258 | $params['scheduled_date'] = '20131212'; |
| 259 | $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params); |
| 260 | $result = $this->callAPISuccess('PledgePayment', 'Get', [ |
| 261 | 'pledge_id' => $this->_pledgeID, |
| 262 | ]); |
| 263 | |
| 264 | $this->assertEquals(6, $result['count']); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule. |
| 269 | */ |
| 270 | public function testCreatePledgePaymentWhereOnlyOnePayment() { |
| 271 | $pledgeParams = [ |
| 272 | 'contact_id' => $this->_individualId, |
| 273 | 'pledge_create_date' => date('Ymd'), |
| 274 | 'start_date' => date('Ymd'), |
| 275 | 'scheduled_date' => 'first day 2015', |
| 276 | 'pledge_amount' => 100.00, |
| 277 | 'pledge_status_id' => '2', |
| 278 | 'pledge_financial_type_id' => '1', |
| 279 | 'pledge_original_installment_amount' => 20, |
| 280 | 'frequency_interval' => 5, |
| 281 | 'frequency_unit' => 'year', |
| 282 | 'frequency_day' => 15, |
| 283 | 'installments' => 1, |
| 284 | 'sequential' => 1, |
| 285 | ]; |
| 286 | |
| 287 | $contributionID = $this->contributionCreate([ |
| 288 | 'contact_id' => $this->_individualId, |
| 289 | 'financial_type_id' => $this->_financialTypeId, |
| 290 | 'invoice_id' => 45, |
| 291 | 'trxn_id' => 45, |
| 292 | ]); |
| 293 | $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams); |
| 294 | |
| 295 | //test the pledge_payment_create function |
| 296 | $params = [ |
| 297 | 'contact_id' => $this->_individualId, |
| 298 | 'pledge_id' => $pledge['id'], |
| 299 | 'contribution_id' => $contributionID, |
| 300 | 'status_id' => 1, |
| 301 | 'actual_amount' => 20, |
| 302 | ]; |
| 303 | $result = $this->callAPISuccess('pledge_payment', 'create', $params); |
| 304 | |
| 305 | //check existing updated not new one created - 'create' means add contribution_id in this context |
| 306 | $afterAdd = $this->callAPISuccess('pledge_payment', 'get', [ |
| 307 | 'contribution_id' => $contributionID, |
| 308 | ]); |
| 309 | $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__); |
| 310 | } |
| 311 | |
| 312 | public function testUpdatePledgePayment() { |
| 313 | $params = [ |
| 314 | 'pledge_id' => $this->_pledgeID, |
| 315 | 'contribution_id' => $this->_contributionID, |
| 316 | 'status_id' => 2, |
| 317 | 'actual_amount' => 20, |
| 318 | ]; |
| 319 | $result = $this->callAPISuccess('pledge_payment', 'create', $params); |
| 320 | $updateparams = [ |
| 321 | 'id' => $result['id'], |
| 322 | 'status_id' => 1, |
| 323 | ]; |
| 324 | |
| 325 | $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__, __FILE__); |
| 326 | $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity); |
| 327 | } |
| 328 | |
| 329 | public function testDeletePledgePayment() { |
| 330 | $params = [ |
| 331 | 'contact_id' => $this->_individualId, |
| 332 | 'pledge_id' => $this->_pledgeID, |
| 333 | 'contribution_id' => $this->_contributionID, |
| 334 | 'status_id' => 1, |
| 335 | 'sequential' => 1, |
| 336 | 'actual_amount' => 20, |
| 337 | ]; |
| 338 | $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params); |
| 339 | |
| 340 | $deleteParams = [ |
| 341 | 'id' => $pledgePayment['id'], |
| 342 | ]; |
| 343 | $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__, __FILE__); |
| 344 | } |
| 345 | |
| 346 | public function testGetFields() { |
| 347 | $result = $this->callAPISuccess('PledgePayment', 'GetFields', []); |
| 348 | $this->assertType('array', $result); |
| 349 | } |
| 350 | |
| 351 | } |