From: eileen <emcnaughton@wikmedia.org> Date: Sun, 19 May 2019 23:32:30 +0000 (+1200) Subject: dev/financial#54 add routine to convert contribute settings X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=504770b451d1da9689999f74653e021eb8194969;p=civicrm-core.git dev/financial#54 add routine to convert contribute settings --- diff --git a/CRM/Upgrade/Incremental/Base.php b/CRM/Upgrade/Incremental/Base.php index c787ca4ac2..5145be1072 100644 --- a/CRM/Upgrade/Incremental/Base.php +++ b/CRM/Upgrade/Incremental/Base.php @@ -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. * diff --git a/CRM/Upgrade/Incremental/php/FiveFifteen.php b/CRM/Upgrade/Incremental/php/FiveFifteen.php index 39f1b1bd7c..2cfb6a2d33 100644 --- a/CRM/Upgrade/Incremental/php/FiveFifteen.php +++ b/CRM/Upgrade/Incremental/php/FiveFifteen.php @@ -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'); + } } diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index a53d26583f..a453d51d75 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -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. */ diff --git a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php index 6f2851a98a..1bd7a387e1 100644 --- a/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php +++ b/tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php @@ -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(); diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php index 74dbaca6ab..a990e124fc 100644 --- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php +++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php @@ -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')); + } + }