Add in upgrade step for upgrading Event date smart groups
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 31 Oct 2019 21:08:33 +0000 (08:08 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 31 Oct 2019 22:53:21 +0000 (09:53 +1100)
Include relation_active_period_date field for upgrade

CRM/Upgrade/Incremental/SmartGroups.php
CRM/Upgrade/Incremental/php/FiveTwenty.php
tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php

index a5f61cf42be7b61b931eed0741315e608534137b..e703e90f0ab747dddbc4f7f58b16b94f2e968955 100644 (file)
@@ -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]);
         }
       }
 
index e8bc16944279165b5354c3026078f235810a1cc3..a0514bbb5e18a50fe747c25cc074df21bbc6ef1d 100644 (file)
@@ -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');
index 0bbc8303baf214034ae5ad08c6bddf44a81268ea..22fc93fffcc0f7355780ed650fe80d00829229f9 100644 (file)
@@ -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
    */