From e2d37012ddd8f131a1b1c57366d805ff45b3d6da Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 19 Dec 2023 14:47:55 +1300 Subject: [PATCH] Add test cover for Dummy deprecations --- CRM/Core/Payment/Dummy.php | 14 ++-- .../Financial/BAO/PaymentProcessorTest.php | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/CRM/Core/Payment/Dummy.php b/CRM/Core/Payment/Dummy.php index 57a963cbc8..61f3e8ca7f 100644 --- a/CRM/Core/Payment/Dummy.php +++ b/CRM/Core/Payment/Dummy.php @@ -216,18 +216,18 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment { $result['payment_status'] = 'Completed'; return $result; } - + // Warn it the contracted variables are not provided + // The list is at https://docs.civicrm.org/dev/en/latest/extensions/payment-processors/paymentclass/#recurring-contribution-parameters + if ($propertyBag->getIsRecur()) { + if (empty($params['frequency_interval']) || empty($params['frequency_unit']) || empty($params['is_recur'])) { + CRM_Core_Error::deprecatedWarning('contracted frequency params not passed'); + } + } // Invoke hook_civicrm_paymentProcessor // In Dummy's case, there is no translation of parameters into // the back-end's canonical set of parameters. But if a processor // does this, it needs to invoke this hook after it has done translation, // but before it actually starts talking to its proprietary back-end. - if ($propertyBag->getIsRecur()) { - if (empty($params['frequency_interval']) || empty($params['frequency_unit'])) { - CRM_Core_Error::deprecatedWarning('contracted frequency params not passed'); - } - } - // no translation in Dummy processor CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $propertyBag); // This means we can test failing transactions by setting a past year in expiry. A full expiry check would // be more complete. diff --git a/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php index 4ca0f0d9a5..2de1c1e793 100644 --- a/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php +++ b/tests/phpunit/CRM/Financial/BAO/PaymentProcessorTest.php @@ -10,6 +10,7 @@ */ use Civi\Api4\PaymentProcessor; +use Civi\Payment\PropertyBag; /** * Class CRM_Financial_BAO_PaymentProcessorTypeTest @@ -17,6 +18,11 @@ use Civi\Api4\PaymentProcessor; */ class CRM_Financial_BAO_PaymentProcessorTest extends CiviUnitTestCase { + public function tearDown(): void { + $this->quickCleanup(['civicrm_payment_processor']); + parent::tearDown(); + } + /** * Check method create() */ @@ -89,6 +95,8 @@ class CRM_Financial_BAO_PaymentProcessorTest extends CiviUnitTestCase { /** * Test the Manual processor supports 'NoEmailProvided' + * + * @throws \CRM_Core_Exception */ public function testManualProcessorSupportsNoEmailProvided(): void { $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['NoEmailProvided']); @@ -102,4 +110,69 @@ class CRM_Financial_BAO_PaymentProcessorTest extends CiviUnitTestCase { $this->assertTrue($found, 'The Manual payment processor should support "NoEmailProvided"'); } + /** + * Test that deprecation warnings are emitted when required recur params not present + * + * @throws \CRM_Core_Exception + * @throws \Civi\Payment\Exception\PaymentProcessorException + */ + public function testDummyCheckArrayAccess(): void { + $this->dummyProcessorCreate(); + $processor = Civi\Payment\System::singleton()->getById($this->ids['PaymentProcessor']['dummy']); + $parameters = ['amount' => 100, 'is_recur' => TRUE]; + try { + $processor->doPayment($parameters); + } + catch (Exception $e) { + $this->assertEquals('contracted frequency params not passed Caller: CRM_Financial_BAO_PaymentProcessorTest::testDummyCheckArrayAccess', $e->getMessage()); + } + } + + /** + * Test that deprecation warnings are emitted when required recur params not present + * + * @throws \CRM_Core_Exception + * @throws \Civi\Payment\Exception\PaymentProcessorException + */ + public function testDummyCheckPropertyBag(): void { + $this->dummyProcessorCreate(); + $processor = \Civi\Payment\System::singleton()->getById($this->ids['PaymentProcessor']['dummy']); + $parameters = ['amount' => 100, 'is_recur' => TRUE]; + $parameters = PropertyBag::cast($parameters); + try { + $processor->doPayment($parameters); + } + catch (Exception $e) { + $this->assertEquals('contracted frequency params not passed Caller: CRM_Financial_BAO_PaymentProcessorTest::testDummyCheckPropertyBag', $e->getMessage()); + } + } + + /** + * Test that deprecation warnings are emitted when required recur params are present + * + * @throws \CRM_Core_Exception + * @throws \Civi\Payment\Exception\PaymentProcessorException + */ + public function testDummyCheckArrayAccessWithParams(): void { + $this->dummyProcessorCreate(); + $processor = Civi\Payment\System::singleton()->getById($this->ids['PaymentProcessor']['dummy']); + $parameters = ['amount' => 100, 'is_recur' => TRUE, 'frequency_unit' => 'month', 'frequency_interval' => 1]; + $parameters = PropertyBag::cast($parameters); + $processor->doPayment($parameters); + } + + /** + * Test that deprecation warnings are emitted when required recur params are present + * + * @throws \CRM_Core_Exception + * @throws \Civi\Payment\Exception\PaymentProcessorException + */ + public function testDummyCheckPropertyBagWithParams(): void { + $this->dummyProcessorCreate(); + $processor = Civi\Payment\System::singleton()->getById($this->ids['PaymentProcessor']['dummy']); + $parameters = ['amount' => 100, 'is_recur' => TRUE, 'frequency_unit' => 'month', 'frequency_interval' => 1]; + $parameters = PropertyBag::cast($parameters); + $processor->doPayment($parameters); + } + } -- 2.25.1