Completed
Pull Request — master (#605)
by Thomas
05:11
created

ViewControllerTest::testPublicIndex()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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