1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Björn Schießle <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
namespace OCA\FilesTextEditor\Tests\Controller; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use OC\HintException; |
27
|
|
|
use OCA\FilesTextEditor\Controller\FileHandlingController; |
28
|
|
|
use OCP\Files\File; |
29
|
|
|
use OCP\Files\Folder; |
30
|
|
|
use OCP\Files\ForbiddenException; |
31
|
|
|
use OCP\IL10N; |
32
|
|
|
use OCP\ILogger; |
33
|
|
|
use OCP\IRequest; |
34
|
|
|
use OCP\Lock\LockedException; |
35
|
|
|
use Test\TestCase; |
36
|
|
|
|
37
|
|
|
class FileHandlingControllerTest extends TestCase { |
38
|
|
|
|
39
|
|
|
/** @var FileHandlingController */ |
40
|
|
|
protected $controller; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
protected $appName; |
44
|
|
|
|
45
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ |
46
|
|
|
protected $requestMock; |
47
|
|
|
|
48
|
|
|
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ |
49
|
|
|
private $l10nMock; |
50
|
|
|
|
51
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ |
52
|
|
|
private $loggerMock; |
53
|
|
|
|
54
|
|
|
/** @var Folder|\PHPUnit_Framework_MockObject_MockObject */ |
55
|
|
|
private $userFolderMock; |
56
|
|
|
|
57
|
|
|
public function setUp() { |
58
|
|
|
parent::setUp(); |
59
|
|
|
$this->appName = 'files_texteditor'; |
60
|
|
|
$this->requestMock = $this->createMock(IRequest::class); |
61
|
|
|
$this->l10nMock = $this->createMock(IL10N::class); |
62
|
|
|
$this->loggerMock = $this->createMock(ILogger::class); |
63
|
|
|
$this->userFolderMock = $this->createMock(Folder::class); |
64
|
|
|
|
65
|
|
|
$this->l10nMock->expects($this->any())->method('t')->willReturnCallback( |
66
|
|
|
function($message) { |
67
|
|
|
return $message; |
68
|
|
|
} |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->controller = new FileHandlingController( |
72
|
|
|
$this->appName, |
73
|
|
|
$this->requestMock, |
74
|
|
|
$this->l10nMock, |
75
|
|
|
$this->loggerMock, |
76
|
|
|
$this->userFolderMock); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @dataProvider dataTestLoad |
81
|
|
|
* |
82
|
|
|
* @param string $filename |
83
|
|
|
* @param string|boolean $fileContent |
84
|
|
|
* @param integer $expectedStatus |
85
|
|
|
* @param string $expectedMessage |
86
|
|
|
*/ |
87
|
|
|
public function testLoad($filename, $fileContent, $expectedStatus, $expectedMessage) { |
88
|
|
|
$file = $this->createMock(File::class); |
89
|
|
|
$file->method('getContent') |
90
|
|
|
->willReturn($fileContent); |
91
|
|
|
$this->userFolderMock->method('get') |
92
|
|
|
->with('//'.$filename) |
93
|
|
|
->willReturn($file); |
94
|
|
|
|
95
|
|
|
$result = $this->controller->load('/', $filename); |
96
|
|
|
$data = $result->getData(); |
97
|
|
|
$status = $result->getStatus(); |
98
|
|
|
$this->assertSame($status, $expectedStatus); |
99
|
|
|
if ($status === 200) { |
100
|
|
|
$this->assertArrayHasKey('filecontents', $data); |
101
|
|
|
$this->assertArrayHasKey('writeable', $data); |
102
|
|
|
$this->assertArrayHasKey('mime', $data); |
103
|
|
|
$this->assertArrayHasKey('mtime', $data); |
104
|
|
|
$this->assertSame($data['filecontents'], $fileContent); |
105
|
|
|
} else { |
106
|
|
|
$this->assertArrayHasKey('message', $data); |
107
|
|
|
$this->assertSame($expectedMessage, $data['message']); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function dataTestLoad() { |
112
|
|
|
return array( |
113
|
|
|
array('test.txt', 'file content', 200, ''), |
114
|
|
|
array('test.txt', '', 200, ''), |
115
|
|
|
array('test.txt', '0', 200, ''), |
116
|
|
|
array('', 'file content', 400, 'Invalid file path supplied.'), |
117
|
|
|
array('test.txt', false, 400, 'Cannot read the file.'), |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function dataLoadExceptionWithException() { |
122
|
|
|
return [ |
123
|
|
|
[new \Exception(), 'An internal server error occurred.'], |
124
|
|
|
[new HintException('error message', 'test exception'), 'test exception'], |
125
|
|
|
[new ForbiddenException('firewall', false), 'firewall'], |
126
|
|
|
[new LockedException('secret/path/https://github.com/owncloud/files_texteditor/pull/96'), 'The file is locked.'], |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @dataProvider dataLoadExceptionWithException |
132
|
|
|
* @param \Exception $exception |
133
|
|
|
* @param string $expectedMessage |
134
|
|
|
*/ |
135
|
|
|
public function testLoadExceptionWithException(\Exception $exception, $expectedMessage) { |
136
|
|
|
$file = $this->createMock(File::class); |
137
|
|
|
$file->method('getContent') |
138
|
|
|
->willThrowException($exception); |
139
|
|
|
$this->userFolderMock->method('get') |
140
|
|
|
->with('//test.txt') |
141
|
|
|
->willReturn($file); |
142
|
|
|
|
143
|
|
|
$result = $this->controller->load('/', 'test.txt'); |
144
|
|
|
$data = $result->getData(); |
145
|
|
|
|
146
|
|
|
$this->assertSame(400, $result->getStatus()); |
147
|
|
|
$this->assertArrayHasKey('message', $data); |
148
|
|
|
$this->assertSame($expectedMessage, $data['message']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @dataProvider dataLoadExceptionWithException |
153
|
|
|
* @param \Exception $exception |
154
|
|
|
* @param string $expectedMessage |
155
|
|
|
*/ |
156
|
|
|
public function testSaveExceptionWithException(\Exception $exception, $expectedMessage) { |
157
|
|
|
$file = $this->createMock(File::class); |
158
|
|
|
$file->method('putContent') |
159
|
|
|
->willThrowException($exception); |
160
|
|
|
$this->userFolderMock->method('get') |
161
|
|
|
->with('/test.txt') |
162
|
|
|
->willReturn($file); |
163
|
|
|
|
164
|
|
|
$file->method('getMTime')->willReturn(42); |
165
|
|
|
$file->method('isUpdateable')->willReturn(true); |
166
|
|
|
|
167
|
|
|
$result = $this->controller->save('/test.txt', 'content', 42); |
168
|
|
|
$data = $result->getData(); |
169
|
|
|
|
170
|
|
|
$this->assertSame(400, $result->getStatus()); |
171
|
|
|
$this->assertArrayHasKey('message', $data); |
172
|
|
|
$this->assertSame($expectedMessage, $data['message']); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @dataProvider dataTestSave |
177
|
|
|
* |
178
|
|
|
* @param $path |
179
|
|
|
* @param $fileContents |
180
|
|
|
* @param $mTime |
181
|
|
|
* @param $fileMTime |
182
|
|
|
* @param $isUpdatable |
183
|
|
|
* @param $expectedStatus |
184
|
|
|
* @param $expectedMessage |
185
|
|
|
*/ |
186
|
|
|
public function testSave($path, $fileContents, $mTime, $fileMTime, $isUpdatable, $expectedStatus, $expectedMessage) { |
187
|
|
|
$file = $this->createMock(File::class); |
188
|
|
|
$this->userFolderMock->method('get') |
189
|
|
|
->with('/test.txt') |
190
|
|
|
->willReturn($file); |
191
|
|
|
|
192
|
|
|
$file->method('getMTime')->willReturn($fileMTime); |
193
|
|
|
$file->method('isUpdateable')->willReturn($isUpdatable); |
194
|
|
|
|
195
|
|
|
if ($expectedStatus === 200) { |
196
|
|
|
$file->expects($this->once()) |
197
|
|
|
->method('putContent')->with($fileContents); |
198
|
|
|
} else { |
199
|
|
|
$file->expects($this->never())->method('putContent'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$result = $this->controller->save($path, $fileContents, $mTime); |
203
|
|
|
$status = $result->getStatus(); |
204
|
|
|
$data = $result->getData(); |
205
|
|
|
|
206
|
|
|
$this->assertSame($expectedStatus, $status); |
207
|
|
|
if ($status === 200) { |
208
|
|
|
$this->assertArrayHasKey('mtime', $data); |
209
|
|
|
$this->assertArrayHasKey('size', $data); |
210
|
|
|
} else { |
211
|
|
|
$this->assertArrayHasKey('message', $data); |
212
|
|
|
$this->assertSame($expectedMessage, $data['message']); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function testFileTooBig() { |
218
|
|
|
$file = $this->createMock(File::class); |
219
|
|
|
$this->userFolderMock->method('get') |
220
|
|
|
->with('//foo.bar') |
221
|
|
|
->willReturn($file); |
222
|
|
|
$file->method('getSize')->willReturn(4194304 + 1); |
223
|
|
|
|
224
|
|
|
$result = $this->controller->load('/', 'foo.bar'); |
225
|
|
|
$data = $result->getData(); |
226
|
|
|
$status = $result->getStatus(); |
227
|
|
|
$this->assertSame(400, $status); |
228
|
|
|
$this->assertArrayHasKey('message', $data); |
229
|
|
|
$this->assertSame('This file is too big to be opened. Please download the file instead.', $data['message']); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function dataTestSave() { |
233
|
|
|
return array ( |
234
|
|
|
array('/test.txt', 'file content', 65638643, 65638643, true, 200, ''), |
235
|
|
|
array('', 'file content', 65638643, 65638643, true, 400, 'File path not supplied'), |
236
|
|
|
array('/test.txt', 'file content', '', 65638643, true, 400, 'File mtime not supplied'), |
237
|
|
|
array('/test.txt', 'file content', 0, 65638643, true, 400, 'File mtime not supplied'), |
238
|
|
|
array('/test.txt', 'file content', 65638643, 32848548, true, 400, 'Cannot save file as it has been modified since opening'), |
239
|
|
|
array('/test.txt', 'file content', 65638643, 65638643, false, 400, 'Insufficient permissions'), |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|