From 7c990617d04cdb233309f428d94e4be3bf3b57e3 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 23 Nov 2017 12:13:10 +1300 Subject: [PATCH] CRM-21466 follow up, add unit test to ensure custom fields can be populated. This involves fixing the caching to be flushed during testing --- CRM/Core/OptionGroup.php | 6 ++++++ CRM/Core/PseudoConstant.php | 3 +++ CRM/Utils/Token.php | 17 +++++++---------- tests/phpunit/api/v3/ContactTest.php | 26 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/CRM/Core/OptionGroup.php b/CRM/Core/OptionGroup.php index 89c8eb13d97a..250c93cff309 100644 --- a/CRM/Core/OptionGroup.php +++ b/CRM/Core/OptionGroup.php @@ -676,6 +676,12 @@ public static function flush($name, $params = array()) { ); } + /** + * 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(); diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 5005b0de97d7..96fa65536e34 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -588,6 +588,9 @@ public static function flush($name = 'cache') { } if ($name == 'cache') { CRM_Core_OptionGroup::flushAll(); + if (isset(\Civi::$statics[__CLASS__])) { + unset(\Civi::$statics[__CLASS__]); + } } } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 1e71d311ec39..be56cce19665 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -660,17 +660,14 @@ public static function &replaceContactTokens( $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 diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 74a88eeb1fdb..08118db78800 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3601,4 +3601,30 @@ public function testContactGreetingsCreate() { $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']); + } + }