From 072b9fb1b43f36c3734f2b8bf65fe383d6d7b2ef Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Thu, 1 Sep 2022 08:53:15 -0400 Subject: [PATCH] fix crash for invoice button --- CRM/Contribute/Form/Task/PDF.php | 4 +- .../Contribute/Form/ContributionViewTest.php | 74 ++++++++++++++++--- 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/CRM/Contribute/Form/Task/PDF.php b/CRM/Contribute/Form/Task/PDF.php index e581b9f3c2..fe8cf34136 100644 --- a/CRM/Contribute/Form/Task/PDF.php +++ b/CRM/Contribute/Form/Task/PDF.php @@ -231,7 +231,7 @@ AND {$this->_componentClause}"; * Contribution Id. * @param array $params * Parameter for pdf or email invoices. - * @param array $contactIds + * @param array|int $contactIds * Contact Id. * @param bool $isCreatePDF * @@ -240,7 +240,7 @@ AND {$this->_componentClause}"; * * @throws \CiviCRM_API3_Exception */ - public static function getElements(array $contribIds, array $params, array $contactIds, bool $isCreatePDF): array { + public static function getElements(array $contribIds, array $params, $contactIds, bool $isCreatePDF): array { $pdfElements = []; $pdfElements['details'] = self::getDetails(implode(',', $contribIds)); $excludeContactIds = []; diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionViewTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionViewTest.php index bf7d275924..811c137969 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionViewTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionViewTest.php @@ -14,6 +14,32 @@ */ class CRM_Contribute_Form_ContributionViewTest extends CiviUnitTestCase { + /** + * @var int + */ + private $contact_id; + + /** + * @var array + */ + private $contribution; + + public function setUp(): void { + parent::setUp(); + $this->contact_id = $this->individualCreate(); + $this->contribution = $this->callAPISuccess('Contribution', 'create', [ + 'contact_id' => $this->contact_id, + 'financial_type_id' => 'Donation', + 'total_amount' => '10', + ]); + } + + public function tearDown(): void { + $this->callAPISuccess('Contribution', 'delete', ['id' => $this->contribution['id']]); + $this->callAPISuccess('Contact', 'delete', ['id' => $this->contact_id]); + parent::tearDown(); + } + /** * Test that can still view a contribution without full permissions. */ @@ -26,19 +52,13 @@ class CRM_Contribute_Form_ContributionViewTest extends CiviUnitTestCase { 'edit contributions', 'delete in CiviContribute', ]; - $contact_id = $this->individualCreate(); - $contribution = $this->callAPISuccess('Contribution', 'create', [ - 'contact_id' => $contact_id, - 'financial_type_id' => 'Donation', - 'total_amount' => '10', - ]); - $_SERVER['REQUEST_URI'] = "civicrm/contact/view/contribution?reset=1&action=view&id={$contribution['id']}&cid={$contact_id}"; + $_SERVER['REQUEST_URI'] = "civicrm/contact/view/contribution?reset=1&action=view&id={$this->contribution['id']}&cid={$this->contact_id}"; $_GET['q'] = $_REQUEST['q'] = 'civicrm/contact/view/contribution'; $_GET['reset'] = $_REQUEST['reset'] = 1; $_GET['action'] = $_REQUEST['action'] = 'view'; - $_GET['id'] = $_REQUEST['id'] = $contribution['id']; - $_GET['cid'] = $_REQUEST['cid'] = $contact_id; + $_GET['id'] = $_REQUEST['id'] = $this->contribution['id']; + $_GET['cid'] = $_REQUEST['cid'] = $this->contact_id; $item = CRM_Core_Invoke::getItem(['civicrm/contact/view/contribution']); ob_start(); @@ -55,4 +75,40 @@ class CRM_Contribute_Form_ContributionViewTest extends CiviUnitTestCase { $this->assertStringContainsString('Mr. Anthony Anderson II', $contents); } + public function testInvoiceDownload() { + Civi::settings()->set('invoicing', 1); + + $_SERVER['REQUEST_URI'] = "civicrm/contribute/invoice?reset=1&id={$this->contribution['id']}&cid={$this->contact_id}"; + $_GET['q'] = $_REQUEST['q'] = 'civicrm/contribute/invoice'; + $_GET['reset'] = $_REQUEST['reset'] = 1; + $_GET['id'] = $_REQUEST['id'] = $this->contribution['id']; + $_GET['cid'] = $_REQUEST['cid'] = $this->contact_id; + + $item = CRM_Core_Invoke::getItem(['civicrm/contribute/invoice']); + ob_start(); + $isOk = FALSE; + try { + CRM_Core_Invoke::runItem($item); + } + catch (CRM_Core_Exception_PrematureExitException $e) { + $this->assertEquals(0, $e->errorData['error_code']); + $this->assertEquals('pdf', $e->errorData['output']); + $this->assertEquals("INV_{$this->contribution['id']}", $e->errorData['fileName']); + $this->assertStringContainsString('INVOICE', $e->errorData['html']); + $isOk = TRUE; + } + finally { + ob_end_clean(); + + unset($_GET['q'], $_REQUEST['q']); + unset($_GET['reset'], $_REQUEST['reset']); + unset($_GET['id'], $_REQUEST['id']); + unset($_GET['cid'], $_REQUEST['cid']); + Civi::settings()->set('invoicing', 0); + } + if (!$isOk) { + $this->fail('It should have entered the catch block.'); + } + } + } -- 2.25.1