Completed
Pull Request — master (#526)
by Thomas
19:24
created

ViewControllerTest::testIndexFirstRunDetection()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 79
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 79
rs 8.8701
cc 3
eloc 64
nc 2
nop 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
		$this->config->expects($this->at(6))
118
			->method('getAppValue')
119
			->with('core', 'shareapi_allow_links', 'no')
120
			->will($this->returnValue('yes'));
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('getUserValue')
188
			->with('user123', $this->appName, 'currentView', null)
189
			->will($this->returnValue(null));
190
191
		$this->config->expects($this->at(4))
192
			->method('getUserValue')
193
			->with('user123', $this->appName, 'skipPopover', 'no')
194
			->will($this->returnValue('someSkipPopoverValue'));
195
196
		$this->config->expects($this->at(5))
197
			->method('getUserValue')
198
			->with('user123', $this->appName, 'showWeekNr', 'no')
199
			->will($this->returnValue('someShowWeekNrValue'));
200
201
		$this->config->expects($this->at(6))
202
			->method('getUserValue')
203
			->with('user123', $this->appName, 'firstRun', null)
204
			->will($this->returnValue('someFirstRunValue'));
205
206
		$actual = $this->controller->index();
207
208
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
209
		$this->assertEquals([
210
			'appVersion' => '42.13.37',
211
			'initialView' => 'month',
212
			'emailAddress' => '[email protected]',
213
			'skipPopover' => 'someSkipPopoverValue',
214
			'weekNumbers' => 'someShowWeekNrValue',
215
			'firstRun' => 'someFirstRunValue',
216
			'defaultColor' => '#ff00ff',
217
			'isPublic' => false,
218
			'isEmbedded' => false,
219
			'isIE' => false,
220
			'token' => '',
221
			'shareeCanEditShares' => 'no',
222
			'shareeCanEditCalendarProperties' => 'yes',
223
		], $actual->getParams());
224
		$this->assertEquals('main', $actual->getTemplateName());
225
	}
226
227
	/**
228
	 * @dataProvider indexFirstRunDetectionProvider
229
	 */
230
	public function testIndexFirstRunDetection($initialView, $expectedFirstRun, $expectsSetRequest) {
231
		$this->request->expects($this->once())
232
			->method('isUserAgent')
233
			->with(['/(MSIE)|(Trident)/'])
234
			->will($this->returnValue(false));
235
236
		$this->config->expects($this->at(0))
237
			->method('getSystemValue')
238
			->with('version')
239
			->will($this->returnValue('9.1.0.0'));
240
241
		$this->userSession->expects($this->once())
242
			->method('getUser')
243
			->will($this->returnValue($this->dummyUser));
244
245
		$this->dummyUser->expects($this->once())
246
			->method('getUID')
247
			->will($this->returnValue('user123'));
248
249
		$this->dummyUser->expects($this->once())
250
			->method('getEMailAddress')
251
			->will($this->returnValue('[email protected]'));
252
253
		$this->config->expects($this->at(1))
254
			->method('getAppValue')
255
			->with($this->appName, 'installed_version')
256
			->will($this->returnValue('42.13.37'));
257
258
		$this->config->expects($this->at(2))
259
			->method('getAppValue')
260
			->with('theming', 'color', '#0082C9')
261
			->will($this->returnValue('#ff00ff'));
262
263
		$this->config->expects($this->at(3))
264
			->method('getUserValue')
265
			->with('user123', $this->appName, 'currentView', null)
266
			->will($this->returnValue($initialView));
267
268
		$this->config->expects($this->at(4))
269
			->method('getUserValue')
270
			->with('user123', $this->appName, 'skipPopover', 'no')
271
			->will($this->returnValue('someSkipPopoverValue'));
272
273
		$this->config->expects($this->at(5))
274
			->method('getUserValue')
275
			->with('user123', $this->appName, 'showWeekNr', 'no')
276
			->will($this->returnValue('someShowWeekNrValue'));
277
278
		$this->config->expects($this->at(6))
279
			->method('getUserValue')
280
			->with('user123', $this->appName, 'firstRun', null)
281
			->will($this->returnValue(null));
282
283
		if ($expectsSetRequest) {
284
			$this->config->expects($this->at(7))
285
				->method('setUserValue')
286
				->with('user123');
287
		}
288
289
		$actual = $this->controller->index();
290
291
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
292
		$this->assertEquals([
293
			'appVersion' => '42.13.37',
294
			'initialView' => $initialView ? 'someRandominitialView' : 'month',
295
			'emailAddress' => '[email protected]',
296
			'skipPopover' => 'someSkipPopoverValue',
297
			'weekNumbers' => 'someShowWeekNrValue',
298
			'firstRun' => $expectedFirstRun,
299
			'defaultColor' => '#ff00ff',
300
			'isPublic' => false,
301
			'isEmbedded' => false,
302
			'isIE' => false,
303
			'token' => '',
304
			'shareeCanEditShares' => 'yes',
305
			'shareeCanEditCalendarProperties' => 'no',
306
		], $actual->getParams());
307
		$this->assertEquals('main', $actual->getTemplateName());
308
	}
309
310
	public function indexFirstRunDetectionProvider() {
311
		return [
312
			[null, 'yes', false],
313
			['someRandominitialView', 'no', true],
314
		];
315
	}
316
317
	/**
318
	 * @dataProvider indexPublicDataProvider
319
	 */
320 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...
321
		$this->request->expects($this->once())
322
			->method('isUserAgent')
323
			->with(['/(MSIE)|(Trident)/'])
324
			->will($this->returnValue($isIE));
325
326
		$this->config->expects($this->at(0))
327
			->method('getSystemValue')
328
			->with('version')
329
			->will($this->returnValue($serverVersion));
330
331
		$this->config->expects($this->at(1))
332
			->method('getAppValue')
333
			->with($this->appName, 'installed_version')
334
			->will($this->returnValue('42.13.37'));
335
336
		$this->config->expects($this->at(2))
337
			->method('getAppValue')
338
			->with('theming', 'color', '#0082C9')
339
			->will($this->returnValue('#ff00ff'));
340
341
		$this->request->expects($this->at(1))
342
			->method('getServerProtocol')
343
			->will($this->returnValue('fancy_protocol'));
344
345
		$this->request->expects($this->at(2))
346
			->method('getServerHost')
347
			->will($this->returnValue('nextcloud-host.tld'));
348
349
		$this->request->expects($this->at(3))
350
			->method('getRequestUri')
351
			->will($this->returnValue('/request/uri/123/42'));
352
353
		$this->urlGenerator->expects($this->at(0))
354
			->method('imagePath')
355
			->with('core', 'favicon-touch.png')
356
			->will($this->returnValue('/core/img/foo'));
357
358
		$this->urlGenerator->expects($this->at(1))
359
			->method('getAbsoluteURL')
360
			->with('/core/img/foo')
361
			->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo'));
362
363
		$this->urlGenerator->expects($this->at(2))
364
			->method('linkTo')
365
			->with('', 'remote.php')
366
			->will($this->returnValue('remote.php'));
367
368
		$this->urlGenerator->expects($this->at(3))
369
			->method('getAbsoluteURL')
370
			->with('remote.php/dav/public-calendars/fancy_token_123?export')
371
			->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export'));
372
373
		$this->request->expects($this->at(4))
374
			->method('getServerProtocol')
375
			->will($this->returnValue('fancy_protocol'));
376
377
		$actual = $this->controller->publicIndexForEmbedding('fancy_token_123');
378
379
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
380
		$this->assertEquals([
381
			'appVersion' => '42.13.37',
382
			'initialView' => 'month',
383
			'emailAddress' => '',
384
			'skipPopover' => 'no',
385
			'weekNumbers' => 'no',
386
			'isPublic' => true,
387
			'isEmbedded' => true,
388
			'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42',
389
			'previewImage' => 'fancy_protocol://foo.bar/core/img/foo',
390
			'firstRun' => 'no',
391
			'isIE' => $isIE,
392
			'defaultColor' => '#ff00ff',
393
			'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
394
			'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
395
			'token' => 'fancy_token_123',
396
			'shareeCanEditShares' => $shareeActions,
397
			'shareeCanEditCalendarProperties' => $shareeCanEdit,
398
			'canSharePublicLink' => 'no',
399
		], $actual->getParams());
400
		$this->assertEquals('main', $actual->getTemplateName());
401
	}
402
403
	/**
404
	 * @dataProvider indexPublicDataProvider
405
	 */
406 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...
407
		$this->request->expects($this->once())
408
			->method('isUserAgent')
409
			->with(['/(MSIE)|(Trident)/'])
410
			->will($this->returnValue($isIE));
411
412
		$this->config->expects($this->at(0))
413
			->method('getSystemValue')
414
			->with('version')
415
			->will($this->returnValue($serverVersion));
416
417
		$this->config->expects($this->at(1))
418
			->method('getAppValue')
419
			->with($this->appName, 'installed_version')
420
			->will($this->returnValue('42.13.37'));
421
422
		$this->config->expects($this->at(2))
423
			->method('getAppValue')
424
			->with('theming', 'color', '#0082C9')
425
			->will($this->returnValue('#ff00ff'));
426
427
		$this->request->expects($this->at(1))
428
			->method('getServerProtocol')
429
			->will($this->returnValue('fancy_protocol'));
430
431
		$this->request->expects($this->at(2))
432
			->method('getServerHost')
433
			->will($this->returnValue('nextcloud-host.tld'));
434
435
		$this->request->expects($this->at(3))
436
			->method('getRequestUri')
437
			->will($this->returnValue('/request/uri/123/42'));
438
439
		$this->urlGenerator->expects($this->at(0))
440
			->method('imagePath')
441
			->with('core', 'favicon-touch.png')
442
			->will($this->returnValue('/core/img/foo'));
443
444
		$this->urlGenerator->expects($this->at(1))
445
			->method('getAbsoluteURL')
446
			->with('/core/img/foo')
447
			->will($this->returnValue('fancy_protocol://foo.bar/core/img/foo'));
448
449
		$this->urlGenerator->expects($this->at(2))
450
			->method('linkTo')
451
			->with('', 'remote.php')
452
			->will($this->returnValue('remote.php'));
453
454
		$this->urlGenerator->expects($this->at(3))
455
			->method('getAbsoluteURL')
456
			->with('remote.php/dav/public-calendars/fancy_token_123?export')
457
			->will($this->returnValue('fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export'));
458
459
		$this->request->expects($this->at(4))
460
			->method('getServerProtocol')
461
			->will($this->returnValue('fancy_protocol'));
462
463
		$actual = $this->controller->publicIndexWithBranding('fancy_token_123');
464
465
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
466
		$this->assertEquals([
467
			'appVersion' => '42.13.37',
468
			'initialView' => 'month',
469
			'emailAddress' => '',
470
			'skipPopover' => 'no',
471
			'weekNumbers' => 'no',
472
			'isPublic' => true,
473
			'isEmbedded' => false,
474
			'shareURL' => 'fancy_protocol://nextcloud-host.tld/request/uri/123/42',
475
			'previewImage' => 'fancy_protocol://foo.bar/core/img/foo',
476
			'firstRun' => 'no',
477
			'isIE' => $isIE,
478
			'defaultColor' => '#ff00ff',
479
			'webcalURL' => 'webcal://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
480
			'downloadURL' => 'fancy_protocol://foo.bar/remote.php/dav/public-calendars/fancy_token_123?export',
481
			'token' => 'fancy_token_123',
482
			'shareeCanEditShares' => $shareeActions,
483
			'shareeCanEditCalendarProperties' => $shareeCanEdit,
484
			'canSharePublicLink' => 'no',
485
		], $actual->getParams());
486
		$this->assertEquals('public', $actual->getTemplateName());
487
	}
488
489 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...
490
		return [
491
			['11.0.0', false, 'yes', 'no'],
492
			['11.0.0', true, 'yes', 'no'],
493
			['12.0.0', false, 'no', 'yes'],
494
			['12.0.0', true, 'no', 'yes'],
495
		];
496
	}
497
}
498