‪TYPO3CMS  9.5
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 
17 use Prophecy\Argument;
18 use Psr\Http\Message\ServerRequestInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪FileControllerTest extends UnitTestCase
31 {
35  protected ‪$fileResourceMock;
36 
40  protected ‪$folderResourceMock;
41 
45  protected ‪$mockFileProcessor;
46 
50  protected ‪$request;
51 
55  protected ‪$response;
56 
60  protected function ‪setUp()
61  {
62  $this->fileResourceMock = $this->getMockBuilder(\‪TYPO3\CMS\Core\Resource\File::class)
63  ->setMethods(['toArray', 'getModificationTime', 'getExtension'])
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->folderResourceMock = $this->getMockBuilder(\‪TYPO3\CMS\Core\Resource\Folder::class)
67  ->setMethods(['getIdentifier'])
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->mockFileProcessor = $this->getMockBuilder(\‪TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
71  ->setMethods(['getErrorMessages'])
72  ->disableOriginalConstructor()
73  ->getMock();
74 
75  $this->fileResourceMock->expects($this->any())->method('toArray')->will($this->returnValue(['id' => 'foo']));
76  $this->fileResourceMock->expects($this->any())->method('getModificationTime')->will($this->returnValue(123456789));
77  $this->fileResourceMock->expects($this->any())->method('getExtension')->will($this->returnValue('html'));
78 
79  $serverRequest = $this->prophesize(ServerRequestInterface::class);
80  ‪$GLOBALS['TYPO3_REQUEST'] = $serverRequest->reveal();
81 
82  $this->request = new ‪ServerRequest();
83  $this->response = new ‪Response();
84  }
85 
90  {
91  $subject = $this->getAccessibleMock(FileController::class, ['dummy']);
92  $this->assertTrue($subject->_call('flattenResultDataValue', true));
93  $this->assertSame([], $subject->_call('flattenResultDataValue', []));
94  }
95 
100  {
101  $subject = $this->getAccessibleMock(FileController::class, ['dummy']);
102 
103  $iconFactoryProphecy = $this->prophesize(IconFactory::class);
104  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
105  $iconProphecy = $this->prophesize(Icon::class);
106  $iconProphecy->render()->shouldBeCalled()->willReturn('');
107  $iconFactoryProphecy->getIconForFileExtension(Argument::cetera())->willReturn($iconProphecy->reveal());
108 
109  $result = $subject->_call('flattenResultDataValue', $this->fileResourceMock);
110  $this->assertSame(
111  [
112  'id' => 'foo',
113  'date' => '29-11-73',
114  'icon' => '',
115  'thumbUrl' => '',
116  ],
117  $result
118  );
119  }
120 
125  {
126  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
127 
128  $fileData = ['delete' => [true]];
129  $subject->_set('fileProcessor', $this->mockFileProcessor);
130  $subject->_set('fileData', $fileData);
131  $subject->_set('redirect', false);
132 
133  $subject->expects($this->once())->method('main');
134 
135  $subject->processAjaxRequest($this->request, $this->response);
136  }
137 
142  {
143  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
144 
145  $fileData = ['editfile' => [true]];
146  $subject->_set('fileProcessor', $this->mockFileProcessor);
147  $subject->_set('fileData', $fileData);
148  $subject->_set('redirect', false);
149 
150  $subject->expects($this->once())->method('main');
151 
152  $subject->processAjaxRequest($this->request, $this->response);
153  }
154 
159  {
160  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
161 
162  $fileData = ['editfile' => [true]];
163  $subject->_set('fileProcessor', $this->mockFileProcessor);
164  $subject->_set('fileData', $fileData);
165  $subject->_set('redirect', false);
166 
167  $result = $subject->processAjaxRequest($this->request, $this->response);
168  $this->assertEquals(200, $result->getStatusCode());
169  }
170 
175  {
176  $subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
177  $this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue(['error occured']));
178  $subject->_set('fileProcessor', $this->mockFileProcessor);
179  $result = $subject->processAjaxRequest($this->request, $this->response);
180  $this->assertEquals(500, $result->getStatusCode());
181  }
182 }
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File
Definition: FileControllerTest.php:2
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\setUp
‪setUp()
Definition: FileControllerTest.php:55
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$response
‪Response PHPUnit_Framework_MockObject_MockObject $response
Definition: FileControllerTest.php:50
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:31
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$mockFileProcessor
‪TYPO3 CMS Core Utility File ExtendedFileUtility PHPUnit_Framework_MockObject_MockObject $mockFileProcessor
Definition: FileControllerTest.php:42
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\flattenResultDataValueFlattensFile
‪flattenResultDataValueFlattensFile()
Definition: FileControllerTest.php:94
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestReturnsStatus500IfErrorOccurs
‪processAjaxRequestReturnsStatus500IfErrorOccurs()
Definition: FileControllerTest.php:169
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$folderResourceMock
‪TYPO3 CMS Core Resource Folder PHPUnit_Framework_MockObject_MockObject $folderResourceMock
Definition: FileControllerTest.php:38
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$fileResourceMock
‪TYPO3 CMS Core Resource File PHPUnit_Framework_MockObject_MockObject $fileResourceMock
Definition: FileControllerTest.php:34
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:28
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:35
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestReturnsStatus200IfNoErrorOccures
‪processAjaxRequestReturnsStatus200IfNoErrorOccures()
Definition: FileControllerTest.php:153
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\$request
‪ServerRequest PHPUnit_Framework_MockObject_MockObject $request
Definition: FileControllerTest.php:46
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\flattenResultDataValueReturnsAnythingElseAsIs
‪flattenResultDataValueReturnsAnythingElseAsIs()
Definition: FileControllerTest.php:84
‪TYPO3\CMS\Backend\Controller\File\FileController
Definition: FileController.php:46
‪$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:119
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest
Definition: FileControllerTest.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileControllerTest\processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData
‪processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData()
Definition: FileControllerTest.php:136