-
Notifications
You must be signed in to change notification settings - Fork 638
/
FileControllerTest.php
182 lines (155 loc) · 6.21 KB
/
FileControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace TYPO3\CMS\Backend\Tests\Unit\Controller\File;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Prophecy\Argument;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Controller\File\FileController;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Tests for \TYPO3\CMS\Backend\Tests\Unit\Controller\File\FileController
*/
class FileControllerTest extends UnitTestCase
{
/**
* @var \TYPO3\CMS\Core\Resource\File|\PHPUnit_Framework_MockObject_MockObject
*/
protected $fileResourceMock;
/**
* @var \TYPO3\CMS\Core\Resource\Folder|\PHPUnit_Framework_MockObject_MockObject
*/
protected $folderResourceMock;
/**
* @var \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility|\PHPUnit_Framework_MockObject_MockObject
*/
protected $mockFileProcessor;
/**
* @var ServerRequest|\PHPUnit_Framework_MockObject_MockObject
*/
protected $request;
/**
* @var Response|\PHPUnit_Framework_MockObject_MockObject
*/
protected $response;
/**
* Sets up this test case.
*/
protected function setUp()
{
$this->fileResourceMock = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\File::class)
->setMethods(['toArray', 'getModificationTime', 'getExtension'])
->disableOriginalConstructor()
->getMock();
$this->folderResourceMock = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\Folder::class)
->setMethods(['getIdentifier'])
->disableOriginalConstructor()
->getMock();
$this->mockFileProcessor = $this->getMockBuilder(\TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::class)
->setMethods(['getErrorMessages'])
->disableOriginalConstructor()
->getMock();
$this->fileResourceMock->expects($this->any())->method('toArray')->will($this->returnValue(['id' => 'foo']));
$this->fileResourceMock->expects($this->any())->method('getModificationTime')->will($this->returnValue(123456789));
$this->fileResourceMock->expects($this->any())->method('getExtension')->will($this->returnValue('html'));
$serverRequest = $this->prophesize(ServerRequestInterface::class);
$GLOBALS['TYPO3_REQUEST'] = $serverRequest->reveal();
$this->request = new ServerRequest();
$this->response = new Response();
}
/**
* @test
*/
public function flattenResultDataValueReturnsAnythingElseAsIs()
{
$subject = $this->getAccessibleMock(FileController::class, ['dummy']);
$this->assertTrue($subject->_call('flattenResultDataValue', true));
$this->assertSame([], $subject->_call('flattenResultDataValue', []));
}
/**
* @test
*/
public function flattenResultDataValueFlattensFile()
{
$subject = $this->getAccessibleMock(FileController::class, ['dummy']);
$iconFactoryProphecy = $this->prophesize(IconFactory::class);
GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
$iconProphecy = $this->prophesize(Icon::class);
$iconProphecy->render()->shouldBeCalled()->willReturn('');
$iconFactoryProphecy->getIconForFileExtension(Argument::cetera())->willReturn($iconProphecy->reveal());
$result = $subject->_call('flattenResultDataValue', $this->fileResourceMock);
$this->assertSame(
[
'id' => 'foo',
'date' => '29-11-73',
'icon' => '',
'thumbUrl' => '',
],
$result
);
}
/**
* @test
*/
public function processAjaxRequestDeleteProcessActuallyDoesNotChangeFileData()
{
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
$fileData = ['delete' => [true]];
$subject->_set('fileProcessor', $this->mockFileProcessor);
$subject->_set('fileData', $fileData);
$subject->_set('redirect', false);
$subject->expects($this->once())->method('main');
$subject->processAjaxRequest($this->request, $this->response);
}
/**
* @test
*/
public function processAjaxRequestEditFileProcessActuallyDoesNotChangeFileData()
{
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
$fileData = ['editfile' => [true]];
$subject->_set('fileProcessor', $this->mockFileProcessor);
$subject->_set('fileData', $fileData);
$subject->_set('redirect', false);
$subject->expects($this->once())->method('main');
$subject->processAjaxRequest($this->request, $this->response);
}
/**
* @test
*/
public function processAjaxRequestReturnsStatus200IfNoErrorOccures()
{
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
$fileData = ['editfile' => [true]];
$subject->_set('fileProcessor', $this->mockFileProcessor);
$subject->_set('fileData', $fileData);
$subject->_set('redirect', false);
$result = $subject->processAjaxRequest($this->request, $this->response);
$this->assertEquals(200, $result->getStatusCode());
}
/**
* @test
*/
public function processAjaxRequestReturnsStatus500IfErrorOccurs()
{
$subject = $this->getAccessibleMock(FileController::class, ['init', 'main']);
$this->mockFileProcessor->expects($this->any())->method('getErrorMessages')->will($this->returnValue(['error occured']));
$subject->_set('fileProcessor', $this->mockFileProcessor);
$result = $subject->processAjaxRequest($this->request, $this->response);
$this->assertEquals(500, $result->getStatusCode());
}
}