Remove testing hack
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Sep 2021 06:47:19 +0000 (18:47 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 3 Sep 2021 07:00:02 +0000 (19:00 +1200)
CRM/Contribute/Form/Task/PDFLetter.php
CRM/Utils/PDF/Document.php
tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php

index 330e1e2d59ea533faceea9804c423d47419bd8f7..781a4845f907b382629e77f133670a1a935a6d12 100644 (file)
@@ -130,6 +130,7 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task {
    * Process the form after the input has been submitted and validated.
    *
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public function postProcess() {
     $formValues = $this->controller->exportValues($this->getName());
@@ -234,10 +235,6 @@ class CRM_Contribute_Form_Task_PDFLetter extends CRM_Contribute_Form_Task {
     CRM_Contact_Form_Task_PDFLetterCommon::createActivities($this, $html_message, $contactIds, CRM_Utils_Array::value('subject', $formValues, ts('Thank you letter')), CRM_Utils_Array::value('campaign_id', $formValues), $contactHtml);
     $html = array_diff_key($html, $emailedHtml);
 
-    if (!empty($formValues['is_unit_test'])) {
-      return $html;
-    }
-
     //CRM-19761
     if (!empty($html)) {
       // Set the filename for the PDF using the Activity Subject, if defined. Remove unwanted characters and limit the length to 200 characters.
index e087a9228aacff7e805a1de6542692d7efed07f7..0b796322a2e82210d5b1b56689a99f27830bdca9 100644 (file)
@@ -68,6 +68,15 @@ class CRM_Utils_PDF_Document {
       'marginBottom' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format), $metric),
       'marginLeft' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_left', $format), $metric),
     ];
+    if (CIVICRM_UF === 'UnitTests' && headers_sent()) {
+      // Streaming content will 'die' in unit tests unless ob_start()
+      // has been called.
+      throw new CRM_Core_Exception_PrematureExitException('_html2doc called', [
+        'html' => $pages,
+        'fileName' => $fileName,
+        'pageStyle' => $pageStyle,
+      ]);
+    }
 
     $ext = pathinfo($fileName, PATHINFO_EXTENSION);
 
index 7b9cfa124fd9fb99403570dff97dd731a0a5844d..1b5919f359f2e0c137a239a4bca3aa91ff05a3d1 100644 (file)
@@ -46,7 +46,6 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
    *
    * @throws \API_Exception
    * @throws \CRM_Core_Exception
-   * @throws \CiviCRM_API3_Exception
    */
   public function tearDown(): void {
     CRM_Utils_Token::$_tokens['contribution'] = NULL;
@@ -215,7 +214,13 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
       $date = CRM_Utils_Date::getToday();
       $displayDate = CRM_Utils_Date::customFormat($date, $format);
 
-      $html = $form->postProcess();
+      try {
+        $form->postProcess();
+        $this->fail('Exception expected');
+      }
+      catch (CRM_Core_Exception_PrematureExitException $e) {
+        $html = $e->errorData['html'];
+      }
       $expectedValues = [
         'Hello Anthony',
         '$ 100.00',
@@ -225,7 +230,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
       ];
 
       foreach ($expectedValues as $val) {
-        $this->assertTrue(strpos($html[$contributionId], $val) !== 0);
+        $this->assertNotSame(strpos($html[$contributionId], $val), 0);
       }
     }
   }
@@ -391,7 +396,6 @@ campaign : Big one
     $this->_individualId2 = $this->individualCreate();
     $htmlMessage = "{aggregate.rendered_token}";
     $formValues = [
-      'is_unit_test' => TRUE,
       'group_by' => 'contact_id',
       'html_message' => $htmlMessage,
       'email_options' => 'both',
@@ -427,7 +431,13 @@ campaign : Big one
     $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues);
     $form->setContributionIds($contributionIDs);
 
-    $html = $form->postProcess();
+    try {
+      $form->postProcess();
+      $this->fail('exception expected.');
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $html = $e->errorData['html'];
+    }
     $this->assertEquals("<table border='1' cellpadding='2' cellspacing='0' class='table'>
   <tbody>
   <tr>
@@ -505,7 +515,7 @@ campaign : Big one
   /**
    * Implements civicrm_tokens().
    */
-  public function hook_tokens(&$tokens) {
+  public function hook_tokens(&$tokens): void {
     $this->hookTokensCalled++;
     $tokens['aggregate'] = ['rendered_token' => 'rendered_token'];
   }
@@ -561,7 +571,7 @@ value=$contact_aggregate+$contribution.total_amount}
    * @param array $tokens
    * @param null $context
    */
-  public function hook_aggregateTokenValues(&$values, $contactIDs, $job = NULL, $tokens = [], $context = NULL) {
+  public function hook_aggregateTokenValues(array &$values, $contactIDs, $job = NULL, $tokens = [], $context = NULL) {
     foreach ($contactIDs as $contactID) {
       CRM_Core_Smarty::singleton()->assign('messageContactID', $contactID);
       $values[$contactID]['aggregate.rendered_token'] = CRM_Core_Smarty::singleton()
@@ -577,7 +587,7 @@ value=$contact_aggregate+$contribution.total_amount}
    *
    * @dataProvider isHtmlTokenInTableCellProvider
    */
-  public function testIsHtmlTokenInTableCell($token, $entity, $textToSearch, $expected) {
+  public function testIsHtmlTokenInTableCell($token, $entity, $textToSearch, $expected): void {
     $this->assertEquals($expected,
       CRM_Contribute_Form_Task_PDFLetter::isHtmlTokenInTableCell($token, $entity, $textToSearch)
     );
@@ -657,11 +667,11 @@ value=$contact_aggregate+$contribution.total_amount}
   }
 
   /**
+   * @param array $contributionParams
+   *
    * @return mixed
-   * @throws \CRM_Core_Exception
-   * @throws \CiviCRM_API3_Exception
    */
-  protected function createContribution($contributionParams = []) {
+  protected function createContribution(array $contributionParams = []) {
     $contributionParams = array_merge([
       'contact_id' => $this->individualCreate(),
       'total_amount' => 100,