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
|
|
|
class SettingsControllerTest extends \PHPUnit_Framework_TestCase { |
25
|
|
|
|
26
|
|
|
private $appName; |
27
|
|
|
private $request; |
28
|
|
|
private $userSession; |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
private $dummyUser; |
32
|
|
|
|
33
|
|
|
private $controller; |
34
|
|
|
|
35
|
|
|
public function setUp() { |
36
|
|
|
$this->appName = 'calendar'; |
37
|
|
|
$this->request = $this->getMockBuilder('\OCP\IRequest') |
38
|
|
|
->disableOriginalConstructor() |
39
|
|
|
->getMock(); |
40
|
|
|
$this->userSession = $this->getMockBuilder('OCP\IUserSession') |
41
|
|
|
->disableOriginalConstructor() |
42
|
|
|
->getMock(); |
43
|
|
|
$this->config = $this->getMockBuilder('OCP\IConfig') |
44
|
|
|
->disableOriginalConstructor() |
45
|
|
|
->getMock(); |
46
|
|
|
|
47
|
|
|
$this->dummyUser = $this->getMockBuilder('OCP\IUser') |
48
|
|
|
->disableOriginalConstructor() |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
$this->userSession->expects($this->once()) |
52
|
|
|
->method('getUser') |
53
|
|
|
->will($this->returnValue($this->dummyUser)); |
54
|
|
|
|
55
|
|
|
$this->dummyUser->expects($this->once()) |
56
|
|
|
->method('getUID') |
57
|
|
|
->will($this->returnValue('user123')); |
58
|
|
|
|
59
|
|
|
$this->controller = new SettingsController($this->appName, |
60
|
|
|
$this->request, $this->userSession, $this->config); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @dataProvider setViewWithAllowedViewDataProvider |
65
|
|
|
*/ |
66
|
|
|
public function testSetViewWithAllowedView($view) { |
67
|
|
|
$this->config->expects($this->once()) |
68
|
|
|
->method('setUserValue') |
69
|
|
|
->with('user123', $this->appName, 'currentView', $view); |
70
|
|
|
|
71
|
|
|
$actual = $this->controller->setConfig('view', $view); |
72
|
|
|
|
73
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
74
|
|
|
$this->assertEquals([], $actual->getData()); |
75
|
|
|
$this->assertEquals(200, $actual->getStatus()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function setViewWithAllowedViewDataProvider() { |
79
|
|
|
return [ |
80
|
|
|
['agendaDay'], |
81
|
|
|
['agendaWeek'], |
82
|
|
|
['month'], |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testSetViewWithForbiddenView() { |
|
|
|
|
87
|
|
|
$actual = $this->controller->setConfig('view','someForbiddenView'); |
88
|
|
|
|
89
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
90
|
|
|
$this->assertEquals([], $actual->getData()); |
91
|
|
|
$this->assertEquals(422, $actual->getStatus()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testSetViewWithConfigException() { |
|
|
|
|
95
|
|
|
$this->config->expects($this->once()) |
96
|
|
|
->method('setUserValue') |
97
|
|
|
->with('user123', $this->appName, 'currentView', 'month') |
98
|
|
|
->will($this->throwException(new \Exception)); |
99
|
|
|
|
100
|
|
|
$actual = $this->controller->setConfig('view', 'month'); |
101
|
|
|
|
102
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
103
|
|
|
$this->assertEquals([], $actual->getData()); |
104
|
|
|
$this->assertEquals(500, $actual->getStatus()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function testGetView() { |
|
|
|
|
108
|
|
|
$this->config->expects($this->once()) |
109
|
|
|
->method('getUserValue') |
110
|
|
|
->with('user123', $this->appName, 'currentView', 'month') |
111
|
|
|
->will($this->returnValue('agendaWeek')); |
112
|
|
|
|
113
|
|
|
$actual = $this->controller->getConfig('view'); |
114
|
|
|
|
115
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
116
|
|
|
$this->assertEquals(['value' => 'agendaWeek'], $actual->getData()); |
117
|
|
|
$this->assertEquals(200, $actual->getStatus()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function testGetViewWithConfigException() { |
|
|
|
|
121
|
|
|
$this->config->expects($this->once()) |
122
|
|
|
->method('getUserValue') |
123
|
|
|
->with('user123', $this->appName, 'currentView', 'month') |
124
|
|
|
->will($this->throwException(new \Exception)); |
125
|
|
|
|
126
|
|
|
$actual = $this->controller->getConfig('view'); |
127
|
|
|
|
128
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
129
|
|
|
$this->assertEquals([], $actual->getData()); |
130
|
|
|
$this->assertEquals(500, $actual->getStatus()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @dataProvider setPopoverWithAllowedValueDataProvider |
135
|
|
|
*/ |
136
|
|
|
public function testSetPopoverWithAllowedValue($value) { |
137
|
|
|
$this->config->expects($this->once()) |
138
|
|
|
->method('setUserValue') |
139
|
|
|
->with('user123', $this->appName, 'skipPopover', $value); |
140
|
|
|
|
141
|
|
|
$actual = $this->controller->setConfig('skipPopover', $value); |
142
|
|
|
|
143
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
144
|
|
|
$this->assertEquals([], $actual->getData()); |
145
|
|
|
$this->assertEquals(200, $actual->getStatus()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setPopoverWithAllowedValueDataProvider() { |
149
|
|
|
return [ |
150
|
|
|
['yes'], |
151
|
|
|
['no'] |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function testSetPopoverWithForbiddenValue() { |
|
|
|
|
156
|
|
|
$actual = $this->controller->setConfig('skipPopover','someForbiddenValue'); |
157
|
|
|
|
158
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
159
|
|
|
$this->assertEquals([], $actual->getData()); |
160
|
|
|
$this->assertEquals(422, $actual->getStatus()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
public function testSetPopoverWithConfigException() { |
|
|
|
|
164
|
|
|
$this->config->expects($this->once()) |
165
|
|
|
->method('setUserValue') |
166
|
|
|
->with('user123', $this->appName, 'skipPopover', 'no') |
167
|
|
|
->will($this->throwException(new \Exception)); |
168
|
|
|
|
169
|
|
|
$actual = $this->controller->setConfig('skipPopover', 'no'); |
170
|
|
|
|
171
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
172
|
|
|
$this->assertEquals([], $actual->getData()); |
173
|
|
|
$this->assertEquals(500, $actual->getStatus()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
public function testGetPopover() { |
|
|
|
|
177
|
|
|
$this->config->expects($this->once()) |
178
|
|
|
->method('getUserValue') |
179
|
|
|
->with('user123', $this->appName, 'skipPopover', 'no') |
180
|
|
|
->will($this->returnValue('agendaWeek')); |
181
|
|
|
|
182
|
|
|
$actual = $this->controller->getConfig('skipPopover'); |
183
|
|
|
|
184
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
185
|
|
|
$this->assertEquals(['value' => 'agendaWeek'], $actual->getData()); |
186
|
|
|
$this->assertEquals(200, $actual->getStatus()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
View Code Duplication |
public function testGetPopoverWithConfigException() { |
|
|
|
|
190
|
|
|
$this->config->expects($this->once()) |
191
|
|
|
->method('getUserValue') |
192
|
|
|
->with('user123', $this->appName, 'skipPopover', 'no') |
193
|
|
|
->will($this->throwException(new \Exception)); |
194
|
|
|
|
195
|
|
|
$actual = $this->controller->getConfig('skipPopover'); |
196
|
|
|
|
197
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
198
|
|
|
$this->assertEquals([], $actual->getData()); |
199
|
|
|
$this->assertEquals(500, $actual->getStatus()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
View Code Duplication |
public function testSetFirstRun() { |
|
|
|
|
203
|
|
|
$this->config->expects($this->once()) |
204
|
|
|
->method('setUserValue') |
205
|
|
|
->with('user123', $this->appName, 'firstRun', 'no'); |
206
|
|
|
|
207
|
|
|
$actual = $this->controller->setConfig('firstRun', 'some_random_ignored_value'); |
208
|
|
|
|
209
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
210
|
|
|
$this->assertEquals([], $actual->getData()); |
211
|
|
|
$this->assertEquals(200, $actual->getStatus()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
View Code Duplication |
public function testSetFirstRunWithException() { |
|
|
|
|
215
|
|
|
$this->config->expects($this->once()) |
216
|
|
|
->method('setUserValue') |
217
|
|
|
->with('user123', $this->appName, 'firstRun', 'no') |
218
|
|
|
->will($this->throwException(new \Exception)); |
219
|
|
|
|
220
|
|
|
$actual = $this->controller->setConfig('firstRun', 'some_random_ignored_value'); |
221
|
|
|
|
222
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
223
|
|
|
$this->assertEquals([], $actual->getData()); |
224
|
|
|
$this->assertEquals(500, $actual->getStatus()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
View Code Duplication |
public function testGetFirstRun() { |
|
|
|
|
228
|
|
|
$this->config->expects($this->once()) |
229
|
|
|
->method('getUserValue') |
230
|
|
|
->with('user123', $this->appName, 'firstRun', 'yes') |
231
|
|
|
->will($this->returnValue('no')); |
232
|
|
|
|
233
|
|
|
$actual = $this->controller->getConfig('firstRun'); |
234
|
|
|
|
235
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
236
|
|
|
$this->assertEquals(['value' => 'no'], $actual->getData()); |
237
|
|
|
$this->assertEquals(200, $actual->getStatus()); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
View Code Duplication |
public function testGetFirstRunWithException() { |
|
|
|
|
241
|
|
|
$this->config->expects($this->once()) |
242
|
|
|
->method('getUserValue') |
243
|
|
|
->with('user123', $this->appName, 'firstRun', 'yes') |
244
|
|
|
->will($this->throwException(new \Exception)); |
245
|
|
|
|
246
|
|
|
$actual = $this->controller->getConfig('firstRun'); |
247
|
|
|
|
248
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
249
|
|
|
$this->assertEquals([], $actual->getData()); |
250
|
|
|
$this->assertEquals(500, $actual->getStatus()); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
View Code Duplication |
public function testGetNotExistingConfig() { |
|
|
|
|
254
|
|
|
$actual = $this->controller->getConfig('foo'); |
255
|
|
|
|
256
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
257
|
|
|
$this->assertEquals([], $actual->getData()); |
258
|
|
|
$this->assertEquals(400, $actual->getStatus()); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
public function testSetNotExistingConfig() { |
|
|
|
|
262
|
|
|
$actual = $this->controller->setConfig('foo', 'bar'); |
263
|
|
|
|
264
|
|
|
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); |
265
|
|
|
$this->assertEquals([], $actual->getData()); |
266
|
|
|
$this->assertEquals(400, $actual->getStatus()); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
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.