From c143680738f4e9c4cf16db9e110d4ca33725e316 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 1 Nov 2019 08:08:33 +1100 Subject: [PATCH] Add in upgrade step for upgrading Event date smart groups Include relation_active_period_date field for upgrade --- CRM/Upgrade/Incremental/SmartGroups.php | 36 +++++++++- CRM/Upgrade/Incremental/php/FiveTwenty.php | 4 ++ .../CRM/Upgrade/Incremental/BaseTest.php | 65 +++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/CRM/Upgrade/Incremental/SmartGroups.php b/CRM/Upgrade/Incremental/SmartGroups.php index a5f61cf42b..e703e90f0a 100644 --- a/CRM/Upgrade/Incremental/SmartGroups.php +++ b/CRM/Upgrade/Incremental/SmartGroups.php @@ -73,6 +73,7 @@ class CRM_Upgrade_Incremental_SmartGroups { 'mailing_job_start_date' => 'mailing_date', 'relationship_start_date' => 'relation_start', 'relationship_end_date' => 'relation_end', + 'event' => 'event', ]; foreach ($fields as $field) { @@ -93,10 +94,32 @@ class CRM_Upgrade_Incremental_SmartGroups { } } foreach ($formValues as $index => $formValue) { + if (!is_array($formValue)) { + if ($index === $relativeFieldName) { + $hasRelative = TRUE; + if (!empty($formValue)) { + $isRelative = TRUE; + } + continue; + } + elseif ($index === 'event_low' || $index === 'event_high') { + if ($isRelative || (!$isRelative && $formValue === '')) { + unset($formValues[$index]); + } + else { + $isHigh = substr($index, -5, 5) === '_high'; + $formValues[$index] = $this->getConvertedDateValue($formValue, $isHigh); + } + } + continue; + } if (!isset($formValue[0])) { // Any actual criteria will have this key set but skip any weird lines continue; } + if ($formValue[0] === $relativeFieldName && !empty($formValue[2])) { + $hasRelative = TRUE; + } if ($formValue[0] === $relativeFieldName && empty($formValue[2])) { unset($formValues[$index]);; } @@ -115,6 +138,9 @@ class CRM_Upgrade_Incremental_SmartGroups { $relativeFieldNames[] = $relativeFieldName; $formValues[] = [$relativeFieldName, '=', 0]; } + elseif (!$hasRelative) { + $formValues[] = [$relativeFieldName, '=', 0]; + } } if ($formValues !== $savedSearch['form_values']) { civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]); @@ -180,8 +206,14 @@ class CRM_Upgrade_Incremental_SmartGroups { foreach ($this->getSearchesWithField($oldName) as $savedSearch) { $formValues = $savedSearch['form_values']; foreach ($formValues as $index => $formValue) { - if ($formValue[0] === $oldName) { - $formValues[$index][0] = $newName; + if (is_array($formValue)) { + if (isset($formValue[0]) && $formValue[0] === $oldName) { + $formValues[$index][0] = $newName; + } + } + elseif ($index === $oldName) { + $formValues[$newName] = $formValue; + unset($formValues[$oldName]); } } diff --git a/CRM/Upgrade/Incremental/php/FiveTwenty.php b/CRM/Upgrade/Incremental/php/FiveTwenty.php index e8bc169442..a0514bbb5e 100644 --- a/CRM/Upgrade/Incremental/php/FiveTwenty.php +++ b/CRM/Upgrade/Incremental/php/FiveTwenty.php @@ -119,6 +119,8 @@ class CRM_Upgrade_Incremental_php_FiveTwenty extends CRM_Upgrade_Incremental_Bas ['old' => 'relation_end_date_low', 'new' => 'relationship_end_date_low'], ['old' => 'relation_end_date_high', 'new' => 'relationship_end_date_high'], ['old' => 'relation_end_date_relative', 'new' => 'relationship_end_date_relative'], + ['old' => 'event_start_date_low', 'new' => 'event_low'], + ['old' => 'event_end_date_high', 'new' => 'event_high'], ], ]); $this->addTask('Update smart groups where jcalendar fields have been converted to datepicker', 'updateSmartGroups', [ @@ -130,6 +132,8 @@ class CRM_Upgrade_Incremental_php_FiveTwenty extends CRM_Upgrade_Incremental_Bas 'mailing_job_start_date', 'relationship_start_date', 'relationship_end_date', + 'event', + 'relation_active_period_date', ], ]); $this->addTask('Clean up unused table "civicrm_persistent"', 'dropTableIfEmpty', 'civicrm_persistent'); diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php index 0bbc8303ba..22fc93fffc 100644 --- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php +++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php @@ -156,6 +156,71 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase { $this->assertEquals('this.week', $savedSearch['form_values'][8][2]); } + /** + * Test upgrading multiple Event smart groups of different formats + */ + public function testMultipleEventSmartGroupDateConversions() { + $this->callAPISuccess('SavedSearch', 'create', [ + 'form_values' => [ + ['event_start_date_low', '=', '20191001000000'], + ['event_end_date_high', '=', '20191031235959'], + 'relative_dates' => [ + 'event' => 'this.month', + ], + ], + ]); + $this->callAPISuccess('SavedSearch', 'create', [ + 'form_values' => [ + ['event_start_date_low', '=', '20191001000000'], + ], + ]); + $this->callAPISuccess('SavedSearch', 'create', [ + 'form_values' => [ + 'event_start_date_low' => '20191001000000', + 'event_end_date_high' => '20191031235959', + 'event_relative' => 'this.month', + ], + ]); + $this->callAPISuccess('SavedSearch', 'create', [ + 'form_values' => [ + 'event_start_date_low' => '10/01/2019', + 'event_end_date_high' => '', + 'event_relative' => '0', + ], + ]); + $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups(); + $smartGroupConversionObject->renameFields([ + ['old' => 'event_start_date_low', 'new' => 'event_low'], + ['old' => 'event_end_date_high', 'new' => 'event_high'], + ]); + $smartGroupConversionObject->updateGroups([ + 'datepickerConversion' => [ + 'event', + ], + ]); + $expectedResults = [ + 1 => [ + 'relative_dates' => [], + 2 => ['event_relative', '=', 'this.month'], + ], + 2 => [ + 0 => ['event_low', '=', '2019-10-01 00:00:00'], + 1 => ['event_relative', '=', 0], + ], + 3 => [ + 'event_relative' => 'this.month', + ], + 4 => [ + 'event_relative' => 0, + 'event_low' => '2019-10-01 00:00:00', + ], + ]; + $savedSearches = $this->callAPISuccess('SavedSearch', 'get', []); + foreach ($savedSearches['values'] as $id => $savedSearch) { + $this->assertEquals($expectedResults[$id], $savedSearch['form_values']); + } + } + /** * Test converting relationship fields */ -- 2.25.1