Merge pull request #23537 from eileenmcnaughton/greet_cust
authorcolemanw <coleman@civicrm.org>
Mon, 23 May 2022 20:44:54 +0000 (16:44 -0400)
committerGitHub <noreply@github.com>
Mon, 23 May 2022 20:44:54 +0000 (16:44 -0400)
Greeting handling - if email_greeting_custom (etc) isset & email_greeting_id is not, set to 'Customized'

CRM/Contact/BAO/Contact.php
CRM/Contact/Import/Parser/Contact.php
tests/phpunit/api/v3/ContactTest.php

index c363b4a88c5028c9720f53ed8c26bb567e3e20ac..1c5919312e9f952a44f94b5fcf7512e6d84367cc 100644 (file)
@@ -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';
index ba2336d279b26007b3fd7e54b2fe42eae8f2a19d..1975bba86cd5f822e07c16d8c27e3c9b2840998f 100644 (file)
@@ -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");
@@ -1995,18 +1982,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);
index 3062f66b89fb4c503fb3a36e06cd7cf373f733b0..ffaf9cb7309a56ece1e142af0d0f54a0aee6f479 100644 (file)
@@ -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
    *