From 7bab3351ddbf0f466a01133da7258f1a92734b49 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 4 Mar 2019 09:09:50 +1300 Subject: [PATCH] Add upgrade routine --- CRM/Upgrade/Incremental/SmartGroups.php | 40 +++++++++++++++++++ CRM/Upgrade/Incremental/php/FiveTwelve.php | 11 ++++- .../CRM/Upgrade/Incremental/BaseTest.php | 36 +++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/CRM/Upgrade/Incremental/SmartGroups.php b/CRM/Upgrade/Incremental/SmartGroups.php index 3c0500a6ec..764e12ab08 100644 --- a/CRM/Upgrade/Incremental/SmartGroups.php +++ b/CRM/Upgrade/Incremental/SmartGroups.php @@ -111,6 +111,45 @@ class CRM_Upgrade_Incremental_SmartGroups { return $dateValue; } + /** + * Rename a smartgroup field. + * + * @param string $oldName + * @param string $newName + */ + public function renameField($oldName, $newName) { + foreach ($this->getSearchesWithField($oldName) as $savedSearch) { + $formValues = $savedSearch['form_values']; + foreach ($formValues as $index => $formValue) { + if ($formValue[0] === $oldName) { + $formValues[$index][0] = $newName; + } + } + + if ($formValues !== $savedSearch['form_values']) { + civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]); + } + } + } + + /** + * Rename pairs of fields + * + * @param array $pairs + * Array or arrays of pairs - e.g + * [ + * ['old' => 'activity_date', 'new' => 'activity_date_time'], + * ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'], + * ['old' => 'activity_date_high', 'new' => 'activity_date_time_high'], + * ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'], + * ] + */ + public function renameFields($pairs) { + foreach ($pairs as $pair) { + $this->renameField($pair['old'], $pair['new']); + } + } + /** * @param $field * @return mixed @@ -121,6 +160,7 @@ class CRM_Upgrade_Incremental_SmartGroups { 'form_values' => ['LIKE' => "%{$field}%"], ])['values']; return $savedSearches; + } } diff --git a/CRM/Upgrade/Incremental/php/FiveTwelve.php b/CRM/Upgrade/Incremental/php/FiveTwelve.php index e514d65329..8202582bb4 100644 --- a/CRM/Upgrade/Incremental/php/FiveTwelve.php +++ b/CRM/Upgrade/Incremental/php/FiveTwelve.php @@ -73,10 +73,19 @@ class CRM_Upgrade_Incremental_php_FiveTwelve extends CRM_Upgrade_Incremental_Bas * @param string $rev */ public function upgrade_5_12_alpha1($rev) { - $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev); + $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); + $this->addTask('Update smart groups to rename filters on activity_date to activity_date_time', 'updateSmartGroups', [ + 'renameFields' => [ + ['old' => 'activity_date', 'new' => 'activity_date_time'], + ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'], + ['old' => 'activity_date_high', 'new' => 'activity_date_time_high'], + ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'], + ], + ]); $this->addTask('Update smart groups where jcalendar fields have been converted to datepicker', 'updateSmartGroups', [ 'datepickerConversion' => [ 'age_asof_date', + 'activity_date_time' ] ]); } diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php index 362fd5babe..20e4cefc11 100644 --- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php +++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php @@ -124,6 +124,42 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase { $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []); $this->assertEquals('IN', $savedSearch['form_values'][0][1]); $this->assertEquals(['1'], $savedSearch['form_values'][0][2]); + + } + + /** + * Test renaming a field. + */ + public function testRenameField() { + $this->callAPISuccess('SavedSearch', 'create', [ + 'form_values' => [ + ['activity_date_low', '=', '01/22/2019'], + ] + ]); + $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups(); + $smartGroupConversionObject->renameField('activity_date_low', 'activity_date_time_low'); + $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []); + $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]); + } + + /** + * Test renaming multiple fields. + */ + public function testRenameFields() { + $this->callAPISuccess('SavedSearch', 'create', [ + 'form_values' => [ + ['activity_date_low', '=', '01/22/2019'], + ['activity_date_relative', '=', 0], + ] + ]); + $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups(); + $smartGroupConversionObject->renameFields([ + ['old' => 'activity_date_low', 'new' => 'activity_date_time_low'], + ['old' => 'activity_date_relative', 'new' => 'activity_date_time_relative'], + ]); + $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []); + $this->assertEquals('activity_date_time_low', $savedSearch['form_values'][0][0]); + $this->assertEquals('activity_date_time_relative', $savedSearch['form_values'][1][0]); } } -- 2.25.1