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