Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#731 Add upgrade routine to convert on_hold to an array for sites with #13731

Merged
merged 1 commit into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions CRM/Upgrade/Incremental/SmartGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ public function datePickerConversion($fields) {
}

foreach ($fields as $field) {
$savedSearches = civicrm_api3('SavedSearch', 'get', [
'options' => ['limit' => 0],
'form_values' => ['LIKE' => "%{$field}%"],
])['values'];
foreach ($savedSearches as $savedSearch) {
foreach ($this->getSearchesWithField($field) as $savedSearch) {
$formValues = $savedSearch['form_values'];
foreach ($formValues as $index => $formValue) {
if (in_array($formValue[0], $fieldPossibilities)) {
Expand All @@ -118,6 +114,30 @@ public function datePickerConversion($fields) {
}
}

/**
* Conversion routine for a form change change from = string to IN array.
*
* For example a checkbox expected [$fieldName, '=', 1]
* whereas select expects [$fieldName, 'IN', [1]]
*
* @param string $field
*/
public function convertEqualsStringToInArray($field) {
foreach ($this->getSearchesWithField($field) as $savedSearch) {
$formValues = $savedSearch['form_values'];
foreach ($formValues as $index => $formValue) {
if ($formValue[0] === $field && !is_array($formValue[2]) && $formValue[1] === '=') {
$formValues[$index][1] = 'IN';
$formValues[$index][2] = [$formValue[2]];
}
}

if ($formValues !== $savedSearch['form_values']) {
civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
}
}
}

/**
* Update message templates.
*/
Expand Down Expand Up @@ -162,4 +182,16 @@ protected function getConvertedDateValue($dateValue) {
return $dateValue;
}

/**
* @param $field
* @return mixed
*/
protected function getSearchesWithField($field) {
$savedSearches = civicrm_api3('SavedSearch', 'get', [
'options' => ['limit' => 0],
'form_values' => ['LIKE' => "%{$field}%"],
])['values'];
return $savedSearches;
}

}
23 changes: 23 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveEleven.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
public function upgrade_5_11_alpha1($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
$this->addTask('Update smart groups where jcalendar fields have been converted to datepicker', 'updateSmartGroups', $rev);
if (Civi::settings()->get('civimail_multiple_bulk_emails')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton shouldn't this be in 5_11_beta1 as it is in the RC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seamuslee001 I guess - I can move it there if you think that is better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seamuslee001 ok updated

$this->addTask('Update any on hold groups to reflect field change', 'updateOnHold', $rev);
}
}

/**
* Upgrade function.
*
* @param string $rev
*/
public function upgrade_5_11_beta1($rev) {
if (Civi::settings()->get('civimail_multiple_bulk_emails')) {
$this->addTask('Update any on hold groups to reflect field change', 'updateOnHold', $rev);
}
}

/**
* Update on hold groups -note the core function layout for this sort of upgrade changed in 5.12 - don't copy this.
*/
public function updateOnHold($ctx, $version) {
$groupUpdateObject = new CRM_Upgrade_Incremental_SmartGroups($version);
$groupUpdateObject->convertEqualsStringToInArray('on_hold');
return TRUE;
}

}
20 changes: 20 additions & 0 deletions tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {

public function tearDown() {
$this->quickCleanup(['civicrm_saved_search']);
}

/**
* Test message upgrade process.
*/
Expand Down Expand Up @@ -99,4 +103,20 @@ public function testSmartGroupDatePickerConversion() {
$this->assertEquals('2019-01-22 00:00:00', $savedSearch['form_values'][1][2]);
}

/**
* Test conversion of on hold group.
*/
public function testOnHoldConversion() {
$this->callAPISuccess('SavedSearch', 'create', [
'form_values' => [
['on_hold', '=', '1'],
]
]);
$smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups('5.11.alpha1');
$smartGroupConversionObject->convertEqualsStringToInArray('on_hold');
$savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []);
$this->assertEquals('IN', $savedSearch['form_values'][0][1]);
$this->assertEquals(['1'], $savedSearch['form_values'][0][2]);
}

}