Completed
Pull Request — master (#12)
by Piotr
02:39
created

PaperHiveControllerTest::testPaperHiveDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
/**
3
 * @author Piotr Mrowczynski <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, Piotr Mrowczynski.
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
namespace OCA\Files_PaperHive\Tests\Controller;
23
24
use OC\HintException;
25
use OCA\Files_PaperHive\Controller\PaperHiveController;
26
use OCP\Files\ForbiddenException;
27
use OCP\Lock\LockedException;
28
use Test\TestCase;
29
30
class PaperHiveControllerTest extends TestCase {
31
32
	/** @var PaperHiveController */
33
	protected $controller;
34
35
	/** @var string */
36
	protected $appName;
37
38
	/** @var \OCP\IRequest | \PHPUnit_Framework_MockObject_MockObject */
39
	protected $requestMock;
40
41
	/** @var \OCP\Http\Client\IResponse | \PHPUnit_Framework_MockObject_MockObject */
42
	protected $responseMock;
43
44
	/** @var \OCP\IL10N | \PHPUnit_Framework_MockObject_MockObject */
45
	private $l10nMock;
46
47
	/** @var \OCP\ILogger | \PHPUnit_Framework_MockObject_MockObject */
48
	private $loggerMock;
49
50
	/** @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject */
51
	private $viewMock;
52
53
	/** @var \OCP\Http\Client\IClient | \PHPUnit_Framework_MockObject_MockObject */
54
	private $clientMock;
55
	
56
	public function setUp() {
57
		parent::setUp();
58
		$this->appName = 'files_paperhive';
59
		$this->requestMock = $this->getMockBuilder('OCP\IRequest')
60
			->disableOriginalConstructor()
61
			->getMock();
62
		$this->responseMock = $this->getMockBuilder('OCP\Http\Client\IResponse')
63
			->disableOriginalConstructor()
64
			->getMock();
65
		$this->l10nMock = $this->getMockBuilder('OCP\IL10N')
66
			->disableOriginalConstructor()
67
			->getMock();
68
		$this->loggerMock = $this->getMockBuilder('OCP\ILogger')
69
			->disableOriginalConstructor()
70
			->getMock();
71
		$this->viewMock = $this->getMockBuilder('OC\Files\View')
72
			->disableOriginalConstructor()
73
			->getMock();
74
		$this->clientMock = $this->getMockBuilder('OCP\Http\Client\IClient')
75
			->disableOriginalConstructor()
76
			->getMock();
77
		
78
		$this->l10nMock->expects($this->any())->method('t')->willReturnCallback(
79
			function($message) {
80
				return $message;
81
			}
82
		);
83
84
		$this->controller = new PaperHiveController(
85
			$this->appName,
86
			$this->requestMock,
87
			$this->l10nMock,
88
			$this->viewMock,
89
			$this->loggerMock,
90
			$this->clientMock);
91
	}
92
93
	private function fakeAll($bookID, $title) {
94
		$contentsDoc = '{' . '"id" : "'.$bookID .'", "title" : "'. $title .'" }';
95
		$contentsDis = '{' . '"discussions" : [ "blabla", "blabla" ]' .'}';
96
		$this->responseMock->expects($this->any())
97
			->method('getBody')
98
			->willReturnOnConsecutiveCalls($contentsDis, $contentsDoc);
99
		$this->clientMock->expects($this->any())
100
			->method('get')
101
			->willReturn($this->responseMock);
102
103
		return $contentsDoc;
104
	}
105
106
	/**
107
	 * Test if the basic parameters has changed
108
	 */
109
	public function testPaperHiveDetails() {
110
		$result = $this->controller->getPaperHiveDetails();
111
		$data = $result->getData();
112
		$status = $result->getStatus();
113
		$this->assertSame($status, 200);
114
		$this->assertArrayHasKey('paperhive_base_url', $data);
115
		$this->assertSame($data['paperhive_base_url'], 'https://paperhive.org');
116
		$this->assertArrayHasKey('paperhive_api_url', $data);
117
		$this->assertSame($data['paperhive_api_url'], '/api/documents/');
118
		$this->assertArrayHasKey('paperhive_document_url', $data);
119
		$this->assertSame($data['paperhive_document_url'], '/documents/');
120
		$this->assertArrayHasKey('paperhive_discussion_api_endpoint', $data);
121
		$this->assertSame($data['paperhive_discussion_api_endpoint'], '/discussions');
122
		$this->assertArrayHasKey('paperhive_extension', $data);
123
		$this->assertSame($data['paperhive_extension'], '.paperhive');
124
	}
125
126
127
	public function dataExceptionWithException() {
128
		return [
129
			[new \Exception(), 'An internal server error occurred.'],
130
			[new ForbiddenException('firewall', false), 'firewall'],
131
			[new LockedException('secret/path/https://github.com/owncloud/files_texteditor/pull/96'), 'The file is locked.'],
132
		];
133
	}
134
135
	/**
136
	 * @dataProvider dataExceptionWithException
137
	 * @param \Exception $exception
138
	 * @param string $expectedMessage
139
	 */
140 View Code Duplication
	public function testLoadMetadataForRenamedDocumentExceptions(\Exception $exception, $expectedMessage) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
		$dir = '';
142
		$bookID = 'Ra5WnkxImoOE';
143
		$title = "Borderland City in New India";
144
		$path = $dir . '/'. $title. '.renamed.paperhive';
145
		$contents = $this->fakeAll($bookID, $title);
0 ignored issues
show
Unused Code introduced by
$contents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
147
		$this->viewMock->expects($this->once())
148
			->method('file_exists')
149
			->willReturn(true);
150
151
		$this->viewMock->expects($this->any())
152
			->method('file_get_contents')
153
			->willReturnCallback(function() use ($exception) {
154
				throw $exception;
155
			});
156
157
		$this->viewMock->expects($this->any())
158
			->method('rename')
159
			->willReturn(false);
160
161
		$result = $this->controller->loadMetadata('/', $path, "true");
162
		$data = $result->getData();
163
164
		$this->assertSame(400, $result->getStatus());
165
		$this->assertArrayHasKey('message', $data);
166
		$this->assertSame($expectedMessage, $data['message']);
167
	}
168
169
	public function testLoadMetadataForRenamedDocumentNoExtension() {
170
		$dir = '';
171
		$bookID = 'Ra5WnkxImoOE';
172
		$title = "Borderland City in New India";
173
		$path = $dir . '/'. $title;
174
		$contents = $this->fakeAll($bookID, $title);
0 ignored issues
show
Unused Code introduced by
$contents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
176
		$result = $this->controller->loadMetadata('/', $path, "true");
177
		$data = $result->getData();
178
179
		$this->assertSame(400, $result->getStatus());
180
		$this->assertArrayHasKey('message', $data);
181
		$this->assertSame('Invalid file path supplied.', $data['message']);
182
	}
183
184
	public function testLoadMetadataForRenamedDocumentNotExisting() {
185
		$dir = '';
186
		$bookID = 'Ra5WnkxImoOE';
187
		$title = "Borderland City in New India";
188
		$path = $dir . '/'. $title. '.renamed.paperhive';
189
		$contents = $this->fakeAll($bookID, $title);
0 ignored issues
show
Unused Code introduced by
$contents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
191
		$this->viewMock->expects($this->once())
192
			->method('file_exists')
193
			->willReturn(false);
194
195
		$result = $this->controller->loadMetadata('/', $path, "true");
196
		$data = $result->getData();
197
198
		$this->assertSame(400, $result->getStatus());
199
		$this->assertArrayHasKey('message', $data);
200
		$this->assertSame('File is obsolete, incorrectly renamed or cannot be read.', $data['message']);
201
	}
202
203
	public function testLoadMetadataForRenamedDocumentWrongContent() {
204
		$dir = '';
205
		$bookID = 'Ra5WnkxImoOE';
206
		$title = "Borderland City in New India";
207
		$path = $dir . '/'. $title. '.renamed.paperhive';
208
		$contents = $this->fakeAll($bookID, $title);
0 ignored issues
show
Unused Code introduced by
$contents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
209
210
		$this->viewMock->expects($this->once())
211
			->method('file_exists')
212
			->willReturn(true);
213
214
		$this->viewMock->expects($this->once())
215
			->method('file_get_contents')
216
			->willReturn('wrong content');
217
218
		$result = $this->controller->loadMetadata('/', $path, "true");
219
		$data = $result->getData();
220
221
		$this->assertSame(400, $result->getStatus());
222
		$this->assertArrayHasKey('message', $data);
223
		$this->assertSame('File is obsolete, incorrectly renamed or cannot be read.', $data['message']);
224
	}
225
226 View Code Duplication
	public function testLoadMetadataForRenamedDocumentFailRename() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
	$dir = '';
228
	$bookID = 'Ra5WnkxImoOE';
229
	$title = "Borderland City in New India";
230
	$path = $dir . '/'. $title. '.renamed.paperhive';
231
	$contents = $this->fakeAll($bookID, $title);
232
233
	$this->viewMock->expects($this->once())
234
		->method('file_exists')
235
		->willReturn(true);
236
237
	$this->viewMock->expects($this->once())
238
		->method('file_get_contents')
239
		->willReturn($contents);
240
241
		$this->viewMock->expects($this->once())
242
			->method('rename')
243
			->willReturn(false);
244
245
	$result = $this->controller->loadMetadata('/', $path, "true");
246
	$data = $result->getData();
247
248
	$this->assertSame(400, $result->getStatus());
249
	$this->assertArrayHasKey('message', $data);
250
	$this->assertSame('File is obsolete, incorrectly renamed or cannot be read.', $data['message']);
251
}
252
253
	public function testLoadMetadataForRenamedDocument() {
254
		$dir = '';
255
		$bookID = 'Ra5WnkxImoOE';
256
		$title = "Borderland City in New India";
257
		$path = $dir . '/'. $title. '.renamed.paperhive';
258
		$contents = $this->fakeAll($bookID, $title);
259
260
		$this->viewMock->expects($this->once())
261
			->method('file_exists')
262
			->willReturn(true);
263
264
		$this->viewMock->expects($this->once())
265
			->method('file_get_contents')
266
			->willReturn($contents);
267
268
		$this->viewMock->expects($this->once())
269
			->method('rename')
270
			->willReturn(true);
271
272
		$result = $this->controller->loadMetadata('/', $path, "true");
273
		$data = $result->getData();
274
275
		$this->assertSame(200, $result->getStatus());
276
		$this->assertArrayHasKey('paperhive_base_url', $data);
277
		$this->assertArrayHasKey('paperhive_api_url', $data);
278
		$this->assertArrayHasKey('paperhive_document_url', $data);
279
		$this->assertArrayHasKey('paperhive_document_id', $data);
280
		$this->assertArrayHasKey('paperhive_discussion_api_endpoint', $data);
281
		$this->assertArrayHasKey('paperhive_extension', $data);
282
		$this->assertArrayHasKey('paperhive_discussion_count', $data);
283
		$this->assertSame($bookID, $data['paperhive_document_id']);
284
		$this->assertSame(2, $data['paperhive_discussion_count']);
285
	}
286
287
	public function testLoadMetadataWithoutDiscussions() {
288
		$dir = '';
289
		$bookID = 'Ra5WnkxImoOE';
290
		$title = "Borderland City in New India";
291
		$path = $dir . '/'. $title. '.rev'.$bookID.'.paperhive';
292
293
		$result = $this->controller->loadMetadata('/', $path, "false");
294
		$data = $result->getData();
295
296
		$this->assertSame(200, $result->getStatus());
297
		$this->assertArrayHasKey('paperhive_base_url', $data);
298
		$this->assertArrayHasKey('paperhive_api_url', $data);
299
		$this->assertArrayHasKey('paperhive_document_url', $data);
300
		$this->assertArrayHasKey('paperhive_document_id', $data);
301
		$this->assertArrayHasKey('paperhive_discussion_api_endpoint', $data);
302
		$this->assertArrayHasKey('paperhive_extension', $data);
303
		$this->assertArrayHasKey('paperhive_discussion_count', $data);
304
		$this->assertSame($bookID, $data['paperhive_document_id']);
305
		$this->assertSame(-1, $data['paperhive_discussion_count']);
306
	}
307
308
	public function testLoadMetadata() {
309
		$dir = '';
310
		$bookID = 'Ra5WnkxImoOE';
311
		$title = "Borderland City in New India";
312
		$path = $dir . '/'. $title. '.rev'.$bookID.'.paperhive';
313
		$contents = $this->fakeAll($bookID, $title);
0 ignored issues
show
Unused Code introduced by
$contents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
314
315
		$result = $this->controller->loadMetadata('/', $path, "true");
316
		$data = $result->getData();
317
318
		$this->assertSame(200, $result->getStatus());
319
		$this->assertArrayHasKey('paperhive_base_url', $data);
320
		$this->assertArrayHasKey('paperhive_api_url', $data);
321
		$this->assertArrayHasKey('paperhive_document_url', $data);
322
		$this->assertArrayHasKey('paperhive_document_id', $data);
323
		$this->assertArrayHasKey('paperhive_discussion_api_endpoint', $data);
324
		$this->assertArrayHasKey('paperhive_extension', $data);
325
		$this->assertArrayHasKey('paperhive_discussion_count', $data);
326
		$this->assertSame($bookID, $data['paperhive_document_id']);
327
		$this->assertSame(2, $data['paperhive_discussion_count']);
328
	}
329
330
	public function dataTestSave() {
331
		return array (
332
			array('', 'Ra5WnkxImoOE', true, true, 200, ''),
333
			array('/test', 'Ra5WnkxImoOE', true, true, 200, ''),
334
			array('', 'Ra5WnkxImoOE', true, true, 400, 'The file already exists.')
335
		);
336
	}
337
338
	/**
339
	 * @dataProvider dataTestSave
340
	 *
341
	 * @param $dir
342
	 * @param $bookID
343
	 * @param $correctDiscussions
344
	 * @param $correctDocument
345
	 * @param $expectedStatus
346
	 * @param $expectedMessage
347
	 */
348
	public function testGetPaperHiveDocument($dir, $bookID, $correctDiscussions, $correctDocument, $expectedStatus, $expectedMessage) {
349
		$title = "Borderland City in New India";
350
		$path = $dir . '/'. $title. '.rev'.$bookID.'.paperhive';
351
		$contents = $this->fakeAll($bookID, $title);
352
353
		if ($expectedStatus === 200) {
354
			$this->viewMock->expects($this->once())
355
				->method('file_put_contents')->with($path, $contents);
356
		} else {
357
			$this->viewMock->expects($this->once())
358
				->method('file_exists')->with($path)
359
				->willReturn(true);
360
			$this->viewMock->expects($this->never())->method(('file_put_contents'));
361
		}
362
363
		$result = $this->controller->generatePaperHiveDocument($dir, $bookID);
364
		$status = $result->getStatus();
365
		$data = $result->getData();
366
367
		$this->assertSame($expectedStatus, $status);
368
		if ($status === 200) {
369
			$this->assertArrayHasKey('path', $data);
370
			$this->assertArrayHasKey('filename', $data);
371
			$this->assertArrayHasKey('extension', $data);
372
			$this->assertArrayHasKey('discussionCount', $data);
373
			$this->assertSame(2, $data['discussionCount']);
374
			$this->assertSame('.rev'.$bookID.'.paperhive', $data['extension']);
375
		} else {
376
			$this->assertArrayHasKey('message', $data);
377
			$this->assertSame($expectedMessage, $data['message']);
378
		}
379
	}
380
381
	/**
382
	 * @dataProvider dataExceptionWithException
383
	 * @param \Exception $exception
384
	 * @param string $expectedMessage
385
	 */
386
	public function testGetDocumentExceptionWithException(\Exception $exception, $expectedMessage) {
387
		$title = "Borderland City in New India";
388
		$dir = '/';
389
		$bookID = 'Ra5WnkxImoOE';
390
		$contents = $this->fakeAll($bookID, $title);
0 ignored issues
show
Unused Code introduced by
$contents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
391
		$this->viewMock->expects($this->any())
392
			->method('file_put_contents')
393
			->willReturnCallback(function() use ($exception) {
394
				throw $exception;
395
			});
396
397
		$result = $this->controller->generatePaperHiveDocument($dir, $bookID);
398
		$data = $result->getData();
399
400
		$this->assertSame(400, $result->getStatus());
401
		$this->assertArrayHasKey('message', $data);
402
		$this->assertSame($expectedMessage, $data['message']);
403
	}
404
}
405