From cf0a66acd8f0bb29c56b39ab3c14343a34f34974 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Fri, 15 Jul 2016 18:53:55 +0530 Subject: [PATCH] Add unit test --- CRM/Contribute/Form/Task/Status.php | 26 ++++-- .../CRM/Contribute/Form/Task/StatusTest.php | 85 +++++++++++++++++++ tests/phpunit/CRM/Custom/Page/AjaxTest.php | 25 ++++++ 3 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php diff --git a/CRM/Contribute/Form/Task/Status.php b/CRM/Contribute/Form/Task/Status.php index e21c6cec51..26aefacf3c 100644 --- a/CRM/Contribute/Form/Task/Status.php +++ b/CRM/Contribute/Form/Task/Status.php @@ -82,6 +82,13 @@ AND {$this->_componentClause}"; $this->assign('single', $this->_single); } + /** + * Sets contribution Ids for unit test. + */ + public function setContributionIds($contributionIds) { + $this->_contributionIds = $contributionIds; + } + /** * Build the form object. */ @@ -209,19 +216,30 @@ AND co.id IN ( $contribIDs )"; */ public function postProcess() { $params = $this->controller->exportValues($this->_name); - $statusID = CRM_Utils_Array::value('contribution_status_id', $params); + // submit the form with values. + self::processForm($this, $params); + + CRM_Core_Session::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success'); + } + + /** + * Process the form with submitted params. + * Also supports unit test. + */ + public static function processForm($form, $params) { + $statusID = CRM_Utils_Array::value('contribution_status_id', $params); $baseIPN = new CRM_Core_Payment_BaseIPN(); $transaction = new CRM_Core_Transaction(); // get the missing pieces for each contribution - $contribIDs = implode(',', $this->_contributionIds); + $contribIDs = implode(',', $form->_contributionIds); $details = self::getDetails($contribIDs); $template = CRM_Core_Smarty::singleton(); // for each contribution id, we just call the baseIPN stuff - foreach ($this->_rows as $row) { + foreach ($form->_rows as $row) { $input = $ids = $objects = array(); $input['component'] = $details[$row['contribution_id']]['component']; @@ -285,8 +303,6 @@ AND co.id IN ( $contribIDs )"; // reset template values before processing next transactions $template->clearTemplateVars(); } - - CRM_Core_Session::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success'); } /** diff --git a/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php b/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php new file mode 100644 index 0000000000..dd9235ea18 --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php @@ -0,0 +1,85 @@ +quickCleanUpFinancialEntities(); + CRM_Utils_Hook::singleton()->reset(); + } + + /** + * Test update pending contribution + */ + public function testUpdatePendingContribution() { + $this->_individualId = $this->individualCreate(); + $form = new CRM_Contribute_Form_Task_Status(); + + // create a pending contribution + $contributionParams = array( + 'contact_id' => $this->_individualId, + 'total_amount' => 100, + 'financial_type_id' => 'Donation', + 'contribution_status_id' => 2, + ); + $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); + $contributionId = $contribution['id']; + $form->setContributionIds(array($contributionId)); + + $form->buildQuickForm(); + + $params = array( + "contribution_status_id" => 1, + "trxn_id_{$contributionId}" => NULL, + "check_number_{$contributionId}" => NULL, + "fee_amount_{$contributionId}" => 0, + "trxn_date_{$contributionId}" => date('m/d/Y'), + "payment_instrument_id_{$contributionId}" => 4, + ); + + CRM_Contribute_Form_Task_Status::processForm($form, $params); + + $contribution = $this->callAPISuccess('Contribution', 'get', array('id' => $contributionId)); + $updatedContribution = $contribution['values'][1]; + + $this->assertEquals('', $updatedContribution['contribution_source']); + $this->assertEquals(date("Y-m-d"), date("Y-m-d", strtotime($updatedContribution['receive_date']))); + $this->assertNotEquals("00:00:00", date("H:i:s", strtotime($updatedContribution['receive_date']))); + $this->assertEquals('Completed', $updatedContribution['contribution_status']); + } + +} diff --git a/tests/phpunit/CRM/Custom/Page/AjaxTest.php b/tests/phpunit/CRM/Custom/Page/AjaxTest.php index bbbc8d03b6..d255094334 100644 --- a/tests/phpunit/CRM/Custom/Page/AjaxTest.php +++ b/tests/phpunit/CRM/Custom/Page/AjaxTest.php @@ -1,4 +1,29 @@