Skip to content

Commit

Permalink
CRM-21466 follow up, add unit test to ensure custom fields can be pop…
Browse files Browse the repository at this point in the history
…ulated.

This involves fixing the caching to be flushed during testing
  • Loading branch information
eileenmcnaughton committed Nov 23, 2017
1 parent fcff378 commit e5a7272
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/BAO/OptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions CRM/Core/OptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

}
14 changes: 7 additions & 7 deletions CRM/Core/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =';
Expand All @@ -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];
}

/**
Expand Down
17 changes: 7 additions & 10 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

}

0 comments on commit e5a7272

Please sign in to comment.