From 78ffc4d7e909613def26eff17879d382dbc50831 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 6 Sep 2021 21:29:50 +1200 Subject: [PATCH] dev/core#2817 Add CaseToken processor Note this isn't quite passing yet - it highlights that the pre-existing case tokens do different stuff with date fields. Thinking --- CRM/Case/Tokens.php | 29 +++++++++++++++ CRM/Core/EntityTokens.php | 5 ++- CRM/Core/SelectValues.php | 2 +- CRM/Utils/Token.php | 4 +-- Civi/Core/Container.php | 2 +- .../CRM/Utils/TokenConsistencyTest.php | 36 ++++++++++++++++--- 6 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 CRM/Case/Tokens.php diff --git a/CRM/Case/Tokens.php b/CRM/Case/Tokens.php new file mode 100644 index 0000000000..490453c9d1 --- /dev/null +++ b/CRM/Case/Tokens.php @@ -0,0 +1,29 @@ + 'Case End Date', '{case.details}' => 'Details', '{case.status_id:label}' => 'Case Status', - '{case.is_deleted}' => 'Case is in the Trash', + '{case.is_deleted:label}' => 'Case is in the Trash', '{case.created_date}' => 'Created Date', '{case.modified_date}' => 'Modified Date', ]; diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 6f065a1e14..46990bed09 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1633,7 +1633,7 @@ class CRM_Utils_Token { return implode(', ', $ret); } // Format date fields - elseif ($entityArray[$token] && $fieldType == CRM_Utils_Type::T_DATE) { + elseif ($entityArray[$token] && in_array($fieldType, [CRM_Utils_Type::T_DATE, CRM_Utils_Type::T_TIMESTAMP, (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME)])) { return CRM_Utils_Date::customFormat($entityArray[$token]); } return implode(', ', (array) $entityArray[$token]); @@ -1829,7 +1829,7 @@ class CRM_Utils_Token { $customTokens = []; $tokenName = $usedForTokenWidget ? "{contribution.custom_%d}" : "custom_%d"; foreach (CRM_Core_BAO_CustomField::getFields($entity) as $id => $info) { - $customTokens[sprintf($tokenName, $id)] = $info['label']; + $customTokens[sprintf($tokenName, $id)] = $info['label'] . ' :: ' . $info['groupTitle']; } return $customTokens; diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 96b85dbd4f..1673f35c27 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -327,7 +327,7 @@ class Container { [] ))->addTag('kernel.event_subscriber')->setPublic(TRUE); - foreach (['Activity', 'Contribute', 'Event', 'Mailing', 'Member'] as $comp) { + foreach (['Activity', 'Contribute', 'Event', 'Mailing', 'Member', 'Case'] as $comp) { $container->setDefinition('crm_' . strtolower($comp) . '_tokens', new Definition( "CRM_{$comp}_Tokens", [] diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index 662bbe9323..d6d9e3274d 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -62,15 +62,41 @@ class CRM_Utils_TokenConsistencyTest extends CiviUnitTestCase { $tokens = CRM_Core_SelectValues::caseTokens(); $this->assertEquals($this->getCaseTokens(), $tokens); $caseID = $this->getCaseID(); - $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, implode("\n", array_keys($this->getCaseTokens())), ['case' => $this->getCaseTokenKeys()]); + $tokenString = implode("\n", array_keys($this->getCaseTokens())); + $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, $tokenString, ['case' => $this->getCaseTokenKeys()]); $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml); // Now do the same without passing in 'knownTokens' - $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, implode("\n", array_keys($this->getCaseTokens()))); + $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, $tokenString); $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml); // And check our deprecated tokens still work. $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, '{case.case_type_id} {case.status_id}'); $this->assertEquals('Housing Support Ongoing', $tokenHtml); + + $additionalTokensFromProcessor = [ + '{case.case_type_id}' => 'Case Type ID', + '{case.status_id}' => 'Case Status', + '{case.case_type_id:name}' => 'Machine name: Case Type', + '{case.status_id:name}' => 'Machine name: Case Status', + ]; + $expectedTokens = array_merge($this->getCaseTokens(), $additionalTokensFromProcessor); + + $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ + 'controller' => __CLASS__, + 'smarty' => FALSE, + 'schema' => ['caseId'], + ]); + $this->assertEquals($expectedTokens, $tokenProcessor->listTokens()); + $tokenProcessor->addRow([ + 'caseId' => $this->getCaseID(), + ]); + $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); + + $tokenProcessor->evaluate(); + foreach ($tokenProcessor->getRows() as $row) { + $text = $row->render('html'); + } + $this->assertEquals($this->getExpectedCaseTokenOutput(), $text); } /** @@ -87,8 +113,8 @@ July 26th, 2021 case details Ongoing No -' . $this->case['created_date'] . ' -' . $this->case['modified_date'] . ' +' . CRM_Utils_Date::customFormat($this->case['created_date']) . ' +' . CRM_Utils_Date::customFormat($this->case['modified_date']) . ' '; } @@ -129,7 +155,7 @@ No '{case.end_date}' => 'Case End Date', '{case.details}' => 'Details', '{case.status_id:label}' => 'Case Status', - '{case.is_deleted}' => 'Case is in the Trash', + '{case.is_deleted:label}' => 'Case is in the Trash', '{case.created_date}' => 'Created Date', '{case.modified_date}' => 'Modified Date', '{case.custom_1}' => 'Enter text here :: Group with field text', -- 2.25.1