From cf56c7305986ce1c6bd51534069cf93ba6963967 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 3 Sep 2021 18:47:19 +1200 Subject: [PATCH] Remove testing hack --- CRM/Contribute/Form/Task/PDFLetter.php | 5 +-- CRM/Utils/PDF/Document.php | 9 ++++++ .../Form/Task/PDFLetterCommonTest.php | 32 ++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CRM/Contribute/Form/Task/PDFLetter.php b/CRM/Contribute/Form/Task/PDFLetter.php index 330e1e2d59ea..781a4845f907 100644 --- a/CRM/Contribute/Form/Task/PDFLetter.php +++ b/CRM/Contribute/Form/Task/PDFLetter.php @@ -130,6 +130,7 @@ public function buildQuickForm() { * 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 @@ public function postProcess() { 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. diff --git a/CRM/Utils/PDF/Document.php b/CRM/Utils/PDF/Document.php index e087a9228aac..0b796322a2e8 100644 --- a/CRM/Utils/PDF/Document.php +++ b/CRM/Utils/PDF/Document.php @@ -68,6 +68,15 @@ public static function html2doc($pages, $fileName, $format = []) { '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); diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php index 7b9cfa124fd9..1b5919f359f2 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -46,7 +46,6 @@ protected function setUp(): void { * * @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 @@ public function testPostProcess(): void { $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 @@ public function testPostProcess(): void { ]; foreach ($expectedValues as $val) { - $this->assertTrue(strpos($html[$contributionId], $val) !== 0); + $this->assertNotSame(strpos($html[$contributionId], $val), 0); } } } @@ -391,7 +396,6 @@ public function testPostProcessGroupByContact(): void { $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 @@ public function testPostProcessGroupByContact(): void { $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(" @@ -505,7 +515,7 @@ public function testPostProcessGroupByContact(): void { /** * 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 @@ public function getHtmlMessage() { * @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 @@ public function hook_aggregateTokenValues(&$values, $contactIDs, $job = NULL, $t * * @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 @@ protected function setSearchSelection(array $entities, CRM_Core_Form $form): voi } /** + * @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,