Skip to content

Commit

Permalink
Merge pull request #15677 from seamuslee001/event_smart_group_upgrade
Browse files Browse the repository at this point in the history
Add in upgrade step for upgrading Event date and Relationship Active Date smart groups
  • Loading branch information
seamuslee001 authored Nov 1, 2019
2 parents e7cbb73 + c143680 commit 8ccf5aa
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
36 changes: 34 additions & 2 deletions CRM/Upgrade/Incremental/SmartGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function datePickerConversion($fields) {
'mailing_job_start_date' => 'mailing_date',
'relationship_start_date' => 'relation_start',
'relationship_end_date' => 'relation_end',
'event' => 'event',
];

foreach ($fields as $field) {
Expand All @@ -93,10 +94,32 @@ public function datePickerConversion($fields) {
}
}
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]);;
}
Expand All @@ -115,6 +138,9 @@ public function datePickerConversion($fields) {
$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]);
Expand Down Expand Up @@ -180,8 +206,14 @@ 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 (is_array($formValue)) {
if (isset($formValue[0]) && $formValue[0] === $oldName) {
$formValues[$index][0] = $newName;
}
}
elseif ($index === $oldName) {
$formValues[$newName] = $formValue;
unset($formValues[$oldName]);
}
}

Expand Down
4 changes: 4 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveTwenty.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public function upgrade_5_20_alpha1($rev) {
['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', [
Expand All @@ -130,6 +132,8 @@ public function upgrade_5_20_alpha1($rev) {
'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');
Expand Down
65 changes: 65 additions & 0 deletions tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,71 @@ public function testSmartGroupMultipleRelatvieDateConversions() {
$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
*/
Expand Down

0 comments on commit 8ccf5aa

Please sign in to comment.