CRM_Mailing_TokensTest - Update spec to match expected behavior
authorTim Otten <totten@civicrm.org>
Tue, 30 Apr 2019 04:26:15 +0000 (21:26 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 30 Apr 2019 04:29:09 +0000 (21:29 -0700)
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

index 721f87f7c087a3466140b6009d569f390d34d1cc..8b31fe906e198da38afe3bbc5a5bbf2d6f5506b5 100644 (file)
@@ -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);
   }
 
 }