| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 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 | require_once 'CiviTest/Contact.php'; |
| 31 | require_once 'CiviTest/Custom.php'; |
| 32 | |
| 33 | /** |
| 34 | * Class CRM_Contribute_BAO_ContributionTest |
| 35 | */ |
| 36 | class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { |
| 37 | |
| 38 | /** |
| 39 | * Create() method (create and update modes). |
| 40 | */ |
| 41 | public function testCreate() { |
| 42 | $contactId = Contact::createIndividual(); |
| 43 | $ids = array('contribution' => NULL); |
| 44 | |
| 45 | $params = array( |
| 46 | 'contact_id' => $contactId, |
| 47 | 'currency' => 'USD', |
| 48 | 'financial_type_id' => 1, |
| 49 | 'contribution_status_id' => 1, |
| 50 | 'payment_instrument_id' => 1, |
| 51 | 'source' => 'STUDENT', |
| 52 | 'receive_date' => '20080522000000', |
| 53 | 'receipt_date' => '20080522000000', |
| 54 | 'non_deductible_amount' => 0.00, |
| 55 | 'total_amount' => 200.00, |
| 56 | 'fee_amount' => 5, |
| 57 | 'net_amount' => 195, |
| 58 | 'trxn_id' => '22ereerwww444444', |
| 59 | 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81', |
| 60 | 'thankyou_date' => '20080522', |
| 61 | ); |
| 62 | |
| 63 | $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); |
| 64 | |
| 65 | $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); |
| 66 | $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); |
| 67 | |
| 68 | //update contribution amount |
| 69 | $ids = array('contribution' => $contribution->id); |
| 70 | $params['fee_amount'] = 10; |
| 71 | $params['net_amount'] = 190; |
| 72 | |
| 73 | $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); |
| 74 | |
| 75 | $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .'); |
| 76 | $this->assertEquals($params['net_amount'], $contribution->net_amount, 'Check for Amount updation.'); |
| 77 | |
| 78 | //Delete Contribution |
| 79 | $this->contributionDelete($contribution->id); |
| 80 | |
| 81 | //Delete Contact |
| 82 | Contact::delete($contactId); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Create() method with custom data. |
| 87 | */ |
| 88 | public function testCreateWithCustomData() { |
| 89 | $contactId = Contact::createIndividual(); |
| 90 | $ids = array('contribution' => NULL); |
| 91 | |
| 92 | //create custom data |
| 93 | $customGroup = Custom::createGroup(array(), 'Contribution'); |
| 94 | $fields = array( |
| 95 | 'label' => 'testFld', |
| 96 | 'data_type' => 'String', |
| 97 | 'html_type' => 'Text', |
| 98 | 'is_active' => 1, |
| 99 | 'custom_group_id' => $customGroup->id, |
| 100 | ); |
| 101 | $customField = CRM_Core_BAO_CustomField::create($fields); |
| 102 | |
| 103 | $params = array( |
| 104 | 'contact_id' => $contactId, |
| 105 | 'currency' => 'USD', |
| 106 | 'financial_type_id' => 1, |
| 107 | 'contribution_status_id' => 1, |
| 108 | 'payment_instrument_id' => 1, |
| 109 | 'source' => 'STUDENT', |
| 110 | 'receive_date' => '20080522000000', |
| 111 | 'receipt_date' => '20080522000000', |
| 112 | 'id' => NULL, |
| 113 | 'non_deductible_amount' => 0.00, |
| 114 | 'total_amount' => 200.00, |
| 115 | 'fee_amount' => 5, |
| 116 | 'net_amount' => 195, |
| 117 | 'trxn_id' => '22ereerwww322323', |
| 118 | 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70', |
| 119 | 'thankyou_date' => '20080522', |
| 120 | ); |
| 121 | |
| 122 | $params['custom'] = array( |
| 123 | $customField->id => array( |
| 124 | -1 => array( |
| 125 | 'value' => 'Test custom value', |
| 126 | 'type' => 'String', |
| 127 | 'custom_field_id' => $customField->id, |
| 128 | 'custom_group_id' => $customGroup->id, |
| 129 | 'table_name' => $customGroup->table_name, |
| 130 | 'column_name' => $customField->column_name, |
| 131 | 'file_id' => NULL, |
| 132 | ), |
| 133 | ), |
| 134 | ); |
| 135 | |
| 136 | $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); |
| 137 | |
| 138 | // Check that the custom field value is saved |
| 139 | $customValueParams = array( |
| 140 | 'entityID' => $contribution->id, |
| 141 | 'custom_' . $customField->id => 1, |
| 142 | ); |
| 143 | $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams); |
| 144 | $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value'); |
| 145 | |
| 146 | $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); |
| 147 | $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.'); |
| 148 | |
| 149 | $this->contributionDelete($contribution->id); |
| 150 | Custom::deleteField($customField); |
| 151 | Custom::deleteGroup($customGroup); |
| 152 | Contact::delete($contactId); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * DeleteContribution() method |
| 157 | */ |
| 158 | public function testDeleteContribution() { |
| 159 | $contactId = Contact::createIndividual(); |
| 160 | $ids = array('contribution' => NULL); |
| 161 | |
| 162 | $params = array( |
| 163 | 'contact_id' => $contactId, |
| 164 | 'currency' => 'USD', |
| 165 | 'financial_type_id' => 1, |
| 166 | 'contribution_status_id' => 1, |
| 167 | 'payment_instrument_id' => 1, |
| 168 | 'source' => 'STUDENT', |
| 169 | 'receive_date' => '20080522000000', |
| 170 | 'receipt_date' => '20080522000000', |
| 171 | 'id' => NULL, |
| 172 | 'non_deductible_amount' => 0.00, |
| 173 | 'total_amount' => 200.00, |
| 174 | 'fee_amount' => 5, |
| 175 | 'net_amount' => 195, |
| 176 | 'trxn_id' => '33ereerwww322323', |
| 177 | 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70', |
| 178 | 'thankyou_date' => '20080522', |
| 179 | ); |
| 180 | |
| 181 | $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); |
| 182 | |
| 183 | $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); |
| 184 | $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); |
| 185 | |
| 186 | $contributiondelete = CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id); |
| 187 | |
| 188 | $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id, |
| 189 | 'id', 'trxn_id', 'Database check for deleted Contribution.' |
| 190 | ); |
| 191 | Contact::delete($contactId); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Create honor-contact method |
| 196 | */ |
| 197 | public function testcreateAndGetHonorContact() { |
| 198 | $firstName = 'John_' . substr(sha1(rand()), 0, 7); |
| 199 | $lastName = 'Smith_' . substr(sha1(rand()), 0, 7); |
| 200 | $email = "{$firstName}.{$lastName}@example.com"; |
| 201 | |
| 202 | //Get profile id of name honoree_individual used to create profileContact |
| 203 | $honoreeProfileId = NULL; |
| 204 | $ufGroupDAO = new CRM_Core_DAO_UFGroup(); |
| 205 | $ufGroupDAO->name = 'honoree_individual'; |
| 206 | if ($ufGroupDAO->find(TRUE)) { |
| 207 | $honoreeProfileId = $ufGroupDAO->id; |
| 208 | } |
| 209 | |
| 210 | $params = array( |
| 211 | 'prefix_id' => 3, |
| 212 | 'first_name' => $firstName, |
| 213 | 'last_name' => $lastName, |
| 214 | 'email-1' => $email, |
| 215 | ); |
| 216 | $softParam = array('soft_credit_type_id' => 1); |
| 217 | |
| 218 | $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray, |
| 219 | NULL, NULL, $honoreeProfileId |
| 220 | ); |
| 221 | |
| 222 | $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName, |
| 223 | 'Database check for created honor contact record.' |
| 224 | ); |
| 225 | //create contribution on behalf of honary. |
| 226 | |
| 227 | $contactId = Contact::createIndividual(); |
| 228 | $softParam['contact_id'] = $honoreeContactId; |
| 229 | |
| 230 | $ids = array('contribution' => NULL); |
| 231 | $param = array( |
| 232 | 'contact_id' => $contactId, |
| 233 | 'currency' => 'USD', |
| 234 | 'financial_type_id' => 4, |
| 235 | 'contribution_status_id' => 1, |
| 236 | 'receive_date' => date('Ymd'), |
| 237 | 'total_amount' => 66, |
| 238 | ); |
| 239 | |
| 240 | $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids); |
| 241 | $id = $contribution->id; |
| 242 | $softParam['contribution_id'] = $id; |
| 243 | $softParam['currency'] = $contribution->currency; |
| 244 | $softParam['amount'] = $contribution->total_amount; |
| 245 | |
| 246 | //Create Soft Contribution for honoree contact |
| 247 | CRM_Contribute_BAO_ContributionSoft::add($softParam); |
| 248 | |
| 249 | $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id', |
| 250 | 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution' |
| 251 | ); |
| 252 | //get honorary information |
| 253 | $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId); |
| 254 | $this->assertEquals(array( |
| 255 | $id => array( |
| 256 | 'honor_type' => 'In Honor of', |
| 257 | 'honorId' => $id, |
| 258 | 'display_name' => 'John Doe', |
| 259 | 'type' => 'Event Fee', |
| 260 | 'type_id' => '4', |
| 261 | 'amount' => '$ 66.00', |
| 262 | 'source' => NULL, |
| 263 | 'receive_date' => date('Y-m-d H:i:s'), |
| 264 | 'contribution_status' => 'Completed', |
| 265 | ), |
| 266 | ), $getHonorContact); |
| 267 | |
| 268 | $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName, |
| 269 | 'Database check for created honor contact record.' |
| 270 | ); |
| 271 | |
| 272 | //get annual contribution information |
| 273 | $annual = CRM_Contribute_BAO_Contribution::annual($contactId); |
| 274 | |
| 275 | $config = CRM_Core_Config::singleton(); |
| 276 | $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name'); |
| 277 | $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount', |
| 278 | 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution' |
| 279 | ); |
| 280 | |
| 281 | //Delete honor contact |
| 282 | Contact::delete($honoreeContactId); |
| 283 | |
| 284 | //Delete Contribution record |
| 285 | $this->contributionDelete($contribution->id); |
| 286 | |
| 287 | //Delete contributor contact |
| 288 | Contact::delete($contactId); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Display sort name during. |
| 293 | * contribution batch update through profile |
| 294 | * sortName(); |
| 295 | */ |
| 296 | public function testsortName() { |
| 297 | $params = array( |
| 298 | 'first_name' => 'Shane', |
| 299 | 'last_name' => 'Whatson', |
| 300 | 'contact_type' => 'Individual', |
| 301 | ); |
| 302 | |
| 303 | $contact = CRM_Contact_BAO_Contact::add($params); |
| 304 | |
| 305 | //Now check $contact is object of contact DAO.. |
| 306 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); |
| 307 | |
| 308 | $contactId = $contact->id; |
| 309 | |
| 310 | $ids = array('contribution' => NULL); |
| 311 | |
| 312 | $param = array( |
| 313 | 'contact_id' => $contactId, |
| 314 | 'currency' => 'USD', |
| 315 | 'financial_type_id' => 1, |
| 316 | 'contribution_status_id' => 1, |
| 317 | 'payment_instrument_id' => 1, |
| 318 | 'source' => 'STUDENT', |
| 319 | 'receive_date' => '20080522000000', |
| 320 | 'receipt_date' => '20080522000000', |
| 321 | 'id' => NULL, |
| 322 | 'non_deductible_amount' => 0.00, |
| 323 | 'total_amount' => 300.00, |
| 324 | 'fee_amount' => 5, |
| 325 | 'net_amount' => 295, |
| 326 | 'trxn_id' => '22ereerwww323', |
| 327 | 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70', |
| 328 | 'thankyou_date' => '20080522', |
| 329 | ); |
| 330 | |
| 331 | $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids); |
| 332 | |
| 333 | $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); |
| 334 | $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); |
| 335 | |
| 336 | //display sort name during batch update |
| 337 | $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id); |
| 338 | |
| 339 | $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.'); |
| 340 | |
| 341 | //Delete Contribution |
| 342 | $this->contributionDelete($contribution->id); |
| 343 | //Delete Contact |
| 344 | Contact::delete($contactId); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Add premium during online Contribution. |
| 349 | * |
| 350 | * AddPremium(); |
| 351 | */ |
| 352 | public function testAddPremium() { |
| 353 | $contactId = Contact::createIndividual(); |
| 354 | |
| 355 | $ids = array( |
| 356 | 'premium' => NULL, |
| 357 | ); |
| 358 | |
| 359 | $params = array( |
| 360 | 'name' => 'TEST Premium', |
| 361 | 'sku' => 111, |
| 362 | 'imageOption' => 'noImage', |
| 363 | 'MAX_FILE_SIZE' => 2097152, |
| 364 | 'price' => 100.00, |
| 365 | 'cost' => 90.00, |
| 366 | 'min_contribution' => 100, |
| 367 | 'is_active' => 1, |
| 368 | ); |
| 369 | $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids); |
| 370 | |
| 371 | $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.'); |
| 372 | |
| 373 | $ids = array('contribution' => NULL); |
| 374 | |
| 375 | $param = array( |
| 376 | 'contact_id' => $contactId, |
| 377 | 'currency' => 'USD', |
| 378 | 'financial_type_id' => 1, |
| 379 | 'contribution_status_id' => 1, |
| 380 | 'payment_instrument_id' => 1, |
| 381 | 'source' => 'STUDENT', |
| 382 | 'receive_date' => '20080522000000', |
| 383 | 'receipt_date' => '20080522000000', |
| 384 | 'id' => NULL, |
| 385 | 'non_deductible_amount' => 0.00, |
| 386 | 'total_amount' => 300.00, |
| 387 | 'fee_amount' => 5, |
| 388 | 'net_amount' => 295, |
| 389 | 'trxn_id' => '33erdfrwvw434', |
| 390 | 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92', |
| 391 | 'thankyou_date' => '20080522', |
| 392 | ); |
| 393 | |
| 394 | $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids); |
| 395 | |
| 396 | $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); |
| 397 | $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); |
| 398 | |
| 399 | //parameter for adding premium to contribution |
| 400 | $data = array( |
| 401 | 'product_id' => $premium->id, |
| 402 | 'contribution_id' => $contribution->id, |
| 403 | 'product_option' => NULL, |
| 404 | 'quantity' => 1, |
| 405 | ); |
| 406 | $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data); |
| 407 | $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .'); |
| 408 | |
| 409 | //Delete Product |
| 410 | CRM_Contribute_BAO_ManagePremiums::del($premium->id); |
| 411 | $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name, |
| 412 | 'id', 'name', 'Database check for deleted Product.' |
| 413 | ); |
| 414 | |
| 415 | //Delete Contribution |
| 416 | $this->contributionDelete($contribution->id); |
| 417 | //Delete Contact |
| 418 | Contact::delete($contactId); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Check duplicate contribution id. |
| 423 | * during the contribution import |
| 424 | * checkDuplicateIds(); |
| 425 | */ |
| 426 | public function testcheckDuplicateIds() { |
| 427 | $contactId = Contact::createIndividual(); |
| 428 | |
| 429 | $ids = array('contribution' => NULL); |
| 430 | |
| 431 | $param = array( |
| 432 | 'contact_id' => $contactId, |
| 433 | 'currency' => 'USD', |
| 434 | 'financial_type_id' => 1, |
| 435 | 'contribution_status_id' => 1, |
| 436 | 'payment_instrument_id' => 1, |
| 437 | 'source' => 'STUDENT', |
| 438 | 'receive_date' => '20080522000000', |
| 439 | 'receipt_date' => '20080522000000', |
| 440 | 'id' => NULL, |
| 441 | 'non_deductible_amount' => 0.00, |
| 442 | 'total_amount' => 300.00, |
| 443 | 'fee_amount' => 5, |
| 444 | 'net_amount' => 295, |
| 445 | 'trxn_id' => '76ereeswww835', |
| 446 | 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82', |
| 447 | 'thankyou_date' => '20080522', |
| 448 | ); |
| 449 | |
| 450 | $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids); |
| 451 | |
| 452 | $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); |
| 453 | $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.'); |
| 454 | $data = array( |
| 455 | 'id' => $contribution->id, |
| 456 | 'trxn_id' => $contribution->trxn_id, |
| 457 | 'invoice_id' => $contribution->invoice_id, |
| 458 | ); |
| 459 | $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data); |
| 460 | $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .'); |
| 461 | |
| 462 | // Delete Contribution |
| 463 | $this->contributionDelete($contribution->id); |
| 464 | // Delete Contact |
| 465 | Contact::delete($contactId); |
| 466 | } |
| 467 | |
| 468 | } |