428ebb9be84067ec4e4883c15a28eb070d96eba4
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Controller / File / FileControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Controller\File;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Http\Response;
18 use TYPO3\CMS\Core\Http\ServerRequest;
19
20 /**
21 * Tests for \TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileController
22 */
23 class FileControllerTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
24 {
25 /**
26 * @var \TYPO3\CMS\Backend\Controller\File\FileController|\TYPO3\Components\TestingFramework\Core\AccessibleObjectInterface
27 */
28 protected $fileController;
29
30 /**
31 * @var \TYPO3\CMS\Core\Resource\File|\PHPUnit_Framework_MockObject_MockObject
32 */
33 protected $fileResourceMock;
34
35 /**
36 * @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject
37 */
38 protected $folderResourceMock;
39
40 /**
41 * @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility|\PHPUnit_Framework_MockObject_MockObject
42 */
43 protected $mockFileProcessor;
44
45 /**
46 * @var ServerRequest|\PHPUnit_Framework_MockObject_MockObject
47 */
48 protected $request;
49
50 /**
51 * @var Response|\PHPUnit_Framework_MockObject_MockObject
52 */
53 protected $response;
54
55 /**
56 * Sets up this test case.
57 */
58 protected function setUp()
59 {
60 $this->fileResourceMock = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\File::class)
61 ->setMethods(['toArray', 'getModificationTime', 'getExtension'])
62 ->disableOriginalConstructor()
63 ->getMock();
64 $this->folderResourceMock = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Folder::class)
65 ->setMethods(['getIdentifier'])
66 ->disableOriginalConstructor()
67 ->getMock();
68 $this->mockFileProcessor = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
69 ->setMethods(['getErrorMessages'])
70 ->disableOriginalConstructor()
71 ->getMock();
72
73 $this->fileResourceMock->expects($this->any())->method('toArray')->will($this->returnValue(['id' => 'foo']));
74 $this->fileResourceMock->expects($this->any())->method('getModificationTime')->will($this->returnValue(123456789));
75 $this->fileResourceMock->expects($this->any())->method('getExtension')->will($this->returnValue('html'));
76
77 $this->request = new ServerRequest();
78 $this->response = new Response();
79 }
80
81 /**
82 * @test
83 */
84 public function flattenResultDataValueFlattensFileAndFolderResourcesButReturnsAnythingElseAsIs()
85 {
86 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['dummy']);
87
88 $this->folderResourceMock->expects($this->once())->method('getIdentifier')->will($this->returnValue('bar'));
89
90 $this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue([]));
91
92 $this->assertTrue($this->fileController->_call('flattenResultDataValue', true));
93 $this->assertSame([], $this->fileController->_call('flattenResultDataValue', []));
94 $result = $this->fileController->_call('flattenResultDataValue', $this->fileResourceMock);
95 $this->assertContains('<span class="t3js-icon icon icon-size-small icon-state-default icon-mimetypes-text-html" data-identifier="mimetypes-text-html">', $result['icon']);
96 unset($result['icon']);
97 $this->assertSame(
98 [
99 'id' => 'foo',
100 'date' => '29-11-73',
101 'thumbUrl' => '',
102 ],
103 $result
104 );
105
106 $this->assertSame(
107 'bar',
108 $this->fileController->_call('flattenResultDataValue', $this->folderResourceMock)
109 );
110 }
111
112 /**
113 * @test
114 */
115 public function processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData()
116 {
117 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
118
119 $fileData = ['delete' => [true]];
120 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
121 $this->fileController->_set('fileData', $fileData);
122 $this->fileController->_set('redirect', false);
123
124 $this->fileController->expects($this->once())->method('main');
125
126 $this->fileController->processAjaxRequest($this->request, $this->response);
127 }
128
129 /**
130 * @test
131 */
132 public function processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData()
133 {
134 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
135
136 $fileData = ['editfile' => [true]];
137 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
138 $this->fileController->_set('fileData', $fileData);
139 $this->fileController->_set('redirect', false);
140
141 $this->fileController->expects($this->once())->method('main');
142
143 $this->fileController->processAjaxRequest($this->request, $this->response);
144 }
145
146 /**
147 * @test
148 */
149 public function processAjaxRequestReturnsStatus200IfNoErrorOccures()
150 {
151 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
152
153 $fileData = ['editfile' => [true]];
154 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
155 $this->fileController->_set('fileData', $fileData);
156 $this->fileController->_set('redirect', false);
157
158 $result = $this->fileController->processAjaxRequest($this->request, $this->response);
159 $this->assertEquals(200, $result->getStatusCode());
160 }
161
162 /**
163 * @test
164 */
165 public function processAjaxRequestReturnsStatus500IfErrorOccurs()
166 {
167 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, ['init', 'main']);
168 $this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue(['error occured']));
169 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
170 $result = $this->fileController->processAjaxRequest($this->request, $this->response);
171 $this->assertEquals(500, $result->getStatusCode());
172 }
173 }