mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
FIX Create image thumbnails for asset-admin for images uploaded through userforms (#969)
This commit is contained in:
parent
319fa4734c
commit
c3d990f70f
15
.travis.yml
15
.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
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
BIN
tests/Control/fixtures/testfile.jpg
Normal file
BIN
tests/Control/fixtures/testfile.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.4 KiB |
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user