Passed
Push — master ( a44cbc...f5d0d8 )
by Georg
04:23
created

ViewControllerTest::testIndexNoMonthFallback()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 79
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 79
rs 8.8701
cc 1
eloc 65
nc 1
nop 0

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