dev/core#2817 Add CaseToken processor
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 6 Sep 2021 09:29:50 +0000 (21:29 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 10 Sep 2021 04:02:46 +0000 (16:02 +1200)
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 [new file with mode: 0644]
CRM/Core/EntityTokens.php
CRM/Core/SelectValues.php
CRM/Utils/Token.php
Civi/Core/Container.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

diff --git a/CRM/Case/Tokens.php b/CRM/Case/Tokens.php
new file mode 100644 (file)
index 0000000..490453c
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Case_Tokens
+ *
+ * Generate "case.*" tokens.
+ */
+class CRM_Case_Tokens extends CRM_Core_EntityTokens {
+
+  /**
+   * Get the entity name for api v4 calls.
+   *
+   * @return string
+   */
+  protected function getApiEntityName(): string {
+    return 'Case';
+  }
+
+}
index 8841dcdb6e12a21e2630f3c9b1175539529e7f9c..ad86cd6d5be5aa05869e3cabf40fa820084e323a 100644 (file)
@@ -383,7 +383,10 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber {
    * @return string[]
    */
   public function getSkippedFields(): array {
-    $fields = ['contact_id'];
+    // tags is offered in 'case' & is one of the only fields that is
+    // 'not a real field' offered up by case - seems like an oddity
+    // we should skip at the top level for now.
+    $fields = ['contact_id', 'tags'];
     if (!CRM_Campaign_BAO_Campaign::isCampaignEnable()) {
       $fields[] = 'campaign_id';
     }
index bb84dd5de4bb0e69097ac0298e2b74675b91a811..390fab086de41d8a88631136429f13f18ee9f418 100644 (file)
@@ -707,7 +707,7 @@ class CRM_Core_SelectValues {
         '{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',
       ];
index 6f065a1e147c9e830fdbfdd94b9d248f0dbfb5b7..46990bed095f7fdeb8f1c2517d915b13e05d22ff 100644 (file)
@@ -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;
index 96b85dbd4fbd505d8c1c7c40713bde7e28190b3b..1673f35c27abae0b6b99cfc55d59ad31bc5bf450 100644 (file)
@@ -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",
         []
index 662bbe9323f8f038ce57fad53af4022ae370345e..d6d9e3274d76a0d26291cd80341a731215a66fc7 100644 (file)
@@ -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',