From f3f321c75b4a90ec7afa743d7b9b6ae2389109f5 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 21 May 2022 18:00:57 +1200 Subject: [PATCH] Greeting handling - if email_greeting_custom (etc) isset & email_greeting_id is not, set to 'Customized' --- CRM/Contact/BAO/Contact.php | 3 +++ CRM/Contact/Import/Parser/Contact.php | 27 ++----------------- tests/phpunit/api/v3/ContactTest.php | 37 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index c363b4a88c..1c5919312e 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -506,6 +506,9 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact implements Civi\Co $missingGreetingParams = []; foreach ($allGreetingParams as $greetingIndex => $greetingParam) { + if (!empty($params[$greetingIndex . '_custom']) && empty($params[$greetingParam])) { + $params[$greetingParam] = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Contact', $greetingParam, 'Customized'); + } // An empty string means NULL if (($params[$greetingParam] ?? NULL) === '') { $params[$greetingParam] = 'null'; diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 2364e68631..8f8a2b191c 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -892,19 +892,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address'); $customFields = $customFields + $addressCustomFields; - //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575 - $elements = [ - 'email_greeting_custom' => 'email_greeting', - 'postal_greeting_custom' => 'postal_greeting', - 'addressee_custom' => 'addressee', - ]; - foreach ($elements as $k => $v) { - if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) { - $label = key(CRM_Core_OptionGroup::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"')); - $params[$v] = $label; - } - } - //format date first $session = CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); @@ -1600,18 +1587,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { if (array_key_exists($key, $locationFields)) { continue; } - if (in_array($key, [ - 'email_greeting', - 'postal_greeting', - 'addressee', - ])) { - // CRM-4575, need to null custom - if ($params["{$key}_id"] != 4) { - $params["{$key}_custom"] = 'null'; - } - unset($params[$key]); - } - else { + + if (1) { if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) { $custom_params = ['id' => $contact['id'], 'return' => $key]; $getValue = civicrm_api3('Contact', 'getvalue', $custom_params); diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 3062f66b89..ffaf9cb730 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -2399,6 +2399,43 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->assertEquals(date('Y-m-d', strtotime('first day of next month -5 years')), $result['values'][$contact2['id']]['birth_date']); } + /** + * Test the greeting fields update sensibly. + */ + public function testGreetingUpdates(): void { + $contactID = $this->individualCreate(); + $greetingFields = ['email_greeting_id:name', 'email_greeting_display', 'email_greeting_custom']; + $currentGreetings = $this->callAPISuccessGetSingle('Contact', ['id' => $contactID, 'version' => 4, 'return' => $greetingFields]); + $this->assertEquals('Dear {contact.first_name}', $currentGreetings['email_greeting_id:name']); + // Change to customized greeting. + $this->callAPISuccess('Contact', 'create', [ + 'id' => $contactID, + 'email_greeting_id' => 'Customized', + 'email_greeting_custom' => 'Howdy', + ]); + $currentGreetings = $this->callAPISuccessGetSingle('Contact', ['version' => 4, 'id' => $contactID, 'return' => $greetingFields]); + $this->assertEquals('Customized', $currentGreetings['email_greeting_id:name']); + $this->assertEquals('Howdy', $currentGreetings['email_greeting_custom']); + $this->assertEquals('Howdy', $currentGreetings['email_greeting_display']); + + // Change back to standard, check email_greeting_custom set to NULL. + $this->callAPISuccess('Contact', 'create', [ + 'id' => $contactID, + 'email_greeting_id' => 'Dear {contact.first_name}', + ]); + $currentGreetings = $this->callAPISuccessGetSingle('Contact', ['id' => $contactID, 'version' => 4, 'return' => $greetingFields]); + $this->assertNull($currentGreetings['email_greeting_custom']); + + $this->callAPISuccess('Contact', 'create', [ + 'id' => $contactID, + 'email_greeting_custom' => 'Howdy', + ]); + $currentGreetings = $this->callAPISuccessGetSingle('Contact', ['version' => 4, 'id' => $contactID, 'return' => $greetingFields]); + $this->assertEquals('Customized', $currentGreetings['email_greeting_id:name']); + $this->assertEquals('Howdy', $currentGreetings['email_greeting_custom']); + $this->assertEquals('Howdy', $currentGreetings['email_greeting_display']); + } + /** * Test Address parameters * -- 2.25.1