Skip to content

Commit

Permalink
Merge pull request #21360 from eileenmcnaughton/case_pdf
Browse files Browse the repository at this point in the history
Remove testing hack
  • Loading branch information
seamuslee001 authored Sep 3, 2021
2 parents e448567 + cf56c73 commit e203174
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
5 changes: 1 addition & 4 deletions CRM/Contribute/Form/Task/PDFLetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions CRM/Utils/PDF/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
32 changes: 21 additions & 11 deletions tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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("<table border='1' cellpadding='2' cellspacing='0' class='table'>
<tbody>
<tr>
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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()
Expand All @@ -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)
);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e203174

Please sign in to comment.