Skip to content

Commit

Permalink
Merge branch '5.5' into 5
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed May 27, 2020
2 parents f27045b + c3d990f commit e54a825
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist: xenial

services:
- mysql
- postgresql

env:
global:
Expand All @@ -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:
Expand All @@ -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

Expand Down
17 changes: 13 additions & 4 deletions code/Control/UserDefinedFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -200,7 +201,7 @@ public function generateConditionalJavascript()
});
})(jQuery);
JS
, 'UserFormsConditional-' . $form->ID);
, 'UserFormsConditional-' . $form->ID);
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions tests/Control/UserDefinedFormControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -367,4 +383,40 @@ public function testRecipientSubjectMergeFields()
// check emails
$this->assertEmailSent('[email protected]', '[email protected]', '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'));
}
}
Binary file added tests/Control/fixtures/testfile.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions tests/Model/UserDefinedFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions tests/UserFormsTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e54a825

Please sign in to comment.