[BUGFIX] Show errors on ajax file actions 35/50835/2
authorAlexander Opitz <opitz.alexander@googlemail.com>
Wed, 23 Nov 2016 11:43:21 +0000 (12:43 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Thu, 1 Dec 2016 16:10:22 +0000 (17:10 +0100)
Respond with status code 500 on ajax file actions, so JS can differ
between success and failure.

Resolves: #78776
Related: #69916
Releases: master, 7.6
Change-Id: I166cb86be64a26d1a3b44bfba626bba3fd151df1
Reviewed-on: https://review.typo3.org/50835
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/backend/Classes/Controller/File/FileController.php
typo3/sysext/backend/Tests/Unit/Controller/File/FileControllerTest.php

index 057a2e2..f3092a3 100644 (file)
@@ -208,8 +208,10 @@ class FileController
         $this->main();
         $errors = $this->fileProcessor->getErrorMessages();
         if (!empty($errors)) {
-            $response->getBody()->write(implode(',', $errors));
-            $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
+            $response->getBody()->write('<t3err>' . implode(',', $errors) . '</t3err>');
+            $response = $response
+                ->withHeader('Content-Type', 'text/html; charset=utf-8')
+                ->withStatus(500, '(AJAX)');
         } else {
             $flatResult = [];
             foreach ($this->fileData as $action => $results) {
index 068423e..9c4f6a5 100644 (file)
@@ -150,4 +150,32 @@ class FileControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
 
         $this->fileController->processAjaxRequest($this->request, $this->response);
     }
+
+    /**
+     * @test
+     */
+    public function processAjaxRequestReturnsStatus200IfNoErrorOccures()
+    {
+        $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
+
+        $fileData = ['editfile' => [true]];
+        $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
+        $this->fileController->_set('fileData', $fileData);
+        $this->fileController->_set('redirect', false);
+
+        $result = $this->fileController->processAjaxRequest($this->request, $this->response);
+        $this->assertEquals(200, $result->getStatusCode());
+    }
+
+    /**
+     * @test
+     */
+    public function processAjaxRequestReturnsStatus500IfErrorOccurs()
+    {
+        $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
+        $this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue(['error occured']));
+        $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
+        $result = $this->fileController->processAjaxRequest($this->request, $this->response);
+        $this->assertEquals(500, $result->getStatusCode());
+    }
 }