From fad372d4bae8224c245a5984c86dac34caafaa47 Mon Sep 17 00:00:00 2001 From: Bauke Zwaan Date: Fri, 12 Mar 2021 09:30:11 +0100 Subject: [PATCH 1/3] Only add attachments when HideFormData-setting is not set for this recipient --- code/Control/UserDefinedFormController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index 1e73ad6..87d4bef 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -323,7 +323,7 @@ JS // Merge fields are used for CMS authors to reference specific form fields in email content $mergeFields = $this->getMergeFieldsMap($emailData['Fields']); - if ($attachments) { + if ($attachments && (bool) $recipient->HideFormData === false) { foreach ($attachments as $file) { /** @var File $file */ if ((int) $file->ID === 0) { From 34883adb120daadc276ce3fcf3cecaa6771ed4f2 Mon Sep 17 00:00:00 2001 From: Bauke Zwaan Date: Tue, 6 Apr 2021 15:46:37 +0200 Subject: [PATCH 2/3] Add testing --- .../Control/UserDefinedFormControllerTest.php | 38 ++++++++++++++++++- tests/php/UserFormsTest.yml | 4 ++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/php/Control/UserDefinedFormControllerTest.php b/tests/php/Control/UserDefinedFormControllerTest.php index 0c2e9a9..70ef313 100644 --- a/tests/php/Control/UserDefinedFormControllerTest.php +++ b/tests/php/Control/UserDefinedFormControllerTest.php @@ -7,7 +7,6 @@ use SilverStripe\Assets\File; use SilverStripe\Assets\Folder; use SilverStripe\Assets\Storage\AssetStore; use SilverStripe\Assets\Upload_Validator; -use InvalidArgumentException; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\Session; @@ -20,7 +19,6 @@ use SilverStripe\Forms\FormAction; use SilverStripe\ORM\DataObject; use SilverStripe\Security\InheritedPermissions; use SilverStripe\UserForms\Control\UserDefinedFormController; -use SilverStripe\UserForms\Model\EditableFormField; use SilverStripe\UserForms\Model\EditableFormField\EditableFileField; use SilverStripe\UserForms\Model\EditableFormField\EditableTextField; use SilverStripe\UserForms\Model\Recipient\EmailRecipient; @@ -28,6 +26,7 @@ use SilverStripe\UserForms\Model\Submission\SubmittedFormField; use SilverStripe\UserForms\Model\UserDefinedForm; use SilverStripe\View\ArrayData; use SilverStripe\View\SSViewer; +use function filesize; /** * @package userforms @@ -424,4 +423,39 @@ class UserDefinedFormControllerTest extends FunctionalTest $store = Injector::inst()->get(AssetStore::class); $this->assertTrue($store->exists($image->getFilename(), $image->getHash(), 'FitMaxWzM1MiwyNjRd')); } + + public function testRecipientAttachment() + { + Config::modify()->set(Upload_Validator::class, 'use_is_uploaded_file', false); + + $userForm = $this->setupFormFrontend('upload-form'); + $controller = UserDefinedFormController::create($userForm); + $field = $this->objFromFixture(EditableFileField::class, 'file-field-1'); + + $path = realpath(__DIR__ . '/fixtures/testfile.jpg'); + $data = [ + $field->Name => [ + 'name' => 'testfile.jpg', + 'type' => 'image/jpeg', + 'tmp_name' => $path, + 'error' => 0, + 'size' => filesize($path), + ] + ]; + $_FILES[$field->Name] = $data[$field->Name]; + + $controller->getRequest()->setSession(new Session([])); + $controller->process($data, $controller->Form()); + + // check emails + $this->assertEmailSent('test@example.com', 'no-reply@example.com', 'Email Subject'); + $email = $this->findEmail('test@example.com', 'no-reply@example.com', 'Email Subject'); + $this->assertNotEmpty($email['AttachedFiles'], 'Recipients receive attachment by default'); + + // no data + $this->assertEmailSent('nodata@example.com', 'no-reply@example.com', 'Email Subject'); + $nodata = $this->findEmail('nodata@example.com', 'no-reply@example.com', 'Email Subject'); + $this->assertEmpty($nodata['AttachedFiles'], 'Recipients with HideFormData do not receive attachment'); + } + } diff --git a/tests/php/UserFormsTest.yml b/tests/php/UserFormsTest.yml index 46cfd30..f086ff1 100644 --- a/tests/php/UserFormsTest.yml +++ b/tests/php/UserFormsTest.yml @@ -478,6 +478,9 @@ SilverStripe\UserForms\Model\UserDefinedForm: Title: 'Form with upload field' Fields: - =>SilverStripe\UserForms\Model\EditableFormField\EditableFileField.file-field-1 + EmailRecipients: + - =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.recipient-1 + - =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.no-data SilverStripe\UserForms\Model\EditableCustomRule: rule1: @@ -498,3 +501,4 @@ SilverStripe\UserForms\Model\EditableCustomRule: FieldValue: 'Show more' ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.conditional-required-text-2 Parent: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.conditional-required-text-3 + From 4a883f1641b725b2ccbcc65ff25df31522b8b2b8 Mon Sep 17 00:00:00 2001 From: Bauke Zwaan Date: Mon, 12 Apr 2021 15:22:45 +0200 Subject: [PATCH 3/3] fix for testing EmailRecipients --- tests/php/UserFormsTest.yml | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/php/UserFormsTest.yml b/tests/php/UserFormsTest.yml index f086ff1..0fb4ea9 100644 --- a/tests/php/UserFormsTest.yml +++ b/tests/php/UserFormsTest.yml @@ -370,6 +370,17 @@ SilverStripe\UserForms\Model\Recipient\EmailRecipient: EmailSubject: Test Email EmailFrom: test3@example.com, test4@example.com + upload-recipient: + EmailAddress: test@example.com + EmailSubject: Email Subject + EmailFrom: no-reply@example.com + + upload-no-data: + EmailAddress: nodata@example.com + EmailSubject: Email Subject + EmailFrom: no-reply@example.com + HideFormData: true + SilverStripe\UserForms\Model\UserDefinedForm: basic-form-page: Content: '

Here is my form

$UserDefinedForm

Thank you for filling it out

' @@ -479,8 +490,8 @@ SilverStripe\UserForms\Model\UserDefinedForm: Fields: - =>SilverStripe\UserForms\Model\EditableFormField\EditableFileField.file-field-1 EmailRecipients: - - =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.recipient-1 - - =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.no-data + - =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.upload-recipient + - =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.upload-no-data SilverStripe\UserForms\Model\EditableCustomRule: rule1: