‪TYPO3CMS  10.4
FileControllerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
19 use Psr\Http\Message\ServerRequestInterface;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪FileControllerTest extends UnitTestCase
35 {
39  protected ‪$fileResourceMock;
40 
44  protected ‪$folderResourceMock;
45 
49  protected ‪$mockFileProcessor;
50 
54  protected ‪$request;
55 
59  protected ‪$response;
60 
64  protected function ‪setUp(): void
65  {
66  $this->fileResourceMock = $this->getMockBuilder(File::class)
67  ->setMethods(['toArray', 'getModificationTime', 'getExtension'])
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->folderResourceMock = $this->getMockBuilder(Folder::class)
71  ->setMethods(['getIdentifier'])
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->mockFileProcessor = $this->getMockBuilder(ExtendedFileUtility::class)
75  ->setMethods(['getErrorMessages'])
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->fileResourceMock->expects(self::any())->method('toArray')->willReturn(['id' => 'foo']);
80  $this->fileResourceMock->expects(self::any())->method('getModificationTime')->willReturn(123456789);
81  $this->fileResourceMock->expects(self::any())->method('getExtension')->willReturn('html');
82 
83  $serverRequest = $this->prophesize(ServerRequestInterface::class);
84  ‪$GLOBALS['TYPO3_REQUEST'] = $serverRequest->reveal();
85 
86  $this->request = new ‪ServerRequest();
87  $this->response = new ‪Response();
88  }
89 
94  {
95  $subject = $this->getAccessibleMock(FileController::class, ['dummy']);
96  self::assertTrue($subject->_call('flattenResultDataValue', true));
97  self::assertSame([], $subject->_call('flattenResultDataValue', []));
98  }
99 
103  public function ‪flattenResultDataValueFlattensFile()
104  {
105  $subject = $this->getAccessibleMock(FileController::class, ['dummy']);
106 
107  $iconFactoryProphecy = $this->prophesize(IconFactory::class);
108  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
109  $iconProphecy = $this->prophesize(Icon::class);
110  $iconProphecy->render()->shouldBeCalled()->willReturn('');
111  $iconFactoryProphecy->getIconForFileExtension(Argument::cetera())->willReturn($iconProphecy->reveal());
112 
113  $result = $subject->_call('flattenResultDataValue', $this->fileResourceMock);
114  self::assertSame(
115  [
116  'id' => 'foo',
117  'date' => '29-11-73',
118  'icon' => '',
119  'thumbUrl' => '',
120  ],
121  $result
122  );
123  }
124 
129  {
130  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
131 
132  $fileData = ['delete' => [true]];
133  $subject->_set('fileProcessor', $this->mockFileProcessor);
134  $subject->_set('fileData', $fileData);
135  $subject->_set('redirect', false);
136 
137  $subject->expects(self::once())->method('main');
138 
139  $subject->processAjaxRequest($this->request, $this->response);
140  }
141 
146  {
147  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
148 
149  $fileData = ['editfile' => [true]];
150  $subject->_set('fileProcessor', $this->mockFileProcessor);
151  $subject->_set('fileData', $fileData);
152  $subject->_set('redirect', false);
153 
154  $subject->expects(self::once())->method('main');
155 
156  $subject->processAjaxRequest($this->request, $this->response);
157  }
158 
163  {
164  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
165 
166  $fileData = ['editfile' => [true]];
167  $subject->_set('fileProcessor', $this->mockFileProcessor);
168  $subject->_set('fileData', $fileData);
169  $subject->_set('redirect', false);
170 
171  $result = $subject->processAjaxRequest($this->request, $this->response);
172  self::assertEquals(200, $result->getStatusCode());
173  }
174 
179  {
180  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
181  $this->mockFileProcessor->expects(self::any())->method('getErrorMessages')->willReturn(['error occurred']);
182  $subject->_set('fileProcessor', $this->mockFileProcessor);
183  $result = $subject->processAjaxRequest($this->request, $this->response);
184  self::assertEquals(500, $result->getStatusCode());
185  }
186 }
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File
Definition: FileControllerTest.php:16
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\setUp
‪setUp()
Definition: FileControllerTest.php:59
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$fileResourceMock
‪TYPO3 CMS Core Resource File PHPUnit Framework MockObject MockObject $fileResourceMock
Definition: FileControllerTest.php:38
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestReturnsStatus200IfNoErrorOccurs
‪processAjaxRequestReturnsStatus200IfNoErrorOccurs()
Definition: FileControllerTest.php:157
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\flattenResultDataValueFlattensFile
‪flattenResultDataValueFlattensFile()
Definition: FileControllerTest.php:98
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestReturnsStatus500IfErrorOccurs
‪processAjaxRequestReturnsStatus500IfErrorOccurs()
Definition: FileControllerTest.php:173
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$response
‪Response PHPUnit Framework MockObject MockObject $response
Definition: FileControllerTest.php:54
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$mockFileProcessor
‪TYPO3 CMS Core Utility File ExtendedFileUtility PHPUnit Framework MockObject MockObject $mockFileProcessor
Definition: FileControllerTest.php:46
‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility
Definition: ExtendedFileUtility.php:70
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$folderResourceMock
‪TYPO3 CMS Core Resource Folder PHPUnit Framework MockObject MockObject $folderResourceMock
Definition: FileControllerTest.php:42
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\flattenResultDataValueReturnsAnythingElseAsIs
‪flattenResultDataValueReturnsAnythingElseAsIs()
Definition: FileControllerTest.php:88
‪TYPO3\CMS\Backend\Controller\File\FileController
Definition: FileController.php:47
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData
‪processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData()
Definition: FileControllerTest.php:123
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest
Definition: FileControllerTest.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData
‪processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData()
Definition: FileControllerTest.php:140
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$request
‪ServerRequest PHPUnit Framework MockObject MockObject $request
Definition: FileControllerTest.php:50