Completed
Pull Request — master (#115)
by Georg
37:42 queued 12:47
created

viewcontrollerTest.php ➔ file_get_contents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
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
62
	private $dummyUser;
63
64
	private $controller;
65
66
	public function setUp() {
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, $expectsWebcalWorkaround) {
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->at(2))
120
				->method('getAppValue')
121
				->with($this->appName, 'installed_version')
122
				->will($this->returnValue('42.13.37'));
123
124
			$this->config->expects($this->at(3))
125
				->method('getUserValue')
126
				->with('user123', $this->appName, 'currentView', 'month')
127
				->will($this->returnValue('someView'));
128
129
			$this->config->expects($this->at(4))
130
				->method('getUserValue')
131
				->with('user123', $this->appName, 'skipPopover', 'no')
132
				->will($this->returnValue('someSkipPopoverValue'));
133
134
			$this->config->expects($this->at(5))
135
				->method('getUserValue')
136
				->with('user123', $this->appName, 'showWeekNr', 'no')
137
				->will($this->returnValue('someShowWeekNrValue'));
138
139
			$this->config->expects($this->at(6))
140
				->method('getAppValue')
141
				->with('theming', 'color', '#0082C9')
142
				->will($this->returnValue('#ff00ff'));
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
				'weekNumbers' => 'someShowWeekNrValue',
153
				'supportsClass' => $expectsSupportsClass,
154
				'defaultColor' => '#ff00ff',
155
				'webCalWorkaround' => $expectsWebcalWorkaround
156
			], $actual->getParams());
157
			$this->assertEquals('main', $actual->getTemplateName());
158
		}
159
160
	}
161
162
	public function indexDataProvider() {
163
		return [
164
			[true, true, '9.0.5.2', false, 'yes'],
165
			[true, false, '9.1.0.0', true, 'no'],
166
			[false, false, '9.0.5.2', false, 'yes'],
167
			[false, false, '9.1.0.0', true, 'no']
168
		];
169
	}
170
171
	public function testGetTimezone() {
172
		$actual = $this->controller->getTimezone('TIMEZONE1.ics');
173
174
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
175
		$this->assertEquals('TIMEZONE1-data', $actual->getData());
176
	}
177
178
	public function testGetTimezoneWithFakeTz() {
179
		$actual = $this->controller->getTimezone('TIMEZONE42.ics');
180
181
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
182
	}
183
184
	public function testGetTimezoneWithRegion() {
185
		$actual = $this->controller->getTimezoneWithRegion('REG', 'CIT.ics');
186
187
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
188
		$this->assertEquals('TIMEZONE DATA WITH REGION AND CITY', $actual->getData());
189
	}
190
191
	public function testGetTimezoneWithRegionWithFakeTz() {
192
		$actual = $this->controller->getTimezoneWithRegion('EUROPE', 'BERLIN.ics');
193
194
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
195
	}
196
}
197