Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileHandlingControllerTest.php « Controller « tests - github.com/nextcloud/files_texteditor.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38fabdd3cedd555fd3377f4b83bc73d4fd3b0799 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
 * @author Björn Schießle <schiessle@owncloud.com>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */


namespace OCA\FilesTextEditor\Tests\Controller;

use OC\HintException;
use OCA\FilesTextEditor\Controller\FileHandlingController;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\ForbiddenException;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IRequest;
use OCP\Lock\LockedException;
use Test\TestCase;

class FileHandlingControllerTest extends TestCase {
	/** @var FileHandlingController */
	protected $controller;

	/** @var string */
	protected $appName;

	/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
	protected $requestMock;

	/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
	private $l10nMock;

	/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
	private $loggerMock;

	/** @var Folder|\PHPUnit_Framework_MockObject_MockObject */
	private $userFolderMock;

	protected function setUp(): void {
		parent::setUp();
		$this->appName = 'files_texteditor';
		$this->requestMock = $this->createMock(IRequest::class);
		$this->l10nMock = $this->createMock(IL10N::class);
		$this->loggerMock = $this->createMock(ILogger::class);
		$this->userFolderMock = $this->createMock(Folder::class);

		$this->l10nMock->expects($this->any())->method('t')->willReturnCallback(
			function ($message) {
				return $message;
			}
		);

		$this->controller = new FileHandlingController(
			$this->appName,
			$this->requestMock,
			$this->l10nMock,
			$this->loggerMock,
			$this->userFolderMock);
	}

	/**
	 * @dataProvider dataTestLoad
	 *
	 * @param string $filename
	 * @param string|boolean $fileContent
	 * @param integer $expectedStatus
	 * @param string $expectedMessage
	 */
	public function testLoad($filename, $fileContent, $expectedStatus, $expectedMessage) {
		$file = $this->createMock(File::class);
		$file->method('getContent')
			->willReturn($fileContent);
		$this->userFolderMock->method('get')
			->with('//'.$filename)
			->willReturn($file);

		$result = $this->controller->load('/', $filename);
		$data = $result->getData();
		$status = $result->getStatus();
		$this->assertSame($status, $expectedStatus);
		if ($status === 200) {
			$this->assertArrayHasKey('filecontents', $data);
			$this->assertArrayHasKey('writeable', $data);
			$this->assertArrayHasKey('mime', $data);
			$this->assertArrayHasKey('mtime', $data);
			$this->assertSame($data['filecontents'], $fileContent);
		} else {
			$this->assertArrayHasKey('message', $data);
			$this->assertSame($expectedMessage, $data['message']);
		}
	}

	public function dataTestLoad() {
		return array(
			array('test.txt', 'file content', 200, ''),
			array('test.txt', '', 200, ''),
			array('test.txt', '0', 200, ''),
			array('', 'file content', 400, 'Invalid file path supplied.'),
			array('test.txt', false, 400, 'Cannot read the file.'),
		);
	}

	public function dataLoadExceptionWithException() {
		return [
			[new \Exception(), 'An internal server error occurred.'],
			[new HintException('error message', 'test exception'), 'test exception'],
			[new ForbiddenException('firewall', false), 'firewall'],
			[new LockedException('secret/path/https://github.com/owncloud/files_texteditor/pull/96'), 'The file is locked.'],
		];
	}

	/**
	 * @dataProvider dataLoadExceptionWithException
	 * @param \Exception $exception
	 * @param string $expectedMessage
	 */
	public function testLoadExceptionWithException(\Exception $exception, $expectedMessage) {
		$file = $this->createMock(File::class);
		$file->method('getContent')
			->willThrowException($exception);
		$this->userFolderMock->method('get')
			->with('//test.txt')
			->willReturn($file);

		$result = $this->controller->load('/', 'test.txt');
		$data = $result->getData();

		$this->assertSame(400, $result->getStatus());
		$this->assertArrayHasKey('message', $data);
		$this->assertSame($expectedMessage, $data['message']);
	}

	/**
	 * @dataProvider dataLoadExceptionWithException
	 * @param \Exception $exception
	 * @param string $expectedMessage
	 */
	public function testSaveExceptionWithException(\Exception $exception, $expectedMessage) {
		$file = $this->createMock(File::class);
		$file->method('putContent')
			->willThrowException($exception);
		$this->userFolderMock->method('get')
			->with('/test.txt')
			->willReturn($file);

		$file->method('getMTime')->willReturn(42);
		$file->method('isUpdateable')->willReturn(true);

		$result = $this->controller->save('/test.txt', 'content', 42);
		$data = $result->getData();

		$this->assertSame(400, $result->getStatus());
		$this->assertArrayHasKey('message', $data);
		$this->assertSame($expectedMessage, $data['message']);
	}

	/**
	 * @dataProvider dataTestSave
	 *
	 * @param $path
	 * @param $fileContents
	 * @param $mTime
	 * @param $fileMTime
	 * @param $isUpdatable
	 * @param $expectedStatus
	 * @param $expectedMessage
	 */
	public function testSave($path, $fileContents, $mTime, $fileMTime, $isUpdatable, $expectedStatus, $expectedMessage) {
		$file = $this->createMock(File::class);
		$this->userFolderMock->method('get')
			->with('/test.txt')
			->willReturn($file);

		$file->method('getMTime')->willReturn($fileMTime);
		$file->method('isUpdateable')->willReturn($isUpdatable);

		if ($expectedStatus === 200) {
			$file->expects($this->once())
				->method('putContent')->with($fileContents);
		} else {
			$file->expects($this->never())->method('putContent');
		}

		$result = $this->controller->save($path, $fileContents, $mTime);
		$status = $result->getStatus();
		$data = $result->getData();

		$this->assertSame($expectedStatus, $status);
		if ($status === 200) {
			$this->assertArrayHasKey('mtime', $data);
			$this->assertArrayHasKey('size', $data);
		} else {
			$this->assertArrayHasKey('message', $data);
			$this->assertSame($expectedMessage, $data['message']);
		}
	}

	public function testFileTooBig() {
		$file = $this->createMock(File::class);
		$this->userFolderMock->method('get')
			->with('//foo.bar')
			->willReturn($file);
		$file->method('getSize')->willReturn(4194304 + 1);

		$result = $this->controller->load('/', 'foo.bar');
		$data = $result->getData();
		$status = $result->getStatus();
		$this->assertSame(400, $status);
		$this->assertArrayHasKey('message', $data);
		$this->assertSame('This file is too big to be opened. Please download the file instead.', $data['message']);
	}

	public function dataTestSave() {
		return array(
			array('/test.txt', 'file content', 65638643, 65638643, true, 200, ''),
			array('', 'file content', 65638643, 65638643, true, 400, 'File path not supplied'),
			array('/test.txt', 'file content', '', 65638643, true, 400, 'File mtime not supplied'),
			array('/test.txt', 'file content', 0, 65638643, true, 400, 'File mtime not supplied'),
			array('/test.txt', 'file content', 65638643, 32848548, true, 400, 'Cannot save file as it has been modified since opening'),
			array('/test.txt', 'file content', 65638643, 65638643, false, 400, 'Insufficient permissions'),
		);
	}
}