X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCRM%2FUtils%2FTokenTest.php;h=450397298c5c7b9b49a42c29fdd43607cd161039;hb=d23a840949552b4781dde118914209bbb504a553;hp=795ce797f03cc1aa378afbe0f0ccecb1530eed3a;hpb=b71494d1e871ce2cca6db4902cb7e72daeb09ddc;p=civicrm-core.git diff --git a/tests/phpunit/CRM/Utils/TokenTest.php b/tests/phpunit/CRM/Utils/TokenTest.php index 795ce797f0..450397298c 100644 --- a/tests/phpunit/CRM/Utils/TokenTest.php +++ b/tests/phpunit/CRM/Utils/TokenTest.php @@ -10,8 +10,8 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { * Basic test on getTokenDetails function. */ public function testGetTokenDetails() { - $contactID = $this->individualCreate(array('preferred_communication_method' => array('Phone', 'Fax'))); - $resolvedTokens = CRM_Utils_Token::getTokenDetails(array($contactID)); + $contactID = $this->individualCreate(['preferred_communication_method' => ['Phone', 'Fax']]); + $resolvedTokens = CRM_Utils_Token::getTokenDetails([$contactID]); $this->assertEquals('Phone, Fax', $resolvedTokens[0][$contactID]['preferred_communication_method']); } @@ -27,26 +27,26 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { // create a contact with multiple email address and among which one is primary $contactID = $this->individualCreate(); $primaryEmail = uniqid() . '@primary.com'; - $this->callAPISuccess('Email', 'create', array( + $this->callAPISuccess('Email', 'create', [ 'contact_id' => $contactID, 'email' => $primaryEmail, 'location_type_id' => 'Other', 'is_primary' => 1, - )); - $this->callAPISuccess('Email', 'create', array( + ]); + $this->callAPISuccess('Email', 'create', [ 'contact_id' => $contactID, 'email' => uniqid() . '@galaxy.com', 'location_type_id' => 'Work', 'is_primary' => 0, - )); - $this->callAPISuccess('Email', 'create', array( + ]); + $this->callAPISuccess('Email', 'create', [ 'contact_id' => $contactID, 'email' => uniqid() . '@galaxy.com', 'location_type_id' => 'Work', 'is_primary' => 0, - )); + ]); - $contactIDs = array($contactID); + $contactIDs = [$contactID]; // when we are fetching contact details ON basis of primary address fields $contactDetails = CRM_Utils_Token::getTokenDetails($contactIDs); @@ -62,22 +62,27 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { */ public function testReplaceGreetingTokens() { $tokenString = 'First Name: {contact.first_name} Last Name: {contact.last_name} Birth Date: {contact.birth_date} Prefix: {contact.prefix_id} Suffix: {contact.individual_suffix}'; - $contactDetails = array( - array( - 2811 => array( + $contactDetails = [ + [ + 2811 => [ 'id' => '2811', 'contact_type' => 'Individual', 'first_name' => 'Morticia', 'last_name' => 'Addams', 'prefix_id' => 2, - ), - ), - ); + ], + ], + ]; $contactId = 2811; $className = 'CRM_Contact_BAO_Contact'; $escapeSmarty = TRUE; CRM_Utils_Token::replaceGreetingTokens($tokenString, $contactDetails, $contactId, $className, $escapeSmarty); $this->assertEquals($tokenString, 'First Name: Morticia Last Name: Addams Birth Date: Prefix: Ms. Suffix: '); + + // Test compatibility with custom tokens (#14943) + $tokenString = 'Custom {custom.custom}'; + CRM_Utils_Token::replaceGreetingTokens($tokenString, $contactDetails, $contactId, $className, $escapeSmarty); + $this->assertEquals($tokenString, 'Custom '); } /** @@ -88,7 +93,7 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { public function testGetTokenDetailsMultipleEmails() { $i = 0; - $params = array( + $params = [ 'do_not_phone' => 1, 'do_not_email' => 0, 'do_not_mail' => 1, @@ -104,22 +109,22 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { 'gender_id' => '1', 'birth_date' => '2017-01-01', // 'city' => 'Metropolis', - ); - $contactIDs = array(); + ]; + $contactIDs = []; while ($i < 27) { $contactIDs[] = $contactID = $this->individualCreate($params); - $this->callAPISuccess('Email', 'create', array( + $this->callAPISuccess('Email', 'create', [ 'contact_id' => $contactID, 'email' => 'goodguy@galaxy.com', 'location_type_id' => 'Other', 'is_primary' => 0, - )); - $this->callAPISuccess('Email', 'create', array( + ]); + $this->callAPISuccess('Email', 'create', [ 'contact_id' => $contactID, 'email' => 'villain@galaxy.com', 'location_type_id' => 'Work', 'is_primary' => 1, - )); + ]); $i++; } unset($params['email']); @@ -142,4 +147,53 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase { } } + /** + * This is a basic test of the token processor (currently testing TokenCompatSubscriber) + * and makes sure that greeting + contact tokens are replaced. + * This is a good example to copy/expand when creating additional tests for token processor + * in "real" situations. + * + * @throws \CRM_Core_Exception + */ + public function testTokenProcessor() { + $params['contact_id'] = $this->individualCreate(); + + // Prepare the processor and general context. + $tokenProc = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), [ + // Unique(ish) identifier for our controller/use-case. + 'controller' => 'civicrm_tokentest', + + // Provide hints about what data will be available for each row. + // Ex: 'schema' => ['contactId', 'activityId', 'caseId'], + 'schema' => ['contactId'], + + // Whether to enable Smarty evaluation. + 'smarty' => (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY), + ]); + + // Define message templates. + $tokenProc->addMessage('body_html', 'Good morning,

{contact.email_greeting} {contact.display_name}

. {custom.foobar} Bye!', 'text/html'); + $tokenProc->addMessage('body_text', 'Good morning, {contact.email_greeting} {contact.display_name} Bye!', 'text/plain'); + + $expect[$params['contact_id']]['html'] = 'Good morning,

Dear Anthony Mr. Anthony Anderson II

. Bye!'; + $expect[$params['contact_id']]['text'] = 'Good morning, Dear Anthony Mr. Anthony Anderson II Bye!'; + + // Define row data. + foreach (explode(',', $params['contact_id']) as $contactId) { + $context = ['contactId' => $contactId]; + $tokenProc->addRow()->context($context); + } + + $tokenProc->evaluate(); + + $this->assertNotEmpty($tokenProc->getRows()); + foreach ($tokenProc->getRows() as $tokenRow) { + /** @var \Civi\Token\TokenRow $tokenRow */ + $html = $tokenRow->render('body_html'); + $text = $tokenRow->render('body_text'); + $this->assertEquals($expect[$params['contact_id']]['html'], $html); + $this->assertEquals($expect[$params['contact_id']]['text'], $text); + } + } + }