Completed
Push — public-sharing ( 745a2a...392572 )
by Thomas
20:56
created

ViewControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 19
Ratio 100 %

Importance

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