Completed
Push — master ( da245d...ed79a4 )
by Georg
04:42
created

ViewControllerTest::indexEmailPublicLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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