Completed
Pull Request — master (#1)
by Thomas
05:10
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
function scandir($directory) {
25
	$dir = substr(__DIR__, 0, -strlen('tests/unit/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) {
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', 'month')
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('getAppValue')
161
				->with('theming', 'color', '#0082C9')
162
				->will($this->returnValue('#ff00ff'));
163
164
			$actual = $this->controller->index();
165
166
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
167
			$this->assertEquals([
168
				'appVersion' => '42.13.37',
169
				'defaultView' => 'someView',
170
				'emailAddress' => '[email protected]',
171
				'skipPopover' => 'someSkipPopoverValue',
172
				'weekNumbers' => 'someShowWeekNrValue',
173
				'supportsClass' => $expectsSupportsClass,
174
				'defaultColor' => '#ff00ff',
175
				'isPublic' => false
176
			], $actual->getParams());
177
			$this->assertEquals('main', $actual->getTemplateName());
178
		}
179
180
	}
181
182 View Code Duplication
	public function indexDataProvider() {
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...
183
		return [
184
			[true, true, '9.0.5.2', false],
185
			[true, false, '9.1.0.0', true],
186
			[false, false, '9.0.5.2', false],
187
			[false, false, '9.1.0.0', true]
188
		];
189
	}
190
191
	/**
192
	 * @dataProvider indexPublicDataProvider
193
	 */
194
	public function testPublicIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass) {
195
		$this->config->expects($this->at(0))
196
			->method('getSystemValue')
197
			->with('version')
198
			->will($this->returnValue($serverVersion));
199
200
		$this->config->expects($this->at(1))
201
			->method('getSystemValue')
202
			->with('asset-pipeline.enabled', false)
203
			->will($this->returnValue($isAssetPipelineEnabled));
204
205
		if ($showAssetPipelineError) {
206
			$actual = $this->controller->index();
207
208
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
209
			$this->assertEquals([], $actual->getParams());
210
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
211
		} else {
212
			$this->config->expects($this->once())
213
				->method('getAppValue')
214
				->with($this->appName, 'installed_version')
215
				->will($this->returnValue('42.13.37'));
216
217
			$actual = $this->controller->publicIndex();
218
219
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
220
			$this->assertEquals([
221
				'appVersion' => '42.13.37',
222
				'defaultView' => 'month',
223
				'emailAddress' => '',
224
				'supportsClass' => $expectsSupportsClass,
225
				'isPublic' => true,
226
				'shareURL' => '://',
227
				'previewImage' => null,
228
			], $actual->getParams());
229
			$this->assertEquals('main', $actual->getTemplateName());
230
		}
231
232
	}
233
234 View Code Duplication
	public function indexPublicDataProvider() {
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...
235
		return [
236
			[true, true, '9.0.5.2', false],
237
			[true, false, '9.1.0.0', true],
238
			[false, false, '9.0.5.2', false],
239
			[false, false, '9.1.0.0', true]
240
		];
241
	}
242
243
	public function testGetTimezone() {
244
		$actual = $this->controller->getTimezone('TIMEZONE1.ics');
245
246
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
247
		$this->assertEquals('TIMEZONE1-data', $actual->getData());
248
	}
249
250
	public function testGetTimezoneWithFakeTz() {
251
		$actual = $this->controller->getTimezone('TIMEZONE42.ics');
252
253
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
254
	}
255
256
	public function testGetTimezoneWithRegion() {
257
		$actual = $this->controller->getTimezoneWithRegion('REG', 'CIT.ics');
258
259
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
260
		$this->assertEquals('TIMEZONE DATA WITH REGION AND CITY', $actual->getData());
261
	}
262
263
	public function testGetTimezoneWithRegionWithFakeTz() {
264
		$actual = $this->controller->getTimezoneWithRegion('EUROPE', 'BERLIN.ics');
265
266
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
267
	}
268
269
	/**
270
	 * @dataProvider indexEmailPublicLink
271
	 */
272
	public function testEmailPublicLink($to, $url, $name) {
273
274
		$this->userSession->expects($this->exactly(1))
275
			->method('getUser')
276
			->will($this->returnValue($this->dummyUser));
277
278
		$actual = $this->controller->sendEmailPublicLink($to, $url, $name);
279
280
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
281
282
	}
283
284
	public function indexEmailPublicLink() {
285
		return [
286
			['[email protected]', 'myurl.tld', 'user123'],
287
			['testtesttld', 'myurl.tld', 'user123'],
288
		];
289
	}
290
}
291