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

ViewControllerTest::testPublicIndex()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 105
Code Lines 83

Duplication

Lines 105
Ratio 100 %

Importance

Changes 7
Bugs 0 Features 3
Metric Value
cc 3
eloc 83
nc 4
nop 6
dl 105
loc 105
rs 8.2857
c 7
b 0
f 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		if (!$showAssetPipelineError) {
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
				'isEmbedded' => false,
150
				'needsAutosize' => $needsAutosize,
151
				'isIE' => $isIE,
152
				'token' => '',
153
			], $actual->getParams());
154
			$this->assertEquals('main', $actual->getTemplateName());
155
		}
156
157
	}
158
159
	public function indexDataProvider() {
160
		return [
161
			[true, true, '9.0.5.2', false, 'yes', true, false],
162
			[true, false, '9.1.0.0', true, 'no', true, false],
163
			[false, false, '9.0.5.2', false, 'yes', true, false],
164
			[false, false, '9.1.0.0', true, 'no', true, false],
165
			[false, false, '11.0.1', true, 'no', false, false],
166
			[false, false, '11.0.1', true, 'no', false, true],
167
		];
168
	}
169
170
	public function testIndexNoMonthFallback() {
171
		$this->config->expects($this->at(0))
172
			->method('getSystemValue')
173
			->with('version')
174
			->will($this->returnValue('9.1.0.0'));
175
176
		$this->config->expects($this->at(1))
177
			->method('getSystemValue')
178
			->with('asset-pipeline.enabled', false)
179
			->will($this->returnValue(false));
180
181
		$this->request->expects($this->once())
182
			->method('isUserAgent')
183
			->with(['/(MSIE)|(Trident)/'])
184
			->will($this->returnValue(false));
185
186
		$this->config->expects($this->at(2))
187
			->method('getSystemValue')
188
			->with('version')
189
			->will($this->returnValue('9.1.0.0'));
190
191
		$this->userSession->expects($this->once())
192
			->method('getUser')
193
			->will($this->returnValue($this->dummyUser));
194
195
		$this->dummyUser->expects($this->once())
196
			->method('getUID')
197
			->will($this->returnValue('user123'));
198
199
		$this->dummyUser->expects($this->once())
200
			->method('getEMailAddress')
201
			->will($this->returnValue('[email protected]'));
202
203
		$this->config->expects($this->at(3))
204
			->method('getAppValue')
205
			->with($this->appName, 'installed_version')
206
			->will($this->returnValue('42.13.37'));
207
208
		$this->config->expects($this->at(4))
209
			->method('getAppValue')
210
			->with('theming', 'color', '#0082C9')
211
			->will($this->returnValue('#ff00ff'));
212
213
		$this->config->expects($this->at(5))
214
			->method('getUserValue')
215
			->with('user123', $this->appName, 'currentView', null)
216
			->will($this->returnValue(null));
217
218
		$this->config->expects($this->at(6))
219
			->method('getUserValue')
220
			->with('user123', $this->appName, 'skipPopover', 'no')
221
			->will($this->returnValue('someSkipPopoverValue'));
222
223
		$this->config->expects($this->at(7))
224
			->method('getUserValue')
225
			->with('user123', $this->appName, 'showWeekNr', 'no')
226
			->will($this->returnValue('someShowWeekNrValue'));
227
228
		$this->config->expects($this->at(8))
229
			->method('getUserValue')
230
			->with('user123', $this->appName, 'firstRun', null)
231
			->will($this->returnValue('someFirstRunValue'));
232
233
		$actual = $this->controller->index();
234
235
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
236
		$this->assertEquals([
237
			'appVersion' => '42.13.37',
238
			'initialView' => 'month',
239
			'emailAddress' => '[email protected]',
240
			'skipPopover' => 'someSkipPopoverValue',
241
			'weekNumbers' => 'someShowWeekNrValue',
242
			'firstRun' => 'someFirstRunValue',
243
			'supportsClass' => true,
244
			'defaultColor' => '#ff00ff',
245
			'webCalWorkaround' => 'no',
246
			'isPublic' => false,
247
			'isEmbedded' => false,
248
			'needsAutosize' => true,
249
			'isIE' => false,
250
			'token' => '',
251
		], $actual->getParams());
252
		$this->assertEquals('main', $actual->getTemplateName());
253
	}
254
255
	/**
256
	 * @dataProvider indexFirstRunDetectionProvider
257
	 */
258
	public function testIndexFirstRunDetection($initialView, $expectedFirstRun, $expectsSetRequest) {
259
		$this->config->expects($this->at(0))
260
			->method('getSystemValue')
261
			->with('version')
262
			->will($this->returnValue('9.1.0.0'));
263
264
		$this->config->expects($this->at(1))
265
			->method('getSystemValue')
266
			->with('asset-pipeline.enabled', false)
267
			->will($this->returnValue(false));
268
269
		$this->request->expects($this->once())
270
			->method('isUserAgent')
271
			->with(['/(MSIE)|(Trident)/'])
272
			->will($this->returnValue(false));
273
274
		$this->config->expects($this->at(2))
275
			->method('getSystemValue')
276
			->with('version')
277
			->will($this->returnValue('9.1.0.0'));
278
279
		$this->userSession->expects($this->once())
280
			->method('getUser')
281
			->will($this->returnValue($this->dummyUser));
282
283
		$this->dummyUser->expects($this->once())
284
			->method('getUID')
285
			->will($this->returnValue('user123'));
286
287
		$this->dummyUser->expects($this->once())
288
			->method('getEMailAddress')
289
			->will($this->returnValue('[email protected]'));
290
291
		$this->config->expects($this->at(3))
292
			->method('getAppValue')
293
			->with($this->appName, 'installed_version')
294
			->will($this->returnValue('42.13.37'));
295
296
		$this->config->expects($this->at(4))
297
			->method('getAppValue')
298
			->with('theming', 'color', '#0082C9')
299
			->will($this->returnValue('#ff00ff'));
300
301
		$this->config->expects($this->at(5))
302
			->method('getUserValue')
303
			->with('user123', $this->appName, 'currentView', null)
304
			->will($this->returnValue($initialView));
305
306
		$this->config->expects($this->at(6))
307
			->method('getUserValue')
308
			->with('user123', $this->appName, 'skipPopover', 'no')
309
			->will($this->returnValue('someSkipPopoverValue'));
310
311
		$this->config->expects($this->at(7))
312
			->method('getUserValue')
313
			->with('user123', $this->appName, 'showWeekNr', 'no')
314
			->will($this->returnValue('someShowWeekNrValue'));
315
316
		$this->config->expects($this->at(8))
317
			->method('getUserValue')
318
			->with('user123', $this->appName, 'firstRun', null)
319
			->will($this->returnValue(null));
320
321
		if ($expectsSetRequest) {
322
			$this->config->expects($this->at(9))
323
				->method('setUserValue')
324
				->with('user123');
325
		}
326
327
		$actual = $this->controller->index();
328
329
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
330
		$this->assertEquals([
331
			'appVersion' => '42.13.37',
332
			'initialView' => $initialView ? 'someRandominitialView' : 'month',
333
			'emailAddress' => '[email protected]',
334
			'skipPopover' => 'someSkipPopoverValue',
335
			'weekNumbers' => 'someShowWeekNrValue',
336
			'firstRun' => $expectedFirstRun,
337
			'supportsClass' => true,
338
			'defaultColor' => '#ff00ff',
339
			'webCalWorkaround' => 'no',
340
			'isPublic' => false,
341
			'isEmbedded' => false,
342
			'needsAutosize' => true,
343
			'isIE' => false,
344
			'token' => '',
345
		], $actual->getParams());
346
		$this->assertEquals('main', $actual->getTemplateName());
347
	}
348
349
	public function indexFirstRunDetectionProvider() {
350
		return [
351
			[null, 'yes', false],
352
			['someRandominitialView', 'no', true],
353
		];
354
	}
355
356
	/**
357
	 * @dataProvider indexPublicDataProvider
358
	 */
359 View Code Duplication
	public function testPublicIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass, $needsAutosize, $isIE) {
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...
360
		$this->config->expects($this->at(0))
361
			->method('getSystemValue')
362
			->with('version')
363
			->will($this->returnValue($serverVersion));
364
365
		$this->config->expects($this->at(1))
366
			->method('getSystemValue')
367
			->with('asset-pipeline.enabled', false)
368
			->will($this->returnValue($isAssetPipelineEnabled));
369
370
		if (!$showAssetPipelineError) {
371
			$this->request->expects($this->once())
372
				->method('isUserAgent')
373
				->with(['/(MSIE)|(Trident)/'])
374
				->will($this->returnValue($isIE));
375
		}
376
377
		if ($showAssetPipelineError) {
378
			$actual = $this->controller->index();
379
380
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
381
			$this->assertEquals([], $actual->getParams());
382
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
383
		} else {
384
			$this->config->expects($this->at(2))
385
				->method('getSystemValue')
386
				->with('version')
387
				->will($this->returnValue($serverVersion));
388
389
			$this->config->expects($this->at(3))
390
				->method('getAppValue')
391
				->with($this->appName, 'installed_version')
392
				->will($this->returnValue('42.13.37'));
393
394
			$this->config->expects($this->at(4))
395
				->method('getAppValue')
396
				->with('theming', 'color', '#0082C9')
397
				->will($this->returnValue('#ff00ff'));
398
399
			$this->request->expects($this->at(1))
400
				->method('getServerProtocol')
401
				->will($this->returnValue('fancy_protocol'));
402
403
			$this->request->expects($this->at(2))
404
				->method('getServerHost')
405
				->will($this->returnValue('nextcloud-host.tld'));
406
407
			$this->request->expects($this->at(3))
408
				->method('getRequestUri')
409
				->will($this->returnValue('/request/uri/123/42'));
410
411
			$this->urlGenerator->expects($this->at(0))
412
				->method('imagePath')
413
				->with('core', 'favicon-touch.png')
414
				->will($this->returnValue('/core/img/foo'));
415
416
			$this->urlGenerator->expects($this->at(1))
417
				->method('getAbsoluteURL')
418
				->with('/core/img/foo')
419
				->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo'));
420
421
			$this->urlGenerator->expects($this->at(2))
422
				->method('linkTo')
423
				->with('', 'remote.php')
424
				->will($this->returnValue('remote.php'));
425
426
			$this->urlGenerator->expects($this->at(3))
427
				->method('getAbsoluteURL')
428
				->with('remote.php/dav/public-calendars/fancy_token_123?export')
429
				->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export'));
430
431
			$this->request->expects($this->at(4))
432
				->method('getServerProtocol')
433
				->will($this->returnValue('fancy_protocol'));
434
435
436
437
			$actual = $this->controller->publicIndex('fancy_token_123');
438
439
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
440
			$this->assertEquals([
441
				'appVersion' => '42.13.37',
442
				'initialView' => 'month',
443
				'emailAddress' => '',
444
				'skipPopover' => 'no',
445
				'weekNumbers' => 'no',
446
				'supportsClass' => $expectsSupportsClass,
447
				'isPublic' => true,
448
				'isEmbedded' => true,
449
				'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42',
450
				'previewImage' => 'fancy_protocol://foo.bar/core/img/foo',
451
				'firstRun' => 'no',
452
				'webCalWorkaround' => 'no',
453
				'needsAutosize' => $needsAutosize,
454
				'isIE' => $isIE,
455
				'defaultColor' => '#ff00ff',
456
				'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
457
				'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
458
				'token' => 'fancy_token_123',
459
			], $actual->getParams());
460
			$this->assertEquals('main', $actual->getTemplateName());
461
		}
462
463
	}
464
465
	/**
466
	 * @dataProvider indexPublicDataProvider
467
	 */
468 View Code Duplication
	public function testPublicIndexWithBranding($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass, $needsAutosize, $isIE) {
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...
469
		$this->config->expects($this->at(0))
470
			->method('getSystemValue')
471
			->with('version')
472
			->will($this->returnValue($serverVersion));
473
474
		$this->config->expects($this->at(1))
475
			->method('getSystemValue')
476
			->with('asset-pipeline.enabled', false)
477
			->will($this->returnValue($isAssetPipelineEnabled));
478
479
		if (!$showAssetPipelineError) {
480
			$this->request->expects($this->once())
481
				->method('isUserAgent')
482
				->with(['/(MSIE)|(Trident)/'])
483
				->will($this->returnValue($isIE));
484
		}
485
486
		if ($showAssetPipelineError) {
487
			$actual = $this->controller->index();
488
489
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
490
			$this->assertEquals([], $actual->getParams());
491
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
492
		} else {
493
			$this->config->expects($this->at(2))
494
				->method('getSystemValue')
495
				->with('version')
496
				->will($this->returnValue($serverVersion));
497
498
			$this->config->expects($this->at(3))
499
				->method('getAppValue')
500
				->with($this->appName, 'installed_version')
501
				->will($this->returnValue('42.13.37'));
502
503
			$this->config->expects($this->at(4))
504
				->method('getAppValue')
505
				->with('theming', 'color', '#0082C9')
506
				->will($this->returnValue('#ff00ff'));
507
508
			$this->request->expects($this->at(1))
509
				->method('getServerProtocol')
510
				->will($this->returnValue('fancy_protocol'));
511
512
			$this->request->expects($this->at(2))
513
				->method('getServerHost')
514
				->will($this->returnValue('nextcloud-host.tld'));
515
516
			$this->request->expects($this->at(3))
517
				->method('getRequestUri')
518
				->will($this->returnValue('/request/uri/123/42'));
519
520
			$this->urlGenerator->expects($this->at(0))
521
				->method('imagePath')
522
				->with('core', 'favicon-touch.png')
523
				->will($this->returnValue('/core/img/foo'));
524
525
			$this->urlGenerator->expects($this->at(1))
526
				->method('getAbsoluteURL')
527
				->with('/core/img/foo')
528
				->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo'));
529
530
			$this->urlGenerator->expects($this->at(2))
531
				->method('linkTo')
532
				->with('', 'remote.php')
533
				->will($this->returnValue('remote.php'));
534
535
			$this->urlGenerator->expects($this->at(3))
536
				->method('getAbsoluteURL')
537
				->with('remote.php/dav/public-calendars/fancy_token_123?export')
538
				->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export'));
539
540
			$this->request->expects($this->at(4))
541
				->method('getServerProtocol')
542
				->will($this->returnValue('fancy_protocol'));
543
544
545
546
			$actual = $this->controller->publicIndexWithBranding('fancy_token_123');
547
548
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
549
			$this->assertEquals([
550
				'appVersion' => '42.13.37',
551
				'initialView' => 'month',
552
				'emailAddress' => '',
553
				'skipPopover' => 'no',
554
				'weekNumbers' => 'no',
555
				'supportsClass' => $expectsSupportsClass,
556
				'isPublic' => true,
557
				'isEmbedded' => false,
558
				'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42',
559
				'previewImage' => 'fancy_protocol://foo.bar/core/img/foo',
560
				'firstRun' => 'no',
561
				'webCalWorkaround' => 'no',
562
				'needsAutosize' => $needsAutosize,
563
				'isIE' => $isIE,
564
				'defaultColor' => '#ff00ff',
565
				'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
566
				'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
567
				'token' => 'fancy_token_123',
568
			], $actual->getParams());
569
			$this->assertEquals('public', $actual->getTemplateName());
570
		}
571
572
	}
573
574
	public function indexPublicDataProvider() {
575
		return [
576
			[true, true, '9.0.5.2', false, true, false],
577
			[true, false, '9.1.0.0', true, true, false],
578
			[false, false, '9.0.5.2', false, true, false],
579
			[false, false, '9.1.0.0', true, true, false],
580
			[false, false, '11.0.0', true, false, false],
581
			[false, false, '11.0.0', true, false, true],
582
		];
583
	}
584
}
585