ViewControllerTest::testPublicIndexWithBranding()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 88

Duplication

Lines 88
Ratio 100 %

Importance

Changes 0
Metric Value
dl 88
loc 88
rs 8.2617
c 0
b 0
f 0
cc 1
nc 1
nop 4

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
23
namespace OCA\Calendar\Controller;
24
25
use PHPUnit\Framework\TestCase;
26
27
class ViewControllerTest extends TestCase {
28
29
	private $appName;
30
	private $request;
31
	private $config;
32
	private $userSession;
33
	private $urlGenerator;
34
35
	private $dummyUser;
36
37
	private $controller;
38
39
	public function setUp() {
40
		$this->appName = 'calendar';
41
		$this->request = $this->getMockBuilder('\OCP\IRequest')
42
			->disableOriginalConstructor()
43
			->getMock();
44
		$this->config = $this->getMockBuilder('\OCP\IConfig')
45
			->disableOriginalConstructor()
46
			->getMock();
47
		$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
48
			->disableOriginalConstructor()
49
			->getMock();
50
51
		$this->dummyUser = $this->getMockBuilder('OCP\IUser')
52
			->disableOriginalConstructor()
53
			->getMock();
54
55
		$this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')
56
			->disableOriginalConstructor()
57
			->getMock();
58
59
		$this->controller = new ViewController($this->appName, $this->request,
60
			$this->userSession, $this->config, $this->urlGenerator);
61
	}
62
63
	/**
64
	 * @dataProvider indexDataProvider
65
	 */
66
	public function testIndex($serverVersion, $isIE, $shareeActions, $shareeCanEdit) {
67
		$this->request->expects($this->once())
68
			->method('isUserAgent')
69
			->with(['/(MSIE)|(Trident)/'])
70
			->will($this->returnValue($isIE));
71
72
		$this->config->expects($this->at(0))
73
			->method('getSystemValue')
74
			->with('version')
75
			->will($this->returnValue($serverVersion));
76
77
		$this->config->expects($this->at(1))
78
			->method('getAppValue')
79
			->with($this->appName, 'installed_version')
80
			->will($this->returnValue('42.13.37'));
81
82
		$this->config->expects($this->at(2))
83
			->method('getAppValue')
84
			->with('theming', 'color', '#0082C9')
85
			->will($this->returnValue('#ff00ff'));
86
87
		$this->config->expects($this->at(3))
88
			->method('getAppValue')
89
			->with('core', 'shareapi_allow_links', 'yes')
90
			->will($this->returnValue('yes'));
91
92
		$this->userSession->expects($this->once())
93
			->method('getUser')
94
			->will($this->returnValue($this->dummyUser));
95
96
		$this->dummyUser->expects($this->once())
97
			->method('getUID')
98
			->will($this->returnValue('user123'));
99
100
		$this->dummyUser->expects($this->once())
101
			->method('getEMailAddress')
102
			->will($this->returnValue('[email protected]'));
103
104
		$this->config->expects($this->at(4))
105
			->method('getUserValue')
106
			->with('user123', $this->appName, 'currentView', null)
107
			->will($this->returnValue('someView'));
108
109
		$this->config->expects($this->at(5))
110
			->method('getUserValue')
111
			->with('user123', $this->appName, 'skipPopover', 'no')
112
			->will($this->returnValue('someSkipPopoverValue'));
113
114
		$this->config->expects($this->at(6))
115
			->method('getUserValue')
116
			->with('user123', $this->appName, 'showWeekNr', 'no')
117
			->will($this->returnValue('someShowWeekNrValue'));
118
119
		$this->config->expects($this->at(7))
120
			->method('getUserValue')
121
			->with('user123', $this->appName, 'firstRun', null)
122
			->will($this->returnValue('someFirstRunValue'));
123
124
		$this->config->expects($this->at(8))
125
			->method('getUserValue')
126
			->with('user123', $this->appName, 'timezone', 'automatic')
127
			->will($this->returnValue('Australia/Adelaide'));
128
129
		$actual = $this->controller->index();
130
131
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
132
		$this->assertEquals([
133
			'appVersion' => '42.13.37',
134
			'initialView' => 'someView',
135
			'emailAddress' => '[email protected]',
136
			'skipPopover' => 'someSkipPopoverValue',
137
			'weekNumbers' => 'someShowWeekNrValue',
138
			'firstRun' => 'someFirstRunValue',
139
			'canSharePublicLink' => 'yes',
140
			'defaultColor' => '#ff00ff',
141
			'isPublic' => false,
142
			'isEmbedded' => false,
143
			'isIE' => $isIE,
144
			'token' => '',
145
			'shareeCanEditShares' => $shareeActions,
146
			'shareeCanEditCalendarProperties' => $shareeCanEdit,
147
			'timezone' => 'Australia/Adelaide',
148
		], $actual->getParams());
149
		$this->assertEquals('main', $actual->getTemplateName());
150
	}
151
152 View Code Duplication
	public function indexDataProvider() {
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...
153
		return [
154
			['11.0.1', false, 'yes', 'no'],
155
			['11.0.1', true, 'yes', 'no'],
156
			['12.0.0', false, 'no', 'yes'],
157
			['12.0.0', true, 'no', 'yes'],
158
		];
159
	}
160
161
	public function testIndexNoMonthFallback() {
162
		$this->request->expects($this->once())
163
			->method('isUserAgent')
164
			->with(['/(MSIE)|(Trident)/'])
165
			->will($this->returnValue(false));
166
167
		$this->config->expects($this->at(0))
168
			->method('getSystemValue')
169
			->with('version')
170
			->will($this->returnValue('12.0.0'));
171
172
		$this->userSession->expects($this->once())
173
			->method('getUser')
174
			->will($this->returnValue($this->dummyUser));
175
176
		$this->dummyUser->expects($this->once())
177
			->method('getUID')
178
			->will($this->returnValue('user123'));
179
180
		$this->dummyUser->expects($this->once())
181
			->method('getEMailAddress')
182
			->will($this->returnValue('[email protected]'));
183
184
		$this->config->expects($this->at(1))
185
			->method('getAppValue')
186
			->with($this->appName, 'installed_version')
187
			->will($this->returnValue('42.13.37'));
188
189
		$this->config->expects($this->at(2))
190
			->method('getAppValue')
191
			->with('theming', 'color', '#0082C9')
192
			->will($this->returnValue('#ff00ff'));
193
194
		$this->config->expects($this->at(3))
195
			->method('getAppValue')
196
			->with('core', 'shareapi_allow_links', 'yes')
197
			->will($this->returnValue('no'));
198
199
		$this->config->expects($this->at(4))
200
			->method('getUserValue')
201
			->with('user123', $this->appName, 'currentView', null)
202
			->will($this->returnValue(null));
203
204
		$this->config->expects($this->at(5))
205
			->method('getUserValue')
206
			->with('user123', $this->appName, 'skipPopover', 'no')
207
			->will($this->returnValue('someSkipPopoverValue'));
208
209
		$this->config->expects($this->at(6))
210
			->method('getUserValue')
211
			->with('user123', $this->appName, 'showWeekNr', 'no')
212
			->will($this->returnValue('someShowWeekNrValue'));
213
214
		$this->config->expects($this->at(7))
215
			->method('getUserValue')
216
			->with('user123', $this->appName, 'firstRun', null)
217
			->will($this->returnValue('someFirstRunValue'));
218
219
		$this->config->expects($this->at(8))
220
			->method('getUserValue')
221
			->with('user123', $this->appName, 'timezone', 'automatic')
222
			->will($this->returnValue('Australia/Adelaide'));
223
224
		$actual = $this->controller->index();
225
226
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
227
		$this->assertEquals([
228
			'appVersion' => '42.13.37',
229
			'initialView' => 'month',
230
			'emailAddress' => '[email protected]',
231
			'skipPopover' => 'someSkipPopoverValue',
232
			'weekNumbers' => 'someShowWeekNrValue',
233
			'firstRun' => 'someFirstRunValue',
234
			'defaultColor' => '#ff00ff',
235
			'isPublic' => false,
236
			'isEmbedded' => false,
237
			'isIE' => false,
238
			'token' => '',
239
			'shareeCanEditShares' => 'no',
240
			'shareeCanEditCalendarProperties' => 'yes',
241
			'canSharePublicLink' => 'no',
242
			'timezone' => 'Australia/Adelaide',
243
		], $actual->getParams());
244
		$this->assertEquals('main', $actual->getTemplateName());
245
	}
246
247
	/**
248
	 * @dataProvider indexFirstRunDetectionProvider
249
	 */
250
	public function testIndexFirstRunDetection($initialView, $expectedFirstRun, $expectsSetRequest) {
251
		$this->request->expects($this->once())
252
			->method('isUserAgent')
253
			->with(['/(MSIE)|(Trident)/'])
254
			->will($this->returnValue(false));
255
256
		$this->config->expects($this->at(0))
257
			->method('getSystemValue')
258
			->with('version')
259
			->will($this->returnValue('9.1.0.0'));
260
261
		$this->userSession->expects($this->once())
262
			->method('getUser')
263
			->will($this->returnValue($this->dummyUser));
264
265
		$this->dummyUser->expects($this->once())
266
			->method('getUID')
267
			->will($this->returnValue('user123'));
268
269
		$this->dummyUser->expects($this->once())
270
			->method('getEMailAddress')
271
			->will($this->returnValue('[email protected]'));
272
273
		$this->config->expects($this->at(1))
274
			->method('getAppValue')
275
			->with($this->appName, 'installed_version')
276
			->will($this->returnValue('42.13.37'));
277
278
		$this->config->expects($this->at(2))
279
			->method('getAppValue')
280
			->with('theming', 'color', '#0082C9')
281
			->will($this->returnValue('#ff00ff'));
282
283
		$this->config->expects($this->at(3))
284
			->method('getAppValue')
285
			->with('core', 'shareapi_allow_links', 'yes')
286
			->will($this->returnValue('no'));
287
288
		$this->config->expects($this->at(4))
289
			->method('getUserValue')
290
			->with('user123', $this->appName, 'currentView', null)
291
			->will($this->returnValue($initialView));
292
293
		$this->config->expects($this->at(5))
294
			->method('getUserValue')
295
			->with('user123', $this->appName, 'skipPopover', 'no')
296
			->will($this->returnValue('someSkipPopoverValue'));
297
298
		$this->config->expects($this->at(6))
299
			->method('getUserValue')
300
			->with('user123', $this->appName, 'showWeekNr', 'no')
301
			->will($this->returnValue('someShowWeekNrValue'));
302
303
		$this->config->expects($this->at(7))
304
			->method('getUserValue')
305
			->with('user123', $this->appName, 'firstRun', null)
306
			->will($this->returnValue(null));
307
308
		$this->config->expects($this->at(8))
309
			->method('getUserValue')
310
			->with('user123', $this->appName, 'timezone', 'automatic')
311
			->will($this->returnValue('Australia/Adelaide'));
312
313
314
		if ($expectsSetRequest) {
315
			$this->config->expects($this->at(9))
316
				->method('setUserValue')
317
				->with('user123');
318
		}
319
320
		$actual = $this->controller->index();
321
322
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
323
		$this->assertEquals([
324
			'appVersion' => '42.13.37',
325
			'initialView' => $initialView ? 'someRandominitialView' : 'month',
326
			'emailAddress' => '[email protected]',
327
			'skipPopover' => 'someSkipPopoverValue',
328
			'weekNumbers' => 'someShowWeekNrValue',
329
			'firstRun' => $expectedFirstRun,
330
			'defaultColor' => '#ff00ff',
331
			'isPublic' => false,
332
			'isEmbedded' => false,
333
			'isIE' => false,
334
			'token' => '',
335
			'shareeCanEditShares' => 'yes',
336
			'shareeCanEditCalendarProperties' => 'no',
337
			'canSharePublicLink' => 'no',
338
			'timezone' => 'Australia/Adelaide',
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 View Code Duplication
	public function testPublicIndex($serverVersion, $isIE, $shareeActions, $shareeCanEdit) {
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...
354
		$this->request->expects($this->once())
355
			->method('isUserAgent')
356
			->with(['/(MSIE)|(Trident)/'])
357
			->will($this->returnValue($isIE));
358
359
		$this->config->expects($this->at(0))
360
			->method('getSystemValue')
361
			->with('version')
362
			->will($this->returnValue($serverVersion));
363
364
		$this->config->expects($this->at(1))
365
			->method('getAppValue')
366
			->with($this->appName, 'installed_version')
367
			->will($this->returnValue('42.13.37'));
368
369
		$this->config->expects($this->at(2))
370
			->method('getAppValue')
371
			->with('theming', 'color', '#0082C9')
372
			->will($this->returnValue('#ff00ff'));
373
374
		$this->config->expects($this->at(3))
375
			->method('getAppValue')
376
			->with('core', 'shareapi_allow_links', 'yes')
377
			->will($this->returnValue('no'));
378
379
		$this->request->expects($this->at(1))
380
			->method('getServerProtocol')
381
			->will($this->returnValue('fancy_protocol'));
382
383
		$this->request->expects($this->at(2))
384
			->method('getServerHost')
385
			->will($this->returnValue('nextcloud-host.tld'));
386
387
		$this->request->expects($this->at(3))
388
			->method('getRequestUri')
389
			->will($this->returnValue('/request/uri/123/42'));
390
391
		$this->urlGenerator->expects($this->at(0))
392
			->method('imagePath')
393
			->with('core', 'favicon-touch.png')
394
			->will($this->returnValue('/core/img/foo'));
395
396
		$this->urlGenerator->expects($this->at(1))
397
			->method('getAbsoluteURL')
398
			->with('/core/img/foo')
399
			->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo'));
400
401
		$this->urlGenerator->expects($this->at(2))
402
			->method('linkTo')
403
			->with('', 'remote.php')
404
			->will($this->returnValue('remote.php'));
405
406
		$this->urlGenerator->expects($this->at(3))
407
			->method('getAbsoluteURL')
408
			->with('remote.php/dav/public-calendars/fancy_token_123?export')
409
			->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export'));
410
411
		$this->request->expects($this->at(4))
412
			->method('getServerProtocol')
413
			->will($this->returnValue('fancy_protocol'));
414
415
		$actual = $this->controller->publicIndexForEmbedding('fancy_token_123');
416
417
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
418
		$this->assertEquals([
419
			'appVersion' => '42.13.37',
420
			'initialView' => 'month',
421
			'emailAddress' => '',
422
			'skipPopover' => 'no',
423
			'weekNumbers' => 'no',
424
			'isPublic' => true,
425
			'isEmbedded' => true,
426
			'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42',
427
			'previewImage' => 'fancy_protocol://foo.bar/core/img/foo',
428
			'firstRun' => 'no',
429
			'isIE' => $isIE,
430
			'defaultColor' => '#ff00ff',
431
			'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
432
			'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
433
			'token' => 'fancy_token_123',
434
			'shareeCanEditShares' => $shareeActions,
435
			'shareeCanEditCalendarProperties' => $shareeCanEdit,
436
			'canSharePublicLink' => 'no',
437
			'timezone' => 'automatic',
438
		], $actual->getParams());
439
		$this->assertEquals('main', $actual->getTemplateName());
440
	}
441
442
	/**
443
	 * @dataProvider indexPublicDataProvider
444
	 */
445 View Code Duplication
	public function testPublicIndexWithBranding($serverVersion, $isIE, $shareeActions, $shareeCanEdit) {
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...
446
		$this->request->expects($this->once())
447
			->method('isUserAgent')
448
			->with(['/(MSIE)|(Trident)/'])
449
			->will($this->returnValue($isIE));
450
451
		$this->config->expects($this->at(0))
452
			->method('getSystemValue')
453
			->with('version')
454
			->will($this->returnValue($serverVersion));
455
456
		$this->config->expects($this->at(1))
457
			->method('getAppValue')
458
			->with($this->appName, 'installed_version')
459
			->will($this->returnValue('42.13.37'));
460
461
		$this->config->expects($this->at(2))
462
			->method('getAppValue')
463
			->with('theming', 'color', '#0082C9')
464
			->will($this->returnValue('#ff00ff'));
465
466
		$this->config->expects($this->at(3))
467
			->method('getAppValue')
468
			->with('core', 'shareapi_allow_links', 'yes')
469
			->will($this->returnValue('no'));
470
471
		$this->request->expects($this->at(1))
472
			->method('getServerProtocol')
473
			->will($this->returnValue('fancy_protocol'));
474
475
		$this->request->expects($this->at(2))
476
			->method('getServerHost')
477
			->will($this->returnValue('nextcloud-host.tld'));
478
479
		$this->request->expects($this->at(3))
480
			->method('getRequestUri')
481
			->will($this->returnValue('/request/uri/123/42'));
482
483
		$this->urlGenerator->expects($this->at(0))
484
			->method('imagePath')
485
			->with('core', 'favicon-touch.png')
486
			->will($this->returnValue('/core/img/foo'));
487
488
		$this->urlGenerator->expects($this->at(1))
489
			->method('getAbsoluteURL')
490
			->with('/core/img/foo')
491
			->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo'));
492
493
		$this->urlGenerator->expects($this->at(2))
494
			->method('linkTo')
495
			->with('', 'remote.php')
496
			->will($this->returnValue('remote.php'));
497
498
		$this->urlGenerator->expects($this->at(3))
499
			->method('getAbsoluteURL')
500
			->with('remote.php/dav/public-calendars/fancy_token_123?export')
501
			->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export'));
502
503
		$this->request->expects($this->at(4))
504
			->method('getServerProtocol')
505
			->will($this->returnValue('fancy_protocol'));
506
507
		$actual = $this->controller->publicIndexWithBranding('fancy_token_123');
508
509
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
510
		$this->assertEquals([
511
			'appVersion' => '42.13.37',
512
			'initialView' => 'month',
513
			'emailAddress' => '',
514
			'skipPopover' => 'no',
515
			'weekNumbers' => 'no',
516
			'isPublic' => true,
517
			'isEmbedded' => false,
518
			'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42',
519
			'previewImage' => 'fancy_protocol://foo.bar/core/img/foo',
520
			'firstRun' => 'no',
521
			'isIE' => $isIE,
522
			'defaultColor' => '#ff00ff',
523
			'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
524
			'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
525
			'token' => 'fancy_token_123',
526
			'shareeCanEditShares' => $shareeActions,
527
			'shareeCanEditCalendarProperties' => $shareeCanEdit,
528
			'canSharePublicLink' => 'no',
529
			'timezone' => 'automatic',
530
		], $actual->getParams());
531
		$this->assertEquals('public', $actual->getTemplateName());
532
	}
533
534 View Code Duplication
	public function indexPublicDataProvider() {
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...
535
		return [
536
			['11.0.0', false, 'yes', 'no'],
537
			['11.0.0', true, 'yes', 'no'],
538
			['12.0.0', false, 'no', 'yes'],
539
			['12.0.0', true, 'no', 'yes'],
540
		];
541
	}
542
}
543