From d02df737040373e76f395584290713aa466a94df Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 29 Apr 2019 21:26:15 -0700 Subject: [PATCH] CRM_Mailing_TokensTest - Update spec to match expected behavior The preceding commits revised the behavior of `{mailing.*}` and `{action.*}` when previewed via `TokenProcessor` (so that they match the preview logic in other cases). This simply changes the spec to match. --- tests/phpunit/CRM/Mailing/TokensTest.php | 47 ++++++++++++++++-------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/tests/phpunit/CRM/Mailing/TokensTest.php b/tests/phpunit/CRM/Mailing/TokensTest.php index 721f87f7c0..8b31fe906e 100644 --- a/tests/phpunit/CRM/Mailing/TokensTest.php +++ b/tests/phpunit/CRM/Mailing/TokensTest.php @@ -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: click here!', '@To opt out: 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); } } -- 2.25.1