|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OCA\Chat\Controller; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use \OCA\Chat\Utility\ControllerTestUtility; |
|
7
|
|
|
use \OCA\Chat\App\Chat; |
|
8
|
|
|
use \OCP\IRequest; |
|
9
|
|
|
use OCA\Chat\OCH\OCH; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
function time(){ |
|
13
|
|
|
return '2324'; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
class AppControllerTest extends ControllerTestUtility { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $appName; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \OCP\Irequest |
|
25
|
|
|
*/ |
|
26
|
|
|
private $request; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \OCA\Chat\Controller\AppController |
|
30
|
|
|
*/ |
|
31
|
|
|
private $controller; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \OCA\Chat\App\Chat |
|
35
|
|
|
*/ |
|
36
|
|
|
private $chat; |
|
37
|
|
|
|
|
38
|
|
|
public function setUp(){ |
|
39
|
|
|
$this->appName = 'chat'; |
|
40
|
|
|
$this->request = $this->getMockBuilder('\OCP\IRequest') |
|
41
|
|
|
->disableOriginalConstructor() |
|
42
|
|
|
->getMock(); |
|
43
|
|
|
|
|
44
|
|
|
$this->userOnlineMapper = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserOnlineMapper') |
|
|
|
|
|
|
45
|
|
|
->disableOriginalConstructor() |
|
46
|
|
|
->getMock(); |
|
47
|
|
|
|
|
48
|
|
|
$this->och = $this->getMockBuilder('\OCA\Chat\OCH\OCH') |
|
|
|
|
|
|
49
|
|
|
->disableOriginalConstructor() |
|
50
|
|
|
->getMock(); |
|
51
|
|
|
|
|
52
|
|
|
$this->syncOnline = $this->getMockBuilder('\OCA\Chat\OCH\Commands\SyncOnline') |
|
|
|
|
|
|
53
|
|
|
->disableOriginalConstructor() |
|
54
|
|
|
->getMock(); |
|
55
|
|
|
|
|
56
|
|
|
$this->contactsManager = $this->getMockBuilder('\OCP\Contacts\IManager') |
|
|
|
|
|
|
57
|
|
|
->disableOriginalConstructor() |
|
58
|
|
|
->getMock(); |
|
59
|
|
|
|
|
60
|
|
|
$this->backendManager = $this->getMockBuilder('\OCA\Chat\IBackendManager') |
|
|
|
|
|
|
61
|
|
|
->disableOriginalConstructor() |
|
62
|
|
|
->getMock(); |
|
63
|
|
|
|
|
64
|
|
|
$this->user = $this->getMockBuilder('\OCP\IUser') |
|
|
|
|
|
|
65
|
|
|
->disableOriginalConstructor() |
|
66
|
|
|
->getMock(); |
|
67
|
|
|
|
|
68
|
|
|
$this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder') |
|
|
|
|
|
|
69
|
|
|
->disableOriginalConstructor() |
|
70
|
|
|
->getMock(); |
|
71
|
|
|
|
|
72
|
|
|
$this->greet = $this->getMockBuilder('\OCA\Chat\OCH\Commands\Greet') |
|
|
|
|
|
|
73
|
|
|
->disableOriginalConstructor() |
|
74
|
|
|
->getMock(); |
|
75
|
|
|
|
|
76
|
|
|
$this->greet = $this->getMockBuilder('\OCP\IConfig') |
|
77
|
|
|
->disableOriginalConstructor() |
|
78
|
|
|
->getMock(); |
|
79
|
|
|
|
|
80
|
|
|
$this->chat = new Chat( |
|
81
|
|
|
$this->backendManager, |
|
82
|
|
|
$this->userOnlineMapper, |
|
83
|
|
|
$this->syncOnline, |
|
84
|
|
|
$this->user, |
|
85
|
|
|
$this->contactsManager, |
|
86
|
|
|
$this->rootFolder |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$this->controller = new AppController( |
|
90
|
|
|
$this->appName, |
|
91
|
|
|
$this->request, |
|
92
|
|
|
$this->chat, |
|
93
|
|
|
$this->contactsManager, |
|
94
|
|
|
$this->config, |
|
|
|
|
|
|
95
|
|
|
$this->greet |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
public function testIndexAnnotations(){ |
|
101
|
|
|
$expectedAnnotations = array('NoAdminRequired', 'NoCSRFRequired'); |
|
102
|
|
|
$this->assertAnnotations($this->controller, 'index', $expectedAnnotations); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testContactsAnnotations(){ |
|
106
|
|
|
$expectedAnnotations = array('NoAdminRequired'); |
|
107
|
|
|
$this->assertAnnotations($this->controller, 'index', $expectedAnnotations); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// |
|
111
|
|
|
public function indexProvider(){ |
|
112
|
|
|
return array( |
|
113
|
|
|
array( |
|
114
|
|
|
array( |
|
115
|
|
|
'id' => 'admin', |
|
116
|
|
|
'displayname' => 'admin', |
|
117
|
|
|
'backends' => array ( |
|
118
|
|
|
'email' => array ( |
|
119
|
|
|
'id' => NULL, |
|
120
|
|
|
'displayname' => 'E-mail', |
|
121
|
|
|
'protocol' => 'email', |
|
122
|
|
|
'namespace' => ' email', |
|
123
|
|
|
'value' => array ( |
|
124
|
|
|
0 => array ( |
|
125
|
|
|
), |
|
126
|
|
|
), |
|
127
|
|
|
), |
|
128
|
|
|
'och' => array ( |
|
129
|
|
|
'id' => NULL, |
|
130
|
|
|
'displayname' => 'ownCloud Handle', |
|
131
|
|
|
'protocol' => 'x-owncloud-handle', |
|
132
|
|
|
'namespace' => 'och', |
|
133
|
|
|
'value' => 'admin', |
|
134
|
|
|
), |
|
135
|
|
|
), |
|
136
|
|
|
'address_book_id' => 'local', |
|
137
|
|
|
'address_book_backend' => '', |
|
138
|
|
|
), |
|
139
|
|
|
array( |
|
140
|
|
|
"timestamp" => time(), |
|
141
|
|
|
"user" => array( |
|
142
|
|
|
'id' => 'admin', |
|
143
|
|
|
'displayname' => 'admin', |
|
144
|
|
|
'backends' => array ( |
|
145
|
|
|
'email' => array ( |
|
146
|
|
|
'id' => NULL, |
|
147
|
|
|
'displayname' => 'E-mail', |
|
148
|
|
|
'protocol' => 'email', |
|
149
|
|
|
'namespace' => ' email', |
|
150
|
|
|
'value' => array ( |
|
151
|
|
|
0 => array ( |
|
152
|
|
|
), |
|
153
|
|
|
), |
|
154
|
|
|
), |
|
155
|
|
|
'och' => array ( |
|
156
|
|
|
'id' => NULL, |
|
157
|
|
|
'displayname' => 'ownCloud Handle', |
|
158
|
|
|
'protocol' => 'x-owncloud-handle', |
|
159
|
|
|
'namespace' => 'och', |
|
160
|
|
|
'value' => 'admin', |
|
161
|
|
|
), |
|
162
|
|
|
), |
|
163
|
|
|
'address_book_id' => 'local', |
|
164
|
|
|
'address_book_backend' => '', |
|
165
|
|
|
), |
|
166
|
|
|
), |
|
167
|
|
|
array( |
|
168
|
|
|
"contacts" => "contacts", // dummy data |
|
169
|
|
|
"contactsList" => array("contact1", "contact2"), |
|
170
|
|
|
"contactsObj" => array("contacts"), |
|
171
|
|
|
), |
|
172
|
|
|
array( |
|
173
|
|
|
"backend1", |
|
174
|
|
|
"backend2" |
|
175
|
|
|
), |
|
176
|
|
|
array( |
|
177
|
|
|
"initconv1", |
|
178
|
|
|
"initconv2" |
|
179
|
|
|
), |
|
180
|
|
|
array( |
|
181
|
|
|
"session_id" => md5(time()) |
|
182
|
|
|
) |
|
183
|
|
|
) |
|
184
|
|
|
); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @dataProvider indexProvider |
|
189
|
|
|
*/ |
|
190
|
|
|
public function testIndex($currentUser, $greetRequestData, $contacts, $backends, $initConvs, $sessionId){ |
|
191
|
|
|
$this->chat->expects($this->once()) |
|
|
|
|
|
|
192
|
|
|
->method('getCurrentUser') |
|
193
|
|
|
->will($this->returnValue($currentUser)); |
|
194
|
|
|
|
|
195
|
|
|
$this->chat->expects($this->once()) |
|
|
|
|
|
|
196
|
|
|
->method('getContacts') |
|
197
|
|
|
->will($this->returnValue($contacts)); |
|
198
|
|
|
|
|
199
|
|
|
$this->chat->expects($this->once()) |
|
|
|
|
|
|
200
|
|
|
->method('getBackends') |
|
201
|
|
|
->will($this->returnValue($backends)); |
|
202
|
|
|
|
|
203
|
|
|
$this->chat->expects($this->once()) |
|
|
|
|
|
|
204
|
|
|
->method('getInitConvs') |
|
205
|
|
|
->will($this->returnValue($initConvs)); |
|
206
|
|
|
|
|
207
|
|
|
$this->greet->expects($this->once()) |
|
208
|
|
|
->method('setRequestData') |
|
209
|
|
|
->with($greetRequestData) |
|
210
|
|
|
->will($this->returnValue(true)); |
|
211
|
|
|
|
|
212
|
|
|
$this->greet->expects($this->once()) |
|
213
|
|
|
->method('execute') |
|
214
|
|
|
->will($this->returnValue($sessionId)); |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
$expectedParams = array( |
|
218
|
|
|
"initvar" => json_encode(array( |
|
219
|
|
|
"contacts" => $contacts['contacts'], |
|
220
|
|
|
"contactsList" => $contacts['contactsList'], |
|
221
|
|
|
"contactsObj" => $contacts['contactsObj'], |
|
222
|
|
|
"backends" => $backends, |
|
223
|
|
|
"initConvs" => $initConvs, |
|
224
|
|
|
"sessionId" => $sessionId['session_id'], |
|
225
|
|
|
)) |
|
226
|
|
|
); |
|
227
|
|
|
|
|
228
|
|
|
$response = $this->controller->index(); |
|
229
|
|
|
$this->assertEquals('main', $response->getTemplateName()); |
|
230
|
|
|
$this->assertEquals($expectedParams, $response->getParams()); |
|
231
|
|
|
} |
|
232
|
|
|
// |
|
233
|
|
|
// |
|
234
|
|
|
public function contactsProvider(){ |
|
235
|
|
|
return array( |
|
236
|
|
|
array( |
|
237
|
|
|
array( |
|
238
|
|
|
"contacts" => "contacts", // dummy data |
|
239
|
|
|
"contactsList" => array("contact1", "contact2"), |
|
240
|
|
|
"contactsObj" => array("contacts"), |
|
241
|
|
|
), |
|
242
|
|
|
) |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @dataProvider contactsProvider |
|
248
|
|
|
*/ |
|
249
|
|
|
public function testContacts($contacts){ |
|
250
|
|
|
$this->chat->expects($this->once()) |
|
|
|
|
|
|
251
|
|
|
->method('getContacts') |
|
252
|
|
|
->will($this->returnValue($contacts)); |
|
253
|
|
|
|
|
254
|
|
|
$expectedData = array( |
|
255
|
|
|
"contacts" => $contacts['contacts'], |
|
256
|
|
|
"contactsList" => $contacts['contactsList'], |
|
257
|
|
|
"contactsObj" => $contacts['contactsObj'] |
|
258
|
|
|
); |
|
259
|
|
|
|
|
260
|
|
|
$response = $this->controller->contacts(); |
|
261
|
|
|
$this->assertEquals('OCP\AppFramework\Http\JSONResponse', get_class($response)); // make sure a JSON response is sent |
|
262
|
|
|
$this->assertEquals($expectedData, $response->getData()); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: