Passed
Pull Request — master (#372)
by Georg
06:09
created

ViewControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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