From e5a7272253610b95967673a1da7c65ac965872f8 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/BAO/OptionValue.php | 2 +- CRM/Core/OptionGroup.php | 13 +++++++++++++ CRM/Core/PseudoConstant.php | 14 +++++++------- CRM/Utils/Token.php | 17 +++++++---------- tests/phpunit/api/v3/ContactTest.php | 26 ++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/CRM/Core/BAO/OptionValue.php b/CRM/Core/BAO/OptionValue.php index 9bb61d054388..238a85a2e825 100644 --- a/CRM/Core/BAO/OptionValue.php +++ b/CRM/Core/BAO/OptionValue.php @@ -234,7 +234,7 @@ public static function add(&$params, $ids = array()) { $optionValue->id = CRM_Utils_Array::value('optionValue', $ids); $optionValue->save(); - CRM_Core_PseudoConstant::flush(); + CRM_Core_OptionGroup::flushAll(); return $optionValue; } diff --git a/CRM/Core/OptionGroup.php b/CRM/Core/OptionGroup.php index 89c8eb13d97a..9f995aa51d07 100644 --- a/CRM/Core/OptionGroup.php +++ b/CRM/Core/OptionGroup.php @@ -676,10 +676,23 @@ public static function flush($name, $params = array()) { ); } + /** + * Flush all the places where option values are cached. + */ public static function flushAll() { + CRM_Core_OptionGroup::flushCachedGreetings(); self::$_values = array(); self::$_cache = array(); CRM_Utils_Cache::singleton()->flush(); } + /** + * Flush any places where greeting strings are cached. + */ + public static function flushCachedGreetings() { + if (isset(Civi::$statics['CRM_Core_PseudoConstant']['greeting'])) { + unset(Civi::$statics['CRM_Core_PseudoConstant']['greeting']); + } + } + } diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 419664441890..36b69f4e4cf2 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -1650,6 +1650,10 @@ public static function countryIDForStateID($stateID) { * array reference of all greetings. */ public static function greeting($filter, $columnName = 'label') { + if (!isset(Civi::$statics[__CLASS__]['greeting'])) { + Civi::$statics[__CLASS__]['greeting'] = array(); + } + $index = $filter['greeting_type'] . '_' . $columnName; // also add contactType to the array @@ -1658,11 +1662,7 @@ public static function greeting($filter, $columnName = 'label') { $index .= '_' . $contactType; } - if (NULL === self::$greeting) { - self::$greeting = array(); - } - - if (!CRM_Utils_Array::value($index, self::$greeting)) { + if (!CRM_Utils_Array::value($index, Civi::$statics[__CLASS__]['greeting'])) { $filterCondition = NULL; if ($contactType) { $filterVal = 'v.filter ='; @@ -1682,10 +1682,10 @@ public static function greeting($filter, $columnName = 'label') { $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) "; } - self::$greeting[$index] = CRM_Core_OptionGroup::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName); + Civi::$statics[__CLASS__]['greeting'][$index] = CRM_Core_OptionGroup::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName); } - return self::$greeting[$index]; + return Civi::$statics[__CLASS__]['greeting'][$index]; } /** diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 7bd729b2b0cc..397a6aef90e0 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -657,17 +657,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']); + } + }