[TASK] Use name-resolution instead of strings where possible: 11
[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 /**
18 * Tests for \TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileController
19 */
20 class FileControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @var \TYPO3\CMS\Backend\Controller\File\FileController|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
24 */
25 protected $fileController;
26
27 /**
28 * @var \TYPO3\CMS\Core\Resource\File|\PHPUnit_Framework_MockObject_MockObject
29 */
30 protected $fileResourceMock;
31
32 /**
33 * @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject
34 */
35 protected $folderResourceMock;
36
37 /**
38 * @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility|\PHPUnit_Framework_MockObject_MockObject
39 */
40 protected $mockFileProcessor;
41
42 /**
43 * @var \TYPO3\CMS\Core\Http\AjaxRequestHandler|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $mockAjaxRequestHandler;
46
47 /**
48 * Sets up this test case.
49 */
50 protected function setUp() {
51 $this->fileResourceMock = $this->getMock(\TYPO3\CMS\Core\Resource\File::class, array('toArray', 'getModificationTime', 'getExtension'), array(), '', FALSE);
52 $this->folderResourceMock = $this->getMock(\TYPO3\CMS\Core\Resource\Folder::class, array('getIdentifier'), array(), '', FALSE);
53 $this->mockFileProcessor = $this->getMock(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class, array('getErrorMessages'), array(), '', FALSE);
54
55 $this->fileResourceMock->expects($this->any())->method('toArray')->will($this->returnValue(array('id' => 'foo')));
56 $this->fileResourceMock->expects($this->any())->method('getModificationTime')->will($this->returnValue(123456789));
57 $this->fileResourceMock->expects($this->any())->method('getExtension')->will($this->returnValue('html'));
58 }
59
60 /**
61 * @test
62 */
63 public function flattenResultDataValueFlattensFileAndFolderResourcesButReturnsAnythingElseAsIs() {
64 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('dummy'));
65
66 $this->folderResourceMock->expects($this->once())->method('getIdentifier')->will($this->returnValue('bar'));
67
68 $this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue(array()));
69
70 $this->assertTrue($this->fileController->_call('flattenResultDataValue', TRUE));
71 $this->assertSame(array(), $this->fileController->_call('flattenResultDataValue', array()));
72
73 $this->assertSame(
74 array(
75 'id' => 'foo',
76 'date' => '29-11-73',
77 'iconClasses' => 't3-icon t3-icon-mimetypes t3-icon-mimetypes-text t3-icon-text-html'
78 ),
79 $this->fileController->_call('flattenResultDataValue', $this->fileResourceMock)
80 );
81
82 $this->assertSame(
83 'bar',
84 $this->fileController->_call('flattenResultDataValue', $this->folderResourceMock)
85 );
86 }
87
88 /**
89 * @test
90 */
91 public function processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData() {
92 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('init', 'main'));
93 $this->mockAjaxRequestHandler = $this->getMock(\TYPO3\CMS\Core\Http\AjaxRequestHandler::class, array('addContent', 'setContentFormat'), array(), '', FALSE);
94
95 $fileData = array('delete' => array(TRUE));
96 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
97 $this->fileController->_set('fileData', $fileData);
98 $this->fileController->_set('redirect', FALSE);
99
100 $this->fileController->expects($this->once())->method('init');
101 $this->fileController->expects($this->once())->method('main');
102 $this->mockAjaxRequestHandler->expects($this->once())->method('addContent')->with('result', $fileData);
103 $this->mockAjaxRequestHandler->expects($this->once())->method('setContentFormat')->with('json');
104
105 $this->fileController->processAjaxRequest(array(), $this->mockAjaxRequestHandler);
106 }
107
108 /**
109 * @test
110 */
111 public function processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData() {
112 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('init', 'main'));
113 $this->mockAjaxRequestHandler = $this->getMock(\TYPO3\CMS\Core\Http\AjaxRequestHandler::class, array('addContent', 'setContentFormat'), array(), '', FALSE);
114
115 $fileData = array('editfile' => array(TRUE));
116 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
117 $this->fileController->_set('fileData', $fileData);
118 $this->fileController->_set('redirect', FALSE);
119
120 $this->fileController->expects($this->once())->method('init');
121 $this->fileController->expects($this->once())->method('main');
122 $this->mockAjaxRequestHandler->expects($this->once())->method('addContent')->with('result', $fileData);
123 $this->mockAjaxRequestHandler->expects($this->once())->method('setContentFormat')->with('json');
124
125 $this->fileController->processAjaxRequest(array(), $this->mockAjaxRequestHandler);
126 }
127
128 /**
129 * @test
130 */
131 public function processAjaxRequestUnzipProcessActuallyDoesNotChangeFileData() {
132 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('init', 'main'));
133 $this->mockAjaxRequestHandler = $this->getMock(\TYPO3\CMS\Core\Http\AjaxRequestHandler::class, array('addContent', 'setContentFormat'), array(), '', FALSE);
134
135 $fileData = array('unzip' => array(TRUE));
136 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
137 $this->fileController->_set('fileData', $fileData);
138 $this->fileController->_set('redirect', FALSE);
139
140 $this->fileController->expects($this->once())->method('init');
141 $this->fileController->expects($this->once())->method('main');
142 $this->mockAjaxRequestHandler->expects($this->once())->method('addContent')->with('result', $fileData);
143 $this->mockAjaxRequestHandler->expects($this->once())->method('setContentFormat')->with('json');
144
145 $this->fileController->processAjaxRequest(array(), $this->mockAjaxRequestHandler);
146 }
147
148 /**
149 * @test
150 */
151 public function processAjaxRequestUploadProcess() {
152 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('init', 'main'));
153 $this->mockAjaxRequestHandler = $this->getMock(\TYPO3\CMS\Core\Http\AjaxRequestHandler::class, array('addContent', 'setContentFormat'), array(), '', FALSE);
154
155 $fileData = array('upload' => array(array($this->fileResourceMock)));
156 $result = array('upload' => array(array(
157 'id' => 'foo',
158 'date' => '29-11-73',
159 'iconClasses' => 't3-icon t3-icon-mimetypes t3-icon-mimetypes-text t3-icon-text-html'
160 )));
161 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
162 $this->fileController->_set('fileData', $fileData);
163 $this->fileController->_set('redirect', FALSE);
164
165 $this->fileController->expects($this->once())->method('init');
166 $this->fileController->expects($this->once())->method('main');
167 $this->mockAjaxRequestHandler->expects($this->once())->method('addContent')->with('result', $result);
168 $this->mockAjaxRequestHandler->expects($this->once())->method('setContentFormat')->with('json');
169
170 $this->fileController->processAjaxRequest(array(), $this->mockAjaxRequestHandler);
171 }
172 }