Skip to content

Commit

Permalink
Pledge - Emit a clear warning if any templates use the old `{$contact…
Browse files Browse the repository at this point in the history
….*}`
  • Loading branch information
totten committed Oct 13, 2021
1 parent 5911b88 commit bbcd76b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CRM/Pledge/BAO/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ public static function sendAcknowledgment(&$form, $params) {
$domainValues[$token] = CRM_Core_DomainTokens::getDomainTokenValues()[$token];
}

$form->assign('contact', new CRM_Utils_GhostArray(function($op, $field) {
$badExpr = '{$contact.' . $field . '}';
CRM_Core_Error::deprecatedWarning("The automated message (\"pledge_acknowledgement\") attempted to use $badExpr. This is no longer supported. Please convert to a {contact.*} token.");
}));
$form->assign('domain', $domainValues);

// handle custom data.
Expand Down
39 changes: 36 additions & 3 deletions tests/phpunit/CRM/Pledge/Form/PledgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
class CRM_Pledge_Form_PledgeTest extends CiviUnitTestCase {

use \Civi\Test\ErrorTestTrait;

/**
* Test the post process function.
*
Expand All @@ -19,6 +21,40 @@ public function testPostProcess(): void {
$loggedInUser = $this->createLoggedInUser();
$this->swapMessageTemplateForInput('pledge_acknowledge', '{domain.name} {contact.first_name}');

$this->submitExamplePledge($loggedInUser);
$mut->checkAllMailLog(['Default Domain Name Anthony']);
$mut->clearMessages();
$this->revertTemplateToReservedTemplate('pledge_acknowledge');
}

/**
* Test the post process function with old/invalid data.
*
* TODO: Remove this test after a few cycles. Say, circa 5.48+?
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function testDeprecated(): void {
$mut = new CiviMailUtils($this);
$loggedInUser = $this->createLoggedInUser();
$this->swapMessageTemplateForInput('pledge_acknowledge', '({domain.name}) ({$contact.first_name})');

[$log] = $this->captureErrors(E_USER_DEPRECATED, function() use ($loggedInUser) {
$this->submitExamplePledge($loggedInUser);
});
$this->assertNotEmpty(preg_grep('/\$contact\.first_name.*is no longer supported/', $log));
$mut->checkAllMailLog(['(Default Domain Name) ()']);
$mut->clearMessages();
$this->revertTemplateToReservedTemplate('pledge_acknowledge');
}

/**
* @param int $loggedInUser
* @throws \API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
protected function submitExamplePledge(int $loggedInUser): void {
$form = $this->getFormObject('CRM_Pledge_Form_Pledge', [
'amount' => 10,
'installments' => 1,
Expand All @@ -32,9 +68,6 @@ public function testPostProcess(): void {
]);
$form->buildForm();
$form->postProcess();
$mut->checkAllMailLog(['Default Domain Name Anthony']);
$mut->clearMessages();
$this->revertTemplateToReservedTemplate('pledge_acknowledge');
}

}

0 comments on commit bbcd76b

Please sign in to comment.