Skip to content

Commit

Permalink
ENH Let AssetAdmin default to Upload_Validator default upload limits (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jan 8, 2024
1 parent 09bf235 commit a144ac1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions code/Controller/AssetAdmin.php
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
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

0 comments on commit a144ac1

Please sign in to comment.