Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test on updateGreeting & remove deprecated fn call #22482

Merged
merged 1 commit into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add unit test on updateGreeting & remove deprecated fn call
The call to tokenDetails loads the contact details and they are passed along - however
if not loaded here they will be loaded later so there is no gain & it
calls a deprecated function
  • Loading branch information
eileenmcnaughton committed Jan 12, 2022
commit a7dfdd1fc8706281d67857ea6ddb83f2c6949937
11 changes: 3 additions & 8 deletions CRM/Contact/BAO/Contact/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1034,17 +1034,12 @@ public static function updateGreeting($params) {
}

if (empty($filterContactFldIds)) {
$greetingDetails = [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if greetingDetails & filterContactFldIds are empty it is the same as returning as the following code is only invoked if they are not empty

$filterContactFldIds[] = 0;
}
else {
// we do token replacement in the replaceGreetingTokens hook
[$greetingDetails] = CRM_Utils_Token::getTokenDetails(array_keys($filterContactFldIds), $greetingsReturnProperties, FALSE, FALSE);
return;
}
// perform token replacement and build update SQL
$contactIds = [];
$cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id ";
foreach ($greetingDetails as $contactID => $contactDetails) {
foreach (array_keys($filterContactFldIds) as $contactID) {
if (!$processAll &&
!array_key_exists($contactID, $filterContactFldIds)
) {
Expand All @@ -1065,7 +1060,7 @@ public static function updateGreeting($params) {
}
}

self::processGreetingTemplate($greetingString, $contactDetails, $contactID, 'CRM_UpdateGreeting');
self::processGreetingTemplate($greetingString, [], $contactID, 'CRM_UpdateGreeting');
$greetingString = CRM_Core_DAO::escapeString($greetingString);
$cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";

Expand Down
15 changes: 13 additions & 2 deletions tests/phpunit/api/v3/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Api4\Contact;

/**
* Class api_v3_JobTest
*
Expand Down Expand Up @@ -180,15 +182,24 @@ public function testDelete(): void {
/**
* Test greeting update job.
*
* Note that this test is about testing the metadata / calling of the function & doesn't test the success of the called function
* Note that this test is about testing the metadata / calling of the
* function & doesn't test the success of the called function
*
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public function testCallUpdateGreetingSuccess(): void {
$contactID = $this->individualCreate();
// Clear out the postal greeting
CRM_Core_DAO::executeQuery('UPDATE civicrm_contact SET postal_greeting_display = NULL WHERE id = ' . $contactID);
$this->callAPISuccess($this->_entity, 'update_greeting', [
'gt' => 'postal_greeting',
'ct' => 'Individual',
]);
$this->assertEquals('Dear Anthony', Contact::get()
->addWhere('id', '=', $contactID)
->addSelect('postal_greeting_display')
->execute()->first()['postal_greeting_display']
);
}

/**
Expand Down