dev/financial#54 add routine to convert contribute settings
authoreileen <emcnaughton@wikmedia.org>
Sun, 19 May 2019 23:32:30 +0000 (11:32 +1200)
committereileen <emcnaughton@wikmedia.org>
Tue, 21 May 2019 06:51:37 +0000 (18:51 +1200)
CRM/Upgrade/Incremental/Base.php
CRM/Upgrade/Incremental/php/FiveFifteen.php
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php
tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php

index c787ca4ac2cd683ad702db5f2bf98948f5f4a328..5145be10721100e7bb5c344a2ef6f1c3c78ca89b 100644 (file)
@@ -196,6 +196,29 @@ class CRM_Upgrade_Incremental_Base {
 
   }
 
+  /**
+   * Re-save any valid values from contribute settings into the normal setting
+   * format.
+   *
+   * We render the array of contribution_invoice_settings and any that have
+   * metadata defined we add to the correct key. This is safe to run even if no
+   * settings are to be converted, per the test in
+   * testConvertUpgradeContributeSettings.
+   *
+   * @param $ctx
+   *
+   * @return bool
+   */
+  public static function updateContributeSettings($ctx) {
+    $settings = Civi::settings()->get('contribution_invoice_settings');
+    $metadata = \Civi\Core\SettingsMetadata::getMetadata();
+    $conversions = array_intersect_key((array) $settings, $metadata);
+    foreach ($conversions as $key => $conversion) {
+      Civi::settings()->set($key, $conversion);
+    }
+    return TRUE;
+  }
+
   /**
    * Do any relevant smart group updates.
    *
index 39f1b1bd7c9c6f621de9e9929f231534b3387401..2cfb6a2d3396d1d8fb7b2eafed469d203acc363e 100644 (file)
@@ -67,21 +67,14 @@ class CRM_Upgrade_Incremental_php_FiveFifteen extends CRM_Upgrade_Incremental_Ba
    * (change the x in the function name):
    */
 
-  //  /**
-  //   * Upgrade function.
-  //   *
-  //   * @param string $rev
-  //   */
-  //  public function upgrade_5_0_x($rev) {
-  //    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
-  //    $this->addTask('Do the foo change', 'taskFoo', ...);
-  //    // Additional tasks here...
-  //    // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
-  //    // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
-  //  }
-
-  // public static function taskFoo(CRM_Queue_TaskContext $ctx, ...) {
-  //   return TRUE;
-  // }
+  /**
+   * Upgrade function.
+   *
+   * @param string $rev
+   */
+  public function upgrade_5_15_alpha1($rev) {
+    $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
+    $this->addTask('Fix errant deferred revenue settings', 'updateContributeSettings');
+  }
 
 }
index a53d26583f60c2c3560d721e3c393a728f75a706..a453d51d754d9a67f317c84f20fdaa0c1b2d4240 100644 (file)
@@ -999,22 +999,6 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
     $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
   }
 
-  /**
-   * Test checkContributeSettings.
-   */
-  public function testCheckContributeSettings() {
-    $settings = Civi::settings()->get('deferred_revenue_enabled');
-    $this->assertNull($settings);
-    $params = array(
-      'contribution_invoice_settings' => array(
-        'deferred_revenue_enabled' => '1',
-      ),
-    );
-    $this->callAPISuccess('Setting', 'create', $params);
-    $settings = Civi::settings()->get('deferred_revenue_enabled');
-    $this->assertEquals($settings, 1, 'Check for settings has failed');
-  }
-
   /**
    * Test allowUpdateRevenueRecognitionDate.
    */
index 6f2851a98af33ed15ace64f4c4167757cf772a09..1bd7a387e1deb977b2a7400c8c1d57cf515cdf21 100644 (file)
@@ -275,8 +275,8 @@ class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase {
    * Test for validating financial type has deferred revenue account relationship.
    */
   public function testcheckFinancialTypeHasDeferred() {
-    Civi::settings()->set('contribution_invoice_settings', array('deferred_revenue_enabled' => '1'));
-    $params = array();
+    Civi::settings()->set('deferred_revenue_enabled', 1);
+    $params = [];
     $valid = CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params);
     $this->assertFalse($valid, "This should have been false");
     $cid = $this->individualCreate();
index 74dbaca6ab43ac27ee9f9b850e7a83c018044c4d..a990e124fcedf3b3ec81d83853bfbadc8ec092ce 100644 (file)
@@ -151,6 +151,8 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
 
   /**
    * Test renaming multiple fields.
+   *
+   * @throws Exception
    */
   public function testRenameFields() {
     $this->callAPISuccess('SavedSearch', 'create', [
@@ -169,4 +171,15 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
     $this->assertEquals('activity_date_time_relative', $savedSearch['form_values'][1][0]);
   }
 
+  /**
+   * Test that a mis-saved variable in 'contribute settings' can be converted to a
+   * 'proper' setting.
+   */
+  public function testConvertUpgradeContributeSettings() {
+    Civi::settings()->set('contribution_invoice_settings', ['foo' => 'bar', 'deferred_revenue_enabled' => 1]);
+    $this->assertEquals(0, Civi::settings()->get('deferred_revenue_enabled'));
+    CRM_Upgrade_Incremental_Base::updateContributeSettings(NULL, 5.1);
+    $this->assertEquals(1, Civi::settings()->get('deferred_revenue_enabled'));
+  }
+
 }