From 6543dfe601b0c5ac58b2062d8fe5d409c1a9d313 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 8 Nov 2022 17:45:50 +1300 Subject: [PATCH] dev/core#3962 Add 'boolean' as a filter for tokens Add unit tests leveraging & demo-ing boolean fitler in greetings --- Civi/Token/TokenProcessor.php | 6 +- .../phpunit/Civi/Token/TokenProcessorTest.php | 8 ++- tests/phpunit/api/v3/JobTest.php | 65 +++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 87290e4aec..1ed89df5fc 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -494,6 +494,10 @@ class TokenProcessor { case 'lower': return mb_strtolower($value); + case 'boolean': + // Cast to 0 or 1 for use in text. + return (int) ((bool) $value); + case 'crmDate': if ($value instanceof \DateTime) { // @todo cludgey. @@ -505,7 +509,7 @@ class TokenProcessor { } default: - throw new \CRM_Core_Exception("Invalid token filter: " . json_encode($filter, JSON_UNESCAPED_SLASHES)); + throw new \CRM_Core_Exception('Invalid token filter: ' . json_encode($filter, JSON_UNESCAPED_SLASHES)); } } diff --git a/tests/phpunit/Civi/Token/TokenProcessorTest.php b/tests/phpunit/Civi/Token/TokenProcessorTest.php index 9279978288..b606ca4c28 100644 --- a/tests/phpunit/Civi/Token/TokenProcessorTest.php +++ b/tests/phpunit/Civi/Token/TokenProcessorTest.php @@ -341,14 +341,18 @@ class TokenProcessorTest extends \CiviUnitTestCase { $this->assertEquals(1, $this->counts['onEvalTokens']); } - public function testFilter() { + public function testFilter(): void { $exampleTokens['foo_bar']['whiz_bang'] = 'Some Text'; + $exampleTokens['foo_bar']['whiz_bop'] = ''; $exampleMessages = [ 'This is {foo_bar.whiz_bang}.' => 'This is Some Text.', 'This is {foo_bar.whiz_bang|lower}...' => 'This is some text...', 'This is {foo_bar.whiz_bang|upper}!' => 'This is SOME TEXT!', + 'This is {foo_bar.whiz_bang|boolean}!' => 'This is 1!', + 'This is {foo_bar.whiz_bop|boolean}!' => 'This is 0!', ]; - $expectExampleCount = /* {#msgs} x {smarty:on,off} */ 6; + // We expect 5 messages to be parsed 2 times each - ie 10 times. + $expectExampleCount = 10; $actualExampleCount = 0; foreach ($exampleMessages as $inputMessage => $expectOutput) { diff --git a/tests/phpunit/api/v3/JobTest.php b/tests/phpunit/api/v3/JobTest.php index 419fe607df..237a2bad76 100644 --- a/tests/phpunit/api/v3/JobTest.php +++ b/tests/phpunit/api/v3/JobTest.php @@ -27,6 +27,15 @@ use Civi\Api4\Contact; */ class api_v3_JobTest extends CiviUnitTestCase { + /** + * Entities to return to their original values during tearDown. + * + * The array is keyed by EntityName. + * + * @var array + */ + private $originalValues = []; + /** * Set up for tests. */ @@ -50,6 +59,12 @@ class api_v3_JobTest extends CiviUnitTestCase { public function tearDown(): void { $this->quickCleanUpFinancialEntities(); $this->quickCleanup(['civicrm_contact', 'civicrm_address', 'civicrm_email', 'civicrm_website', 'civicrm_phone', 'civicrm_job', 'civicrm_action_log', 'civicrm_action_schedule', 'civicrm_group', 'civicrm_group_contact'], TRUE); + $this->quickCleanup(['civicrm_contact', 'civicrm_address', 'civicrm_email', 'civicrm_website', 'civicrm_phone'], TRUE); + foreach ($this->originalValues as $entity => $entities) { + foreach ($entities as $values) { + $this->callAPISuccess($entity, 'create', $values); + } + } parent::tearDown(); } @@ -103,6 +118,56 @@ class api_v3_JobTest extends CiviUnitTestCase { $this->assertAPIDeleted('Job', $createResult['id']); } + /** + * Test processing strings with boolean's in them. + * + * e.g {if {contact.first_name|boolean} + * + * @dataProvider dataProviderNamesAndGreetings + */ + public function testUpdateGreetingBooleanToken($params, $expectedEmailGreeting): void { + $this->setEmailGreetingTemplateToConditional(); + $contactID = $this->individualCreate($params); + $this->assertEquals($expectedEmailGreeting, Contact::get()->addSelect('email_greeting_display')->addWhere('id', '=', $contactID)->execute()->first()['email_greeting_display']); + } + + /** + * Data provider for testing email greeting template. + */ + public function dataProviderNamesAndGreetings(): array { + return [ + [ + 'params' => ['first_name' => 'Anthony'], + 'expected' => 'Dear Anthony', + ], + [ + 'params' => ['first_name' => ''], + 'expected' => 'Dear Friend', + ], + [ + // This isn't really an issue with the |boolean provider + // but it would be without it - https://lab.civicrm.org/dev/core/-/issues/3962 + 'params' => ['first_name' => "O'Shea"], + 'expected' => "Dear O'Shea", + ], + ]; + } + + /** + * Set the Individual email template to use {if {contact.first_name|boolean}. + */ + protected function setEmailGreetingTemplateToConditional(): void { + $this->originalValues['OptionValue']['email'] = reset($this->callAPISuccess('OptionValue', 'get', [ + 'option_group_id' => 'email_greeting', + 'is_default' => TRUE, + 'filter' => 1, + ])['values']); + $this->callAPISuccess('OptionValue', 'create', [ + 'id' => $this->originalValues['OptionValue']['email']['id'], + 'label' => '{if {contact.first_name|boolean}}Dear {contact.first_name}{else}Dear Friend{/if}', + ]); + } + /** * Test greeting update job. * -- 2.25.1