Passed
Pull Request — master (#372)
by Georg
05:28
created

ViewControllerTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 444
Duplicated Lines 2.7 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 7
Bugs 0 Features 1
Metric Value
dl 12
loc 444
rs 10
c 7
b 0
f 1
wmc 14
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 23 1
B testIndex() 6 93 3
A indexDataProvider() 0 10 1
B testIndexNoMonthFallback() 0 82 1
B testIndexFirstRunDetection() 0 88 3
A indexFirstRunDetectionProvider() 0 6 1
B testPublicIndex() 6 103 3
A indexPublicDataProvider() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
class ViewControllerTest extends \PHPUnit_Framework_TestCase {
25
26
	private $appName;
27
	private $request;
28
	private $config;
29
	private $userSession;
30
	private $urlGenerator;
31
32
	private $dummyUser;
33
34
	private $controller;
35
36
	public function setUp() {
37
		$this->appName = 'calendar';
38
		$this->request = $this->getMockBuilder('\OCP\IRequest')
39
			->disableOriginalConstructor()
40
			->getMock();
41
		$this->config = $this->getMockBuilder('\OCP\IConfig')
42
			->disableOriginalConstructor()
43
			->getMock();
44
		$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
45
			->disableOriginalConstructor()
46
			->getMock();
47
48
		$this->dummyUser = $this->getMockBuilder('OCP\IUser')
49
			->disableOriginalConstructor()
50
			->getMock();
51
52
		$this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')
53
			->disableOriginalConstructor()
54
			->getMock();
55
56
		$this->controller = new ViewController($this->appName, $this->request,
57
			$this->userSession, $this->config, $this->urlGenerator);
58
	}
59
60
	/**
61
	 * @dataProvider indexDataProvider
62
	 */
63
	public function testIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass, $expectsWebcalWorkaround, $needsAutosize, $isIE) {
64
		$this->config->expects($this->at(0))
65
			->method('getSystemValue')
66
			->with('version')
67
			->will($this->returnValue($serverVersion));
68
69
		$this->config->expects($this->at(1))
70
			->method('getSystemValue')
71
			->with('asset-pipeline.enabled', false)
72
			->will($this->returnValue($isAssetPipelineEnabled));
73
74 View Code Duplication
		if (!$showAssetPipelineError) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
			$this->request->expects($this->once())
76
				->method('isUserAgent')
77
				->with(['/(MSIE)|(Trident)/'])
78
				->will($this->returnValue($isIE));
79
		}
80
81
		if ($showAssetPipelineError) {
82
			$actual = $this->controller->index();
83
84
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
85
			$this->assertEquals([], $actual->getParams());
86
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
87
		} else {
88
			$this->config->expects($this->at(2))
89
				->method('getSystemValue')
90
				->with('version')
91
				->will($this->returnValue($serverVersion));
92
93
			$this->config->expects($this->at(3))
94
				->method('getAppValue')
95
				->with($this->appName, 'installed_version')
96
				->will($this->returnValue('42.13.37'));
97
98
			$this->config->expects($this->at(4))
99
				->method('getAppValue')
100
				->with('theming', 'color', '#0082C9')
101
				->will($this->returnValue('#ff00ff'));
102
103
			$this->userSession->expects($this->once())
104
				->method('getUser')
105
				->will($this->returnValue($this->dummyUser));
106
107
			$this->dummyUser->expects($this->once())
108
				->method('getUID')
109
				->will($this->returnValue('user123'));
110
111
			$this->dummyUser->expects($this->once())
112
				->method('getEMailAddress')
113
				->will($this->returnValue('[email protected]'));
114
115
			$this->config->expects($this->at(5))
116
				->method('getUserValue')
117
				->with('user123', $this->appName, 'currentView', null)
118
				->will($this->returnValue('someView'));
119
120
			$this->config->expects($this->at(6))
121
				->method('getUserValue')
122
				->with('user123', $this->appName, 'skipPopover', 'no')
123
				->will($this->returnValue('someSkipPopoverValue'));
124
125
			$this->config->expects($this->at(7))
126
				->method('getUserValue')
127
				->with('user123', $this->appName, 'showWeekNr', 'no')
128
				->will($this->returnValue('someShowWeekNrValue'));
129
130
			$this->config->expects($this->at(8))
131
				->method('getUserValue')
132
				->with('user123', $this->appName, 'firstRun', null)
133
				->will($this->returnValue('someFirstRunValue'));
134
135
			$actual = $this->controller->index();
136
137
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
138
			$this->assertEquals([
139
				'appVersion' => '42.13.37',
140
				'initialView' => 'someView',
141
				'emailAddress' => '[email protected]',
142
				'skipPopover' => 'someSkipPopoverValue',
143
				'weekNumbers' => 'someShowWeekNrValue',
144
				'firstRun' => 'someFirstRunValue',
145
				'supportsClass' => $expectsSupportsClass,
146
				'defaultColor' => '#ff00ff',
147
				'webCalWorkaround' => $expectsWebcalWorkaround,
148
				'isPublic' => false,
149
				'needsAutosize' => $needsAutosize,
150
				'isIE' => $isIE,
151
			], $actual->getParams());
152
			$this->assertEquals('main', $actual->getTemplateName());
153
		}
154
155
	}
156
157
	public function indexDataProvider() {
158
		return [
159
			[true, true, '9.0.5.2', false, 'yes', true, false],
160
			[true, false, '9.1.0.0', true, 'no', true, false],
161
			[false, false, '9.0.5.2', false, 'yes', true, false],
162
			[false, false, '9.1.0.0', true, 'no', true, false],
163
			[false, false, '11.0.1', true, 'no', false, false],
164
			[false, false, '11.0.1', true, 'no', false, true],
165
		];
166
	}
167
168
	public function testIndexNoMonthFallback() {
169
		$this->config->expects($this->at(0))
170
			->method('getSystemValue')
171
			->with('version')
172
			->will($this->returnValue('9.1.0.0'));
173
174
		$this->config->expects($this->at(1))
175
			->method('getSystemValue')
176
			->with('asset-pipeline.enabled', false)
177
			->will($this->returnValue(false));
178
179
		$this->request->expects($this->once())
180
			->method('isUserAgent')
181
			->with(['/(MSIE)|(Trident)/'])
182
			->will($this->returnValue(false));
183
184
		$this->config->expects($this->at(2))
185
			->method('getSystemValue')
186
			->with('version')
187
			->will($this->returnValue('9.1.0.0'));
188
189
		$this->userSession->expects($this->once())
190
			->method('getUser')
191
			->will($this->returnValue($this->dummyUser));
192
193
		$this->dummyUser->expects($this->once())
194
			->method('getUID')
195
			->will($this->returnValue('user123'));
196
197
		$this->dummyUser->expects($this->once())
198
			->method('getEMailAddress')
199
			->will($this->returnValue('[email protected]'));
200
201
		$this->config->expects($this->at(3))
202
			->method('getAppValue')
203
			->with($this->appName, 'installed_version')
204
			->will($this->returnValue('42.13.37'));
205
206
		$this->config->expects($this->at(4))
207
			->method('getAppValue')
208
			->with('theming', 'color', '#0082C9')
209
			->will($this->returnValue('#ff00ff'));
210
211
		$this->config->expects($this->at(5))
212
			->method('getUserValue')
213
			->with('user123', $this->appName, 'currentView', null)
214
			->will($this->returnValue(null));
215
216
		$this->config->expects($this->at(6))
217
			->method('getUserValue')
218
			->with('user123', $this->appName, 'skipPopover', 'no')
219
			->will($this->returnValue('someSkipPopoverValue'));
220
221
		$this->config->expects($this->at(7))
222
			->method('getUserValue')
223
			->with('user123', $this->appName, 'showWeekNr', 'no')
224
			->will($this->returnValue('someShowWeekNrValue'));
225
226
		$this->config->expects($this->at(8))
227
			->method('getUserValue')
228
			->with('user123', $this->appName, 'firstRun', null)
229
			->will($this->returnValue('someFirstRunValue'));
230
231
		$actual = $this->controller->index();
232
233
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
234
		$this->assertEquals([
235
			'appVersion' => '42.13.37',
236
			'initialView' => 'month',
237
			'emailAddress' => '[email protected]',
238
			'skipPopover' => 'someSkipPopoverValue',
239
			'weekNumbers' => 'someShowWeekNrValue',
240
			'firstRun' => 'someFirstRunValue',
241
			'supportsClass' => true,
242
			'defaultColor' => '#ff00ff',
243
			'webCalWorkaround' => 'no',
244
			'isPublic' => false,
245
			'needsAutosize' => true,
246
			'isIE' => false,
247
		], $actual->getParams());
248
		$this->assertEquals('main', $actual->getTemplateName());
249
	}
250
251
	/**
252
	 * @dataProvider indexFirstRunDetectionProvider
253
	 */
254
	public function testIndexFirstRunDetection($initialView, $expectedFirstRun, $expectsSetRequest) {
255
		$this->config->expects($this->at(0))
256
			->method('getSystemValue')
257
			->with('version')
258
			->will($this->returnValue('9.1.0.0'));
259
260
		$this->config->expects($this->at(1))
261
			->method('getSystemValue')
262
			->with('asset-pipeline.enabled', false)
263
			->will($this->returnValue(false));
264
265
		$this->request->expects($this->once())
266
			->method('isUserAgent')
267
			->with(['/(MSIE)|(Trident)/'])
268
			->will($this->returnValue(false));
269
270
		$this->config->expects($this->at(2))
271
			->method('getSystemValue')
272
			->with('version')
273
			->will($this->returnValue('9.1.0.0'));
274
275
		$this->userSession->expects($this->once())
276
			->method('getUser')
277
			->will($this->returnValue($this->dummyUser));
278
279
		$this->dummyUser->expects($this->once())
280
			->method('getUID')
281
			->will($this->returnValue('user123'));
282
283
		$this->dummyUser->expects($this->once())
284
			->method('getEMailAddress')
285
			->will($this->returnValue('[email protected]'));
286
287
		$this->config->expects($this->at(3))
288
			->method('getAppValue')
289
			->with($this->appName, 'installed_version')
290
			->will($this->returnValue('42.13.37'));
291
292
		$this->config->expects($this->at(4))
293
			->method('getAppValue')
294
			->with('theming', 'color', '#0082C9')
295
			->will($this->returnValue('#ff00ff'));
296
297
		$this->config->expects($this->at(5))
298
			->method('getUserValue')
299
			->with('user123', $this->appName, 'currentView', null)
300
			->will($this->returnValue($initialView));
301
302
		$this->config->expects($this->at(6))
303
			->method('getUserValue')
304
			->with('user123', $this->appName, 'skipPopover', 'no')
305
			->will($this->returnValue('someSkipPopoverValue'));
306
307
		$this->config->expects($this->at(7))
308
			->method('getUserValue')
309
			->with('user123', $this->appName, 'showWeekNr', 'no')
310
			->will($this->returnValue('someShowWeekNrValue'));
311
312
		$this->config->expects($this->at(8))
313
			->method('getUserValue')
314
			->with('user123', $this->appName, 'firstRun', null)
315
			->will($this->returnValue(null));
316
317
		if ($expectsSetRequest) {
318
			$this->config->expects($this->at(9))
319
				->method('setUserValue')
320
				->with('user123');
321
		}
322
323
		$actual = $this->controller->index();
324
325
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
326
		$this->assertEquals([
327
			'appVersion' => '42.13.37',
328
			'initialView' => $initialView ? 'someRandominitialView' : 'month',
329
			'emailAddress' => '[email protected]',
330
			'skipPopover' => 'someSkipPopoverValue',
331
			'weekNumbers' => 'someShowWeekNrValue',
332
			'firstRun' => $expectedFirstRun,
333
			'supportsClass' => true,
334
			'defaultColor' => '#ff00ff',
335
			'webCalWorkaround' => 'no',
336
			'isPublic' => false,
337
			'needsAutosize' => true,
338
			'isIE' => false,
339
		], $actual->getParams());
340
		$this->assertEquals('main', $actual->getTemplateName());
341
	}
342
343
	public function indexFirstRunDetectionProvider() {
344
		return [
345
			[null, 'yes', false],
346
			['someRandominitialView', 'no', true],
347
		];
348
	}
349
350
	/**
351
	 * @dataProvider indexPublicDataProvider
352
	 */
353
	public function testPublicIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass, $needsAutosize, $isIE) {
354
		$this->config->expects($this->at(0))
355
			->method('getSystemValue')
356
			->with('version')
357
			->will($this->returnValue($serverVersion));
358
359
		$this->config->expects($this->at(1))
360
			->method('getSystemValue')
361
			->with('asset-pipeline.enabled', false)
362
			->will($this->returnValue($isAssetPipelineEnabled));
363
364 View Code Duplication
		if (!$showAssetPipelineError) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
365
			$this->request->expects($this->once())
366
				->method('isUserAgent')
367
				->with(['/(MSIE)|(Trident)/'])
368
				->will($this->returnValue($isIE));
369
		}
370
371
		if ($showAssetPipelineError) {
372
			$actual = $this->controller->index();
373
374
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
375
			$this->assertEquals([], $actual->getParams());
376
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
377
		} else {
378
			$this->config->expects($this->at(2))
379
				->method('getSystemValue')
380
				->with('version')
381
				->will($this->returnValue($serverVersion));
382
383
			$this->config->expects($this->at(3))
384
				->method('getAppValue')
385
				->with($this->appName, 'installed_version')
386
				->will($this->returnValue('42.13.37'));
387
388
			$this->config->expects($this->at(4))
389
				->method('getAppValue')
390
				->with('theming', 'color', '#0082C9')
391
				->will($this->returnValue('#ff00ff'));
392
393
			$this->request->expects($this->at(1))
394
				->method('getServerProtocol')
395
				->will($this->returnValue('fancy_protocol'));
396
397
			$this->request->expects($this->at(2))
398
				->method('getServerHost')
399
				->will($this->returnValue('nextcloud-host.tld'));
400
401
			$this->request->expects($this->at(3))
402
				->method('getRequestUri')
403
				->will($this->returnValue('/request/uri/123/42'));
404
405
			$this->urlGenerator->expects($this->at(0))
406
				->method('imagePath')
407
				->with('core', 'favicon-touch.png')
408
				->will($this->returnValue('/core/img/foo'));
409
410
			$this->urlGenerator->expects($this->at(1))
411
				->method('getAbsoluteURL')
412
				->with('/core/img/foo')
413
				->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo'));
414
415
			$this->urlGenerator->expects($this->at(2))
416
				->method('linkTo')
417
				->with('', 'remote.php')
418
				->will($this->returnValue('remote.php'));
419
420
			$this->urlGenerator->expects($this->at(3))
421
				->method('getAbsoluteURL')
422
				->with('remote.php/dav/public-calendars/fancy_token_123?export')
423
				->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export'));
424
425
			$this->request->expects($this->at(4))
426
				->method('getServerProtocol')
427
				->will($this->returnValue('fancy_protocol'));
428
429
430
431
			$actual = $this->controller->publicIndex('fancy_token_123');
432
433
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
434
			$this->assertEquals([
435
				'appVersion' => '42.13.37',
436
				'initialView' => 'month',
437
				'emailAddress' => '',
438
				'skipPopover' => 'no',
439
				'weekNumbers' => 'no',
440
				'supportsClass' => $expectsSupportsClass,
441
				'isPublic' => true,
442
				'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42',
443
				'previewImage' => 'fancy_protocol://foo.bar/core/img/foo',
444
				'firstRun' => 'no',
445
				'webCalWorkaround' => 'no',
446
				'needsAutosize' => $needsAutosize,
447
				'isIE' => $isIE,
448
				'defaultColor' => '#ff00ff',
449
				'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
450
				'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
451
			], $actual->getParams());
452
			$this->assertEquals('main', $actual->getTemplateName());
453
		}
454
455
	}
456
457
	public function indexPublicDataProvider() {
458
		return [
459
			[true, true, '9.0.5.2', false, true, false],
460
			[true, false, '9.1.0.0', true, true, false],
461
			[false, false, '9.0.5.2', false, true, false],
462
			[false, false, '9.1.0.0', true, true, false],
463
			[false, false, '11.0.0', true, false, false],
464
			[false, false, '11.0.0', true, false, true],
465
		];
466
	}
467
}
468