Merge pull request #16575 from colemanw/savedSearch
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 18 Feb 2020 06:52:13 +0000 (19:52 +1300)
committerGitHub <noreply@github.com>
Tue, 18 Feb 2020 06:52:13 +0000 (19:52 +1300)
Make savedSearch bao sane

CRM/Contact/BAO/Group.php
CRM/Contact/BAO/SavedSearch.php
api/v3/SavedSearch.php
tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php
tests/phpunit/CRM/Mailing/BAO/MailingTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContactTest.php
tests/phpunit/api/v3/SyntaxConformanceTest.php

index 2cad5888bb72ed0ea7af2d5e06639a9f07d3728c..bfc620b835dbb34b2cf3f33451ef8a10413d2c57 100644 (file)
@@ -515,7 +515,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
       if (isset($ssParams['saved_search_id'])) {
         $ssParams['id'] = $ssParams['saved_search_id'];
       }
-
+      $params['form_values'] = $params['formValues'];
       $savedSearch = CRM_Contact_BAO_SavedSearch::create($params);
 
       $params['saved_search_id'] = $savedSearch->id;
index 295502d770e01e663b83e9bd0f5e866120ded911..eafb14ccc242b6d855c189123b5efab8ffb7f9d4 100644 (file)
@@ -336,20 +336,7 @@ LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_
    */
   public static function create(&$params) {
     $savedSearch = new CRM_Contact_DAO_SavedSearch();
-    if (isset($params['formValues']) &&
-      !empty($params['formValues'])
-    ) {
-      $savedSearch->form_values = serialize($params['formValues']);
-    }
-    else {
-      $savedSearch->form_values = NULL;
-    }
-
-    $savedSearch->is_active = CRM_Utils_Array::value('is_active', $params, 1);
-    $savedSearch->mapping_id = CRM_Utils_Array::value('mapping_id', $params, 'null');
-    $savedSearch->custom_search_id = CRM_Utils_Array::value('custom_search_id', $params, 'null');
-    $savedSearch->id = CRM_Utils_Array::value('id', $params, NULL);
-
+    $savedSearch->copyValues($params, TRUE);
     $savedSearch->save();
 
     return $savedSearch;
index 3146cda4eac75bb957f4bbfcbeab69423919bff9..14a6f599837517bb9ed3bf699e25136a42776b4a 100644 (file)
  */
 function civicrm_api3_saved_search_create($params) {
   civicrm_api3_verify_one_mandatory($params, NULL, ['form_values', 'where_clause']);
-  // The create function of the dao expects a 'formValues' that is
-  // not serialized. The get function returns form_values, that is
-  // serialized.
-  // So for the create API, I guess it should work for serialized and
-  // unserialized form_values.
-
-  if (isset($params["form_values"])) {
-    if (is_array($params["form_values"])) {
-      $params["formValues"] = $params["form_values"];
-    }
-    else {
-      // Assume that form_values is serialized.
-      $params["formValues"] = \CRM_Utils_String::unserialize($params["form_values"]);
-    }
-  }
 
   $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'SavedSearch');
   _civicrm_api3_saved_search_result_cleanup($result);
   return $result;
 }
 
+/**
+ * @param array $fields
+ */
+function _civicrm_api3_saved_search_create_spec(&$fields) {
+  $fields['form_values']['api.aliases'][] = 'formValues';
+}
+
 /**
  * Delete an existing saved search.
  *
index b2fddc00aec6fc2770c762b4f799e76f14d589af..582f129c7226a42516bd25a950ccf1f48865a34e 100644 (file)
@@ -1178,7 +1178,7 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase {
     ]);
 
     // create smart group which will contain all Male contacts
-    $smartGroupParams = ['formValues' => ['gender_id' => 1]];
+    $smartGroupParams = ['form_values' => ['gender_id' => 1]];
     $smartGroupID = $this->smartGroupCreate(
       $smartGroupParams,
       [
index b27a4f2ab274b77c8a42749697d82f56f82b8152..acf200c46615a092b0311d7bac98c0006936e8ff 100644 (file)
@@ -290,7 +290,7 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
       }
       else {
         $groupIDs[$i] = $this->smartGroupCreate([
-          'formValues' => ['last_name' => (($i == 6) ? 'smart5' : 'smart' . $i)],
+          'form_values' => ['last_name' => (($i == 6) ? 'smart5' : 'smart' . $i)],
         ], $params);
       }
     }
@@ -458,7 +458,7 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
 
     // Setup
     $smartGroupParams = [
-      'formValues' => ['contact_type' => ['IN' => ['Individual']]],
+      'form_values' => ['contact_type' => ['IN' => ['Individual']]],
     ];
     $group = $this->smartGroupCreate($smartGroupParams);
     $sms_provider = $this->callAPISuccess('SmsProvider', 'create', [
index a799c723185a5d13eb96df14c8f132cc9a231d0f..173c06e6e1ebf239ecc31515661961246b5db6ec 100644 (file)
@@ -1289,10 +1289,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
    * @return int
    */
   public function smartGroupCreate($smartGroupParams = [], $groupParams = [], $contactType = 'Household') {
-    $smartGroupParams = array_merge([
-      'formValues' => ['contact_type' => ['IN' => [$contactType]]],
-    ],
-      $smartGroupParams);
+    $smartGroupParams = array_merge(['form_values' => ['contact_type' => ['IN' => [$contactType]]]], $smartGroupParams);
     $savedSearch = CRM_Contact_BAO_SavedSearch::create($smartGroupParams);
 
     $groupParams['saved_search_id'] = $savedSearch->id;
index 407f1fc3472321130178ee01485024d7a4a2f0c3..aa64a3b529050e37e5b69f515ccdc8350b566e6f 100644 (file)
@@ -4407,7 +4407,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     ]);
 
     $ssParams = [
-      'formValues' => [
+      'form_values' => [
         // Child of
         'display_relationship_type' => $rtype1['id'] . '_a_b',
         'sort_name' => 'Adams',
@@ -4415,14 +4415,14 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     ];
     $g1ID = $this->smartGroupCreate($ssParams, ['name' => uniqid(), 'title' => uniqid()]);
     $ssParams = [
-      'formValues' => [
+      'form_values' => [
         // Household Member of
         'display_relationship_type' => $rtype2['id'] . '_a_b',
       ],
     ];
     $g2ID = $this->smartGroupCreate($ssParams, ['name' => uniqid(), 'title' => uniqid()]);
     $ssParams = [
-      'formValues' => [
+      'form_values' => [
         // Household Member is
         'display_relationship_type' => $rtype2['id'] . '_b_a',
       ],
@@ -4430,12 +4430,9 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     // the reverse of g2 which adds another layer for overlap at related contact filter
     $g3ID = $this->smartGroupCreate($ssParams, ['name' => uniqid(), 'title' => uniqid()]);
     CRM_Contact_BAO_GroupContactCache::loadAll();
-    $g1Contacts = $this->callAPISuccess('contact', 'get', ['group' => $g1ID]);
-    $g2Contacts = $this->callAPISuccess('contact', 'get', ['group' => $g2ID]);
-    $g3Contacts = $this->callAPISuccess('contact', 'get', ['group' => $g3ID]);
-    $this->assertTrue($g1Contacts['count'] == 1);
-    $this->assertTrue($g2Contacts['count'] == 2);
-    $this->assertTrue($g3Contacts['count'] == 1);
+    $this->callAPISuccessGetCount('contact', ['group' => $g1ID], 1);
+    $this->callAPISuccessGetCount('contact', ['group' => $g2ID], 2);
+    $this->callAPISuccessGetCount('contact', ['group' => $g3ID], 1);
   }
 
   /**
index 30c4e9e51b046bcac1e12c8b1396634fa32561b1..e4b0f35b91795b6749187b407269b9dfc68e1036 100644 (file)
@@ -96,7 +96,6 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
       'Payment',
       'Order',
       //work fine in local
-      'SavedSearch',
       'Logging',
     ];
     $this->toBeImplemented['delete'] = [
@@ -724,15 +723,6 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase {
         // View mode is part of the navigation which is not retrieved by the api.
         'cant_return' => ['view_mode'],
       ],
-      'SavedSearch' => [
-        // I think the fields below are generated based on form_values.
-        'cant_update' => [
-          'search_custom_id',
-          'where_clause',
-          'select_tables',
-          'where_tables',
-        ],
-      ],
       'StatusPreference' => [
         'break_return' => [
           'ignore_severity',