Completed
Pull Request — master (#605)
by Thomas
10:38
created

ViewControllerTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 1
Metric Value
cc 1
eloc 25
c 2
b 2
f 1
nc 1
nop 0
dl 0
loc 31
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/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
65
	private $dummyUser;
66
67
	private $controller;
68
69
	public function setUp() {
70
		$this->appName = 'calendar';
71
		$this->request = $this->getMockBuilder('\OCP\IRequest')
72
			->disableOriginalConstructor()
73
			->getMock();
74
		$this->config = $this->getMockBuilder('\OCP\IConfig')
75
			->disableOriginalConstructor()
76
			->getMock();
77
		$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
78
			->disableOriginalConstructor()
79
			->getMock();
80
81
		$this->dummyUser = $this->getMockBuilder('OCP\IUser')
82
			->disableOriginalConstructor()
83
			->getMock();
84
85
		$this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
86
			->disableOriginalConstructor()
87
			->getMock();
88
89
		$this->l10n = $this->getMockBuilder('OC\L10N\L10N')
90
			->disableOriginalConstructor()
91
			->getMock();
92
93
		$this->defaults = $this->getMockBuilder('OCP\Defaults')
94
			->disableOriginalConstructor()
95
			->getMock();
96
97
		$this->controller = new ViewController($this->appName, $this->request,
98
			$this->userSession, $this->config, $this->mailer, $this->l10n, $this->defaults);
99
	}
100
101
	/**
102
	 * @dataProvider indexDataProvider
103
	 */
104
	public function testIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass) {
105
		$this->config->expects($this->at(0))
106
			->method('getSystemValue')
107
			->with('version')
108
			->will($this->returnValue($serverVersion));
109
110
		$this->config->expects($this->at(1))
111
			->method('getSystemValue')
112
			->with('asset-pipeline.enabled', false)
113
			->will($this->returnValue($isAssetPipelineEnabled));
114
115
		if ($showAssetPipelineError) {
116
			$actual = $this->controller->index();
117
118
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
119
			$this->assertEquals([], $actual->getParams());
120
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
121
		} else {
122
			$this->userSession->expects($this->once())
123
				->method('getUser')
124
				->will($this->returnValue($this->dummyUser));
125
126
			$this->dummyUser->expects($this->once())
127
				->method('getUID')
128
				->will($this->returnValue('user123'));
129
130
			$this->dummyUser->expects($this->once())
131
				->method('getEMailAddress')
132
				->will($this->returnValue('[email protected]'));
133
134
			$this->config->expects($this->at(2))
135
				->method('getAppValue')
136
				->with($this->appName, 'installed_version')
137
				->will($this->returnValue('42.13.37'));
138
139
			$this->config->expects($this->at(3))
140
				->method('getUserValue')
141
				->with('user123', $this->appName, 'currentView', 'month')
142
				->will($this->returnValue('someView'));
143
144
			$this->config->expects($this->at(4))
145
				->method('getUserValue')
146
				->with('user123', $this->appName, 'skipPopover', 'no')
147
				->will($this->returnValue('someSkipPopoverValue'));
148
149
			$this->config->expects($this->at(5))
150
				->method('getUserValue')
151
				->with('user123', $this->appName, 'showWeekNr', 'no')
152
				->will($this->returnValue('someShowWeekNrValue'));
153
154
			$this->config->expects($this->at(6))
155
				->method('getAppValue')
156
				->with('theming', 'color', '#0082C9')
157
				->will($this->returnValue('#ff00ff'));
158
159
			$actual = $this->controller->index();
160
161
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
162
			$this->assertEquals([
163
				'appVersion' => '42.13.37',
164
				'defaultView' => 'someView',
165
				'emailAddress' => '[email protected]',
166
				'skipPopover' => 'someSkipPopoverValue',
167
				'weekNumbers' => 'someShowWeekNrValue',
168
				'supportsClass' => $expectsSupportsClass,
169
				'defaultColor' => '#ff00ff',
170
				'isPublic' => false
171
			], $actual->getParams());
172
			$this->assertEquals('main', $actual->getTemplateName());
173
		}
174
175
	}
176
177 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...
178
		return [
179
			[true, true, '9.0.5.2', false],
180
			[true, false, '9.1.0.0', true],
181
			[false, false, '9.0.5.2', false],
182
			[false, false, '9.1.0.0', true]
183
		];
184
	}
185
186
	/**
187
	 * @dataProvider indexPublicDataProvider
188
	 */
189
	public function testPublicIndex($isAssetPipelineEnabled, $showAssetPipelineError, $serverVersion, $expectsSupportsClass) {
190
		$this->config->expects($this->at(0))
191
			->method('getSystemValue')
192
			->with('version')
193
			->will($this->returnValue($serverVersion));
194
195
		$this->config->expects($this->at(1))
196
			->method('getSystemValue')
197
			->with('asset-pipeline.enabled', false)
198
			->will($this->returnValue($isAssetPipelineEnabled));
199
200
		if ($showAssetPipelineError) {
201
			$actual = $this->controller->index();
202
203
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
204
			$this->assertEquals([], $actual->getParams());
205
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
206
		} else {
207
			$this->config->expects($this->once())
208
				->method('getAppValue')
209
				->with($this->appName, 'installed_version')
210
				->will($this->returnValue('42.13.37'));
211
212
			$actual = $this->controller->publicIndex();
213
214
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
215
			$this->assertEquals([
216
				'appVersion' => '42.13.37',
217
				'defaultView' => 'month',
218
				'emailAddress' => '',
219
				'supportsClass' => $expectsSupportsClass,
220
				'isPublic' => true
221
			], $actual->getParams());
222
			$this->assertEquals('main', $actual->getTemplateName());
223
		}
224
225
	}
226
227 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...
228
		return [
229
			[true, true, '9.0.5.2', false],
230
			[true, false, '9.1.0.0', true],
231
			[false, false, '9.0.5.2', false],
232
			[false, false, '9.1.0.0', true]
233
		];
234
	}
235
236
	public function testGetTimezone() {
237
		$actual = $this->controller->getTimezone('TIMEZONE1.ics');
238
239
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
240
		$this->assertEquals('TIMEZONE1-data', $actual->getData());
241
	}
242
243
	public function testGetTimezoneWithFakeTz() {
244
		$actual = $this->controller->getTimezone('TIMEZONE42.ics');
245
246
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
247
	}
248
249
	public function testGetTimezoneWithRegion() {
250
		$actual = $this->controller->getTimezoneWithRegion('REG', 'CIT.ics');
251
252
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
253
		$this->assertEquals('TIMEZONE DATA WITH REGION AND CITY', $actual->getData());
254
	}
255
256
	public function testGetTimezoneWithRegionWithFakeTz() {
257
		$actual = $this->controller->getTimezoneWithRegion('EUROPE', 'BERLIN.ics');
258
259
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
260
	}
261
262
	/**
263
	 * @dataProvider indexEmailPublicLink
264
	 */
265
	public function testEmailPublicLink($to, $url, $name) {
266
267
		$this->userSession->expects($this->exactly(1))
268
			->method('getUser')
269
			->will($this->returnValue($this->dummyUser));
270
271
		$actual = $this->controller->sendEmailPublicLink($to, $url, $name);
272
273
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
274
275
	}
276
277
	public function indexEmailPublicLink() {
278
		return [
279
			['[email protected]', 'myurl.tld', 'user123'],
280
			['testtesttld', 'myurl.tld', 'user123'],
281
		];
282
	}
283
}
284