Fix test
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 23 Sep 2021 11:42:30 +0000 (23:42 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 23 Sep 2021 19:58:36 +0000 (07:58 +1200)
Fixes test logic I accidentally voided

CRM/Activity/Form/Task/PDF.php
CRM/Core/Form/Task/PDFLetterCommon.php
tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php

index 5c13becd96dd99e210601acd866ab66dca0aae96..760bde39eadce58050a7185db7b45de6e2ea4ac5 100644 (file)
@@ -108,20 +108,12 @@ class CRM_Activity_Form_Task_PDF extends CRM_Activity_Form_Task {
    *   The name registered with the TokenProcessor
    * @param array $formValues
    *   The values submitted through the form
-   *
-   * @return string
-   *   If formValues['is_unit_test'] is true, otherwise outputs document to browser
    */
-  public function renderFromRows($rows, $msgPart, $formValues) {
+  public function renderFromRows($rows, $msgPart, $formValues): void {
     $html = [];
     foreach ($rows as $row) {
       $html[] = $row->render($msgPart);
     }
-
-    if (!empty($formValues['is_unit_test'])) {
-      return $html;
-    }
-
     if (!empty($html)) {
       $this->outputFromHtml($formValues, $html);
     }
@@ -133,13 +125,13 @@ class CRM_Activity_Form_Task_PDF extends CRM_Activity_Form_Task {
    * @param array $formValues
    * @param array $html
    */
-  protected function outputFromHtml($formValues, array $html) {
+  protected function outputFromHtml(array $formValues, array $html): void {
     $fileName = $this->getFileName();
-    if ($formValues['document_type'] === 'pdf') {
+    if ($this->getSubmittedValue('document_type') === 'pdf') {
       CRM_Utils_PDF_Utils::html2pdf($html, $fileName . '.pdf', FALSE, $formValues);
     }
     else {
-      CRM_Utils_PDF_Document::html2doc($html, $fileName . '.' . $formValues['document_type'], $formValues);
+      CRM_Utils_PDF_Document::html2doc($html, $fileName . '.' . $this->getSubmittedValue('document_type'), $formValues);
     }
   }
 
index f267a10d8569a07d346a674d715d79a267f60256..f3774ce75ba4ebda48cc6e87f2be1514f4dc98c1 100644 (file)
@@ -377,21 +377,14 @@ class CRM_Core_Form_Task_PDFLetterCommon {
    *   The values submitted through the form
    *
    * @deprecated
-   *
-   * @return array
-   *   If formValues['is_unit_test'] is true, otherwise outputs document to browser
    */
-  public static function renderFromRows($rows, $msgPart, $formValues) {
+  public static function renderFromRows($rows, $msgPart, $formValues): void {
     CRM_Core_Error::deprecatedFunctionWarning('no alternative');
     $html = [];
     foreach ($rows as $row) {
       $html[] = $row->render($msgPart);
     }
 
-    if (!empty($formValues['is_unit_test'])) {
-      return $html;
-    }
-
     if (!empty($html)) {
       self::outputFromHtml($formValues, $html);
     }
index 97c07607974f126e8560dcbbc93a377adf49d6ec..2903c9a8955a50c522ef6db6075130cbddefabb3 100644 (file)
@@ -48,8 +48,12 @@ class CRM_Activity_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
     $this->assertEquals(array_merge($this->getActivityTokens(), CRM_Core_SelectValues::domainTokens()), $tokenProcessor->listTokens());
     $html_message = "\n" . implode("\n", CRM_Utils_Array::collect('0', $data)) . "\n";
     $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
-    $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
-
+    try {
+      $output = $form->createDocument([$activity['id']], $html_message, []);
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $output = $e->errorData['html'];
+    }
     // Check some basic fields
     foreach ($data as $line) {
       $this->assertStringContainsString("\n" . $line[1] . "\n", $output[0]);
@@ -100,7 +104,12 @@ class CRM_Activity_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
     $html_message = "Custom: {activity.$cf}";
     $activityIds = CRM_Utils_Array::collect('id', $activities);
     $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
-    $output = $form->createDocument($activityIds, $html_message, ['is_unit_test' => TRUE]);
+    try {
+      $output = $form->createDocument($activityIds, $html_message, []);
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $output = $e->errorData['html'];
+    }
     // Should have one row of output per activity
     $this->assertCount(count($activities), $output);
 
@@ -132,7 +141,7 @@ class CRM_Activity_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
     ];
     $html_message = "\n" . implode("\n", CRM_Utils_Array::collect('0', $data)) . "\n";
     $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
-    $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
+    $output = $form->createDocument([$activity['id']], $html_message, []);
 
     foreach ($data as $line) {
       $this->assertContains("\n" . $line[1] . "\n", $output[0]);
@@ -147,10 +156,19 @@ class CRM_Activity_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
    */
   public function testCreateDocumentUnknownTokens(): void {
     $activity = $this->activityCreate();
-    $html_message = 'Unknown token: ';
-    $form = $this->getFormObject('CRM_Activity_Form_Task_PDF');
-    $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]);
-    $this->assertEquals($html_message, $output[0]);
+    $html_message = 'Unknown token:{activity.something_unknown}';
+    $form = $this->getFormObject('CRM_Activity_Form_Task_PDF', ['document_type' => 'pdf']);
+    try {
+      $form->createDocument([$activity['id']], $html_message, []);
+    }
+    catch (CRM_Core_Exception_PrematureExitException $e) {
+      $html = $e->errorData['html'];
+      $this->assertStringContainsString('<div id="crm-container">
+Unknown token:
+    </div>', $html);
+      return;
+    }
+    $this->fail('should be unreachable');
   }
 
 }