From d69445182f2fa394f27aa15bb43ef7a8d8ea2b5f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 13 Jul 2015 03:00:24 +1200 Subject: [PATCH] test fixes --- CRM/Contribute/BAO/Contribution.php | 2 +- CRM/Core/Payment/AuthorizeNetIPN.php | 18 +++---- CRM/Core/PseudoConstant.php | 2 +- CRM/Financial/BAO/PaymentProcessor.php | 4 +- Civi/Payment/System.php | 2 + .../CRM/Core/Payment/AuthorizeNetIPNTest.php | 4 +- .../CRM/Core/Payment/AuthorizeNetTest.php | 18 ++----- tests/phpunit/CiviTest/AuthorizeNet.php | 52 ------------------ tests/phpunit/CiviTest/CiviUnitTestCase.php | 53 +++++++++++++++---- 9 files changed, 63 insertions(+), 92 deletions(-) delete mode 100644 tests/phpunit/CiviTest/AuthorizeNet.php diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 36d37426b6..40dff6d26e 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -2203,7 +2203,7 @@ WHERE contribution_id = %1 "; $this->is_test ? 'test' : 'live' ); $ids['paymentProcessor'] = $paymentProcessorID; - $this->_relatedObjects['paymentProcessor'] = &$paymentProcessor; + $this->_relatedObjects['paymentProcessor'] = $paymentProcessor; } elseif ($required) { throw new Exception("Could not find payment processor for contribution record: " . $this->id); diff --git a/CRM/Core/Payment/AuthorizeNetIPN.php b/CRM/Core/Payment/AuthorizeNetIPN.php index c479e6d298..ea9540f851 100644 --- a/CRM/Core/Payment/AuthorizeNetIPN.php +++ b/CRM/Core/Payment/AuthorizeNetIPN.php @@ -101,6 +101,7 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { public function recur(&$input, &$ids, &$objects, $first) { $this->_isRecurring = TRUE; $recur = &$objects['contributionRecur']; + $paymentProcessorObject = $objects['contribution']->_relatedObjects['paymentProcessor']['object']; // do a subscription check if ($recur->processor_id != $input['subscription_id']) { @@ -152,9 +153,7 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN { $objects['contribution']->total_amount = $input['amount']; $objects['contribution']->trxn_id = $input['trxn_id']; - // since we have processor loaded for sure at this point, - // check and validate gateway MD5 response if present - $this->checkMD5($ids, $input); + $this->checkMD5($paymentProcessorObject, $input); if ($input['response_code'] == 1) { // Approved @@ -330,17 +329,14 @@ INNER JOIN civicrm_membership_payment mp ON m.id = mp.membership_id AND mp.contr } /** - * @param $ids - * @param $input + * Check and validate gateway MD5 response if present. + * + * @param CRM_Core_Payment_AuthorizeNet $paymentObject + * @param array $input * * @return bool */ - public function checkMD5($ids, $input) { - $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ids['paymentProcessor'], - $input['is_test'] ? 'test' : 'live' - ); - $paymentObject = $paymentProcessor['object']; - + public function checkMD5($paymentObject, $input) { if (!$paymentObject->checkMD5($input['MD5_Hash'], $input['trxn_id'], $input['amount'], TRUE)) { CRM_Core_Error::debug_log_message("MD5 Verification failed."); echo "Failure: Security verification failed

"; diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 352e9e89e4..079fdb73fb 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -1425,7 +1425,7 @@ WHERE id = %1"; * @return array * array of all payment processors */ - public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) { + public static function paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) { $condition = "is_test = "; $condition .= ($test) ? '1' : '0'; diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index 34e761b481..83550bdb59 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -29,8 +29,6 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ - * */ /** @@ -81,6 +79,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces ); CRM_Financial_BAO_FinancialTypeAccount::add($values); } + Civi\Payment\System::singleton()->flushProcessors(); return $processor; } @@ -170,6 +169,7 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces $testDAO->delete(); $dao->delete(); + Civi\Payment\System::singleton()->flushProcessors(); } /** diff --git a/Civi/Payment/System.php b/Civi/Payment/System.php index 42b1152806..c9ba4bc951 100644 --- a/Civi/Payment/System.php +++ b/Civi/Payment/System.php @@ -103,6 +103,8 @@ class System { */ public function flushProcessors() { $this->cache = array(); + \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors(NULL, TRUE); + \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('test', TRUE); } } diff --git a/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php b/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php index 20997d1628..0661a7604e 100644 --- a/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/AuthorizeNetIPNTest.php @@ -16,9 +16,7 @@ class CRM_Core_Payment_AuthorizeNetIPNTest extends CiviUnitTestCase { public function setUp() { parent::setUp(); - $this->_paymentProcessorID = $this->paymentProcessorCreate(array( - 'payment_processor_type_id' => 'AuthNet', - )); + $this->_paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate(); $this->_contactID = $this->individualCreate(); $contributionPage = $this->callAPISuccess('contribution_page', 'create', array( 'title' => "Test Contribution Page", diff --git a/tests/phpunit/CRM/Core/Payment/AuthorizeNetTest.php b/tests/phpunit/CRM/Core/Payment/AuthorizeNetTest.php index 55759d15f5..5f671dbee0 100644 --- a/tests/phpunit/CRM/Core/Payment/AuthorizeNetTest.php +++ b/tests/phpunit/CRM/Core/Payment/AuthorizeNetTest.php @@ -36,17 +36,9 @@ class CRM_Core_Payment_AuthorizeNetTest extends CiviUnitTestCase { public function setUp() { parent::setUp(); - $this->paymentProcessor = new AuthorizeNet(); - $this->processorParams = $this->paymentProcessor->create(); + $this->_paymentProcessorID = $this->paymentProcessorAuthorizeNetCreate(); - $paymentProcessor = array( - 'user_name' => $this->processorParams->user_name, - 'password' => $this->processorParams->password, - 'url_recur' => $this->processorParams->url_recur, - 'signature' => '', - ); - - $this->processor = new CRM_Core_Payment_AuthorizeNet('Contribute', $paymentProcessor); + $this->processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID); $this->_financialTypeId = 1; // for some strange unknown reason, in batch mode this value gets set to null @@ -85,7 +77,7 @@ class CRM_Core_Payment_AuthorizeNetTest extends CiviUnitTestCase { 'invoice_id' => $invoiceID, 'contribution_status_id' => 2, 'is_test' => 1, - 'payment_processor_id' => $this->processorParams->id, + 'payment_processor_id' => $this->_paymentProcessorID, ); $recur = CRM_Contribute_BAO_ContributionRecur::add($contributionRecurParams); @@ -129,7 +121,7 @@ class CRM_Core_Payment_AuthorizeNetTest extends CiviUnitTestCase { 'from_email_address' => "{$firstName}.{$lastName}@example.com", 'receive_date' => date('Ymd'), 'receipt_date_time' => '', - 'payment_processor_id' => $this->processorParams->id, + 'payment_processor_id' => $this->_paymentProcessorID, 'price_set_id' => '', 'total_amount' => $amount, 'currency' => 'USD', @@ -176,7 +168,7 @@ class CRM_Core_Payment_AuthorizeNetTest extends CiviUnitTestCase { // turn verifySSL off CRM_Core_BAO_Setting::setItem('0', CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL'); - $this->processor->doDirectPayment($params); + $this->processor->doPayment($params); // turn verifySSL on CRM_Core_BAO_Setting::setItem('0', CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL'); diff --git a/tests/phpunit/CiviTest/AuthorizeNet.php b/tests/phpunit/CiviTest/AuthorizeNet.php deleted file mode 100644 index 15cd2e1684..0000000000 --- a/tests/phpunit/CiviTest/AuthorizeNet.php +++ /dev/null @@ -1,52 +0,0 @@ - 'Authorize', - 'domain_id' => CRM_Core_Config::domainID(), - 'payment_processor_type' => 'AuthNet', - 'is_active' => 1, - 'is_default' => 0, - 'is_test' => 1, - 'user_name' => '4y5BfuW7jm', - 'password' => '4cAmW927n8uLf5J8', - 'url_site' => 'https://test.authorize.net/gateway/transact.dll', - 'url_recur' => 'https://apitest.authorize.net/xml/v1/request.api', - 'class_name' => 'Payment_AuthorizeNet', - 'billing_mode' => 1, - ); - $paymentProcessor->copyValues($paymentParams); - $paymentProcessor->save(); - return $paymentProcessor; - } - - /** - * Helper function to delete a PayPal Pro - * payment processor - * @param int $id - * Id of the PayPal Pro payment processor. - * to be deleted - * @return bool - * true if payment processor deleted, false otherwise - */ - public function delete($id) { - $paymentProcessor = new CRM_Financial_DAO_PaymentProcessor(); - $paymentProcessor->id = $id; - if ($paymentProcessor->find(TRUE)) { - $result = $paymentProcessor->delete(); - } - return $result; - } - -} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index adde913aca..aa27dfda3a 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1439,6 +1439,35 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { return $result['id']; } + /** + * Create test Authorize.net instance. + * + * @param array $params + * + * @return mixed + */ + public function paymentProcessorAuthorizeNetCreate($params = array()) { + $params = array_merge(array( + 'name' => 'Authorize', + 'domain_id' => CRM_Core_Config::domainID(), + 'payment_processor_type_id' => 'AuthNet', + 'title' => 'AuthNet', + 'is_active' => 1, + 'is_default' => 0, + 'is_test' => 1, + 'is_recur' => 1, + 'user_name' => '4y5BfuW7jm', + 'password' => '4cAmW927n8uLf5J8', + 'url_site' => 'https://test.authorize.net/gateway/transact.dll', + 'url_recur' => 'https://apitest.authorize.net/xml/v1/request.api', + 'class_name' => 'Payment_AuthorizeNet', + 'billing_mode' => 1, + ), $params); + + $result = $this->callAPISuccess('payment_processor', 'create', $params); + return $result['id']; + } + /** * Create Participant. * @@ -1607,8 +1636,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { /** * Create contribution. * - * @param int $cID - * Contact_id. + * @param int|array $params + * Array of parameters or Contact_id (legacy) . * @param int $cTypeID * Id of financial type. * @param int $invoiceID @@ -1619,21 +1648,23 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @return int * id of created contribution */ - public function contributionCreate($cID, $cTypeID = 1, $invoiceID = 67890, $trxnID = 12345, $paymentInstrumentID = 1, $isFee = TRUE) { - $params = array( + public function contributionCreate($params, $cTypeID = 1, $invoiceID = 67890, $trxnID = 12345, $paymentInstrumentID + = 1, $isFee = TRUE) { + if (!is_array($params)) { + $params = array('contact_id' => $params); + } + $params = array_merge(array( 'domain_id' => 1, - 'contact_id' => $cID, 'receive_date' => date('Ymd'), 'total_amount' => 100.00, - 'financial_type_id' => empty($cTypeID) ? 1 : $cTypeID, + 'financial_type_id' => $cTypeID, 'payment_instrument_id' => empty($paymentInstrumentID) ? 1 : $paymentInstrumentID, 'non_deductible_amount' => 10.00, 'trxn_id' => $trxnID, 'invoice_id' => $invoiceID, 'source' => 'SSF', 'contribution_status_id' => 1, - // 'note' => 'Donating for Nobel Cause', *Fixme - ); + ), $params); if ($isFee) { $params['fee_amount'] = 5.00; @@ -3179,6 +3210,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) 'contact_id' => $this->_contactID, 'contribution_page_id' => $this->_contributionPageID, 'payment_processor_id' => $this->_paymentProcessorID, + 'is_test' => 1, ), )); $this->_contributionRecurID = $contributionRecur['id']; @@ -3191,7 +3223,10 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) public function setupMembershipRecurringPaymentProcessorTransaction() { $this->ids['membership_type'] = $this->membershipTypeCreate(); //create a contribution so our membership & contribution don't both have id = 1 - $this->contributionCreate($this->_contactID, 1, 'abcd', '345j'); + $this->contributionCreate(array( + 'contact_id' => $this->_contactID, + 'is_test' => 1), + 1, 'abcd', '345j'); $this->setupRecurringPaymentProcessorTransaction(); $this->ids['membership'] = $this->callAPISuccess('membership', 'create', array( -- 2.25.1