From 9ce03d874daaa8bbdb453e2c143abb2db3bd5c5a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 11 Oct 2021 14:24:24 +1300 Subject: [PATCH] Remove call to legacy getTokenDetails In this case it is just being used as a v3 api call --- CRM/Contribute/Form/Task/PDF.php | 15 ++-- .../CRM/Contribute/Form/Task/PDFTest.php | 68 +++++++++++++++++++ 2 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 tests/phpunit/CRM/Contribute/Form/Task/PDFTest.php diff --git a/CRM/Contribute/Form/Task/PDF.php b/CRM/Contribute/Form/Task/PDF.php index 39cfd68685..77f777315d 100644 --- a/CRM/Contribute/Form/Task/PDF.php +++ b/CRM/Contribute/Form/Task/PDF.php @@ -228,6 +228,7 @@ AND {$this->_componentClause}"; * @return array * array of common elements * + * @throws \CiviCRM_API3_Exception */ public static function getElements($contribIds, $params, $contactIds) { $pdfElements = []; @@ -242,21 +243,17 @@ AND {$this->_componentClause}"; $pdfElements['createPdf'] = FALSE; if (!empty($pdfElements['params']['output']) && - ($pdfElements['params']['output'] == "pdf_invoice" || $pdfElements['params']['output'] == "pdf_receipt") + ($pdfElements['params']['output'] === 'pdf_invoice' || $pdfElements['params']['output'] === 'pdf_receipt') ) { $pdfElements['createPdf'] = TRUE; } $excludeContactIds = []; if (!$pdfElements['createPdf']) { - $returnProperties = [ - 'email' => 1, - 'do_not_email' => 1, - 'is_deceased' => 1, - 'on_hold' => 1, - ]; - - list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, $returnProperties, FALSE, FALSE); + $contactDetails = civicrm_api3('Contact', 'get', [ + 'return' => ['email', 'do_not_email', 'is_deceased', 'on_hold'], + 'id' => ['IN' => $contactIds], + ])['values']; $pdfElements['suppressedEmails'] = 0; $suppressedEmails = 0; foreach ($contactDetails as $id => $values) { diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFTest.php new file mode 100644 index 0000000000..e265d0bc65 --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFTest.php @@ -0,0 +1,68 @@ +quickCleanUpFinancialEntities(); + parent::tearDown(); + } + + /** + * Test the send pdf task filters out contacts who should not receive the + * receipt. + * + * @throws \API_Exception + */ + public function testSendPDF(): void { + $variants = [[], ['do_not_email' => TRUE], ['email' => ''], ['is_deceased' => TRUE], ['on_hold' => 1]]; + $searchValues = [ + 'task' => CRM_Core_Task::PDF_LETTER, + 'radio_ts' => 'ts_sel', + ]; + foreach ($variants as $variant) { + $contactID = $this->individualCreate($variant); + $contributionID = $this->contributionCreate(['contact_id' => $contactID]); + $searchValues['mark_x_' . $contributionID] = 1; + if (!empty($variant['on_hold'])) { + Email::update() + ->addWhere('contact_id', '=', $contactID) + ->setValues(['on_hold' => TRUE])->execute(); + } + } + + $form = $this->getFormObject('CRM_Contribute_Form_Task_PDF', [ + 'receipt_update' => 1, + ], NULL, $searchValues); + $form->buildForm(); + $form->postProcess(); + $status = CRM_Core_Session::singleton()->getStatus(TRUE); + $this->assertEquals([ + 'text' => 'Email was NOT sent to 4 contacts (no email address on file, or communication preferences specify DO NOT EMAIL, or contact is deceased).', + 'title' => 'Email Error', + 'type' => 'error', + 'options' => NULL, + ], $status[0]); + } + +} -- 2.25.1