From 42da9de9099da7534e547441945f0fb19094ef08 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 18 Jun 2022 09:52:00 +1200 Subject: [PATCH] Always throw exception in tests for pdf out headers_sent() was always true before and hence the tests were written to expect the exception would be called. Fixing that caused the tests to fail - but fixing it is good --- CRM/Utils/PDF/Document.php | 2 +- CRM/Utils/PDF/Utils.php | 3 ++- tests/phpunit/CRM/Report/Utils/ReportTest.php | 16 ++++------------ 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php index f0f623bc86..0fde697dc5 100644 --- a/CRM/Utils/PDF/Document.php +++ b/CRM/Utils/PDF/Document.php @@ -68,7 +68,7 @@ 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()) { + if (CIVICRM_UF === 'UnitTests') { // Streaming content will 'die' in unit tests unless ob_start() // has been called. throw new CRM_Core_Exception_PrematureExitException('_html2doc called', [ diff --git a/CRM/Utils/PDF/Utils.php b/CRM/Utils/PDF/Utils.php index a751baa1e3..014c2d846e 100644 --- a/CRM/Utils/PDF/Utils.php +++ b/CRM/Utils/PDF/Utils.php @@ -190,12 +190,13 @@ class CRM_Utils_PDF_Utils { } // CRM-19183 remove .pdf extension from filename $fileName = basename($fileName, ".pdf"); - if (CIVICRM_UF === 'UnitTests' && headers_sent()) { + if (CIVICRM_UF === 'UnitTests') { // Streaming content will 'die' in unit tests unless ob_start() // has been called. throw new CRM_Core_Exception_PrematureExitException('_html2pdf_dompdf called', [ 'html' => $html, 'fileName' => $fileName, + 'output' => 'pdf', ]); } $dompdf->stream($fileName); diff --git a/tests/phpunit/CRM/Report/Utils/ReportTest.php b/tests/phpunit/CRM/Report/Utils/ReportTest.php index cb85435e19..42f09cc06d 100644 --- a/tests/phpunit/CRM/Report/Utils/ReportTest.php +++ b/tests/phpunit/CRM/Report/Utils/ReportTest.php @@ -125,12 +125,7 @@ ENDOUTPUT; * Test when you choose PDF from the actions dropdown. * * We're not too concerned about the actual report rows - there's other - * tests for that - we're more interested in does it echo it in pdf - * format. - * - * This isn't great but otherwise dompdf complains about headers already sent. - * @runInSeparateProcess - * @preserveGlobalState disabled + * tests for that - we're more interested in does it hit the pdf output function. */ public function testOutputPdf(): void { $contents = ''; @@ -159,11 +154,8 @@ ENDOUTPUT; $_SERVER['QUERY_STRING'] = 'reset=1'; } - // A bit weird - it will send the output to the browser, which here is the - // console, then throw a specific exception. So we capture the output - // and the exception. + // In the unit text context it throws an exception for us to check. try { - ob_start(); CRM_Report_Utils_Report::processReport([ 'instanceId' => $report_instance['id'], 'format' => 'pdf', @@ -171,9 +163,9 @@ ENDOUTPUT; ]); } catch (CRM_Core_Exception_PrematureExitException $e) { - $contents = ob_get_clean(); + $contents = $e->errorData['html']; + $this->assertEquals('pdf', $e->errorData['output']); } - $this->assertStringStartsWith('%PDF', $contents); $this->assertStringContainsString("id_value={$last_contact['id']}", $contents); } -- 2.25.1