From 93287bd4fd37caae4f4a5226703b5530eaf2de58 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 28 Jun 2019 00:56:31 +1200 Subject: [PATCH] dev/core#1030 fix mis-saving of hidden smart group when sending mail from search builder Fixes a bug where sending a civimail from search builder was sending out to the wrong list --- CRM/Contact/BAO/Group.php | 3 ++- CRM/Contact/Form/Task.php | 5 ++++- .../CRM/Mailing/Form/Task/AdhocMailingTest.php | 12 +++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index a2e5905f15..552db8f9d1 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -695,7 +695,8 @@ 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(CRM_Contact_BAO_Query::convertFormValues($params['form_values'])); + $formValues = $params['search_context'] === 'builder' ? $params['form_values'] : CRM_Contact_BAO_Query::convertFormValues($params['form_values']); + $savedSearch->form_values = serialize($formValues); $savedSearch->mapping_id = $mappingId; $savedSearch->search_custom_id = CRM_Utils_Array::value('search_custom_id', $params); $savedSearch->save(); diff --git a/CRM/Contact/Form/Task.php b/CRM/Contact/Form/Task.php index 397a8d50a9..78d0275520 100644 --- a/CRM/Contact/Form/Task.php +++ b/CRM/Contact/Form/Task.php @@ -543,7 +543,10 @@ class CRM_Contact_Form_Task extends CRM_Core_Form_Task { 'group_type' => ['2' => 1], // queryParams have been preprocessed esp WRT any entity reference fields - see + // https://github.com/civicrm/civicrm-core/pull/13250 - 'form_values' => $this->get('queryParams'), + // Advanced search sets queryParams, for builder you need formValues. + // This is kinda fragile but .... see CRM_Mailing_Form_Task_AdhocMailingTest for test effort. + // Moral never touch anything ever again and the house of cards will stand tall, unless there is a breeze + 'form_values' => $this->get('isSearchBuilder') ? $this->get('formValues') : $this->get('queryParams'), 'saved_search_id' => $ssId, 'search_custom_id' => $this->get('customSearchID'), 'search_context' => $this->get('context'), diff --git a/tests/phpunit/CRM/Mailing/Form/Task/AdhocMailingTest.php b/tests/phpunit/CRM/Mailing/Form/Task/AdhocMailingTest.php index 8c86b4ed2e..d782ed79f1 100644 --- a/tests/phpunit/CRM/Mailing/Form/Task/AdhocMailingTest.php +++ b/tests/phpunit/CRM/Mailing/Form/Task/AdhocMailingTest.php @@ -28,15 +28,18 @@ * Test class for CRM_Contact_Form_Task_EmailCommon. * @group headless */ -class CRM_Mailing_Form_Task_AdHocMailingTest extends CiviUnitTestCase { +class CRM_Mailing_Form_Task_AdhocMailingTest extends CiviUnitTestCase { + /** + * @throws \Exception + */ protected function setUp() { parent::setUp(); $this->_contactIds = [ $this->individualCreate(['first_name' => 'Antonia', 'last_name' => 'D`souza']), $this->individualCreate(['first_name' => 'Anthony', 'last_name' => 'Collins']), ]; - $this->_optionValue = $this->callApiSuccess('optionValue', 'create', [ + $this->_optionValue = $this->callAPISuccess('optionValue', 'create', [ 'label' => '"Seamus Lee" ', 'option_group_id' => 'from_email_address', ]); @@ -64,6 +67,9 @@ class CRM_Mailing_Form_Task_AdHocMailingTest extends CiviUnitTestCase { ]; $form = $this->getFormObject('CRM_Mailing_Form_Task_AdhocMailing', $formValues, 'Builder'); $form->setAction(CRM_Core_Action::PROFILE); + $form->set('formValues', $formValues); + $form->set('isSearchBuilder', 1); + $form->set('context', 'builder'); try { $form->preProcess(); } @@ -71,7 +77,7 @@ class CRM_Mailing_Form_Task_AdHocMailingTest extends CiviUnitTestCase { // Nothing to see here. } $savedSearch = $this->callAPISuccessGetSingle('SavedSearch', []); - $this->assertEquals(['bla'], $savedSearch); + $this->assertEquals($formValues, $savedSearch['form_values']); } } -- 2.25.1