mantis-matrix-integration – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | <?php |
2 | |||
3 | namespace MatrixPhp\Tests; |
||
4 | |||
5 | use MatrixPhp\Exceptions\MatrixException; |
||
6 | use MatrixPhp\Exceptions\MatrixHttpLibException; |
||
7 | use MatrixPhp\Exceptions\ValidationException; |
||
8 | use GuzzleHttp\Client; |
||
9 | use GuzzleHttp\Psr7\Response; |
||
10 | use GuzzleHttp\Psr7\Request; |
||
11 | use MatrixPhp\MatrixHttpApi; |
||
12 | use phpDocumentor\Reflection\DocBlock\Tags\Param; |
||
13 | |||
14 | class MatrixHttpApiTest extends BaseTestCase { |
||
15 | protected $userId = "@alice:matrix.org"; |
||
16 | protected $roomId = '#foo:matrix.org'; |
||
17 | protected $token = "Dp0YKRXwx0iWDhFj7lg3DVjwsWzGcUIgARljgyAip2JD8qd5dSaWcxowTKEFetPulfLijAhv8eO" |
||
18 | . "mUSScyGcWgZyNMRTBmoJ0RFc0HotPvTBZU98yKRLtat7V43aCpFmK"; |
||
19 | protected $testPath = "/account/whoami"; |
||
20 | protected $deviceId = "QBUAZIFURK"; |
||
21 | protected $displayName = "test_name"; |
||
22 | protected $authBody = [ |
||
23 | "auth" => [ |
||
24 | "type" => "example.type.foo", |
||
25 | "session" => "xxxxx", |
||
26 | "example_credential" => "verypoorsharedsecret" |
||
27 | ] |
||
28 | ]; |
||
29 | protected $oneTimeKeys = ["curve25519:AAAAAQ" => "/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/Ve4vU8"]; |
||
30 | protected $deviceKeys = [ |
||
31 | "user_id" => "@alice:example.com", |
||
32 | "device_id" => "JLAFKJWSCS", |
||
33 | "algorithms" => [ |
||
34 | "m.olm.curve25519-aes-sha256", |
||
35 | "m.megolm.v1.aes-sha" |
||
36 | ], |
||
37 | "keys" => [ |
||
38 | "curve25519:JLAFKJWSCS" => "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI", |
||
39 | "ed25519:JLAFKJWSCS" => "lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI" |
||
40 | ], |
||
41 | "signatures" => [ |
||
42 | "@alice:example.com" => [ |
||
43 | "ed25519:JLAFKJWSCS" => ("dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2gi" |
||
44 | . "MIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA") |
||
45 | ] |
||
46 | ] |
||
47 | ]; |
||
48 | |||
49 | protected $mxurl = "mxc://example.com/OonjUOmcuVpUnmOWKtzPmAFe"; |
||
50 | |||
51 | protected MatrixHttpApi $api; |
||
52 | |||
53 | protected function setUp(): void |
||
54 | { |
||
55 | parent::setUp(); |
||
56 | $this->api = new MatrixHttpApi('http://example.com'); |
||
57 | } |
||
58 | |||
59 | /////////////////////////// |
||
60 | // class TestTagsApi |
||
61 | /////////////////////////// |
||
62 | public function testGetUserTags() { |
||
63 | $tagsUrl = "http://example.com/_matrix/client/r0/user/%40alice%3Amatrix.org/rooms/%23foo%3Amatrix.org/tags"; |
||
64 | $container = []; |
||
65 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
66 | $this->api->setClient(new Client(['handler' => $handler])); |
||
67 | $this->api->getUserTags($this->userId, $this->roomId); |
||
68 | /** @var Request $req */ |
||
69 | $req = array_get($container, '0.request'); |
||
70 | |||
71 | $this->assertEquals('GET', $req->getMethod()); |
||
72 | $this->assertEquals($tagsUrl, (string)$req->getUri()); |
||
73 | } |
||
74 | |||
75 | public function testAddUserTag() { |
||
76 | $tagsUrl = "http://example.com/_matrix/client/r0/user/%40alice%3Amatrix.org/rooms/%23foo%3Amatrix.org/tags/foo"; |
||
77 | $container = []; |
||
78 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
79 | $this->api->setClient(new Client(['handler' => $handler])); |
||
80 | $this->api->addUserTag($this->userId, $this->roomId, 'foo', null, ["order" => "5"]); |
||
81 | |||
82 | /** @var Request $req */ |
||
83 | $req = array_get($container, '0.request'); |
||
84 | |||
85 | $this->assertEquals('PUT', $req->getMethod()); |
||
86 | $this->assertEquals($tagsUrl, (string)$req->getUri()); |
||
87 | } |
||
88 | |||
89 | public function testRemoveUserTag() { |
||
90 | $tagsUrl = "http://example.com/_matrix/client/r0/user/%40alice%3Amatrix.org/rooms/%23foo%3Amatrix.org/tags/foo"; |
||
91 | $container = []; |
||
92 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
93 | $this->api->setClient(new Client(['handler' => $handler])); |
||
94 | $this->api->removeUserTag($this->userId, $this->roomId, 'foo'); |
||
95 | |||
96 | /** @var Request $req */ |
||
97 | $req = array_get($container, '0.request'); |
||
98 | |||
99 | $this->assertEquals('DELETE', $req->getMethod()); |
||
100 | $this->assertEquals($tagsUrl, (string)$req->getUri()); |
||
101 | } |
||
102 | |||
103 | /////////////////////////// |
||
104 | // class TestAccountDataApi |
||
105 | /////////////////////////// |
||
106 | public function testSetAccountData() { |
||
107 | $accountDataUrl = "http://example.com/_matrix/client/r0/user/%40alice%3Amatrix.org/account_data/foo"; |
||
108 | $container = []; |
||
109 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
110 | $this->api->setClient(new Client(['handler' => $handler])); |
||
111 | $this->api->setAccountData($this->userId, 'foo', ['bar' => 1]); |
||
112 | |||
113 | /** @var Request $req */ |
||
114 | $req = array_get($container, '0.request'); |
||
115 | |||
116 | $this->assertEquals('PUT', $req->getMethod()); |
||
117 | $this->assertEquals($accountDataUrl, (string)$req->getUri()); |
||
118 | } |
||
119 | |||
120 | public function testSetRoomAccountData() { |
||
121 | $accountDataUrl = 'http://example.com/_matrix/client/r0/user/%40alice%3Amatrix.org/'; |
||
122 | $accountDataUrl .= 'rooms/%23foo%3Amatrix.org/account_data/foo'; |
||
123 | $container = []; |
||
124 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
125 | $this->api->setClient(new Client(['handler' => $handler])); |
||
126 | $this->api->setRoomAccountData($this->userId, $this->roomId, 'foo', ['bar' => 1]); |
||
127 | |||
128 | /** @var Request $req */ |
||
129 | $req = array_get($container, '0.request'); |
||
130 | |||
131 | $this->assertEquals('PUT', $req->getMethod()); |
||
132 | $this->assertEquals($accountDataUrl, (string)$req->getUri()); |
||
133 | } |
||
134 | |||
135 | /////////////////////////// |
||
136 | // class TestAccountDataApi |
||
137 | /////////////////////////// |
||
138 | public function testUnban() { |
||
139 | $unbanUrl = 'http://example.com/_matrix/client/r0/rooms/%23foo%3Amatrix.org/unban'; |
||
140 | $container = []; |
||
141 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
142 | $this->api->setClient(new Client(['handler' => $handler])); |
||
143 | $this->api->unbanUser($this->roomId, $this->userId); |
||
144 | |||
145 | /** @var Request $req */ |
||
146 | $req = array_get($container, '0.request'); |
||
147 | |||
148 | $this->assertEquals('POST', $req->getMethod()); |
||
149 | $this->assertEquals($unbanUrl, (string)$req->getUri()); |
||
150 | } |
||
151 | |||
152 | /////////////////////////// |
||
153 | // class TestDeviceApi |
||
154 | /////////////////////////// |
||
155 | public function testGetDevices() { |
||
156 | $getDevicesUrl = "http://example.com/_matrix/client/r0/devices"; |
||
157 | $container = []; |
||
158 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
159 | $this->api->setClient(new Client(['handler' => $handler])); |
||
160 | $this->api->getDevices(); |
||
161 | |||
162 | /** @var Request $req */ |
||
163 | $req = array_get($container, '0.request'); |
||
164 | |||
165 | $this->assertEquals('GET', $req->getMethod()); |
||
166 | $this->assertEquals($getDevicesUrl, (string)$req->getUri()); |
||
167 | } |
||
168 | |||
169 | public function testGetDevice() { |
||
170 | $getDevicesUrl = "http://example.com/_matrix/client/r0/devices/" . $this->deviceId; |
||
171 | $container = []; |
||
172 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
173 | $this->api->setClient(new Client(['handler' => $handler])); |
||
174 | $this->api->getDevice($this->deviceId); |
||
175 | |||
176 | /** @var Request $req */ |
||
177 | $req = array_get($container, '0.request'); |
||
178 | |||
179 | $this->assertEquals('GET', $req->getMethod()); |
||
180 | $this->assertEquals($getDevicesUrl, (string)$req->getUri()); |
||
181 | } |
||
182 | |||
183 | public function testUpdateDeviceInfo() { |
||
184 | $getDevicesUrl = "http://example.com/_matrix/client/r0/devices/" . $this->deviceId; |
||
185 | $container = []; |
||
186 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
187 | $this->api->setClient(new Client(['handler' => $handler])); |
||
188 | $this->api->updateDeviceInfo($this->deviceId, $this->displayName); |
||
189 | |||
190 | /** @var Request $req */ |
||
191 | $req = array_get($container, '0.request'); |
||
192 | |||
193 | $this->assertEquals('PUT', $req->getMethod()); |
||
194 | $this->assertEquals($getDevicesUrl, (string)$req->getUri()); |
||
195 | } |
||
196 | |||
197 | public function testDeleteDevice() { |
||
198 | $getDevicesUrl = "http://example.com/_matrix/client/r0/devices/" . $this->deviceId; |
||
199 | $container = []; |
||
200 | // Test for 401 status code of User-Interactive Auth API |
||
201 | $handler = $this->getMockClientHandler([new Response(401, [], '{}')], $container); |
||
202 | $this->api->setClient(new Client(['handler' => $handler])); |
||
203 | $this->expectException(MatrixHttpLibException::class); |
||
204 | |||
205 | try { |
||
206 | $this->api->deleteDevice($this->authBody, $this->deviceId); |
||
207 | } catch (MatrixHttpLibException $e) { |
||
208 | /** @var Request $req */ |
||
209 | $req = array_get($container, '0.request'); |
||
210 | |||
211 | $this->assertEquals('DELETE', $req->getMethod()); |
||
212 | $this->assertEquals($getDevicesUrl, (string)$req->getUri()); |
||
213 | |||
214 | throw $e; |
||
215 | } |
||
216 | } |
||
217 | |||
218 | public function testDeleteDevices() { |
||
219 | $getDevicesUrl = "http://example.com/_matrix/client/r0/delete_devices/"; |
||
220 | $container = []; |
||
221 | // Test for 401 status code of User-Interactive Auth API |
||
222 | $handler = $this->getMockClientHandler([new Response(401, [], '{}')], $container); |
||
223 | $this->api->setClient(new Client(['handler' => $handler])); |
||
224 | $this->expectException(MatrixHttpLibException::class); |
||
225 | |||
226 | $this->api->deleteDevices($this->authBody, [$this->deviceId]); |
||
227 | |||
228 | /** @var Request $req */ |
||
229 | $req = array_get($container, '0.request'); |
||
230 | |||
231 | $this->assertEquals('POST', $req->getMethod()); |
||
232 | $this->assertEquals($getDevicesUrl, (string)$req->getUri()); |
||
233 | } |
||
234 | |||
235 | /////////////////////////// |
||
236 | // class TestKeysApi |
||
237 | /////////////////////////// |
||
238 | /** |
||
239 | * @param array $args |
||
240 | * |
||
241 | * @dataProvider uploadKeysProvider |
||
242 | */ |
||
243 | public function testUploadKeys(array $args) { |
||
244 | $uploadKeysUrl = 'http://example.com/_matrix/client/r0/keys/upload'; |
||
245 | $container = []; |
||
246 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
247 | $this->api->setClient(new Client(['handler' => $handler])); |
||
248 | $this->api->uploadKeys($args); |
||
249 | |||
250 | /** @var Request $req */ |
||
251 | $req = array_get($container, '0.request'); |
||
252 | |||
253 | $this->assertEquals('POST', $req->getMethod()); |
||
254 | $this->assertEquals($uploadKeysUrl, (string)$req->getUri()); |
||
255 | } |
||
256 | |||
257 | public function uploadKeysProvider(): array { |
||
258 | return [ |
||
259 | [[]], |
||
260 | [['device_keys' => $this->deviceKeys]], |
||
261 | [['one_time_keys' => $this->oneTimeKeys]], |
||
262 | ]; |
||
263 | } |
||
264 | |||
265 | public function testQueryKeys() { |
||
266 | $queryKeyUrl = 'http://example.com/_matrix/client/r0/keys/query'; |
||
267 | $container = []; |
||
268 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
269 | $this->api->setClient(new Client(['handler' => $handler])); |
||
270 | $this->api->queryKeys([$this->userId => $this->deviceId], 10); |
||
271 | |||
272 | /** @var Request $req */ |
||
273 | $req = array_get($container, '0.request'); |
||
274 | |||
275 | $this->assertEquals('POST', $req->getMethod()); |
||
276 | $this->assertEquals($queryKeyUrl, (string)$req->getUri()); |
||
277 | } |
||
278 | |||
279 | public function testClaimKeys() { |
||
280 | $claimKeysUrl = 'http://example.com/_matrix/client/r0/keys/claim'; |
||
281 | $container = []; |
||
282 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
283 | $this->api->setClient(new Client(['handler' => $handler])); |
||
284 | $keyRequest = [$this->userId => [$this->deviceId => 'algo']]; |
||
285 | $this->api->claimKeys($keyRequest, 1000); |
||
286 | |||
287 | /** @var Request $req */ |
||
288 | $req = array_get($container, '0.request'); |
||
289 | |||
290 | $this->assertEquals('POST', $req->getMethod()); |
||
291 | $this->assertEquals($claimKeysUrl, (string)$req->getUri()); |
||
292 | } |
||
293 | |||
294 | public function testKeyChange() { |
||
295 | $keyChangeUrl = 'http://example.com/_matrix/client/r0/keys/changes'; |
||
296 | $container = []; |
||
297 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
298 | $this->api->setClient(new Client(['handler' => $handler])); |
||
299 | $this->api->keyChanges('s72594_4483_1934', 's75689_5632_2435'); |
||
300 | |||
301 | /** @var Request $req */ |
||
302 | $req = array_get($container, '0.request'); |
||
303 | |||
304 | $this->assertEquals('GET', $req->getMethod()); |
||
305 | $this->assertEquals($keyChangeUrl, explode('?', (string)$req->getUri())[0]); |
||
306 | } |
||
307 | |||
308 | /////////////////////////// |
||
309 | // class TestSendToDeviceApi |
||
310 | /////////////////////////// |
||
311 | public function testSendToDevice() { |
||
312 | $txnId = $this->invokePrivateMethod($this->api, 'makeTxnId'); |
||
313 | $sendToDeviceUrl = "http://example.com/_matrix/client/r0/sendToDevice/m.new_device/" . $txnId; |
||
314 | $container = []; |
||
315 | $handler = $this->getMockClientHandler([new Response(200, [], '{}')], $container); |
||
316 | $this->api->setClient(new Client(['handler' => $handler])); |
||
317 | $payload = [$this->userId => [$this->deviceId => ['test' => 1]]]; |
||
318 | $this->api->sendToDevice('m.new_device', $payload, $txnId); |
||
319 | |||
320 | /** @var Request $req */ |
||
321 | $req = array_get($container, '0.request'); |
||
322 | |||
323 | $this->assertEquals('PUT', $req->getMethod()); |
||
324 | $this->assertEquals($sendToDeviceUrl, (string)$req->getUri()); |
||
325 | } |
||
326 | |||
327 | /////////////////////////// |
||
328 | // class TestMainApi |
||
329 | /////////////////////////// |
||
330 | public function testSendTokenHeader() { |
||
331 | $mapi = new MatrixHttpApi("http://example.com", $this->token); |
||
332 | |||
333 | $r = sprintf('{"application/json": {"user_id": "%s"}}', $this->userId); |
||
334 | $container = []; |
||
335 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
336 | $mapi->setClient(new Client(['handler' => $handler])); |
||
337 | |||
338 | $this->invokePrivateMethod($mapi, 'send', ['GET', $this->testPath]); |
||
339 | /** @var Request $req */ |
||
340 | $req = array_get($container, '0.request'); |
||
341 | |||
342 | $this->assertEquals('GET', $req->getMethod()); |
||
343 | $this->assertEquals(sprintf('Bearer %s', $this->token), $req->getHeader('Authorization')[0]); |
||
344 | } |
||
345 | |||
346 | public function testSendUserAgentHeader() { |
||
347 | $mapi = new MatrixHttpApi("http://example.com", $this->token); |
||
348 | |||
349 | $r = sprintf('{"application/json": {"user_id": "%s"}}', $this->userId); |
||
350 | $container = []; |
||
351 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
352 | $mapi->setClient(new Client(['handler' => $handler])); |
||
353 | |||
354 | $this->invokePrivateMethod($mapi, 'send', ['GET', $this->testPath]); |
||
355 | /** @var Request $req */ |
||
356 | $req = array_get($container, '0.request'); |
||
357 | |||
358 | $this->assertEquals('GET', $req->getMethod()); |
||
359 | $this->assertEquals('php-matrix-sdk/' . $mapi::VERSION, $req->getHeader('User-Agent')[0]); |
||
360 | } |
||
361 | |||
362 | public function testSendTokenQuery() { |
||
363 | $mapi = new MatrixHttpApi("http://example.com", $this->token, null, 500, false); |
||
364 | |||
365 | $r = sprintf('{"application/json": {"user_id": "%s"}}', $this->userId); |
||
366 | $container = []; |
||
367 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
368 | $mapi->setClient(new Client(['handler' => $handler])); |
||
369 | |||
370 | $this->invokePrivateMethod($mapi, 'send', ['GET', $this->testPath]); |
||
371 | /** @var Request $req */ |
||
372 | $req = array_get($container, '0.request'); |
||
373 | |||
374 | $this->assertEquals('GET', $req->getMethod()); |
||
375 | $this->assertStringContainsString($this->token, $req->getRequestTarget()); |
||
376 | } |
||
377 | |||
378 | public function testSendUserId() { |
||
379 | $mapi = new MatrixHttpApi("http://example.com", $this->token, $this->userId); |
||
380 | |||
381 | $r = sprintf('{"application/json": {"user_id": "%s"}}', $this->userId); |
||
382 | $container = []; |
||
383 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
384 | $mapi->setClient(new Client(['handler' => $handler])); |
||
385 | |||
386 | $this->invokePrivateMethod($mapi, 'send', ['GET', $this->testPath]); |
||
387 | /** @var Request $req */ |
||
388 | $req = array_get($container, '0.request'); |
||
389 | |||
390 | $this->assertEquals('GET', $req->getMethod()); |
||
391 | $this->assertStringContainsString(urlencode($this->userId), $req->getRequestTarget()); |
||
392 | } |
||
393 | |||
394 | public function testSendUnsupMethod() { |
||
395 | $this->expectException(MatrixException::class); |
||
396 | $mapi = new MatrixHttpApi("http://example.com", $this->token, $this->userId); |
||
397 | $this->invokePrivateMethod($mapi, 'send', ['GOT', $this->testPath]); |
||
398 | } |
||
399 | |||
400 | public function testSendRequestError() { |
||
401 | $this->expectException(MatrixHttpLibException::class); |
||
402 | $mapi = new MatrixHttpApi("http://example.com"); |
||
403 | $this->invokePrivateMethod($mapi, 'send', ['GET', $this->testPath]); |
||
404 | } |
||
405 | |||
406 | /////////////////////////// |
||
407 | // class TestMediaApi |
||
408 | /////////////////////////// |
||
409 | public function testMediaDownload() { |
||
410 | $dlUrl = "http://example.com/_matrix/media/r0/download/" . substr($this->mxurl, 6); |
||
411 | $container = []; |
||
412 | $response = new Response(200, [ |
||
413 | 'Content-Type' => 'application/php' |
||
414 | ], file_get_contents('./tests/TestHelper.php')); |
||
415 | $handler = $this->getMockClientHandler([$response], $container); |
||
416 | $this->api->setClient(new Client(['handler' => $handler])); |
||
417 | $this->api->mediaDownload($this->mxurl, false); |
||
418 | |||
419 | /** @var Request $req */ |
||
420 | $req = array_get($container, '0.request'); |
||
421 | |||
422 | $this->assertEquals('GET', $req->getMethod()); |
||
423 | $this->assertEquals($dlUrl, explode('?', (string)$req->getUri())[0]); |
||
424 | } |
||
425 | |||
426 | public function testMediaDownloadWrongUrl() { |
||
427 | $this->expectException(ValidationException::class); |
||
428 | |||
429 | $this->api->mediaDownload(substr($this->mxurl, 6)); |
||
430 | } |
||
431 | |||
432 | public function testGetThumbnail() { |
||
433 | $dlUrl = "http://example.com/_matrix/media/r0/thumbnail/" . substr($this->mxurl, 6); |
||
434 | $container = []; |
||
435 | $response = new Response(200, [ |
||
436 | 'Content-Type' => 'application/php' |
||
437 | ], file_get_contents('./tests/TestHelper.php')); |
||
438 | $handler = $this->getMockClientHandler([$response], $container); |
||
439 | $this->api->setClient(new Client(['handler' => $handler])); |
||
440 | $this->api->getThumbnail($this->mxurl, 28, 28, 'scale', false); |
||
441 | |||
442 | /** @var Request $req */ |
||
443 | $req = array_get($container, '0.request'); |
||
444 | |||
445 | $this->assertEquals('GET', $req->getMethod()); |
||
446 | $this->assertEquals($dlUrl, explode('?', (string)$req->getUri())[0]); |
||
447 | } |
||
448 | |||
449 | public function testThumbnailWrongUrl() { |
||
450 | $this->expectException(ValidationException::class); |
||
451 | |||
452 | $this->api->getThumbnail(substr($this->mxurl, 6), 28, 28); |
||
453 | } |
||
454 | |||
455 | public function testThumbnailWrongMethod() { |
||
456 | $this->expectException(ValidationException::class); |
||
457 | |||
458 | $this->api->getThumbnail($this->mxurl, 28, 28, 'cut', false); |
||
459 | } |
||
460 | |||
461 | public function testGetUrlPreview() { |
||
462 | $mediaUrl = "http://example.com/_matrix/media/r0/preview_url"; |
||
463 | $r = json_encode(TestHelper::EXAMPLE_PREVIEW_URL); |
||
464 | $container = []; |
||
465 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
466 | $this->api->setClient(new Client(['handler' => $handler])); |
||
467 | $this->api->getUrlPreview("https://google.com/", 1510610716656); |
||
468 | |||
469 | /** @var Request $req */ |
||
470 | $req = array_get($container, '0.request'); |
||
471 | |||
472 | $this->assertEquals('GET', $req->getMethod()); |
||
473 | $this->assertEquals($mediaUrl, explode('?', (string)$req->getUri())[0]); |
||
474 | } |
||
475 | |||
476 | |||
477 | /////////////////////////// |
||
478 | // class TestRoomApi |
||
479 | /////////////////////////// |
||
480 | /** |
||
481 | * @dataProvider createRoomVisibilityProvider |
||
482 | */ |
||
483 | public function testCreateRoomVisibility(bool $isPublic, string $visibility) { |
||
484 | $createRoomUrl = "http://example.com/_matrix/client/r0/createRoom"; |
||
485 | $container = []; |
||
486 | $r = '{"room_id": "!sefiuhWgwghwWgh:example.com"}'; |
||
487 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
488 | $this->api->setClient(new Client(['handler' => $handler])); |
||
489 | $this->api->createRoom("#test:example.com", 'test', $isPublic); |
||
490 | |||
491 | /** @var Request $req */ |
||
492 | $req = array_get($container, '0.request'); |
||
493 | |||
494 | $this->assertEquals('POST', $req->getMethod()); |
||
495 | $this->assertEquals($createRoomUrl, (string)$req->getUri()); |
||
496 | $body = json_decode($req->getBody()->getContents(), true); |
||
497 | $this->assertEquals("#test:example.com", $body['room_alias_name']); |
||
498 | $this->assertEquals($visibility, $body['visibility']); |
||
499 | $this->assertEquals('test', $body['name']); |
||
500 | } |
||
501 | |||
502 | public function createRoomVisibilityProvider(): array { |
||
503 | return [ |
||
504 | [true, 'public'], |
||
505 | [false, 'private'], |
||
506 | ]; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * @dataProvider createRoomFederationProvider |
||
511 | */ |
||
512 | public function testCreateRoomFederation(bool $isFederated) { |
||
513 | $createRoomUrl = "http://example.com/_matrix/client/r0/createRoom"; |
||
514 | $container = []; |
||
515 | $r = '{"room_id": "!sefiuhWgwghwWgh:example.com"}'; |
||
516 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
517 | $this->api->setClient(new Client(['handler' => $handler])); |
||
518 | |||
519 | $this->api->createRoom("#test:example.com", 'test', false, [], $isFederated); |
||
520 | /** @var Request $req */ |
||
521 | $req = array_get($container, '0.request'); |
||
522 | |||
523 | $this->assertEquals('POST', $req->getMethod()); |
||
524 | $this->assertEquals($createRoomUrl, (string)$req->getUri()); |
||
525 | $body = json_decode($req->getBody()->getContents(), true); |
||
526 | $this->assertEquals("#test:example.com", $body['room_alias_name']); |
||
527 | $this->assertEquals($isFederated, array_key_exists('m.federate', array_get($body, 'creation_content', []))); |
||
528 | } |
||
529 | |||
530 | |||
531 | public function createRoomFederationProvider(): array { |
||
532 | return [ |
||
533 | [true], |
||
534 | [false], |
||
535 | ]; |
||
536 | } |
||
537 | |||
538 | /////////////////////////// |
||
539 | // class TestRoomApi |
||
540 | /////////////////////////// |
||
541 | public function testWhoami() { |
||
542 | $mapi = new MatrixHttpApi("http://example.com", $this->token); |
||
543 | $whoamiUrl = "http://example.com/_matrix/client/r0/account/whoami"; |
||
544 | |||
545 | $r = sprintf('{"user_id": "%s"}', $this->userId); |
||
546 | $container = []; |
||
547 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
548 | $mapi->setClient(new Client(['handler' => $handler])); |
||
549 | |||
550 | $mapi->whoami(); |
||
551 | /** @var Request $req */ |
||
552 | $req = array_get($container, '0.request'); |
||
553 | |||
554 | $this->assertEquals('GET', $req->getMethod()); |
||
555 | $this->assertStringContainsString($req->getRequestTarget(), $whoamiUrl); |
||
556 | } |
||
557 | |||
558 | public function testWhoamiUnauth() { |
||
559 | $this->expectException(MatrixException::class); |
||
560 | |||
561 | $r = sprintf('{"user_id": "%s"}', $this->userId); |
||
562 | $container = []; |
||
563 | $handler = $this->getMockClientHandler([new Response(200, [], $r)], $container); |
||
564 | $this->api->setClient(new Client(['handler' => $handler])); |
||
565 | |||
566 | $this->api->whoami(); |
||
567 | } |
||
568 | |||
569 | |||
570 | } |