| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | Copyright CiviCRM LLC. All rights reserved. | |
| 6 | | | |
| 7 | | This work is published under the GNU AGPLv3 license with some | |
| 8 | | permitted exceptions and without any warranty. For full license | |
| 9 | | and copyright information, see https://civicrm.org/licensing | |
| 10 | +--------------------------------------------------------------------+ |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Test class for CRM_Contact_Form_Task_PDFLetterCommon. |
| 15 | * |
| 16 | * @group headless |
| 17 | */ |
| 18 | class CRM_Contact_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { |
| 19 | |
| 20 | /** |
| 21 | * Contact ID. |
| 22 | * |
| 23 | * @var int |
| 24 | */ |
| 25 | protected $contactId; |
| 26 | |
| 27 | /** |
| 28 | * {@inheritdoc} |
| 29 | */ |
| 30 | protected function setUp(): void { |
| 31 | parent::setUp(); |
| 32 | $this->contactId = $this->createLoggedInUser(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Test the pdf filename is assigned as expected. |
| 37 | * |
| 38 | * @param string|null $pdfFileName |
| 39 | * Value for pdf_file_name param. |
| 40 | * @param string|null $activitySubject |
| 41 | * Value of the subject of the activity. |
| 42 | * @param bool|null $isLiveMode |
| 43 | * TRUE when the form is in live mode, NULL when it is a preview. |
| 44 | * @param string $expectedFilename |
| 45 | * Expected filename assigned to the pdf. |
| 46 | * |
| 47 | * @dataProvider getFilenameCases |
| 48 | */ |
| 49 | public function testFilenameIsAssigned(?string $pdfFileName, ?string $activitySubject, ?bool $isLiveMode, string $expectedFilename): void { |
| 50 | // @todo - remove this cid - it helps direct the form controller but is |
| 51 | // pretty cludgey. |
| 52 | $_REQUEST['cid'] = $this->contactId; |
| 53 | $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', [ |
| 54 | 'pdf_file_name' => $pdfFileName, |
| 55 | 'subject' => $activitySubject, |
| 56 | 'document_type' => 'pdf', |
| 57 | 'buttons' => [ |
| 58 | '_qf_PDF_upload' => $isLiveMode, |
| 59 | ], |
| 60 | ]); |
| 61 | $form->_contactIds = [$this->contactId]; |
| 62 | $fileNameAssigned = $this->submitForm($form)['fileName']; |
| 63 | $this->assertEquals($expectedFilename, $fileNameAssigned); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * DataProvider for testFilenameIsAssigned. |
| 68 | * |
| 69 | * @return array |
| 70 | * Array with the test information. |
| 71 | */ |
| 72 | public function getFilenameCases(): array { |
| 73 | return [ |
| 74 | [ |
| 75 | 'FilenameInParam', |
| 76 | 'FilenameInActivitySubject', |
| 77 | NULL, |
| 78 | 'FilenameInParam_preview', |
| 79 | ], |
| 80 | [ |
| 81 | 'FilenameInParam', |
| 82 | 'FilenameInActivitySubject', |
| 83 | TRUE, |
| 84 | 'FilenameInParam', |
| 85 | ], |
| 86 | [ |
| 87 | NULL, |
| 88 | 'FilenameInActivitySubject', |
| 89 | NULL, |
| 90 | 'FilenameInActivitySubject_preview', |
| 91 | ], |
| 92 | [ |
| 93 | NULL, |
| 94 | 'FilenameInActivitySubject', |
| 95 | TRUE, |
| 96 | 'FilenameInActivitySubject', |
| 97 | ], |
| 98 | [ |
| 99 | NULL, |
| 100 | NULL, |
| 101 | NULL, |
| 102 | 'CiviLetter_preview', |
| 103 | ], |
| 104 | [ |
| 105 | NULL, |
| 106 | NULL, |
| 107 | TRUE, |
| 108 | 'CiviLetter', |
| 109 | ], |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param \CRM_Core_Form $form |
| 115 | * |
| 116 | * @return int|mixed |
| 117 | */ |
| 118 | protected function submitForm(CRM_Core_Form $form) { |
| 119 | $form->preProcess(); |
| 120 | try { |
| 121 | $form->postProcess(); |
| 122 | } |
| 123 | catch (CRM_Core_Exception_PrematureExitException $e) { |
| 124 | return $e->errorData; |
| 125 | |
| 126 | } |
| 127 | $this->fail('line should be unreachable'); |
| 128 | } |
| 129 | |
| 130 | } |