Theming: Handle errors on uploaded files properly

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2017-09-20 12:32:41 +02:00
parent 42bd942619
commit c337c8fa45
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
2 changed files with 47 additions and 17 deletions

View File

@ -207,11 +207,32 @@ class ThemingController extends Controller {
} }
$newLogo = $this->request->getUploadedFile('uploadlogo'); $newLogo = $this->request->getUploadedFile('uploadlogo');
$newBackgroundLogo = $this->request->getUploadedFile('upload-login-background'); $newBackgroundLogo = $this->request->getUploadedFile('upload-login-background');
$error = null;
$phpFileUploadErrors = array(
0 => $this->l10n->t('There is no error, the file uploaded with success'),
1 => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
2 => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
3 => $this->l10n->t('The uploaded file was only partially uploaded'),
4 => $this->l10n->t('No file was uploaded'),
6 => $this->l10n->t('Missing a temporary folder'),
7 => $this->l10n->t('Failed to write file to disk.'),
8 => $this->l10n->t('A PHP extension stopped the file upload.'),
);
if (empty($newLogo) && empty($newBackgroundLogo)) { if (empty($newLogo) && empty($newBackgroundLogo)) {
$error = $this->l10n->t('No file uploaded');
}
if (!empty($newLogo) && array_key_exists('error', $newLogo) && $newLogo['error'] !== UPLOAD_ERR_OK) {
$error = $phpFileUploadErrors[$newLogo['error']];
}
if (!empty($newBackgroundLogo) && array_key_exists('error', $newBackgroundLogo) && $newBackgroundLogo['error'] !== UPLOAD_ERR_OK) {
$error = $phpFileUploadErrors[$newBackgroundLogo['error']];
}
if ($error !== null) {
return new DataResponse( return new DataResponse(
[ [
'data' => [ 'data' => [
'message' => $this->l10n->t('No file uploaded') 'message' => $error
] ]
], ],
Http::STATUS_UNPROCESSABLE_ENTITY Http::STATUS_UNPROCESSABLE_ENTITY

View File

@ -131,8 +131,9 @@ class ThemingControllerTest extends TestCase {
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->once())
->method('t') ->method('t')
->with($message) ->will($this->returnCallback(function($str) {
->willReturn($message); return $str;
}));
$this->scssCacher $this->scssCacher
->expects($this->once()) ->expects($this->once())
->method('getCachedSCSS') ->method('getCachedSCSS')
@ -183,8 +184,9 @@ class ThemingControllerTest extends TestCase {
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->once())
->method('t') ->method('t')
->with($message) ->will($this->returnCallback(function($str) {
->willReturn($message); return $str;
}));
$expected = new DataResponse( $expected = new DataResponse(
[ [
@ -215,10 +217,11 @@ class ThemingControllerTest extends TestCase {
->with('upload-login-background') ->with('upload-login-background')
->willReturn(null); ->willReturn(null);
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->any())
->method('t') ->method('t')
->with('No file uploaded') ->will($this->returnCallback(function($str) {
->willReturn('No file uploaded'); return $str;
}));
$expected = new DataResponse( $expected = new DataResponse(
[ [
@ -282,6 +285,7 @@ class ThemingControllerTest extends TestCase {
'tmp_name' => $tmpLogo, 'tmp_name' => $tmpLogo,
'type' => 'text/svg', 'type' => 'text/svg',
'name' => 'logo.svg', 'name' => 'logo.svg',
'error' => 0,
]); ]);
$this->request $this->request
->expects($this->at(2)) ->expects($this->at(2))
@ -289,10 +293,11 @@ class ThemingControllerTest extends TestCase {
->with('upload-login-background') ->with('upload-login-background')
->willReturn(null); ->willReturn(null);
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->any())
->method('t') ->method('t')
->with('Saved') ->will($this->returnCallback(function($str) {
->willReturn('Saved'); return $str;
}));
$file = $this->createMock(ISimpleFile::class); $file = $this->createMock(ISimpleFile::class);
@ -357,12 +362,14 @@ class ThemingControllerTest extends TestCase {
'tmp_name' => $tmpLogo, 'tmp_name' => $tmpLogo,
'type' => 'text/svg', 'type' => 'text/svg',
'name' => 'logo.svg', 'name' => 'logo.svg',
'error' => 0,
]); ]);
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->any())
->method('t') ->method('t')
->with('Saved') ->will($this->returnCallback(function($str) {
->willReturn('Saved'); return $str;
}));
$file = $this->createMock(ISimpleFile::class); $file = $this->createMock(ISimpleFile::class);
$folder = $this->createMock(ISimpleFolder::class); $folder = $this->createMock(ISimpleFolder::class);
@ -425,12 +432,14 @@ class ThemingControllerTest extends TestCase {
'tmp_name' => $tmpLogo, 'tmp_name' => $tmpLogo,
'type' => 'text/svg', 'type' => 'text/svg',
'name' => 'logo.svg', 'name' => 'logo.svg',
'error' => 0,
]); ]);
$this->l10n $this->l10n
->expects($this->once()) ->expects($this->any())
->method('t') ->method('t')
->with('Unsupported image type') ->will($this->returnCallback(function($str) {
->willReturn('Unsupported image type'); return $str;
}));
$folder = $this->createMock(ISimpleFolder::class); $folder = $this->createMock(ISimpleFolder::class);
$this->appData $this->appData