[!!!][FEATURE] Introduce PSR-7-based Routing for Backend AJAX Requests
[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\ServerRequest;
18 use TYPO3\CMS\Core\Http\Response;
19
20 /**
21 * Tests for \TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileController
22 */
23 class FileControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * @var \TYPO3\CMS\Backend\Controller\File\FileController|\TYPO3\CMS\Core\Tests\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 $this->fileResourceMock = $this->getMock(\TYPO3\CMS\Core\Resource\File::class, array('toArray', 'getModificationTime', 'getExtension'), array(), '', FALSE);
60 $this->folderResourceMock = $this->getMock(\TYPO3\CMS\Core\Resource\Folder::class, array('getIdentifier'), array(), '', FALSE);
61 $this->mockFileProcessor = $this->getMock(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class, array('getErrorMessages'), array(), '', FALSE);
62
63 $this->fileResourceMock->expects($this->any())->method('toArray')->will($this->returnValue(array('id' => 'foo')));
64 $this->fileResourceMock->expects($this->any())->method('getModificationTime')->will($this->returnValue(123456789));
65 $this->fileResourceMock->expects($this->any())->method('getExtension')->will($this->returnValue('html'));
66
67 $this->request = new ServerRequest();
68 $this->response = new Response();
69 }
70
71 /**
72 * @test
73 */
74 public function flattenResultDataValueFlattensFileAndFolderResourcesButReturnsAnythingElseAsIs() {
75 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('dummy'));
76
77 $this->folderResourceMock->expects($this->once())->method('getIdentifier')->will($this->returnValue('bar'));
78
79 $this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue(array()));
80
81 $this->assertTrue($this->fileController->_call('flattenResultDataValue', TRUE));
82 $this->assertSame(array(), $this->fileController->_call('flattenResultDataValue', array()));
83 $result = $this->fileController->_call('flattenResultDataValue', $this->fileResourceMock);
84 $this->assertContains('<span class="icon icon-size-small icon-state-default icon-mimetypes-text-html">', $result['icon']);
85 unset($result['icon']);
86 $this->assertSame(
87 array(
88 'id' => 'foo',
89 'date' => '29-11-73',
90 'thumbUrl' => '',
91 ),
92 $result
93 );
94
95 $this->assertSame(
96 'bar',
97 $this->fileController->_call('flattenResultDataValue', $this->folderResourceMock)
98 );
99 }
100
101 /**
102 * @test
103 */
104 public function processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData() {
105 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('init', 'main'));
106
107 $fileData = array('delete' => array(TRUE));
108 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
109 $this->fileController->_set('fileData', $fileData);
110 $this->fileController->_set('redirect', FALSE);
111
112 $this->fileController->expects($this->once())->method('main');
113
114 $this->fileController->processAjaxRequest($this->request, $this->response);
115 }
116
117 /**
118 * @test
119 */
120 public function processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData() {
121 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('init', 'main'));
122
123 $fileData = array('editfile' => array(TRUE));
124 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
125 $this->fileController->_set('fileData', $fileData);
126 $this->fileController->_set('redirect', FALSE);
127
128 $this->fileController->expects($this->once())->method('main');
129
130 $this->fileController->processAjaxRequest($this->request, $this->response);
131 }
132
133 /**
134 * @test
135 */
136 public function processAjaxRequestUnzipProcessActuallyDoesNotChangeFileData() {
137 $this->fileController = $this->getAccessibleMock(\TYPO3\CMS\Backend\Controller\File\FileController::class, array('init', 'main'));
138
139 $fileData = array('unzip' => array(TRUE));
140 $this->fileController->_set('fileProcessor', $this->mockFileProcessor);
141 $this->fileController->_set('fileData', $fileData);
142 $this->fileController->_set('redirect', FALSE);
143
144 $this->fileController->expects($this->once())->method('main');
145
146 $this->fileController->processAjaxRequest($this->request, $this->response);
147 }
148
149 }