Completed
Pull Request — master (#329)
by Georg
08:42
created

ViewControllerTest::testPublicIndex()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 52
Code Lines 42

Duplication

Lines 6
Ratio 11.54 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 3
eloc 42
nc 4
nop 6
dl 6
loc 52
rs 9.4929
c 4
b 0
f 1

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
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->userSession->expects($this->once())
89
				->method('getUser')
90
				->will($this->returnValue($this->dummyUser));
91
92
			$this->dummyUser->expects($this->once())
93
				->method('getUID')
94
				->will($this->returnValue('user123'));
95
96
			$this->dummyUser->expects($this->once())
97
				->method('getEMailAddress')
98
				->will($this->returnValue('[email protected]'));
99
100
			$this->config->expects($this->at(2))
101
				->method('getAppValue')
102
				->with($this->appName, 'installed_version')
103
				->will($this->returnValue('42.13.37'));
104
105
			$this->config->expects($this->at(3))
106
				->method('getUserValue')
107
				->with('user123', $this->appName, 'currentView', null)
108
				->will($this->returnValue('someView'));
109
110
			$this->config->expects($this->at(4))
111
				->method('getUserValue')
112
				->with('user123', $this->appName, 'skipPopover', 'no')
113
				->will($this->returnValue('someSkipPopoverValue'));
114
115
			$this->config->expects($this->at(5))
116
				->method('getUserValue')
117
				->with('user123', $this->appName, 'showWeekNr', 'no')
118
				->will($this->returnValue('someShowWeekNrValue'));
119
120
			$this->config->expects($this->at(6))
121
				->method('getUserValue')
122
				->with('user123', $this->appName, 'firstRun', null)
123
				->will($this->returnValue('someFirstRunValue'));
124
125
			$this->config->expects($this->at(7))
126
				->method('getAppValue')
127
				->with('theming', 'color', '#0082C9')
128
				->will($this->returnValue('#ff00ff'));
129
130
			$actual = $this->controller->index();
131
132
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
133
			$this->assertEquals([
134
				'appVersion' => '42.13.37',
135
				'initialView' => 'someView',
136
				'emailAddress' => '[email protected]',
137
				'skipPopover' => 'someSkipPopoverValue',
138
				'weekNumbers' => 'someShowWeekNrValue',
139
				'firstRun' => 'someFirstRunValue',
140
				'supportsClass' => $expectsSupportsClass,
141
				'defaultColor' => '#ff00ff',
142
				'webCalWorkaround' => $expectsWebcalWorkaround,
143
				'isPublic' => false,
144
				'needsAutosize' => $needsAutosize,
145
				'isIE' => $isIE,
146
			], $actual->getParams());
147
			$this->assertEquals('main', $actual->getTemplateName());
148
		}
149
150
	}
151
152
	public function indexDataProvider() {
153
		return [
154
			[true, true, '9.0.5.2', false, 'yes', true, false],
155
			[true, false, '9.1.0.0', true, 'no', true, false],
156
			[false, false, '9.0.5.2', false, 'yes', true, false],
157
			[false, false, '9.1.0.0', true, 'no', true, false],
158
			[false, false, '11.0.1', true, 'no', false, false],
159
			[false, false, '11.0.1', true, 'no', false, true],
160
		];
161
	}
162
163
	public function testIndexNoMonthFallback() {
164
		$this->config->expects($this->at(0))
165
			->method('getSystemValue')
166
			->with('version')
167
			->will($this->returnValue('9.1.0.0'));
168
169
		$this->config->expects($this->at(1))
170
			->method('getSystemValue')
171
			->with('asset-pipeline.enabled', false)
172
			->will($this->returnValue(false));
173
174
		$this->request->expects($this->once())
175
			->method('isUserAgent')
176
			->with(['/(MSIE)|(Trident)/'])
177
			->will($this->returnValue(false));
178
179
		$this->userSession->expects($this->once())
180
			->method('getUser')
181
			->will($this->returnValue($this->dummyUser));
182
183
		$this->dummyUser->expects($this->once())
184
			->method('getUID')
185
			->will($this->returnValue('user123'));
186
187
		$this->dummyUser->expects($this->once())
188
			->method('getEMailAddress')
189
			->will($this->returnValue('[email protected]'));
190
191
		$this->config->expects($this->at(2))
192
			->method('getAppValue')
193
			->with($this->appName, 'installed_version')
194
			->will($this->returnValue('42.13.37'));
195
196
		$this->config->expects($this->at(3))
197
			->method('getUserValue')
198
			->with('user123', $this->appName, 'currentView', null)
199
			->will($this->returnValue(null));
200
201
		$this->config->expects($this->at(4))
202
			->method('getUserValue')
203
			->with('user123', $this->appName, 'skipPopover', 'no')
204
			->will($this->returnValue('someSkipPopoverValue'));
205
206
		$this->config->expects($this->at(5))
207
			->method('getUserValue')
208
			->with('user123', $this->appName, 'showWeekNr', 'no')
209
			->will($this->returnValue('someShowWeekNrValue'));
210
211
		$this->config->expects($this->at(6))
212
			->method('getUserValue')
213
			->with('user123', $this->appName, 'firstRun', null)
214
			->will($this->returnValue('someFirstRunValue'));
215
216
		$this->config->expects($this->at(7))
217
			->method('getAppValue')
218
			->with('theming', 'color', '#0082C9')
219
			->will($this->returnValue('#ff00ff'));
220
221
		$actual = $this->controller->index();
222
223
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
224
		$this->assertEquals([
225
			'appVersion' => '42.13.37',
226
			'initialView' => 'month',
227
			'emailAddress' => '[email protected]',
228
			'skipPopover' => 'someSkipPopoverValue',
229
			'weekNumbers' => 'someShowWeekNrValue',
230
			'firstRun' => 'someFirstRunValue',
231
			'supportsClass' => true,
232
			'defaultColor' => '#ff00ff',
233
			'webCalWorkaround' => 'no',
234
			'isPublic' => false,
235
			'needsAutosize' => true,
236
			'isIE' => false,
237
		], $actual->getParams());
238
		$this->assertEquals('main', $actual->getTemplateName());
239
	}
240
241
	/**
242
	 * @dataProvider indexFirstRunDetectionProvider
243
	 */
244
	public function testIndexFirstRunDetection($initialView, $expectedFirstRun, $expectsSetRequest) {
245
		$this->config->expects($this->at(0))
246
			->method('getSystemValue')
247
			->with('version')
248
			->will($this->returnValue('9.1.0.0'));
249
250
		$this->config->expects($this->at(1))
251
			->method('getSystemValue')
252
			->with('asset-pipeline.enabled', false)
253
			->will($this->returnValue(false));
254
255
		$this->request->expects($this->once())
256
			->method('isUserAgent')
257
			->with(['/(MSIE)|(Trident)/'])
258
			->will($this->returnValue(false));
259
260
		$this->userSession->expects($this->once())
261
			->method('getUser')
262
			->will($this->returnValue($this->dummyUser));
263
264
		$this->dummyUser->expects($this->once())
265
			->method('getUID')
266
			->will($this->returnValue('user123'));
267
268
		$this->dummyUser->expects($this->once())
269
			->method('getEMailAddress')
270
			->will($this->returnValue('[email protected]'));
271
272
		$this->config->expects($this->at(2))
273
			->method('getAppValue')
274
			->with($this->appName, 'installed_version')
275
			->will($this->returnValue('42.13.37'));
276
277
		$this->config->expects($this->at(3))
278
			->method('getUserValue')
279
			->with('user123', $this->appName, 'currentView', null)
280
			->will($this->returnValue($initialView));
281
282
		$this->config->expects($this->at(4))
283
			->method('getUserValue')
284
			->with('user123', $this->appName, 'skipPopover', 'no')
285
			->will($this->returnValue('someSkipPopoverValue'));
286
287
		$this->config->expects($this->at(5))
288
			->method('getUserValue')
289
			->with('user123', $this->appName, 'showWeekNr', 'no')
290
			->will($this->returnValue('someShowWeekNrValue'));
291
292
		$this->config->expects($this->at(6))
293
			->method('getUserValue')
294
			->with('user123', $this->appName, 'firstRun', null)
295
			->will($this->returnValue(null));
296
297
		$this->config->expects($this->at(7))
298
			->method('getAppValue')
299
			->with('theming', 'color', '#0082C9')
300
			->will($this->returnValue('#ff00ff'));
301
302
		if ($expectsSetRequest) {
303
			$this->config->expects($this->at(8))
304
				->method('setUserValue')
305
				->with('user123');
306
		}
307
308
		$actual = $this->controller->index();
309
310
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
311
		$this->assertEquals([
312
			'appVersion' => '42.13.37',
313
			'initialView' => $initialView ? 'someRandominitialView' : 'month',
314
			'emailAddress' => '[email protected]',
315
			'skipPopover' => 'someSkipPopoverValue',
316
			'weekNumbers' => 'someShowWeekNrValue',
317
			'firstRun' => $expectedFirstRun,
318
			'supportsClass' => true,
319
			'defaultColor' => '#ff00ff',
320
			'webCalWorkaround' => 'no',
321
			'isPublic' => false,
322
			'needsAutosize' => true,
323
			'isIE' => false,
324
		], $actual->getParams());
325
		$this->assertEquals('main', $actual->getTemplateName());
326
	}
327
328
	public function indexFirstRunDetectionProvider() {
329
		return [
330
			[null, 'yes', false],
331
			['someRandominitialView', 'no', true],
332
		];
333
	}
334
335
	/**
336
	 * @dataProvider indexPublicDataProvider
337
	 */
338
	public function testPublicIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass, $needsAutosize, $isIE) {
339
		$this->config->expects($this->at(0))
340
			->method('getSystemValue')
341
			->with('version')
342
			->will($this->returnValue($serverVersion));
343
344
		$this->config->expects($this->at(1))
345
			->method('getSystemValue')
346
			->with('asset-pipeline.enabled', false)
347
			->will($this->returnValue($isAssetPipelineEnabled));
348
349 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...
350
			$this->request->expects($this->once())
351
				->method('isUserAgent')
352
				->with(['/(MSIE)|(Trident)/'])
353
				->will($this->returnValue($isIE));
354
		}
355
356
		if ($showAssetPipelineError) {
357
			$actual = $this->controller->index();
358
359
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
360
			$this->assertEquals([], $actual->getParams());
361
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
362
		} else {
363
			$this->config->expects($this->once())
364
				->method('getAppValue')
365
				->with($this->appName, 'installed_version')
366
				->will($this->returnValue('42.13.37'));
367
368
			$actual = $this->controller->publicIndex();
369
370
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
371
			$this->assertEquals([
372
				'appVersion' => '42.13.37',
373
				'initialView' => 'month',
374
				'emailAddress' => '',
375
				'skipPopover' => 'no',
376
				'weekNumbers' => 'no',
377
				'supportsClass' => $expectsSupportsClass,
378
				'isPublic' => true,
379
				'shareURL' => '://',
380
				'previewImage' => null,
381
				'firstRun' => 'no',
382
				'webCalWorkaround' => 'no',
383
				'needsAutosize' => $needsAutosize,
384
				'isIE' => $isIE,
385
			], $actual->getParams());
386
			$this->assertEquals('main', $actual->getTemplateName());
387
		}
388
389
	}
390
391
	public function indexPublicDataProvider() {
392
		return [
393
			[true, true, '9.0.5.2', false, true, false],
394
			[true, false, '9.1.0.0', true, true, false],
395
			[false, false, '9.0.5.2', false, true, false],
396
			[false, false, '9.1.0.0', true, true, false],
397
			[false, false, '11.0.0', true, false, false],
398
			[false, false, '11.0.0', true, false, true],
399
		];
400
	}
401
}
402