From 34883adb120daadc276ce3fcf3cecaa6771ed4f2 Mon Sep 17 00:00:00 2001 From: Bauke Zwaan Date: Tue, 6 Apr 2021 15:46:37 +0200 Subject: [PATCH] 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 +