Merge pull request #11712 from jitendrapurohit/CRM-21795
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
index c3360f6c4d86bc14457b250d4e9109b974afcb12..2ce5aa878081f03cc4decb2051fe193aa49e45a9 100644 (file)
@@ -3,7 +3,7 @@
  * +--------------------------------------------------------------------+
  * | CiviCRM version 4.7                                                |
  * +--------------------------------------------------------------------+
- * | Copyright CiviCRM LLC (c) 2004-2017                                |
+ * | Copyright CiviCRM LLC (c) 2004-2018                                |
  * +--------------------------------------------------------------------+
  * | This file is a part of CiviCRM.                                    |
  * |                                                                    |
@@ -475,4 +475,47 @@ class api_v3_CustomValueTest extends CiviUnitTestCase {
     $this->assertEquals('custom_value.id', $fields['custom_value.id']['name']);
   }
 
+  /**
+   * Test that custom fields in greeting strings are updated.
+   */
+  public function testUpdateCustomGreetings() {
+    // Create a custom group with one field.
+    $customGroupResult = $this->callAPISuccess('CustomGroup', 'create', array(
+      'sequential' => 1,
+      'title' => "test custom group",
+      'extends' => "Individual",
+    ));
+    $customFieldResult = $this->callAPISuccess('CustomField', 'create', array(
+      'custom_group_id' => $customGroupResult['id'],
+      'label' => "greeting test",
+      'data_type' => "String",
+      'html_type' => "Text",
+    ));
+    $customFieldId = $customFieldResult['id'];
+
+    // Create a contact with an email greeting format that includes the new custom field.
+    $contactResult = $this->callAPISuccess('Contact', 'create', array(
+      'contact_type' => 'Individual',
+      'email' => substr(sha1(rand()), 0, 7) . '@yahoo.com',
+      'email_greeting_id' => "Customized",
+      'email_greeting_custom' => "Dear {contact.custom_{$customFieldId}}",
+    ));
+    $cid = $contactResult['id'];
+
+    // Define testing values.
+    $uniq = uniqid();
+    $testGreetingValue = "Dear $uniq";
+
+    // Update contact's custom field with CustomValue.create
+    $customValueResult = $this->callAPISuccess('CustomValue', 'create', array(
+      'entity_id' => $cid,
+      "custom_{$customFieldId}" => $uniq,
+      'entity_table' => "civicrm_contact",
+    ));
+
+    $contact = $this->callAPISuccessGetSingle('Contact', array('id' => $cid, 'return' => 'email_greeting'));
+    $this->assertEquals($testGreetingValue, $contact['email_greeting_display']);
+
+  }
+
 }