CRM-19690 - CRM_Mailing_TokensTest - Define negative test scenario
authorTim Otten <totten@civicrm.org>
Thu, 22 Dec 2016 18:50:43 +0000 (11:50 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 22 Dec 2016 18:50:43 +0000 (11:50 -0700)
tests/phpunit/CRM/Mailing/TokensTest.php

index 5461e695447d91701e2b35143ce699d6be3123a3..2cbd3124be749eb44fbf27482601fd7e39c8bb19 100644 (file)
@@ -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());
+    }
+  }
+
 }