Completed
Push — fix_untranslated_strings ( 07522f...8348da )
by
unknown
17:53
created

ViewControllerTest::indexDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 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) {
90
		$this->config->expects($this->once())
91
			->method('getSystemValue')
92
			->with('asset-pipeline.enabled', false)
93
			->will($this->returnValue($isAssetPipelineEnabled));
94
95
		if ($isAssetPipelineEnabled) {
96
			$actual = $this->controller->index();
97
98
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
99
			$this->assertEquals([], $actual->getParams());
100
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
101
		} else {
102
			$this->userSession->expects($this->once())
103
				->method('getUser')
104
				->will($this->returnValue($this->dummyUser));
105
106
			$this->dummyUser->expects($this->once())
107
				->method('getUID')
108
				->will($this->returnValue('user123'));
109
110
			$this->dummyUser->expects($this->once())
111
				->method('getEMailAddress')
112
				->will($this->returnValue('[email protected]'));
113
114
			$this->config->expects($this->once())
115
				->method('getAppValue')
116
				->with($this->appName, 'installed_version')
117
				->will($this->returnValue('42.13.37'));
118
119
			$this->config->expects($this->at(2))
120
				->method('getUserValue')
121
				->with('user123', $this->appName, 'currentView', 'month')
122
				->will($this->returnValue('someView'));
123
124
			$actual = $this->controller->index();
125
126
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
127
			$this->assertEquals([
128
				'appVersion' => '42.13.37',
129
				'defaultView' => 'someView',
130
				'emailAddress' => '[email protected]',
131
			], $actual->getParams());
132
			$this->assertEquals('main', $actual->getTemplateName());
133
		}
134
135
	}
136
137
	public function indexDataProvider() {
138
		return [
139
			[true],
140
			[false]
141
		];
142
	}
143
144
	public function testGetTimezone() {
145
		$actual = $this->controller->getTimezone('TIMEZONE1.ics');
146
147
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
148
		$this->assertEquals('TIMEZONE1-data', $actual->getData());
149
	}
150
151
	public function testGetTimezoneWithFakeTz() {
152
		$actual = $this->controller->getTimezone('TIMEZONE42.ics');
153
154
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
155
	}
156
157
	public function testGetTimezoneWithRegion() {
158
		$actual = $this->controller->getTimezoneWithRegion('REG', 'CIT.ics');
159
160
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
161
		$this->assertEquals('TIMEZONE DATA WITH REGION AND CITY', $actual->getData());
162
	}
163
164
	public function testGetTimezoneWithRegionWithFakeTz() {
165
		$actual = $this->controller->getTimezoneWithRegion('EUROPE', 'BERLIN.ics');
166
167
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
168
	}
169
}
170