From 5c065cc0574023022bef4d8edeb8669a81ffac71 Mon Sep 17 00:00:00 2001 From: Jon goldberg - Linux laptop Date: Sat, 4 May 2013 15:07:53 -0700 Subject: [PATCH] Soft Credit API plus unit tests --- api/v3/ContributionSoft.php | 100 ++++ tests/phpunit/api/v3/ContributionSoftTest.php | 486 ++++++++++++++++++ 2 files changed, 586 insertions(+) create mode 100644 api/v3/ContributionSoft.php create mode 100644 tests/phpunit/api/v3/ContributionSoftTest.php diff --git a/api/v3/ContributionSoft.php b/api/v3/ContributionSoft.php new file mode 100644 index 0000000000..978f9eccff --- /dev/null +++ b/api/v3/ContributionSoft.php @@ -0,0 +1,100 @@ +_apiversion = 3; + $this->_individualId = $this->individualCreate(); + $this->_softIndividual1Id = $this->individualCreate(); + $this->_softIndividual2Id = $this->individualCreate(); + $this->_contributionId = $this->contributionCreate($this->_individualId); + + $paymentProcessor = $this->processorCreate(); + $this->_params = array( + 'contact_id' => $this->_individualId, + 'receive_date' => '20120511', + 'total_amount' => 100.00, + 'financial_type_id' => $this->_contributionTypeId, + 'non_deductible_amount' => 10.00, + 'fee_amount' => 5.00, + 'net_amount' => 95.00, + 'source' => 'SSF', + 'contribution_status_id' => 1, + 'version' => $this->_apiversion, + ); + $this->_processorParams = array( + 'domain_id' => 1, + 'name' => 'Dummy', + 'payment_processor_type_id' => 10, + 'financial_account_id' => 12, + 'is_active' => 1, + 'user_name' => '', + 'url_site' => 'http://dummy.com', + 'url_recur' => 'http://dummy.com', + 'billing_mode' => 1, + ); +// $this->_pageParams = array( +// 'version' => 3, +// 'title' => 'Test Contribution Page', +// 'financial_type_id' => 1, +// 'currency' => 'USD', +// 'financial_account_id' => 1, +// 'payment_processor' => $paymentProcessor->id, +// 'is_active' => 1, +// 'is_allow_other_amount' => 1, +// 'min_amount' => 10, +// 'max_amount' => 1000, +// ); + } + + function tearDown() { + + $this->contributionTypeDelete(); + $this->quickCleanup(array( + 'civicrm_contribution', + 'civicrm_event', + 'civicrm_contribution_page', + 'civicrm_participant', + 'civicrm_participant_payment', + 'civicrm_line_item', + 'civicrm_financial_trxn', + 'civicrm_financial_item', + 'civicrm_entity_financial_trxn', + 'civicrm_contact', + 'civicrm_contribution_soft' + )); + } + + function testGetEmptyParamsContributionSoft() { + $params = array(); + $contribution = civicrm_api('contribution_soft', 'get', $params); + $this->assertEquals($contribution['is_error'], 1); + $this->assertEquals($contribution['error_message'], 'Mandatory key(s) missing from params array: version'); + } + + function testGetParamsNotArrayContributionSoft() { + $params = 'contact_id= 1'; + $contribution = civicrm_api('contribution', 'get', $params); + $this->assertEquals($contribution['is_error'], 1); + $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array'); + } + + function testGetContributionSoft() { + //We don't test for PCP fields because there's no PCP API, so we can't create campaigns. + $p = array( + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + + $this->_softcontribution = civicrm_api('contribution_soft', 'create', $p); + $this->assertEquals($this->_softcontribution['is_error'], 0, 'In line ' . __LINE__); + $params = array( + 'id' => $this->_softcontribution['id'], + 'version' => $this->_apiversion, + ); + $softcontribution = civicrm_api('contribution_soft', 'get', $params); + $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__); + $this->assertEquals(1,$softcontribution['count']); + + $this->documentMe($params, $softcontribution, __FUNCTION__, __FILE__); + $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__); + + //create a second soft contribution on the same hard contribution - we are testing that 'id' gets the right soft contribution id (not the contribution id) + $p['contact_id'] = $this->_softIndividual2Id; + $this->_softcontribution2 = civicrm_api('contribution_soft', 'create', $p); + $this->assertAPISuccess($this->_softcontribution2, 'In line ' . __LINE__); + + $params = array( + 'version' => $this->_apiversion, + ); + // now we have 2 - test getcount + $softcontribution = civicrm_api('contribution_soft', 'getcount', array( + 'version' => $this->_apiversion, + )); + + $this->assertEquals(2, $softcontribution); + //test id only format + $softcontribution = civicrm_api('contribution_soft', 'get', array + ('version' => $this->_apiversion, + 'id' => $this->_softcontribution['id'], + 'format.only_id' => 1, + ) + ); + $this->assertEquals($this->_softcontribution['id'], $softcontribution, print_r($softcontribution,true) . " in line " . __LINE__); + //test id only format - second soft credit + $softcontribution = civicrm_api('contribution_soft', 'get', array + ('version' => $this->_apiversion, + 'id' => $this->_softcontribution2['id'], + 'format.only_id' => 1, + ) + ); + $this->assertEquals($this->_softcontribution2['id'], $softcontribution); + $softcontribution = civicrm_api('contribution_soft', 'get', array( + 'version' => $this->_apiversion, + 'id' => $this->_softcontribution['id'], + )); + //test id as field + $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__); + $this->assertEquals(1, $softcontribution['count'], 'In line ' . __LINE__); + $this->assertEquals($this->_softcontribution['id'], $softcontribution['id'] ) ; + //test get by contact id works + $softcontribution = civicrm_api('contribution_soft', 'get', array('version' => $this->_apiversion, 'contact_id' => $this->_softIndividual2Id)); + $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__ . "get with contact_id" . print_r(array('version' => $this->_apiversion, 'contact_id' => $this->_softIndividual2Id), TRUE)); + + $this->assertEquals(1, $softcontribution['count'], 'In line ' . __LINE__); + civicrm_api('contribution_soft', 'Delete', array( + 'id' => $this->_softcontribution['id'], + 'version' => $this->_apiversion, + )); + civicrm_api('Contribution', 'Delete', array( + 'id' => $this->_softcontribution2['id'], + 'version' => $this->_apiversion, + )); + } + + + ///////////////// civicrm_contribution_soft + function testCreateEmptyParamsContributionSoft() { + + + $params = array('version' => $this->_apiversion); + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $this->assertEquals($softcontribution['is_error'], 1, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['error_message'], 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id', 'In line ' . __LINE__); + } + + function testCreateParamsNotArrayContributionSoft() { + + $params = 'contact_id= 1'; + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $this->assertEquals($softcontribution['is_error'], 1); + $this->assertEquals($softcontribution['error_message'], 'Input variable `params` is not an array'); + } + + function testCreateParamsWithoutRequiredKeysContributionSoft() { + $params = array('version' => 3); + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $this->assertEquals($softcontribution['is_error'], 1); + $this->assertEquals($softcontribution['error_message'], 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'); + } + + function testCreateContributionSoftInvalidContact() { + + $params = array( + 'contact_id' => 999, + 'contribution_id' => $this->_contributionId, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $this->assertEquals($softcontribution['error_message'], 'contact_id is not valid : 999', 'In line ' . __LINE__); + } + + function testCreateContributionSoftInvalidContributionId() { + + $params = array( + 'contribution_id' => 999999, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $this->assertEquals($softcontribution['error_message'], 'contribution_id is not valid : 999999', 'In line ' . __LINE__); + } + + /* + * Function tests that additional financial records are created when fee amount is recorded + */ + function testCreateContributionSoft() { + $params = array( + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $this->documentMe($params, $softcontribution, __FUNCTION__, __FILE__); + $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__); + } + + /** + * Test using example code + */ +// function testContributionSoftCreateExample() { +// //make sure at least one page exists since there is a truncate in tear down +// $page = civicrm_api('contribution_page', 'create', $this->_pageParams); +// $this->assertAPISuccess($page); +// //FIXME: Can't written until ContributionSoftDelete is written +// require_once 'api/v3/examples/ContributionSoftCreate.php'; +// $result = contribution_soft_create_example(); +// $this->assertAPISuccess($result); +// $contributionId = $result['id']; +// $expectedResult = contribution_soft_create_expectedresult(); +// $this->checkArrayEquals($result, $expectedResult); +// $this->contributionDelete($contributionId); +// } +// + //To Update Soft Contribution + function testCreateUpdateContributionSoft() { + //create a soft credit + $params = array( + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $softcontributionID = $softcontribution['id']; + + $old_params = array( + 'contribution_soft_id' => $softcontributionID, + 'version' => $this->_apiversion, + ); + $original = civicrm_api('contribution_soft', 'get', $old_params); + //Make sure it came back + $this->assertTrue(empty($original['is_error']), 'In line ' . __LINE__); + $this->assertEquals($original['id'], $softcontributionID, 'In line ' . __LINE__); + //set up list of old params, verify + $old_contribution_id = $original['values'][$softcontributionID]['contribution_id']; + $old_contact_id = $original['values'][$softcontributionID]['contact_id']; + $old_amount = $original['values'][$softcontributionID]['amount']; + $old_currency = $original['values'][$softcontributionID]['currency']; + + //check against original values + $this->assertEquals($old_contribution_id, $this->_contributionId, 'In line ' . __LINE__); + $this->assertEquals($old_contact_id, $this->_softIndividual1Id, 'In line ' . __LINE__); + $this->assertEquals($old_amount, 10.00, 'In line ' . __LINE__); + $this->assertEquals($old_currency, 'USD', 'In line ' . __LINE__); + $params = array( + 'id' => $softcontributionID, + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 7.00, + 'currency' => 'CAD', + 'version' => $this->_apiversion, + ); + + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + + $new_params = array( + 'id' => $softcontribution['id'], + 'version' => $this->_apiversion, + ); + $softcontribution = civicrm_api('contribution_soft', 'get', $new_params); + //check against original values + $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00, 'In line ' . __LINE__); + $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD', 'In line ' . __LINE__); + + $params = array( + 'id' => $softcontributionID, + 'version' => $this->_apiversion, + ); + $result = civicrm_api('contribution_soft', 'delete', $params); + $this->assertEquals($result['is_error'], 0, 'in line' . __LINE__); + } + + ///////////////// civicrm_contribution_soft_delete methods + function testDeleteEmptyParamsContributionSoft() { + $params = array('version' => $this->_apiversion); + $softcontribution = civicrm_api('contribution_soft', 'delete', $params); + $this->assertEquals($softcontribution['is_error'], 1); + } + + function testDeleteParamsNotArrayContributionSoft() { + $params = 'id= 1'; + $softcontribution = civicrm_api('contribution_soft', 'delete', $params); + $this->assertEquals($softcontribution['is_error'], 1); + $this->assertEquals($softcontribution['error_message'], 'Input variable `params` is not an array'); + } + + function testDeleteWrongParamContributionSoft() { + $params = array( + 'contribution_source' => 'SSF', + 'version' => $this->_apiversion, + ); + $softcontribution = civicrm_api('contribution_soft', 'delete', $params); + $this->assertEquals($softcontribution['is_error'], 1); + } + + function testDeleteContributionSoft() { + //create a soft credit + $params = array( + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + + $softcontribution = civicrm_api('contribution_soft', 'create', $params); + $softcontributionID = $softcontribution['id']; + $params = array( + 'id' => $softcontributionID, + 'version' => $this->_apiversion, + ); + $result = civicrm_api('contribution_soft', 'delete', $params); + $this->documentMe($params, $result, __FUNCTION__, __FILE__); + $this->assertEquals($result['is_error'], 0, 'In line ' . __LINE__); + } + + ///////////////// civicrm_contribution_search methods + + /** + * Test civicrm_contribution_soft_search with wrong params type + */ + function testSearchWrongParamsType() { + $params = 'a string'; + $result = civicrm_api('contribution_soft', 'get', $params); + + $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__); + $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__); + } + + /** + * Test civicrm_contribution_search with empty params. + * All available contributions expected. + */ + function testSearchEmptyParams() { + $params = array('version' => $this->_apiversion); + + $p = array( + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + $softcontribution = civicrm_api('contribution_soft', 'create', $p); + + $result = civicrm_api('contribution_soft', 'get', $params); + // We're taking the first element. + $res = $result['values'][$softcontribution['id']]; + + $this->assertEquals($p['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__); + $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__); + $this->assertEquals($p['amount'], $res['amount'], 'In line ' . __LINE__); + $this->assertEquals($p['currency'], $res['currency'], 'In line ' . __LINE__); + } + + /** + * Test civicrm_contribution_soft_search. Success expected. + */ + function testSearch() { + $p1 = array( + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual1Id, + 'amount' => 10.00, + 'currency' => 'USD', + 'version' => $this->_apiversion, + ); + $softcontribution1 = civicrm_api('contribution_soft', 'create', $p1); + + $p2 = array( + 'contribution_id' => $this->_contributionId, + 'contact_id' => $this->_softIndividual2Id, + 'amount' => 25.00, + 'currency' => 'CAD', + 'version' => $this->_apiversion, + ); + $softcontribution2 = civicrm_api('contribution_soft', 'create', $p2); + + $params = array( + 'id' => $softcontribution2['id'], + 'version' => $this->_apiversion, + ); + $result = civicrm_api('contribution_soft', 'get', $params); + $res = $result['values'][$softcontribution2['id']]; + + $this->assertEquals($p2['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__); + $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__); + $this->assertEquals($p2['amount'], $res['amount'], 'In line ' . __LINE__); + $this->assertEquals($p2['currency'], $res['currency'], 'In line ' . __LINE__ ); + } +} + -- 2.25.1