FileHandlingControllerTest::testFileTooBig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
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\Files_Texteditor\Tests\Controller;
24
25
26
use OC\HintException;
27
use OCA\Files_Texteditor\Controller\FileHandlingController;
28
use OCP\Files\ForbiddenException;
29
use OCP\Lock\LockedException;
30
use Test\TestCase;
31
32
class FileHandlingControllerTest extends TestCase {
33
34
	/** @var FileHandlingController */
35
	protected $controller;
36
37
	/** @var string */
38
	protected $appName;
39
40
	/** @var \OCP\IRequest | \PHPUnit_Framework_MockObject_MockObject */
41
	protected $requestMock;
42
43
	/** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject */
44
	private $l10nMock;
45
46
	/** @var \OCP\ILogger | \PHPUnit_Framework_MockObject_MockObject */
47
	private $loggerMock;
48
49
	/** @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject */
50
	private $viewMock;
51
52
	public function setUp() {
53
		parent::setUp();
54
		$this->appName = 'files_texteditor';
55
		$this->requestMock = $this->getMockBuilder('OCP\IRequest')
56
			->disableOriginalConstructor()
57
			->getMock();
58
		$this->l10nMock = $this->getMockBuilder('OCP\IL10N')
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$this->loggerMock = $this->getMockBuilder('OCP\ILogger')
62
			->disableOriginalConstructor()
63
			->getMock();
64
		$this->viewMock = $this->getMockBuilder('OC\Files\View')
65
			->disableOriginalConstructor()
66
			->getMock();
67
68
		$this->l10nMock->expects($this->any())->method('t')->willReturnCallback(
69
			function($message) {
70
				return $message;
71
			}
72
		);
73
74
		$this->controller = new FileHandlingController(
75
			$this->appName,
76
			$this->requestMock,
77
			$this->l10nMock,
78
			$this->viewMock,
79
			$this->loggerMock);
80
	}
81
82
	/**
83
	 * @dataProvider dataTestLoad
84
	 *
85
	 * @param string $filename
86
	 * @param string|boolean $fileContent
87
	 * @param integer $expectedStatus
88
	 * @param string $expectedMessage
89
	 */
90
	public function testLoad($filename, $fileContent, $expectedStatus, $expectedMessage) {
91
		$this->viewMock->expects($this->any())
92
			->method('file_get_contents')
93
			->willReturn($fileContent);
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
137
		$this->viewMock->expects($this->any())
138
			->method('file_get_contents')
139
			->willReturnCallback(function() use ($exception) {
140
				throw $exception;
141
			});
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
158
		$this->viewMock->expects($this->any())
159
			->method('file_put_contents')
160
			->willReturnCallback(function() use ($exception) {
161
				throw $exception;
162
			});
163
164
		$this->viewMock->expects($this->any())
165
			->method('filemtime')
166
			->willReturn(42);
167
		$this->viewMock->expects($this->any())
168
			->method('isUpdatable')
169
			->willReturn(true);
170
171
		$result = $this->controller->save('/test.txt', 'content', 42);
172
		$data = $result->getData();
173
174
		$this->assertSame(400, $result->getStatus());
175
		$this->assertArrayHasKey('message', $data);
176
		$this->assertSame($expectedMessage, $data['message']);
177
	}
178
179
	/**
180
	 * @dataProvider dataTestSave
181
	 *
182
	 * @param $path
183
	 * @param $fileContents
184
	 * @param $mTime
185
	 * @param $fileMTime
186
	 * @param $isUpdatable
187
	 * @param $expectedStatus
188
	 * @param $expectedMessage
189
	 */
190
	public function testSave($path, $fileContents, $mTime, $fileMTime, $isUpdatable, $expectedStatus, $expectedMessage) {
191
192
		$this->viewMock->expects($this->any())
193
			->method('filemtime')
194
			->willReturn($fileMTime);
195
196
		$this->viewMock->expects($this->any())
197
			->method('isUpdatable')
198
			->willReturn($isUpdatable);
199
200
		if ($expectedStatus === 200) {
201
			$this->viewMock->expects($this->once())
202
				->method('file_put_contents')->with($path, $fileContents);
203
		} else {
204
			$this->viewMock->expects($this->never())->method(('file_put_contents'));
205
		}
206
207
		$result = $this->controller->save($path, $fileContents, $mTime);
208
		$status = $result->getStatus();
209
		$data = $result->getData();
210
211
		$this->assertSame($expectedStatus, $status);
212
		if ($status === 200) {
213
			$this->assertArrayHasKey('mtime', $data);
214
			$this->assertArrayHasKey('size', $data);
215
		} else {
216
			$this->assertArrayHasKey('message', $data);
217
			$this->assertSame($expectedMessage, $data['message']);
218
		}
219
220
	}
221
222
	public function testFileTooBig() {
223
		$this->viewMock->expects($this->any())
224
			->method('filesize')
225
			->willReturn(4194304 + 1);
226
227
		$result = $this->controller->load('/', 'foo.bar');
228
		$data = $result->getData();
229
		$status = $result->getStatus();
230
		$this->assertSame(400, $status);
231
		$this->assertArrayHasKey('message', $data);
232
		$this->assertSame('This file is too big to be opened. Please download the file instead.', $data['message']);
233
	}
234
235
	public function dataTestSave() {
236
		return array (
237
			array('/test.txt', 'file content', 65638643, 65638643, true, 200, ''),
238
			array('', 'file content', 65638643, 65638643, true, 400, 'File path not supplied'),
239
			array('/test.txt', 'file content', '', 65638643, true, 400, 'File mtime not supplied'),
240
			array('/test.txt', 'file content', 0, 65638643, true, 400, 'File mtime not supplied'),
241
			array('/test.txt', 'file content', 65638643, 32848548, true, 400, 'Cannot save file as it has been modified since opening'),
242
			array('/test.txt', 'file content', 65638643, 65638643, false, 400, 'Insufficient permissions'),
243
		);
244
	}
245
246
}
247