Commit 9d6c8f83 authored by totten's avatar totten Committed by GitHub

Merge pull request #14156 from totten/5.13-mail-tokens

CiviMail - Restore support for preview of "mailing"/"action" tokens via TokenProcessor/Flexmailer
parents dab9c393 d02df737
......@@ -65,7 +65,8 @@ class CRM_Mailing_ActionTokens extends \Civi\Token\AbstractTokenSubscriber {
* @inheritDoc
*/
public function checkActive(\Civi\Token\TokenProcessor $processor) {
return !empty($processor->context['mailingId']) || !empty($processor->context['mailing']);
return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
|| in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
}
/**
......@@ -85,7 +86,9 @@ class CRM_Mailing_ActionTokens extends \Civi\Token\AbstractTokenSubscriber {
// replaceSubscribeInviteTokens().
if (empty($row->context['mailingJobId']) || empty($row->context['mailingActionTarget']['hash'])) {
throw new \CRM_Core_Exception("Error: Cannot use action tokens unless context defines mailingJobId and mailingActionTarget.");
// Strictly speaking, it doesn't make much sense to generate action-tokens when there's no job ID, but traditional CiviMail
// does this in v5.6+ for "Preview" functionality. Relaxing this strictness check ensures parity between newer+older styles.
// throw new \CRM_Core_Exception("Error: Cannot use action tokens unless context defines mailingJobId and mailingActionTarget.");
}
if ($field === 'eventQueueId') {
......
......@@ -61,7 +61,8 @@ class CRM_Mailing_Tokens extends \Civi\Token\AbstractTokenSubscriber {
* @inheritDoc
*/
public function checkActive(\Civi\Token\TokenProcessor $processor) {
return !empty($processor->context['mailingId']) || !empty($processor->context['mailing']);
return !empty($processor->context['mailingId']) || !empty($processor->context['mailing'])
|| in_array('mailingId', $processor->context['schema']) || in_array('mailing', $processor->context['schema']);
}
/**
......
......@@ -102,16 +102,25 @@ class CRM_Mailing_TokensTest extends \CiviUnitTestCase {
$this->assertEquals(1, $count);
}
public function getExampleTokensForUseWithoutMailingJob() {
$cases = [];
$cases[] = ['text/plain', 'To opt out: {action.optOutUrl}!', '@To opt out: .*civicrm/mailing/optout.*&jid=&qid=@'];
$cases[] = ['text/html', 'To opt out: <a href="{action.optOutUrl}">click here</a>!', '@To opt out: <a href=".*civicrm/mailing/optout.*&amp;jid=&amp;qid=.*">click@'];
return $cases;
}
/**
* Check the behavior in the erroneous situation where someone uses
* a mailing-related token without providing a mailing ID.
* When previewing a mailing, there is no active mailing job, so one cannot
* generate fully formed URLs which reference the job. The current behavior
* is to link to a placeholder URL which has blank values for key fields
* like `jid` and `qid`.
*
* This current behavior may be wise or unwise - either way, having ensures
* that changes are intentional.
*
* @dataProvider getExampleTokensForUseWithoutMailingJob
*/
public function testTokensWithoutMailing() {
// We only need one case to see that the mailing-object works as
// an alternative to the mailing-id.
$inputTemplateFormat = 'text/plain';
$inputTemplate = 'To optout: {action.optOutUrl}!';
public function testTokensWithoutMailingJob($inputTemplateFormat, $inputTemplateText, $expectRegex) {
$mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', array(
'name' => 'Example Name',
));
......@@ -120,17 +129,23 @@ class CRM_Mailing_TokensTest extends \CiviUnitTestCase {
$p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), array(
'mailing' => $mailing,
));
$p->addMessage('example', $inputTemplate, $inputTemplateFormat);
$p->addMessage('example', $inputTemplateText, $inputTemplateFormat);
$p->addRow()->context(array(
'contactId' => $contact->id,
));
try {
$p->evaluate();
$this->fail('TokenProcessor::evaluate() should have thrown an exception');
}
catch (CRM_Core_Exception $e) {
$this->assertRegExp(';Cannot use action tokens unless context defines mailingJobId and mailingActionTarget;', $e->getMessage());
}
// try {
// $p->evaluate();
// $this->fail('TokenProcessor::evaluate() should have thrown an exception');
// }
// catch (CRM_Core_Exception $e) {
// $this->assertRegExp(';Cannot use action tokens unless context defines mailingJobId and mailingActionTarget;', $e->getMessage());
// }
$p->evaluate();
// FIXME: For compatibility with
$actual = $p->getRow(0)->render('example');
$this->assertRegExp($expectRegex, $actual);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment