Test calling CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors
authoreileen <emcnaughton@wikimedia.org>
Mon, 14 Oct 2019 02:56:23 +0000 (15:56 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 14 Oct 2019 02:59:17 +0000 (15:59 +1300)
Add test to ensure calling with Test capability only returns testMode processors

CRM/Core/Payment.php
tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 502d332df305b91535b98bb04b91b191ca6d991f..69b97a342fbe7cb885e0c2dd71f5c62cef8b4fd5 100644 (file)
@@ -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.
    *
index 33bc618ce453ca77bdc2a762825de0b25ab92007..6063c42aa6c3d53f6a6d44a7929d4fe698cc9cac 100644 (file)
@@ -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');
+
+  }
+
 }
index 8cc6355d740174084fb3ccc21fbff8027d7ad92c..1d7eb810b3ce79b09ae74e56171a8db220f7f70b 100644 (file)
@@ -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);
   }