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 Dec 4, 2017
1 parent 0db8cd8 commit 7c99061
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CRM/Core/OptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions CRM/Core/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__]);
}
}
}

Expand Down
17 changes: 7 additions & 10 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 7c99061

Please sign in to comment.