1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be |
4
|
|
|
* This file is licensed under the AGPL version 3 or later. |
5
|
|
|
* See the COPYING file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace OCA\Chat\OCH\Commands; |
9
|
|
|
|
10
|
|
|
include_once(__DIR__ . '/../../../autoloader.php'); |
11
|
|
|
include_once(__DIR__ . '/../../../vendor/Pimple/Pimple.php'); |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use OCA\Chat\Core\API; |
15
|
|
|
use OCA\Chat\OCH\Commands\SendChatMsg; |
16
|
|
|
use OCA\Chat\App\Chat; |
17
|
|
|
use OCA\Chat\Db\DBException; |
18
|
|
|
use OCA\Chat\OCH\Exceptions\RequestDataInvalid; |
19
|
|
|
use OCA\Chat\OCH\Db\UserOnline; |
20
|
|
|
use OCA\Chat\OCH\Db\User; |
21
|
|
|
use OCA\Chat\OCH\Db\PushMessage; |
22
|
|
|
|
23
|
|
|
// DONE |
24
|
|
|
class SendChatMsgTest extends \PHPUnit_Framework_TestCase { |
25
|
|
|
|
26
|
|
|
public static $command; |
27
|
|
|
public static $pushMessageCount; |
28
|
|
|
|
29
|
|
|
public function setUp(){ |
30
|
|
|
$app = new Chat(); |
|
|
|
|
31
|
|
|
$this->container = $app->getContainer(); |
|
|
|
|
32
|
|
|
$this->container['API'] = $this->getMockBuilder('\OCA\Chat\Core\API') |
33
|
|
|
->disableOriginalConstructor() |
34
|
|
|
->getMock(); |
35
|
|
|
$this->container['API']->expects($this->any()) |
36
|
|
|
->method('log') |
37
|
|
|
->will($this->returnValue(null)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testOmmittedConvId(){ |
41
|
|
|
$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'CONV-ID-MUST-BE-PROVIDED'); |
42
|
|
|
|
43
|
|
|
$this->container['API']->expects($this->any()) |
44
|
|
|
->method('prepareQuery') |
45
|
|
|
->will($this->returnValue(true)); |
46
|
|
|
|
47
|
|
|
// logic |
48
|
|
|
$sendChatMsg = new SendChatMsg($this->container); |
|
|
|
|
49
|
|
|
$sendChatMsg->setRequestData(array( |
50
|
|
|
'user' => array ( |
51
|
|
|
'id' => 'admin', |
52
|
|
|
'online' => false, |
53
|
|
|
'displayname' => 'admin', |
54
|
|
|
'backends' => array ( |
55
|
|
|
'och' => array ( |
56
|
|
|
'id' => NULL, |
57
|
|
|
'displayname' => 'ownCloud Handle', |
58
|
|
|
'protocol' => 'x-owncloud-handle', |
59
|
|
|
'namespace' => 'och', |
60
|
|
|
'value' => 'admin', |
61
|
|
|
), |
62
|
|
|
), |
63
|
|
|
'address_book_id' => 'admin', |
64
|
|
|
'address_book_backend' => 'localusers', |
65
|
|
|
), |
66
|
|
|
'session_id' => 'c08809598b01894c468873fab54291aa', |
67
|
|
|
'timestamp' => 1397328934.658, |
68
|
|
|
'chat_msg' => "TestMsg", |
69
|
|
|
//'conv_id' => 'addeimnpr', |
|
|
|
|
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testOmmittedChatMsg(){ |
74
|
|
|
$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'CHAT-MSG-MUST-BE-PROVIDED'); |
75
|
|
|
|
76
|
|
|
$this->container['API']->expects($this->any()) |
77
|
|
|
->method('prepareQuery') |
78
|
|
|
->will($this->returnValue(true)); |
79
|
|
|
|
80
|
|
|
// logic |
81
|
|
|
$sendChatMsg = new SendChatMsg($this->container); |
|
|
|
|
82
|
|
|
$sendChatMsg->setRequestData(array( |
83
|
|
|
'user' => array ( |
84
|
|
|
'id' => 'admin', |
85
|
|
|
'online' => false, |
86
|
|
|
'displayname' => 'admin', |
87
|
|
|
'backends' => array ( |
88
|
|
|
'och' => array ( |
89
|
|
|
'id' => NULL, |
90
|
|
|
'displayname' => 'ownCloud Handle', |
91
|
|
|
'protocol' => 'x-owncloud-handle', |
92
|
|
|
'namespace' => 'och', |
93
|
|
|
'value' => 'admin', |
94
|
|
|
), |
95
|
|
|
), |
96
|
|
|
'address_book_id' => 'admin', |
97
|
|
|
'address_book_backend' => 'localusers', |
98
|
|
|
), |
99
|
|
|
'session_id' => 'c08809598b01894c468873fab54291aa', |
100
|
|
|
'timestamp' => 1397328934.658, |
101
|
|
|
'conv_id' => 'addeimnpr', |
102
|
|
|
//'chat_msg' => '' |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testOmmittedTimestamp(){ |
107
|
|
|
$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'TIMESTAMP-MUST-BE-PROVIDED'); |
108
|
|
|
|
109
|
|
|
$this->container['API']->expects($this->any()) |
110
|
|
|
->method('prepareQuery') |
111
|
|
|
->will($this->returnValue(true)); |
112
|
|
|
|
113
|
|
|
// logic |
114
|
|
|
$sendChatMsg = new SendChatMsg($this->container); |
|
|
|
|
115
|
|
|
$sendChatMsg->setRequestData(array( |
116
|
|
|
'user' => array ( |
117
|
|
|
'id' => 'admin', |
118
|
|
|
'online' => false, |
119
|
|
|
'displayname' => 'admin', |
120
|
|
|
'backends' => array ( |
121
|
|
|
'och' => array ( |
122
|
|
|
'id' => NULL, |
123
|
|
|
'displayname' => 'ownCloud Handle', |
124
|
|
|
'protocol' => 'x-owncloud-handle', |
125
|
|
|
'namespace' => 'och', |
126
|
|
|
'value' => 'admin', |
127
|
|
|
), |
128
|
|
|
), |
129
|
|
|
'address_book_id' => 'admin', |
130
|
|
|
'address_book_backend' => 'localusers', |
131
|
|
|
), |
132
|
|
|
'session_id' => 'c08809598b01894c468873fab54291aa', |
133
|
|
|
//'timestamp' => 1397328934.658, |
|
|
|
|
134
|
|
|
'conv_id' => 'addeimnpr', |
135
|
|
|
'chat_msg' => 'test' |
136
|
|
|
)); |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Test if the created command in sendchatmsg.php is okay and is only created for recevier !== sender |
141
|
|
|
public function testCreatedCommand(){ |
142
|
|
|
|
143
|
|
|
$this->container['PushMessageMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\PushMessageMapper') |
144
|
|
|
->disableOriginalConstructor() |
145
|
|
|
->getMock(); |
146
|
|
|
|
147
|
|
|
$this->container['PushMessageMapper']->expects($this->any()) |
148
|
|
|
->method('insert') |
149
|
|
|
->will($this->returnCallback(function($pushMessage){ |
150
|
|
|
SendChatMsgTest::$command = $pushMessage->getCommand(); |
151
|
|
|
SendChatMsgTest::$pushMessageCount++; |
152
|
|
|
})); |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
$user1 = new User(); // This is a receiver |
156
|
|
|
$user1->setConversationId('addeimnpr'); |
157
|
|
|
$user1->setUser('derp'); |
158
|
|
|
$user1->setSessionId('c08809598b01894c4asdfasdf68873fab54291aa'); |
159
|
|
|
|
160
|
|
|
$user2 = new User(); // This is a sender but is also stored in the DB -> no need to create a pushmessage |
161
|
|
|
$user2->setConversationId('addeimnpr'); |
162
|
|
|
$user2->setUser('admin'); |
163
|
|
|
$user2->setSessionId('c08809598b01894c468873fab54291aa'); |
164
|
|
|
|
165
|
|
|
$this->container['UserMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserMapper') |
166
|
|
|
->disableOriginalConstructor() |
167
|
|
|
->getMock(); |
168
|
|
|
|
169
|
|
|
$this->container['UserMapper']->expects($this->any()) |
170
|
|
|
->method('findSessionsByConversation') |
171
|
|
|
->will($this->returnValue(array($user1, $user2))); |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
$this->container['messageMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\MessageMapper') |
175
|
|
|
->disableOriginalConstructor() |
176
|
|
|
->getMock(); |
177
|
|
|
|
178
|
|
|
$sendChatMsg = new SendChatMsg($this->container); |
|
|
|
|
179
|
|
|
$sendChatMsg->setRequestData(array( |
180
|
|
|
'user' => array ( |
181
|
|
|
'id' => 'admin', |
182
|
|
|
'online' => false, |
183
|
|
|
'displayname' => 'admin', |
184
|
|
|
'backends' => array ( |
185
|
|
|
'och' => array ( |
186
|
|
|
'id' => NULL, |
187
|
|
|
'displayname' => 'ownCloud Handle', |
188
|
|
|
'protocol' => 'x-owncloud-handle', |
189
|
|
|
'namespace' => 'och', |
190
|
|
|
'value' => 'admin', |
191
|
|
|
), |
192
|
|
|
), |
193
|
|
|
'address_book_id' => 'admin', |
194
|
|
|
'address_book_backend' => 'localusers', |
195
|
|
|
), |
196
|
|
|
'session_id' => 'c08809598b01894c468873fab54291aa', |
197
|
|
|
'timestamp' => 1397328934.658, |
198
|
|
|
'conv_id' => 'addeimnpr', |
199
|
|
|
'chat_msg' => 'test' |
200
|
|
|
)); |
201
|
|
|
$sendChatMsg->execute(); |
202
|
|
|
|
203
|
|
|
$expectedCommand = json_encode(array( |
204
|
|
|
'type' => 'send_chat_msg', |
205
|
|
|
'data' => array( |
206
|
|
|
'user' => array ( |
207
|
|
|
'id' => 'admin', |
208
|
|
|
'online' => false, |
209
|
|
|
'displayname' => 'admin', |
210
|
|
|
'backends' => array ( |
211
|
|
|
'och' => array ( |
212
|
|
|
'id' => NULL, |
213
|
|
|
'displayname' => 'ownCloud Handle', |
214
|
|
|
'protocol' => 'x-owncloud-handle', |
215
|
|
|
'namespace' => 'och', |
216
|
|
|
'value' => 'admin', |
217
|
|
|
), |
218
|
|
|
), |
219
|
|
|
'address_book_id' => 'admin', |
220
|
|
|
'address_book_backend' => 'localusers', |
221
|
|
|
), |
222
|
|
|
'conv_id' => 'addeimnpr', |
223
|
|
|
'timestamp' => 1397328934.658, |
224
|
|
|
'chat_msg' => 'test' |
225
|
|
|
) |
226
|
|
|
)); |
227
|
|
|
|
228
|
|
|
$this->assertEquals($expectedCommand, SendChatMsgTest::$command); |
229
|
|
|
$this->assertEquals(1, SendChatMsgTest::$pushMessageCount); |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.