dev/core#561 Add in Upgrade routine to convert log_date smart group searches to their...
authorSeamus Lee <seamuslee001@gmail.com>
Fri, 1 Nov 2019 21:19:23 +0000 (08:19 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 1 Nov 2019 22:21:14 +0000 (09:21 +1100)
Fix log date function call in upgrade

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

index e703e90f0ab747dddbc4f7f58b16b94f2e968955..2bd5329d991fa52d77b4a18084960b9ba4f7f438 100644 (file)
@@ -74,6 +74,8 @@ class CRM_Upgrade_Incremental_SmartGroups {
       'relationship_start_date' => 'relation_start',
       'relationship_end_date' => 'relation_end',
       'event' => 'event',
+      'created_date' => 'log',
+      'modified_date' => 'log',
     ];
 
     foreach ($fields as $field) {
@@ -254,4 +256,40 @@ class CRM_Upgrade_Incremental_SmartGroups {
 
   }
 
+  public function renameLogFields() {
+    $addedDate = FALSE;
+    foreach ($this->getSearchesWithField('log_date') as $savedSearch) {
+      $formValues = $savedSearch['form_values'];
+      foreach ($formValues as $index => $formValue) {
+        if (isset($formValue[0]) && $formValue[0] === 'log_date') {
+          if ($formValue[2] == 1) {
+            $addedDate = TRUE;
+          }
+          else {
+            $addedDate = FALSE;
+          }
+        }
+        if (isset($formValue[0]) && ($formValue[0] === 'log_date_high' || $formValue[0] === 'log_date_low')) {
+          $isHigh = substr($index, -5, 5) === '_high';
+          if ($addedDate) {
+            $fieldName = 'created_date';
+          }
+          else {
+            $fieldName = 'modified_date';
+          }
+          if ($isHigh) {
+            $fieldName .= '_high';
+          }
+          else {
+            $fieldName .= '_low';
+          }
+          $formValues[$index][0] = $fieldName;
+        }
+      }
+      if ($formValues !== $savedSearch['form_values']) {
+        civicrm_api3('SavedSearch', 'create', ['id' => $savedSearch['id'], 'form_values' => $formValues]);
+      }
+    }
+  }
+
 }
index a0514bbb5e18a50fe747c25cc074df21bbc6ef1d..fd22ad4a0589284095382f976fea6b531df5dad2 100644 (file)
@@ -123,6 +123,9 @@ class CRM_Upgrade_Incremental_php_FiveTwenty extends CRM_Upgrade_Incremental_Bas
         ['old' => 'event_end_date_high', 'new' => 'event_high'],
       ],
     ]);
+    $this->addTask('Convert Log date searches to their final names either created date or modified date', 'updateSmartGroups', [
+      'renameLogFields' => [],
+    ]);
     $this->addTask('Update smart groups where jcalendar fields have been converted to datepicker', 'updateSmartGroups', [
       'datepickerConversion' => [
         'birth_date',
@@ -134,6 +137,8 @@ class CRM_Upgrade_Incremental_php_FiveTwenty extends CRM_Upgrade_Incremental_Bas
         'relationship_end_date',
         'event',
         'relation_active_period_date',
+        'created_date',
+        'modified_date',
       ],
     ]);
     $this->addTask('Clean up unused table "civicrm_persistent"', 'dropTableIfEmpty', 'civicrm_persistent');
index 22fc93fffcc0f7355780ed650fe80d00829229f9..63ebb74665a0e2d99b6a0bd3c8c323ca51976955 100644 (file)
@@ -221,6 +221,77 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test Log Date conversion
+   */
+  public function testLogDateConversion() {
+    // Create two sets of searches one set for added by and one for modified by
+    // Each set contains a relative search on this.month and a specific date search low
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        ['log_date', '=', 1],
+        ['log_date_low', '=', '20191001000000'],
+        ['log_date_high', '=', '20191031235959'],
+        'relative_dates' => [
+          'log' => 'this.month',
+        ],
+      ],
+    ]);
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        ['log_date', '=', 1],
+        ['log_date_low', '=', '20191001000000'],
+      ],
+    ]);
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        ['log_date', '=', 2],
+        ['log_date_low', '=', '20191001000000'],
+        ['log_date_high', '=', '20191031235959'],
+        'relative_dates' => [
+          'log' => 'this.month',
+        ],
+      ],
+    ]);
+    $this->callAPISuccess('SavedSearch', 'create', [
+      'form_values' => [
+        ['log_date', '=', 2],
+        ['log_date_low', '=', '20191001000000'],
+      ],
+    ]);
+    $smartGroupConversionObject = new CRM_Upgrade_Incremental_SmartGroups();
+    $smartGroupConversionObject->renameLogFields();
+    $smartGroupConversionObject->updateGroups([
+      'datepickerConversion' => [
+        'created_date',
+        'modified_date',
+      ],
+    ]);
+    $savedSearhes = $this->callAPISuccess('SavedSearch', 'get', []);
+    $expectedResults = [
+      1 => [
+        0 => ['log_date', '=', 1],
+        'relative_dates' => [],
+        3 => ['created_date_relative', '=', 'this.month'],
+      ],
+      2 => [
+        0 => ['log_date', '=', 1],
+        1 => ['created_date_low', '=', '2019-10-01 00:00:00'],
+        2 => ['created_date_relative', '=', 0],
+      ],
+      3 => [
+        0 => ['log_date', '=', 2],
+        'relative_dates' => [],
+        3 => ['modified_date_relative', '=', 'this.month'],
+      ],
+      4 => [
+        0 => ['log_date', '=', 2],
+        1 => ['modified_date_low', '=', '2019-10-01 00:00:00'],
+        2 => ['modified_date_relative', '=', 0],
+      ],
+    ];
+  }
+
   /**
    * Test converting relationship fields
    */