Passed
Pull Request — master (#453)
by Georg
05:58
created

ViewControllerTest::testIndexNoMonthFallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

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