From: Eileen McNaughton Date: Thu, 30 Apr 2015 02:42:07 +0000 (-0600) Subject: CRM-16367 (backoffice Contribution page) testability refactoring X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=31760f8150a6fa504c3e8cb70bfdce5fa024820c;p=civicrm-core.git CRM-16367 (backoffice Contribution page) testability refactoring --- diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index f7fce622e5..d705c5717c 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -69,13 +69,6 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP */ public $_ppID; - /** - * The id of the pledge that we are processing. - * - * @var int - */ - public $_pledgeID; - /** * Is this contribution associated with an online. * financial transaction @@ -989,7 +982,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP // Get the submitted form values. $submittedValues = $this->controller->exportValues($this->_name); - $contribution = $this->submit($submittedValues, $this->_action); + $contribution = $this->submit($submittedValues, $this->_action, $this->_ppID); $session = CRM_Core_Session::singleton(); $buttonName = $this->controller->getButtonName(); if ($this->_context == 'standalone') { @@ -1029,7 +1022,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP * * @throws CRM_Core_Exception */ - protected function processCreditCard($submittedValues, $lineItem) { + protected function processCreditCard($submittedValues, $lineItem, $pledgePaymentID) { $sendReceipt = $contribution = FALSE; $unsetParams = array( @@ -1304,7 +1297,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP // Store contribution id in payment record. CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id); - CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID, + CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, array($this->_ppID), $contribution->contribution_status_id, NULL, @@ -1419,7 +1412,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP 'cancel_date_time' => '', ); - $this->submit(array_merge($defaults, $params), $action); + $this->submit(array_merge($defaults, $params), $action, CRM_Utils_Array::value('pledge_payment_id', $params)); } /** @@ -1432,7 +1425,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP * @return array * @throws \Exception */ - protected function submit($submittedValues, $action) { + protected function submit($submittedValues, $action, $pledgePaymentID) { $softParams = $softIDs = array(); $pId = $contribution = $isRelatedId = FALSE; if (!empty($submittedValues['price_set_id']) && $action & CRM_Core_Action::UPDATE) { @@ -1602,7 +1595,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP // Credit Card Contribution. if ($this->_mode) { - $this->processCreditCard($submittedValues, $lineItem); + $this->processCreditCard($submittedValues, $lineItem, $pledgePaymentID); return FALSE; } else { @@ -1834,26 +1827,26 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP ); //update pledge payment status. - if ((($this->_ppID && $contribution->id) && $action & CRM_Core_Action::ADD) || + if ((($pledgePaymentID && $contribution->id) && $action & CRM_Core_Action::ADD) || (($pledgePaymentId) && $action & CRM_Core_Action::UPDATE) ) { - if ($this->_ppID) { + if ($pledgePaymentID) { //store contribution id in payment record. - CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_ppID, 'contribution_id', $contribution->id); + CRM_Core_DAO::setFieldValue('CRM_Pledge_DAO_PledgePayment', $pledgePaymentID, 'contribution_id', $contribution->id); } else { - $this->_ppID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', + $pledgePaymentID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $contribution->id, 'id', 'contribution_id' ); - $this->_pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', - $contribution->id, - 'pledge_id', - 'contribution_id' - ); } + $pledgeID = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', + $contribution->id, + 'pledge_id', + 'contribution_id' + ); $adjustTotalAmount = FALSE; if (CRM_Utils_Array::value('option_type', $formValues) == 2) { @@ -1872,8 +1865,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } if ($updatePledgePaymentStatus) { - CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($this->_pledgeID, - array($this->_ppID), + CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, + array($pledgePaymentID), $contribution->contribution_status_id, NULL, $contribution->total_amount, diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 7eea8f1d77..2945c77e5c 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -135,7 +135,8 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { 'receive_date_time' => '11:27PM', 'contact_id' => $this->_individualId, 'payment_instrument_id' => array_search('Check', $this->paymentInstruments), - )); + ), + CRM_Core_Action::ADD); $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1); } @@ -151,7 +152,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { 'receive_date_time' => '11:27PM', 'contact_id' => $this->_individualId, 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments), - )); + ), CRM_Core_Action::ADD); $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1); } @@ -170,7 +171,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { 'contact_id' => $this->_individualId, 'is_email_receipt' => TRUE, 'from_email_address' => 'test@test.com', - )); + ), CRM_Core_Action::ADD); $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1); $mut->checkMailLog(array( '

Please print this receipt for your records.

', @@ -179,4 +180,42 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { $mut->stop(); } + /** + * Test that a contribution is assigned against a pledge. + */ + public function testUpdatePledge() { + $pledge = $this->callAPISuccess('pledge', 'create', array( + 'contact_id' => $this->_individualId, + 'pledge_create_date' => date('Ymd'), + 'start_date' => date('Ymd'), + 'amount' => 100.00, + 'pledge_status_id' => '2', + 'pledge_financial_type_id' => '1', + 'pledge_original_installment_amount' => 20, + 'frequency_interval' => 5, + 'frequency_unit' => 'year', + 'frequency_day' => 15, + 'installments' => 2, + 'sequential' => 1, + )); + $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array( + 'pledge_id' => $pledge['id'], + 'options' => array('limit' => 1), + 'return' => 'id', + )); + $form = new CRM_Contribute_Form_Contribution(); + $form->testSubmit(array( + 'total_amount' => 50, + 'financial_type_id' => 1, + 'receive_date' => '04/21/2015', + 'receive_date_time' => '11:27PM', + 'contact_id' => $this->_individualId, + 'payment_instrument_id' => array_search('Check', $this->paymentInstruments), + 'pledge_payment_id' => $pledgePaymentID, + ), CRM_Core_Action::ADD); + $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID)); + $this->assertNotEmpty($pledgePayment['contribution_id']); + $this->assertEquals($pledgePayment['actual_amount'], 50); + $this->assertEquals(1, $pledgePayment['status_id']); + } }