TYPO3 CMS  TYPO3_8-7
FileControllerTest.php
Go to the documentation of this file.
1 <?php
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 
19 
23 class FileControllerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
28  protected $fileController;
29 
33  protected $fileResourceMock;
34 
39 
43  protected $mockFileProcessor;
44 
48  protected $request;
49 
53  protected $response;
54 
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 
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 
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 
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 
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 
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 }