From 4f3d62b9af9c3c4e8c798015ebea4059b49f1e9c Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Tue, 9 Oct 2018 17:30:37 +0530 Subject: [PATCH] (dev/core#394) : Wildcards are ignored in some smart group criteria, when the smart group is directly generated for a mailing --- CRM/Contact/BAO/Group.php | 2 +- CRM/Mailing/BAO/Mailing.php | 4 ++- tests/phpunit/CRM/Contact/BAO/GroupTest.php | 37 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 1cc2bd19d0..5ebf74c518 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -681,7 +681,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { //create/update saved search record. $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $ssId; - $savedSearch->form_values = serialize($params['form_values']); + $savedSearch->form_values = serialize(CRM_Contact_BAO_Query::convertFormValues($params['form_values'])); $savedSearch->mapping_id = $mappingId; $savedSearch->search_custom_id = CRM_Utils_Array::value('search_custom_id', $params); $savedSearch->save(); diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index 1f0f0fa94f..e2f7e57e56 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -160,7 +160,9 @@ class CRM_Mailing_BAO_Mailing extends CRM_Mailing_DAO_Mailing { ->param('#groupIDs', $groupIDs) ->execute(); while ($groupDAO->fetch()) { - if ($groupDAO->cache_date == NULL) { + // hidden smart groups always have a cache date and there is no other way + // we can rebuilt the contact list from UI so consider such smart group + if ($groupDAO->cache_date == NULL || $groupDAO->is_hidden) { CRM_Contact_BAO_GroupContactCache::load($groupDAO); } if ($groupType == 'Include') { diff --git a/tests/phpunit/CRM/Contact/BAO/GroupTest.php b/tests/phpunit/CRM/Contact/BAO/GroupTest.php index 279c24fbc9..d6128fc333 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupTest.php @@ -237,4 +237,41 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { $testUpdate = CRM_Contact_BAO_Group::create($params2); } + /** + * Ensure that when hidden smart group is created, wildcard string value is not ignored + */ + public function testHiddenSmartGroup() { + $customGroup = $this->customGroupCreate(); + $fields = array( + 'label' => 'testFld', + 'data_type' => 'String', + 'html_type' => 'Text', + 'custom_group_id' => $customGroup['id'], + ); + $customFieldID = CRM_Core_BAO_CustomField::create($fields)->id; + + $contactID = $this->individualCreate(['custom_' . $customFieldID => 'abc']); + + $hiddenSmartParams = [ + 'group_type' => ['2' => 1], + 'form_values' => ['custom_' . $customFieldID => ['LIKE' => '%a%']], + 'saved_search_id' => NULL, + 'search_custom_id' => NULL, + 'search_context' => 'advanced', + ]; + list($smartGroupID, $savedSearchID) = CRM_Contact_BAO_Group::createHiddenSmartGroup($hiddenSmartParams); + + $mailingID = $this->callAPISuccess('Mailing', 'create', [])['id']; + $this->callAPISuccess('MailingGroup', 'create', array( + 'mailing_id' => $mailingID, + 'group_type' => 'Include', + 'entity_table' => 'civicrm_group', + 'entity_id' => $smartGroupID, + )); + + CRM_Mailing_BAO_Mailing::getRecipients($mailingID); + $recipients = $this->callAPISuccess('MailingRecipients', 'get', ['mailing_id' => $mailingID]); + $this->assertEquals(1, $recipients['count'], 'Check recipient count'); + } + } -- 2.25.1