X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fapi%2Fv3%2FContributionTest.php;h=dd95b8ddb10edc6cac10f24a0bcf5b04cb3f8742;hb=1cf3c2b12178f08639f681ced2aa8d7e5c9ebca9;hp=6e557393132f3c16a3cfa395a69885c5b36860c5;hpb=a055fd3bcb8341e6a9c25e32432cf4f4776a2781;p=civicrm-core.git diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 6e55739313..dd95b8ddb1 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -1,9 +1,9 @@ callAPISuccess('line_item','get',array( 'entity_id' => $contribution['id'], + 'contribution_id' => $contribution['id'], 'entity_table' => 'civicrm_contribution', 'sequential' => 1, )); @@ -424,18 +425,13 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__); $lineItems = $this->callAPISuccess('line_item','get',array( 'entity_id' => $contribution['id'], + 'contribution_id' => $contribution['id'], 'entity_table' => 'civicrm_contribution', 'sequential' => 1, )); $this->assertEquals(1, $lineItems['count']); $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); - $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); - $lineItems = $this->callAPISuccess('line_item','get',array( - 'entity_id' => $contribution['id'], - 'entity_table' => 'civicrm_contribution', - 'sequential' => 1, - )); - $this->assertEquals(1, $lineItems['count']); + $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']); $this->_checkFinancialRecords($contribution, 'offline'); $this->contributionGetnCheck($params, $contribution['id']); } @@ -513,6 +509,20 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__); } + + /* + * Create test with unique field name on source + */ + function testCreateDefaultNow() { + + $params = $this->_params; + unset($params['receive_date']); + + $contribution = $this->callAPISuccess('contribution', 'create', $params); + $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id'])); + $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date']))); + } + /* * Create test with unique field name on source */ @@ -614,19 +624,19 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $description = "Demonstrates creating contribution with SoftCredit"; $subfile = "ContributionCreateWithSoftCredit"; $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual')); - $params = $this->_params + array( - 'soft_credit_to' => $contact2['id'], - + $softparams = array( + 'contact_id' => $contact2['id'], + 'amount' => 50, + 'soft_credit_type_id' => 3 ); + $params = $this->_params + array('soft_credit' => array(1 => $softparams)); $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); - // $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit_to', 'sequential' => 1)); - // $this->assertEquals($contact2['id'], $result['values'][$result['id']]['soft_credit_to']) ; - // well - the above doesn't work yet so lets do SQL - $query = "SELECT count(*) FROM civicrm_contribution_soft WHERE contact_id = " . $contact2['id']; + $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1)); - $count = CRM_Core_DAO::singleValueQuery($query); - $this->assertEquals(1, $count); + $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']); + $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']); + $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']); $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id'])); $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id'])); @@ -639,7 +649,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { //make sure at least on page exists since there is a truncate in tear down $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams); $this->assertAPISuccess($page); - require_once 'api/v3/examples/ContributionCreate.php'; + require_once 'api/v3/examples/Contribution/Create.php'; $result = contribution_create_example(); $this->assertAPISuccess($result); $contributionId = $result['id']; @@ -680,10 +690,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase { )); $this->assertEquals(1, $lineItems['count']); $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); - $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); + $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']); $lineItems = $this->callAPISuccess('line_item','get',array( 'entity_id' => $contribution['id'], + 'contribution_id' => $contribution['id'], 'entity_table' => 'civicrm_contribution', 'sequential' => 1, )); @@ -1201,7 +1212,30 @@ class api_v3_ContributionTest extends CiviUnitTestCase { function testCompleteTransaction() { $mut = new CiviMailUtils( $this, true ); $this->createLoggedInUser(); - $params = array_merge($this->_params, array('contribution_status_id' => 1,)); + $params = array_merge($this->_params, array('contribution_status_id' => 2,)); + $contribution = $this->callAPISuccess('contribution','create', $params); + $this->callAPISuccess('contribution', 'completetransaction', array( + 'id' => $contribution['id'], + )); + $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,)); + $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']); + $mut->checkMailLog(array( + 'Receipt - Contribution', + 'Please print this confirmation for your records.', + )); + $mut->stop(); + } + + /** + * CRM-14151 + * Test completing a transaction via the API + * + * tests. + */ + function testCompleteTransactionWithReceiptDateSet() { + $mut = new CiviMailUtils( $this, true ); + $this->createLoggedInUser(); + $params = array_merge($this->_params, array('contribution_status_id' => 2,'receipt_date' => 'now')); $contribution = $this->callAPISuccess('contribution','create', $params); $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array( 'id' => $contribution['id'], @@ -1227,9 +1261,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $mut->clearMessages(); $this->createLoggedInUser(); $contributionID = $this->createPendingParticipantContribution(); - $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array( - 'id' => $contributionID, - ) + $this->callAPISuccess('contribution', 'completetransaction', array( + 'id' => $contributionID,) ); $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids['participant'], 'return' => 'participant_status_id')); $this->assertEquals(1, $participantStatus); @@ -1238,8 +1271,142 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'Event', 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.', )); - $mut->stop(); + $mut->stop(); + } + + /** + * test membership is renewed when transaction completed + */ + function testCompleteTransactionMembershipPriceSet() { + $this->createPriceSetWithPage('membership'); + $this->setUpPendingContribution($this->_ids['price_field_value'][0]); + $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution'])); + $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership'])); + $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']); + $this->cleanUpAfterPriceSets(); + } + + /** + * test membership is renewed when transaction completed + */ + function testCompleteTransactionMembershipPriceSetTwoTerms() { + $this->createPriceSetWithPage('membership'); + $this->setUpPendingContribution($this->_ids['price_field_value'][1]); + $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution'])); + $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership'])); + $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']); + $this->cleanUpAfterPriceSets(); + } + + function cleanUpAfterPriceSets() { + $this->quickCleanUpFinancialEntities(); + $this->contactDelete($this->_ids['contact']); + $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids['price_field_value'][0])); + $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids['price_field_value'][1])); + $this->callAPISuccess('price_field', 'delete', array('id' => $this->_ids['price_field'][0])); + $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set'])); + } + + + /** + * this could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage + * on parent class at some point (fn is not in 4.4) + * @param $entity + * @param array $params + */ + function createPriceSetWithPage($entity, $params = array()) { + $membershipTypeID = $this->membershipTypeCreate(); + $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array( + 'title' => "Test Contribution Page", + 'financial_type_id' => 1, + 'currency' => 'NZD', + 'goal_amount' => 50, + 'is_pay_later' => 1, + 'is_monetary' => TRUE, + 'is_email_receipt' => FALSE, + )); + $priceSet = $this->callAPISuccess('price_set', 'create', array( + 'is_quick_config' => 0, + 'extends' => 'CiviMember', + 'financial_type_id' => 1, + 'title' => 'my Page' + )); + $priceSetID = $priceSet['id']; + + CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID ); + $priceField = $this->callAPISuccess('price_field', 'create', array( + 'price_set_id' => $priceSetID , + 'label' => 'Goat Breed', + 'html_type' => 'Radio', + )); + $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array( + 'price_set_id' => $priceSetID , + 'price_field_id' => $priceField['id'], + 'label' => 'Long Haired Goat', + 'amount' => 20, + 'membership_type_id' => $membershipTypeID, + 'membership_num_terms' => 1, + ) + ); + $this->_ids['price_field_value'] = array($priceFieldValue['id']); + $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array( + 'price_set_id' => $priceSetID , + 'price_field_id' => $priceField['id'], + 'label' => 'Shoe-eating Goat', + 'amount' => 10, + 'membership_type_id' => $membershipTypeID, + 'membership_num_terms' => 2, + ) + ); + $this->_ids['price_field_value'][] = $priceFieldValue['id']; + $this->_ids['price_set'] = $priceSetID; + $this->_ids['contribution_page'] = $contributionPageResult['id']; + $this->_ids['price_field'] = array($priceField['id']); + + $this->_ids['membership_type'] = $membershipTypeID; + } + + /** + * Set up a pending transaction with a specific price field id + * @param $priceFieldValueID + */ + function setUpPendingContribution($priceFieldValueID){ + $contactID = $this->individualCreate(); + $membership = $this->callAPISuccess('membership', 'create', array( + 'contact_id' => $contactID, + 'membership_type_id' => $this->_ids['membership_type'], + 'start_date' => 'yesterday - 1 year', + 'end_date' => 'yesterday', + )); + $contribution = $this->callAPISuccess('contribution', 'create', array( + 'domain_id' => 1, + 'contact_id' => $contactID, + 'receive_date' => date('Ymd'), + 'total_amount' => 100.00, + 'financial_type_id' => 1, + 'payment_instrument_id' => 'Credit Card', + 'non_deductible_amount' => 10.00, + 'trxn_id' => 'jdhfi88', + 'invoice_id' => 'djfhiewuyr', + 'source' => 'SSF', + 'contribution_status_id' => 2, + 'contribution_page_id' => $this->_ids['contribution_page'], + 'api.membership_payment.create' => array('membership_id' => $membership['id']), + )); + $this->callAPISuccess('line_item', 'create', array( + 'entity_id' => $contribution['id'], + 'entity_table' => 'civicrm_contribution', + 'price_field_id' => $this->_ids['price_field'][0], + 'qty' => 1, + 'unit_price' => 20, + 'line_total' => 20, + 'financial_type_id' => 1, + 'price_field_value_id' => $priceFieldValueID, + )); + $this->_ids['contact'] = $contactID; + $this->_ids['contribution'] = $contribution['id']; + $this->_ids['membership'] = $membership['id']; } /** @@ -1291,8 +1458,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'participant_id' => $participant['id'], 'contribution_id' => $contribution['id'], )); - $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array( - + $this->callAPISuccess('contribution', 'sendconfirmation', array( 'id' => $contribution['id'], 'receipt_from_email' => 'api@civicrm.org', ) @@ -1357,7 +1523,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase { return $contribution['id']; } - function _getFinancialTrxnAmount($contId) { + /** + * @param $contId + * + * @return null|string + */function _getFinancialTrxnAmount($contId) { $query = "SELECT SUM( ft.total_amount ) AS total FROM civicrm_financial_trxn AS ft @@ -1369,7 +1539,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase { return $result; } - function _getFinancialItemAmount($contId) { + /** + * @param $contId + * + * @return null|string + */function _getFinancialItemAmount($contId) { $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); $query = "SELECT SUM(amount) @@ -1380,7 +1554,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase { return $result; } - function _checkFinancialItem($contId, $context) { + /** + * @param $contId + * @param $context + */ + function _checkFinancialItem($contId, $context) { if ($context != 'paylater') { $params = array ( 'entity_id' => $contId, @@ -1444,7 +1622,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase { } } - function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) { + /** + * @param $contribution + * @param $context + * @param null $instrumentId + */ + function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) { $trxnParams = array( 'entity_id' => $contribution['id'], 'entity_table' => 'civicrm_contribution', @@ -1511,7 +1694,10 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams); } - function _addPaymentInstrument () { + /** + * @return mixed + */ + function _addPaymentInstrument () { $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name'); $optionParams = array( 'option_group_id' => $gId, @@ -1534,7 +1720,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase { return $optionValue['values'][$optionValue['id']]['value']; } - function _checkFinancialRecords($params,$context) { + /** + * @param $params + * @param $context + */ + function _checkFinancialRecords($params,$context) { $entityParams = array( 'entity_id' => $params['id'], 'entity_table' => 'civicrm_contribution',