From 0b3cb19d8129f95b1544e96494e522fc6a61b95c Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 9 May 2017 20:21:23 +1200 Subject: [PATCH] CRM-19876 add test to try to replicate token issue --- CRM/Utils/Token.php | 10 +---- tests/phpunit/CRM/Utils/Token.php | 62 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 124394cd2e..257a1b1893 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1184,11 +1184,7 @@ class CRM_Utils_Token { $className = NULL, $jobID = NULL ) { - if (empty($contactIDs)) { - // putting a fatal here so we can track if/when this happens - CRM_Core_Error::fatal(); - } - // @todo this functions needs unit tests. + $params = array(); foreach ($contactIDs as $key => $contactID) { $params[] = array( @@ -1239,11 +1235,9 @@ class CRM_Utils_Token { } } - //get the total number of contacts to fetch from database. - $numberofContacts = count($contactIDs); $query = new CRM_Contact_BAO_Query($params, $returnProperties); - $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); + $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, count($contactIDs)); $contactDetails = &$details[0]; diff --git a/tests/phpunit/CRM/Utils/Token.php b/tests/phpunit/CRM/Utils/Token.php index 5010669761..04e7394d98 100644 --- a/tests/phpunit/CRM/Utils/Token.php +++ b/tests/phpunit/CRM/Utils/Token.php @@ -15,4 +15,66 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { $this->assertEquals('Phone, Fax', $resolvedTokens[0][$contactID]['preferred_communication_method']); } + /** + * Test getting multiple contacts. + * + * Check for situation described in CRM-19876. + */ + public function testGetTokenDetailsMultipleEmails() { + $i = 0; + + $params = array( + 'do_not_phone' => 1, + 'do_not_email' => 0, + 'do_not_mail' => 1, + 'do_not_sms' => 1, + 'do_not_trade' => 1, + 'is_opt_out' => 0, + 'email' => 'guardians@galaxy.com', + 'legal_identifier' => 'convict 56', + 'nick_name' => 'bob', + 'contact_source' => 'bargain basement', + 'formal_title' => 'Your silliness', + 'job_title' => 'World Saviour', + 'gender_id' => '1', + 'birth_date' => '2017-01-01', + // 'city' => 'Metropolis', + ); + $contactIDs = array(); + while ($i < 27) { + $contactIDs[] = $contactID = $this->individualCreate($params); + $this->callAPISuccess('Email', 'create', array( + 'contact_id' => $contactID, + 'email' => 'goodguy@galaxy.com', + 'location_type_id' => 'Other', + 'is_primary' => 0, + )); + $this->callAPISuccess('Email', 'create', array( + 'contact_id' => $contactID, + 'email' => 'villain@galaxy.com', + 'location_type_id' => 'Work', + 'is_primary' => 1, + )); + $i++; + } + unset($params['email']); + + $resolvedTokens = CRM_Utils_Token::getTokenDetails($contactIDs); + foreach ($contactIDs as $contactID) { + $resolvedContactTokens = $resolvedTokens[0][$contactID]; + $this->assertEquals('Individual', $resolvedContactTokens['contact_type']); + $this->assertEquals('Anderson, Anthony', $resolvedContactTokens['sort_name']); + $this->assertEquals('en_US', $resolvedContactTokens['preferred_language']); + $this->assertEquals('Both', $resolvedContactTokens['preferred_mail_format']); + $this->assertEquals(3, $resolvedContactTokens['prefix_id']); + $this->assertEquals(3, $resolvedContactTokens['suffix_id']); + $this->assertEquals('Mr. Anthony J. Anderson II', $resolvedContactTokens['addressee_display']); + $this->assertEquals('villain@galaxy.com', $resolvedContactTokens['email']); + + foreach ($params as $key => $value) { + $this->assertEquals($value, $resolvedContactTokens[$key]); + } + } + } + } -- 2.25.1