Skip to content

Commit

Permalink
[REF] Pre process cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 31, 2021
1 parent d5524df commit 0008e8a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
30 changes: 2 additions & 28 deletions CRM/Contact/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,8 @@ public static function getLoggingOptions() {
* @param CRM_Core_Form $form
*/
public static function preProcess(&$form) {
if (!isset($form->_single)) {
// @todo ensure this is already set.
$form->_single = FALSE;
}
$form->_emails = [];

// @TODO remove these line and to it somewhere more appropriate. Currently some classes (e.g Case
// are having to re-write contactIds afterwards due to this inappropriate variable setting
// If we don't have any contact IDs, use the logged in contact ID
$form->_contactIds = $form->_contactIds ?: [CRM_Core_Session::getLoggedInContactID()];

$fromEmailValues = CRM_Core_BAO_Email::getFromEmail();

$form->_emails = $fromEmailValues;
$defaults = [];
$form->_fromEmails = $fromEmailValues;
$form->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
if (is_numeric(key($form->_fromEmails))) {
$emailID = (int) key($form->_fromEmails);
$defaults = CRM_Core_BAO_Email::getEmailSignatureDefaults($emailID);
Expand All @@ -66,19 +52,7 @@ public static function preProcess(&$form) {
$defaults['from_email_address'] = current(CRM_Core_BAO_Domain::getNameAndEmail(FALSE, TRUE));
}
$form->setDefaults($defaults);
$messageText = [];
$messageSubject = [];
$dao = new CRM_Core_BAO_MessageTemplate();
$dao->is_active = 1;
$dao->find();
while ($dao->fetch()) {
$messageText[$dao->id] = $dao->msg_text;
$messageSubject[$dao->id] = $dao->msg_subject;
}

$form->assign('message', $messageText);
$form->assign('messageSubject', $messageSubject);
parent::preProcess($form);
$form->setTitle('Print/Merge Document');
}

/**
Expand Down
31 changes: 21 additions & 10 deletions tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,19 @@ protected function setUp(): void {
* @dataProvider getFilenameCases
*/
public function testFilenameIsAssigned(?string $pdfFileName, ?string $activitySubject, ?bool $isLiveMode, string $expectedFilename): void {
// @todo - remove this cid - it helps direct the form controller but is
// pretty cludgey.
$_REQUEST['cid'] = $this->contactId;
$form = $this->getFormObject('CRM_Contact_Form_Task_PDF', [
'pdf_file_name' => $pdfFileName,
'subject' => $activitySubject,
'_contactIds' => [],
'document_type' => 'pdf',
'buttons' => [
'_qf_PDF_upload' => $isLiveMode,
],
]);
$fileNameAssigned = NULL;
$form->preProcess();
try {
$form->postProcess();
}
catch (CRM_Core_Exception_PrematureExitException $e) {
$fileNameAssigned = $e->errorData['fileName'];
}

$form->_contactIds = [$this->contactId];
$fileNameAssigned = $this->submitForm($form)['fileName'];
$this->assertEquals($expectedFilename, $fileNameAssigned);
}

Expand Down Expand Up @@ -116,4 +110,21 @@ public function getFilenameCases(): array {
];
}

/**
* @param \CRM_Core_Form $form
*
* @return int|mixed
*/
protected function submitForm(CRM_Core_Form $form) {
$form->preProcess();
try {
$form->postProcess();
}
catch (CRM_Core_Exception_PrematureExitException $e) {
return $e->errorData;

}
$this->fail('line should be unreachable');
}

}
1 change: 0 additions & 1 deletion tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3336,7 +3336,6 @@ public function onPost($op, $objectName, $objectId, &$objectRef) {
* @param string $pageName
*
* @return \CRM_Core_Form
* @throws \CRM_Core_Exception
*/
public function getFormObject($class, $formValues = [], $pageName = '') {
$_POST = $formValues;
Expand Down

0 comments on commit 0008e8a

Please sign in to comment.