Completed
Pull Request — master (#185)
by Georg
04:54
created

ViewControllerTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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