CRM-21466 follow up, add unit test to ensure custom fields can be populated.
authoreileen <emcnaughton@wikimedia.org>
Wed, 22 Nov 2017 23:13:10 +0000 (12:13 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 4 Dec 2017 02:47:06 +0000 (15:47 +1300)
This involves fixing the caching to be flushed during testing

CRM/Core/OptionGroup.php
CRM/Core/PseudoConstant.php
CRM/Utils/Token.php
tests/phpunit/api/v3/ContactTest.php

index 89c8eb13d97ae630122f725bb287e2cf6ae889a5..250c93cff309723831aec292d907c1066d97aec0 100644 (file)
@@ -676,6 +676,12 @@ WHERE  v.option_group_id = g.id
     );
   }
 
+  /**
+   * Flush all the places where option values are cached.
+   *
+   * Note that this is called from CRM_Core_PseudoConstant::flush() so we should resist
+   * the intuitive urge to flush that class.
+   */
   public static function flushAll() {
     self::$_values = array();
     self::$_cache = array();
index 5005b0de97d70c7ad9324326cabde89b62cbb9a6..96fa65536e34992467a1eaf15d0ca4e31a338e15 100644 (file)
@@ -588,6 +588,9 @@ class CRM_Core_PseudoConstant {
     }
     if ($name == 'cache') {
       CRM_Core_OptionGroup::flushAll();
+      if (isset(\Civi::$statics[__CLASS__])) {
+        unset(\Civi::$statics[__CLASS__]);
+      }
     }
   }
 
index 1e71d311ec39cf813b648a06497aeb125970e8bc..be56cce19665da11b249222d95c7a57d4361ee35 100644 (file)
@@ -660,17 +660,14 @@ class CRM_Utils_Token {
     $returnBlankToken = FALSE,
     $escapeSmarty = FALSE
   ) {
-    $key = 'contact';
-    if (self::$_tokens[$key] == NULL) {
-      // This should come from UF
-
-      self::$_tokens[$key]
-        = array_merge(
-          array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
-          array('checksum', 'contact_id')
-        );
-    }
+    // Refresh contact tokens in case they have changed. There is heavy caching
+    // in exportable fields so there is no benefit in doing this conditionally.
+    self::$_tokens['contact'] = array_merge(
+      array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
+      array('checksum', 'contact_id')
+    );
 
+    $key = 'contact';
     // here we intersect with the list of pre-configured valid tokens
     // so that we remove anything we do not recognize
     // I hope to move this step out of here soon and
index 74a88eeb1fdbaa7d17342999f3ce2a87b0cdc6cc..08118db788005f399683b81a5d7c1d91bb7d4bba 100644 (file)
@@ -3601,4 +3601,30 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->assertEquals('Alan\'s Show', $contact['addressee_display']);
   }
 
+  /**
+   * Test that creating a contact with various contact greetings works.
+   */
+  public function testContactGreetingsCreateWithCustomField() {
+    $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
+    $contact = $this->callAPISuccess('Contact', 'create', array('first_name' => 'Alan', 'contact_type' => 'Individual', 'custom_' . $ids['custom_field_id'] => 'Mice'));
+
+    // Change postal greeting to involve a custom field.
+    $postalOption = $this->callAPISuccessGetSingle('OptionValue', array('option_group_id' => 'postal_greeting', 'filter' => 1, 'is_default' => 1));
+    $this->callAPISuccess('OptionValue', 'create', array(
+      'id' => $postalOption['id'],
+      'name' => 'Dear {contact.first_name} {contact.custom_' . $ids['custom_field_id'] . '}',
+      'label' => 'Dear {contact.first_name} {contact.custom_' . $ids['custom_field_id'] . '}',
+    ));
+
+    // Update contact & see if postal greeting now reflects the new string.
+    $this->callAPISuccess('Contact', 'create', array('id' => $contact['id'], 'last_name' => 'MouseyMousey'));
+    $contact = $this->callAPISuccessGetSingle('Contact', array('id' => $contact['id'], 'return' => 'postal_greeting'));
+    $this->assertEquals('Dear Alan Mice', $contact['postal_greeting_display']);
+
+    //Cleanup
+    $this->callAPISuccess('OptionValue', 'create', array('id' => $postalOption['id'], 'name' => 'Dear {contact.first_name}'));
+    $this->customFieldDelete($ids['custom_field_id']);
+    $this->customGroupDelete($ids['custom_group_id']);
+  }
+
 }