diff --git a/.travis.yml b/.travis.yml index 42d5f8275..73b9ff54a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ dist: xenial services: - mysql + - postgresql env: global: @@ -12,17 +13,15 @@ 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.x-dev PHPUNIT_TEST=1 PHPCS_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.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=MYSQL RECIPE_VERSION=4.x-dev PHPUNIT_TEST=1 PHPUNIT_COVERAGE_TEST=1 - php: 7.3 + env: DB=PGSQL RECIPE_VERSION=4.x-dev PHPUNIT_TEST=1 + - php: 7.4 env: DB=MYSQL RECIPE_VERSION=4.x-dev PHPUNIT_TEST=1 before_script: @@ -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 4f2e8049f..692f5ca8f 100644 --- a/code/Control/UserDefinedFormController.php +++ b/code/Control/UserDefinedFormController.php @@ -5,6 +5,7 @@ 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 @@ public function generateConditionalJavascript() }); })(jQuery); JS - , 'UserFormsConditional-' . $form->ID); + , 'UserFormsConditional-' . $form->ID); } } @@ -255,10 +256,8 @@ public function process($data, $form) // 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 @@ public function process($data, $form) 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 434edce2a..709f9d4bc 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 @@ protected function setUp() { 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 @@ public function testRecipientSubjectMergeFields() // 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 000000000..7aee322fe Binary files /dev/null and b/tests/Control/fixtures/testfile.jpg differ diff --git a/tests/Model/UserDefinedFormTest.php b/tests/Model/UserDefinedFormTest.php index 7f3ae51b5..1c0e7b9c6 100644 --- a/tests/Model/UserDefinedFormTest.php +++ b/tests/Model/UserDefinedFormTest.php @@ -176,8 +176,14 @@ public function testGetEmailTemplateDropdownValues() $result = $recipient->getEmailTemplateDropdownValues(); // Installation path can be as a project when testing in Travis, so check partial match - $this->assertContains('email' . DIRECTORY_SEPARATOR . 'SubmittedFormEmail', key($result)); - $this->assertSame('SubmittedFormEmail', current($result)); + $foundKey = false; + foreach (array_keys($result) as $key) { + if (strpos($key, 'email' . DIRECTORY_SEPARATOR . 'SubmittedFormEmail') !== false) { + $foundKey = true; + } + } + $this->assertTrue($foundKey); + $this->assertTrue(in_array('SubmittedFormEmail', array_values($result))); } public function testEmailTemplateExists() diff --git a/tests/UserFormsTest.yml b/tests/UserFormsTest.yml index f0d687117..7d0925892 100644 --- a/tests/UserFormsTest.yml +++ b/tests/UserFormsTest.yml @@ -247,6 +247,11 @@ SilverStripe\UserForms\Model\EditableFormField\EditableRadioField: - =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-y-2 - =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-n-2 +SilverStripe\UserForms\Model\EditableFormField\EditableFileField: + file-field-1: + Name: 'file_field_name' + Title: 'File field title' + SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd: group1end: Name: group1end @@ -437,6 +442,11 @@ SilverStripe\UserForms\Model\UserDefinedForm: Fields: - =>SilverStripe\UserForms\Model\EditableFormField\EditableDropdown.basic-dropdown + upload-form: + Title: 'Form with upload field' + Fields: + - =>SilverStripe\UserForms\Model\EditableFormField\EditableFileField.file-field-1 + SilverStripe\UserForms\Model\EditableCustomRule: rule1: Display: Show