Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Let AssetAdmin default to Upload_Validator default upload limits #1430

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions code/Controller/AssetAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,9 +1280,10 @@ protected function getUpload()
// filter out '' since this would be a regex problem on JS end
array_filter(File::getAllowedExtensions() ?? [])
);
$upload->getValidator()->setAllowedMaxFileSize(
$this->config()->max_upload_size
);
$maxFileSize = $this->config()->get('max_upload_size');
if ($maxFileSize !== null) {
$upload->getValidator()->setAllowedMaxFileSize($maxFileSize);
}

return $upload;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/php/Forms/UploadFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace SilverStripe\AssetAdmin\Tests\Forms;

use ReflectionMethod;
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\AssetAdmin\Tests\Forms\FileFormBuilderTest\FileOwner;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use Silverstripe\Assets\Dev\TestAssetStore;
use SilverStripe\Assets\Upload;
use SilverStripe\Assets\Upload_Validator;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
Expand Down Expand Up @@ -44,6 +47,35 @@ protected function tearDown(): void
parent::tearDown();
}

public function provideGetUploadMaxFileSize(): array
{
return [
[
'adminMaxFileSize' => null,
'expected' => 100,
],
[
'adminMaxFileSize' => 200,
'expected' => 200,
],
];
}

/**
* @dataProvider provideGetUploadMaxFileSize
*/
public function testGetUploadMaxFileSize(?int $adminMaxFileSize, int $expected): void
{
Upload_Validator::config()->set('default_max_file_size', ['*' => 100]);
AssetAdmin::config()->set('max_upload_size', $adminMaxFileSize);
$admin = new AssetAdmin();
$reflectionGetUpload = new ReflectionMethod($admin, 'getUpload');
$reflectionGetUpload->setAccessible(true);
/** @var Upload $upload */
$upload = $reflectionGetUpload->invoke($admin);
$this->assertSame($expected, $upload->getValidator()->getAllowedMaxFileSize());
}

public function testGetAttributes()
{
$field = UploadField::create('MyField');
Expand Down