Completed
Push — public-sharing ( 03ea7d...46eadf )
by Thomas
19:24
created

ViewControllerTest::testIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 42
c 3
b 0
f 0
nc 2
nop 4
dl 0
loc 54
rs 9.6716

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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