[REF] dev/core#4418 Add in Upgrade script to fix double json encoding
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 6 Jul 2023 23:32:53 +0000 (09:32 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 6 Jul 2023 23:46:47 +0000 (09:46 +1000)
CRM/Upgrade/Incremental/php/FiveSixtyFour.php
tests/phpunit/CRM/Upgrade/Incremental/php/FiveSixtyFourTest.php [new file with mode: 0644]

index 6cf0f09ec5964727ffe1faa6ba75685da0d48fc8..1a5a5012847101407bc36a5e7764f18d946761c1 100644 (file)
@@ -9,6 +9,8 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\Api4\PaymentProcessor;
+
 /**
  * Upgrade logic for the 5.64.x series.
  *
@@ -33,6 +35,7 @@ class CRM_Upgrade_Incremental_php_FiveSixtyFour extends CRM_Upgrade_Incremental_
     $this->addTask('Drop unused civicrm_action_mapping table', 'dropTable', 'civicrm_action_mapping');
     $this->addTask('Update post_URL/cancel_URL in logging tables', 'updateLogging');
     $this->addTask('Add in Everybody ACL Role option value', 'addEveryBodyAclOptionValue');
+    $this->addTask('Fix double json encoding of accepted_credit_cards field in payment processor table', 'fixDoubleEscapingPaymentProcessorCreditCards');
   }
 
   public static function updateLogging($ctx): bool {
@@ -58,4 +61,17 @@ CHANGE `cancel_URL` `cancel_url` varchar(255) DEFAULT NULL COMMENT 'Redirect to
     return TRUE;
   }
 
+  /**
+   * Fix any double json encoding in Payment Processor accepted_credit_cards field
+   */
+  public static function fixDoubleEscapingPaymentProcessorCreditCards() {
+    $paymentProcessors = PaymentProcessor::get(FALSE)->execute();
+    foreach ($paymentProcessors as $paymentProcessor) {
+      if (is_numeric(array_keys($paymentProcessor['accepted_credit_cards'])[0])) {
+        PaymentProcessor::update(FALSE)->addValue('accepted_credit_cards', json_decode($paymentProcessor['accepted_credit_cards'], TRUE))->addWhere('id', '=', $paymentProcessor['id'])->execute();
+      }
+    }
+    return TRUE;
+  }
+
 }
diff --git a/tests/phpunit/CRM/Upgrade/Incremental/php/FiveSixtyFourTest.php b/tests/phpunit/CRM/Upgrade/Incremental/php/FiveSixtyFourTest.php
new file mode 100644 (file)
index 0000000..2868137
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Civi\Api4\PaymentProcessor;
+
+/**
+ * Class CRM_Upgrade_Incremental_php_FiveSixtyFour
+ * @group headless
+ */
+class CRM_Upgrade_Incremental_php_FiveSixtyFourTest extends CiviUnitTestCase {
+
+  use CRM_Core_Payment_AuthorizeNetTrait;
+
+  /**
+   * Test that fixing the double json encode works as expected
+   */
+  public function testFixDobuleJsonEncode() {
+    $this->createAuthorizeNetProcessor();
+    $this->paymentProcessorAuthorizeNetCreate();
+    $creditCards = [
+      'visa' => 'visa',
+      'mastercard' => 'mastercard',
+    ];
+    PaymentProcessor::update()->addValue('accepted_credit_cards', $creditCards)->addWhere('id', '=', $this->ids['PaymentProcessor']['anet'])->execute();
+    PaymentProcessor::update()->addValue('accepted_credit_cards', json_encode($creditCards))->addWhere('id', '=', $this->ids['PaymentProcessor']['authorize_net'])->execute();
+    CRM_Upgrade_Incremental_php_FiveSixtyFour::fixDoubleEscapingPaymentProcessorCreditCards();
+    $paymentProcessors = PaymentProcessor::get()->execute();
+    foreach ($paymentProcessors as $paymentProcessor) {
+      $this->assertEquals($creditCards, $paymentProcessor['accepted_credit_cards']);
+    }
+  }
+
+}