ViewControllerTest::testPublicIndexWithBranding()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 103

Duplication

Lines 103
Ratio 100 %

Importance

Changes 0
Metric Value
dl 103
loc 103
rs 8
c 0
b 0
f 0
cc 2
nc 2
nop 5

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