diff --git a/.travis.yml b/.travis.yml index 18819fc..333275b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ dist: xenial services: - mysql + - postgresql env: global: @@ -12,16 +13,14 @@ env: matrix: include: - - php: 5.6 - env: DB=MYSQL RECIPE_VERSION=4.3.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1 - - php: 7.0 - env: DB=MYSQL RECIPE_VERSION=4.3.x-dev PHPUNIT_TEST=1 - php: 7.1 - env: DB=MYSQL RECIPE_VERSION=4.4.x-dev PHPUNIT_COVERAGE_TEST=1 + env: DB=MYSQL RECIPE_VERSION=4.3.x-dev PHPUNIT_TEST=1 PHPCS_TEST=1 + - php: 7.1 + env: DB=MYSQL RECIPE_VERSION=4.4.x-dev PHPUNIT_TEST=1 PHPUNIT_COVERAGE_TEST=1 - php: 7.2 - env: DB=PGSQL RECIPE_VERSION=4.4.x-dev PHPUNIT_TEST=1 NPM_TEST=1 + env: DB=MYSQL RECIPE_VERSION=4.5.x-dev PHPUNIT_TEST=1 NPM_TEST=1 - php: 7.3 - env: DB=MYSQL RECIPE_VERSION=4.5.x-dev PHPUNIT_TEST=1 + env: DB=PGSQL RECIPE_VERSION=4.5.x-dev PHPUNIT_TEST=1 - php: 7.3 env: DB=MYSQL RECIPE_VERSION=4.x-dev PHPUNIT_TEST=1 @@ -34,7 +33,7 @@ before_script: # Install composer dependencies - composer validate - composer require --no-update silverstripe/recipe-cms:$RECIPE_VERSION - - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.2.x-dev; fi + - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.x-dev --prefer-dist; fi - composer install --prefer-source --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile - if [[ $NPM_TEST ]]; then nvm install $TRAVIS_NODE_VERSION && nvm use $TRAVIS_NODE_VERSION && npm install -g yarn && yarn install --network-concurrency 1 && yarn run build; fi diff --git a/code/Control/UserDefinedFormController.php b/code/Control/UserDefinedFormController.php index 4f2e804..692f5ca 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -5,6 +5,7 @@ namespace SilverStripe\UserForms\Control; use Exception; use PageController; use Psr\Log\LoggerInterface; +use SilverStripe\AssetAdmin\Controller\AssetAdmin; use SilverStripe\Assets\File; use SilverStripe\Assets\Upload; use SilverStripe\Control\Controller; @@ -200,7 +201,7 @@ class UserDefinedFormController extends PageController }); })(jQuery); JS - , 'UserFormsConditional-' . $form->ID); + , 'UserFormsConditional-' . $form->ID); } } @@ -255,10 +256,8 @@ JS // create the file from post data $upload = Upload::create(); - $file = File::create(); - $file->ShowInSearch = 0; try { - $upload->loadIntoFile($_FILES[$field->Name], $file, $foldername); + $upload->loadIntoFile($_FILES[$field->Name], null, $foldername); } catch (ValidationException $e) { $validationResult = $e->getResult(); foreach ($validationResult->getMessages() as $message) { @@ -267,6 +266,16 @@ JS Controller::curr()->redirectBack(); return; } + /** @var AssetContainer|File $file */ + $file = $upload->getFile(); + $file->ShowInSearch = 0; + $file->write(); + + // generate image thumbnail to show in asset-admin + // you can run userforms without asset-admin, so need to ensure asset-admin is installed + if (class_exists(AssetAdmin::class)) { + AssetAdmin::singleton()->generateThumbnails($file); + } // write file to form field $submittedField->UploadedFileID = $file->ID; diff --git a/tests/Control/UserDefinedFormControllerTest.php b/tests/Control/UserDefinedFormControllerTest.php index 434edce..709f9d4 100644 --- a/tests/Control/UserDefinedFormControllerTest.php +++ b/tests/Control/UserDefinedFormControllerTest.php @@ -2,14 +2,21 @@ namespace SilverStripe\UserForms\Tests\Control; +use SilverStripe\Assets\Dev\TestAssetStore; +use SilverStripe\Assets\File; +use SilverStripe\Assets\Storage\AssetStore; +use SilverStripe\Assets\Upload_Validator; use SilverStripe\Control\HTTPResponse; +use SilverStripe\Control\Session; use SilverStripe\Core\Config\Config; +use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\CSSContentParser; use SilverStripe\Dev\FunctionalTest; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FormAction; use SilverStripe\ORM\DataObject; use SilverStripe\UserForms\Control\UserDefinedFormController; +use SilverStripe\UserForms\Model\EditableFormField\EditableFileField; use SilverStripe\UserForms\Model\EditableFormField\EditableTextField; use SilverStripe\UserForms\Model\Recipient\EmailRecipient; use SilverStripe\UserForms\Model\Submission\SubmittedFormField; @@ -32,9 +39,18 @@ class UserDefinedFormControllerTest extends FunctionalTest { parent::setUp(); + // Set backend and base url + TestAssetStore::activate('AssetStoreTest'); + Config::modify()->merge(SSViewer::class, 'themes', ['simple', '$default']); } + public function tearDown() + { + TestAssetStore::reset(); + parent::tearDown(); + } + public function testProcess() { $form = $this->setupFormFrontend(); @@ -367,4 +383,40 @@ class UserDefinedFormControllerTest extends FunctionalTest // check emails $this->assertEmailSent('test@example.com', 'no-reply@example.com', 'Email Subject: Basic Value'); } + + public function testImageThumbnailCreated() + { + 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()); + + /** @var File $image */ + // Getting File instead of Image so that we still delete the physical file in case it was + // created with the wrong ClassName + // Using StartsWith in-case of existing file so was created as testfile-v2.jpg + $image = File::get()->filter(['Name:StartsWith' => 'testfile'])->last(); + $this->assertNotNull($image); + + // Assert thumbnail variant created + /** @var AssetStore $store */ + $store = Injector::inst()->get(AssetStore::class); + $this->assertTrue($store->exists($image->getFilename(), $image->getHash(), 'FitMaxWzM1MiwyNjRd')); + } } diff --git a/tests/Control/fixtures/testfile.jpg b/tests/Control/fixtures/testfile.jpg new file mode 100644 index 0000000..7aee322 Binary files /dev/null and b/tests/Control/fixtures/testfile.jpg differ diff --git a/tests/UserFormsTest.yml b/tests/UserFormsTest.yml index 9098845..1336d62 100644 --- a/tests/UserFormsTest.yml +++ b/tests/UserFormsTest.yml @@ -175,6 +175,11 @@ SilverStripe\UserForms\Model\EditableFormField\EditableRadioField: - =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-5 - =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-6 +SilverStripe\UserForms\Model\EditableFormField\EditableFileField: + file-field-1: + Name: 'file_field_name' + Title: 'File field title' + SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd: group1end: Name: group1end @@ -349,3 +354,8 @@ SilverStripe\UserForms\Model\UserDefinedForm: Title: Form with MultipleOption fields Fields: - =>SilverStripe\UserForms\Model\EditableFormField\EditableDropdown.basic-dropdown + + upload-form: + Title: 'Form with upload field' + Fields: + - =>SilverStripe\UserForms\Model\EditableFormField\EditableFileField.file-field-1