From 31eddd0da6957ff957a57da4c6078a65acd33851 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 14 Oct 2019 15:56:23 +1300 Subject: [PATCH] Test calling CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors Add test to ensure calling with Test capability only returns testMode processors --- CRM/Core/Payment.php | 9 +++++++ .../Financial/BAO/PaymentProcessorTest.php | 27 +++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 8 +++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 502d332df3..69b97a342f 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -174,6 +174,15 @@ abstract class CRM_Core_Payment { return $this->paymentInstrumentID ? $this->paymentInstrumentID : $this->_paymentProcessor['payment_instrument_id']; } + /** + * Getter for the id. + * + * @return int + */ + public function getID() { + return (int) $this->_paymentProcessor['id']; + } + /** * Set payment Instrument id. * diff --git a/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php index 33bc618ce4..6063c42aa6 100644 --- a/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php +++ b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php @@ -63,4 +63,31 @@ class CRM_Financial_BAO_PaymentProcessorTest extends CiviUnitTestCase { $this->assertEquals($cards, $expectedCards, 'Verify correct credit card types are returned'); } + /** + * Test the processor retrieval function. + * + * @throws \CiviCRM_API3_Exception + */ + public function testGetProcessors() { + $testProcessor = $this->dummyProcessorCreate(); + $testProcessorID = $testProcessor->getID(); + $liveProcessorID = $testProcessorID + 1; + + $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'TestMode']); + $this->assertEquals([$testProcessorID, 0], array_keys($processors), 'Only the test processor and the manual processor should be returned'); + + $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'TestMode'], [$liveProcessorID]); + $this->assertEquals([$testProcessorID], array_keys($processors), 'Only the test processor should be returned'); + + $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'TestMode'], [$testProcessorID]); + $this->assertEquals([$testProcessorID], array_keys($processors), 'Only the test processor should be returned'); + + $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'LiveMode']); + $this->assertEquals([$liveProcessorID, 0], array_keys($processors), 'Only the Live processor and the manual processor should be returned'); + + $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'LiveMode'], [$liveProcessorID]); + $this->assertEquals([$liveProcessorID], array_keys($processors), 'Only the Live processor should be returned'); + + } + } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 8cc6355d74..1d7eb810b3 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -806,9 +806,15 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * * @return \CRM_Core_Payment_Dummy * Instance of Dummy Payment Processor + * + * @throws \CiviCRM_API3_Exception */ - public function dummyProcessorCreate($processorParams = array()) { + public function dummyProcessorCreate($processorParams = []) { $paymentProcessorID = $this->processorCreate($processorParams); + // For the tests we don't need a live processor, but as core ALWAYS creates a processor in live mode and one in test mode we do need to create both + // Otherwise we are testing a scenario that only exists in tests (and some tests fail because the live processor has not been defined). + $processorParams['is_test'] = FALSE; + $this->processorCreate($processorParams); return System::singleton()->getById($paymentProcessorID); } -- 2.25.1