From b4a0d8e7be44ebac9e53c6f5d32e2dd07a348f73 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 22 Dec 2016 11:50:43 -0700 Subject: [PATCH] CRM-19690 - CRM_Mailing_TokensTest - Define negative test scenario --- tests/phpunit/CRM/Mailing/TokensTest.php | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/phpunit/CRM/Mailing/TokensTest.php b/tests/phpunit/CRM/Mailing/TokensTest.php index 5461e69544..2cbd3124be 100644 --- a/tests/phpunit/CRM/Mailing/TokensTest.php +++ b/tests/phpunit/CRM/Mailing/TokensTest.php @@ -101,4 +101,35 @@ class CRM_Mailing_TokensTest extends \CiviUnitTestCase { $this->assertEquals(1, $count); } + /** + * Check the behavior in the erroneous situation where someone uses + * a mailing-related token without providing a mailing ID. + */ + 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}!'; + + $mailing = CRM_Core_DAO::createTestObject('CRM_Mailing_DAO_Mailing', array( + 'name' => 'Example Name', + )); + $contact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_Contact'); + + $p = new \Civi\Token\TokenProcessor(Civi::service('dispatcher'), array( + 'mailing' => $mailing, + )); + $p->addMessage('example', $inputTemplate, $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()); + } + } + } -- 2.25.1