From d013fe3d1298abe4cf401434063f6f0006e16da7 Mon Sep 17 00:00:00 2001 From: Tano Rojas Date: Fri, 20 Aug 2021 23:16:14 +0700 Subject: [PATCH] Add tests for pdf filename assignation --- .../Contact/Form/Task/PDFLetterCommonTest.php | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php diff --git a/tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php new file mode 100644 index 0000000000..cdd3d9f3ad --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php @@ -0,0 +1,119 @@ +contactId = $this->createLoggedInUser(); + } + + /** + * Test the pdf filename is assigned as expected. + * + * @param string|null $pdfFileName + * Value for pdf_file_name param. + * @param string|null $activitySubject + * Value of the subject of the activity. + * @param bool|null $isLiveMode + * TRUE when the form is in live mode, NULL when it is a preview. + * @param string $expectedFilename + * Expected filename assigned to the pdf. + * + * @dataProvider getFilenameCases + */ + public function testFilenameIsAssigned(?string $pdfFileName, ?string $activitySubject, ?bool $isLiveMode, string $expectedFilename): void { + $_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']; + } + + $this->assertEquals($expectedFilename, $fileNameAssigned); + } + + /** + * DataProvider for testFilenameIsAssigned. + * + * @return array + * Array with the test information. + */ + public function getFilenameCases(): array { + return [ + [ + 'FilenameInParam', + 'FilenameInActivitySubject', + NULL, + 'FilenameInParam_preview', + ], + [ + 'FilenameInParam', + 'FilenameInActivitySubject', + TRUE, + 'FilenameInParam', + ], + [ + NULL, + 'FilenameInActivitySubject', + NULL, + 'FilenameInActivitySubject_preview', + ], + [ + NULL, + 'FilenameInActivitySubject', + TRUE, + 'FilenameInActivitySubject', + ], + [ + NULL, + NULL, + NULL, + 'CiviLetter_preview', + ], + [ + NULL, + NULL, + TRUE, + 'CiviLetter', + ], + ]; + } + +} -- 2.25.1