Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ref] intial testing on case tokens, make knownTokens optional #21289

Merged
merged 1 commit into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -1633,10 +1633,13 @@ public static function replaceEntityTokens($entity, $entityArray, $str, $knownTo
* @return string
* @throws \CiviCRM_API3_Exception
*/
public static function replaceCaseTokens($caseId, $str, $knownTokens = [], $escapeSmarty = FALSE) {
if (!$knownTokens || empty($knownTokens['case'])) {
public static function replaceCaseTokens($caseId, $str, $knownTokens = NULL, $escapeSmarty = FALSE): string {
if (strpos($str, '{case.') === FALSE) {
return $str;
}
if (!$knownTokens) {
$knownTokens = self::getTokens($str);
}
$case = civicrm_api3('case', 'getsingle', ['id' => $caseId]);
return self::replaceEntityTokens('case', $case, $str, $knownTokens, $escapeSmarty);
}
Expand Down
150 changes: 150 additions & 0 deletions tests/phpunit/CRM/Utils/TokenConsistencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?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 |
+--------------------------------------------------------------------+
*/

/**
* CRM_Utils_TokenConsistencyTest
*
* Class for ensuring tokens have internal consistency.
*
* @group Tokens
*
* @group headless
*/
class CRM_Utils_TokenConsistencyTest extends CiviUnitTestCase {

use CRMTraits_Custom_CustomDataTrait;

/**
* Created case.
*
* @var array
*/
protected $case;

/**
* Post test cleanup.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function tearDown(): void {
$this->quickCleanup(['civicrm_case', 'civicrm_case_type']);
parent::tearDown();
}

/**
* Test that case tokens are consistently rendered.
*
* @throws \API_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testCaseTokenConsistency(): void {
$this->createLoggedInUser();
CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase');
$this->createCustomGroupWithFieldOfType(['extends' => 'Case']);
$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()]);
$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())));
$this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml);
}

/**
* Get expected output from token parsing.
*
* @return string
*/
protected function getExpectedCaseTokenOutput(): string {
return '1
Housing Support
Case Subject
July 23rd, 2021
July 26th, 2021
case details
Ongoing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case status "ongoing" when it has an end date is a little bit unrealistic, but technically possible. Otherwise I think this PR is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@demeritcowboy I just 'grabbed' 'status_id' => 1, - should it change or should we merge this & ignore

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems unlikely to be used in a way that would enforce that cases with status ongoing should have an end date (or vice-versa). So I'd say it's inconsistent but not "dangerous".

Also noticing that case.contact_id and case.client_id are valid tokens although don't seem to appear in the dropdown on a form. I'm not sure where the dropdown list comes from but I guess that dropdown would be the authority.

No
' . $this->case['modified_date'] . '
' . $this->case['created_date'] . '
';
}

/**
* @return int
*/
protected function getContactID(): int {
if (!isset($this->ids['Contact'][0])) {
$this->ids['Contact'][0] = $this->individualCreate();
}
return $this->ids['Contact'][0];
}

/**
* Get the keys for the case tokens.
*
* @return array
*/
public function getCaseTokenKeys(): array {
$return = [];
foreach (array_keys($this->getCaseTokens()) as $key) {
$return[] = substr($key, 6, -1);
}
return $return;
}

/**
* Get declared tokens.
*
* @return string[]
*/
public function getCaseTokens(): array {
return [
'{case.id}' => 'Case ID',
'{case.case_type_id}' => 'Case Type ID',
'{case.subject}' => 'Case Subject',
'{case.start_date}' => 'Case Start Date',
'{case.end_date}' => 'Case End Date',
'{case.details}' => 'Details',
'{case.status_id}' => 'Case Status',
'{case.is_deleted}' => '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',
];
}

/**
* Get case ID.
*
* @return int
*/
protected function getCaseID(): int {
if (!isset($this->case)) {
$this->case = $this->callAPISuccess('Case', 'create', [
'case_type_id' => 'housing_support',
'activity_subject' => 'Case Subject',
'client_id' => $this->getContactID(),
'status_id' => 1,
'subject' => 'Case Subject',
'start_date' => '2021-07-23 15:39:20',
'end_date' => '2021-07-26 18:07:20',
'medium_id' => 2,
'details' => 'case details',
'activity_details' => 'blah blah',
'sequential' => 1,
])['values'][0];
}
return $this->case['id'];
}

}