1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 Facebook, Inc. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
namespace Facebook\Tests; |
25
|
|
|
|
26
|
|
|
use Facebook\Facebook; |
27
|
|
|
use Facebook\FacebookClient; |
28
|
|
|
use Facebook\Http\GraphRawResponse; |
29
|
|
|
use Facebook\HttpClients\FacebookHttpClientInterface; |
30
|
|
|
use Facebook\PersistentData\PersistentDataInterface; |
31
|
|
|
use Facebook\Url\UrlDetectionInterface; |
32
|
|
|
use Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface; |
33
|
|
|
use Facebook\FacebookRequest; |
34
|
|
|
use Facebook\Authentication\AccessToken; |
35
|
|
|
use Facebook\GraphNodes\GraphEdge; |
36
|
|
|
use Facebook\Tests\FakeGraphApi\FakeGraphApiForResumableUpload; |
37
|
|
|
|
38
|
|
|
class FooClientInterface implements FacebookHttpClientInterface |
39
|
|
|
{ |
40
|
|
|
public function send($url, $method, $body, array $headers, $timeOut) |
41
|
|
|
{ |
42
|
|
|
return new GraphRawResponse( |
43
|
|
|
"HTTP/1.1 1337 OK\r\nDate: Mon, 19 May 2014 18:37:17 GMT", |
44
|
|
|
'{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
class FooPersistentDataInterface implements PersistentDataInterface |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
public function get($key) |
52
|
|
|
{ |
53
|
|
|
return 'foo'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function set($key, $value) |
57
|
|
|
{ |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
class FooUrlDetectionInterface implements UrlDetectionInterface |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
public function getCurrentUrl() |
64
|
|
|
{ |
65
|
|
|
return 'https://foo.bar'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
class FooBarPseudoRandomStringGenerator implements PseudoRandomStringGeneratorInterface |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
public function getPseudoRandomString($length) |
72
|
|
|
{ |
73
|
|
|
return 'csprs123'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
class FacebookTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
protected $config = [ |
80
|
|
|
'app_id' => '1337', |
81
|
|
|
'app_secret' => 'foo_secret', |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @expectedException \Facebook\Exceptions\FacebookSDKException |
86
|
|
|
*/ |
87
|
|
|
public function testInstantiatingWithoutAppIdThrows() |
88
|
|
|
{ |
89
|
|
|
// unset value so there is no fallback to test expected Exception |
90
|
|
|
putenv(Facebook::APP_ID_ENV_NAME.'='); |
91
|
|
|
$config = [ |
92
|
|
|
'app_secret' => 'foo_secret', |
93
|
|
|
]; |
94
|
|
|
new Facebook($config); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @expectedException \Facebook\Exceptions\FacebookSDKException |
99
|
|
|
*/ |
100
|
|
|
public function testInstantiatingWithoutAppSecretThrows() |
101
|
|
|
{ |
102
|
|
|
// unset value so there is no fallback to test expected Exception |
103
|
|
|
putenv(Facebook::APP_SECRET_ENV_NAME.'='); |
104
|
|
|
$config = [ |
105
|
|
|
'app_id' => 'foo_id', |
106
|
|
|
]; |
107
|
|
|
new Facebook($config); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @expectedException \InvalidArgumentException |
112
|
|
|
*/ |
113
|
|
|
public function testSettingAnInvalidHttpClientHandlerThrows() |
114
|
|
|
{ |
115
|
|
|
$config = array_merge($this->config, [ |
116
|
|
|
'http_client_handler' => 'foo_handler', |
117
|
|
|
]); |
118
|
|
|
new Facebook($config); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testCurlHttpClientHandlerCanBeForced() |
122
|
|
|
{ |
123
|
|
|
if (!extension_loaded('curl')) { |
124
|
|
|
$this->markTestSkipped('cURL must be installed to test cURL client handler.'); |
125
|
|
|
} |
126
|
|
|
$config = array_merge($this->config, [ |
127
|
|
|
'http_client_handler' => 'curl' |
128
|
|
|
]); |
129
|
|
|
$fb = new Facebook($config); |
130
|
|
|
$this->assertInstanceOf( |
131
|
|
|
'Facebook\HttpClients\FacebookCurlHttpClient', |
132
|
|
|
$fb->getClient()->getHttpClientHandler() |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function testStreamHttpClientHandlerCanBeForced() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$config = array_merge($this->config, [ |
139
|
|
|
'http_client_handler' => 'stream' |
140
|
|
|
]); |
141
|
|
|
$fb = new Facebook($config); |
142
|
|
|
$this->assertInstanceOf( |
143
|
|
|
'Facebook\HttpClients\FacebookStreamHttpClient', |
144
|
|
|
$fb->getClient()->getHttpClientHandler() |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function testGuzzleHttpClientHandlerCanBeForced() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$config = array_merge($this->config, [ |
151
|
|
|
'http_client_handler' => 'guzzle' |
152
|
|
|
]); |
153
|
|
|
$fb = new Facebook($config); |
154
|
|
|
$this->assertInstanceOf( |
155
|
|
|
'Facebook\HttpClients\FacebookGuzzleHttpClient', |
156
|
|
|
$fb->getClient()->getHttpClientHandler() |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @expectedException \InvalidArgumentException |
162
|
|
|
*/ |
163
|
|
|
public function testSettingAnInvalidPersistentDataHandlerThrows() |
164
|
|
|
{ |
165
|
|
|
$config = array_merge($this->config, [ |
166
|
|
|
'persistent_data_handler' => 'foo_handler', |
167
|
|
|
]); |
168
|
|
|
new Facebook($config); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
View Code Duplication |
public function testPersistentDataHandlerCanBeForced() |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$config = array_merge($this->config, [ |
174
|
|
|
'persistent_data_handler' => 'memory' |
175
|
|
|
]); |
176
|
|
|
$fb = new Facebook($config); |
177
|
|
|
$this->assertInstanceOf( |
178
|
|
|
'Facebook\PersistentData\FacebookMemoryPersistentDataHandler', |
179
|
|
|
$fb->getRedirectLoginHelper()->getPersistentDataHandler() |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testSettingAnInvalidUrlHandlerThrows() |
184
|
|
|
{ |
185
|
|
|
$expectedException = (PHP_MAJOR_VERSION > 5 && class_exists('TypeError')) |
186
|
|
|
? 'TypeError' |
187
|
|
|
: 'PHPUnit_Framework_Error'; |
188
|
|
|
|
189
|
|
|
$this->setExpectedException($expectedException); |
190
|
|
|
|
191
|
|
|
$config = array_merge($this->config, [ |
192
|
|
|
'url_detection_handler' => 'foo_handler', |
193
|
|
|
]); |
194
|
|
|
new Facebook($config); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testTheUrlHandlerWillDefaultToTheFacebookImplementation() |
198
|
|
|
{ |
199
|
|
|
$fb = new Facebook($this->config); |
200
|
|
|
$this->assertInstanceOf('Facebook\Url\FacebookUrlDetectionHandler', $fb->getUrlDetectionHandler()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
View Code Duplication |
public function testAnAccessTokenCanBeSetAsAString() |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$fb = new Facebook($this->config); |
206
|
|
|
$fb->setDefaultAccessToken('foo_token'); |
207
|
|
|
$accessToken = $fb->getDefaultAccessToken(); |
208
|
|
|
|
209
|
|
|
$this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken); |
210
|
|
|
$this->assertEquals('foo_token', (string)$accessToken); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
View Code Duplication |
public function testAnAccessTokenCanBeSetAsAnAccessTokenEntity() |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$fb = new Facebook($this->config); |
216
|
|
|
$fb->setDefaultAccessToken(new AccessToken('bar_token')); |
217
|
|
|
$accessToken = $fb->getDefaultAccessToken(); |
218
|
|
|
|
219
|
|
|
$this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken); |
220
|
|
|
$this->assertEquals('bar_token', (string)$accessToken); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @expectedException \InvalidArgumentException |
225
|
|
|
*/ |
226
|
|
|
public function testSettingAnInvalidPseudoRandomStringGeneratorThrows() |
227
|
|
|
{ |
228
|
|
|
$config = array_merge($this->config, [ |
229
|
|
|
'pseudo_random_string_generator' => 'foo_generator', |
230
|
|
|
]); |
231
|
|
|
new Facebook($config); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
public function testMcryptCsprgCanBeForced() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
if (!function_exists('mcrypt_create_iv')) { |
237
|
|
|
$this->markTestSkipped( |
238
|
|
|
'Mcrypt must be installed to test mcrypt_create_iv().' |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$config = array_merge($this->config, [ |
243
|
|
|
'persistent_data_handler' => 'memory', // To keep session errors from happening |
244
|
|
|
'pseudo_random_string_generator' => 'mcrypt' |
245
|
|
|
]); |
246
|
|
|
$fb = new Facebook($config); |
247
|
|
|
$this->assertInstanceOf( |
248
|
|
|
'Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator', |
249
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
View Code Duplication |
public function testOpenSslCsprgCanBeForced() |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
if (!function_exists('openssl_random_pseudo_bytes')) { |
256
|
|
|
$this->markTestSkipped( |
257
|
|
|
'The OpenSSL extension must be enabled to test openssl_random_pseudo_bytes().' |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$config = array_merge($this->config, [ |
262
|
|
|
'persistent_data_handler' => 'memory', // To keep session errors from happening |
263
|
|
|
'pseudo_random_string_generator' => 'openssl' |
264
|
|
|
]); |
265
|
|
|
$fb = new Facebook($config); |
266
|
|
|
$this->assertInstanceOf( |
267
|
|
|
'Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator', |
268
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
View Code Duplication |
public function testUrandomCsprgCanBeForced() |
|
|
|
|
273
|
|
|
{ |
274
|
|
|
if (ini_get('open_basedir')) { |
275
|
|
|
$this->markTestSkipped( |
276
|
|
|
'Cannot test /dev/urandom generator due to open_basedir constraint.' |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
if (!is_readable('/dev/urandom')) { |
281
|
|
|
$this->markTestSkipped( |
282
|
|
|
'/dev/urandom not found or is not readable.' |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$config = array_merge($this->config, [ |
287
|
|
|
'persistent_data_handler' => 'memory', // To keep session errors from happening |
288
|
|
|
'pseudo_random_string_generator' => 'urandom' |
289
|
|
|
]); |
290
|
|
|
$fb = new Facebook($config); |
291
|
|
|
$this->assertInstanceOf( |
292
|
|
|
'Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator', |
293
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @expectedException \InvalidArgumentException |
299
|
|
|
*/ |
300
|
|
|
public function testSettingAnAccessThatIsNotStringOrAccessTokenThrows() |
301
|
|
|
{ |
302
|
|
|
$config = array_merge($this->config, [ |
303
|
|
|
'default_access_token' => 123, |
304
|
|
|
]); |
305
|
|
|
new Facebook($config); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testCreatingANewRequestWillDefaultToTheProperConfig() |
309
|
|
|
{ |
310
|
|
|
$config = array_merge($this->config, [ |
311
|
|
|
'default_access_token' => 'foo_token', |
312
|
|
|
'enable_beta_mode' => true, |
313
|
|
|
'default_graph_version' => 'v1337', |
314
|
|
|
]); |
315
|
|
|
$fb = new Facebook($config); |
316
|
|
|
|
317
|
|
|
$request = $fb->request('FOO_VERB', '/foo'); |
318
|
|
|
$this->assertEquals('1337', $request->getApp()->getId()); |
319
|
|
|
$this->assertEquals('foo_secret', $request->getApp()->getSecret()); |
320
|
|
|
$this->assertEquals('foo_token', (string)$request->getAccessToken()); |
321
|
|
|
$this->assertEquals('v1337', $request->getGraphVersion()); |
322
|
|
|
$this->assertEquals( |
323
|
|
|
FacebookClient::BASE_GRAPH_URL_BETA, |
324
|
|
|
$fb->getClient()->getBaseGraphUrl() |
325
|
|
|
); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function testCanInjectCustomHandlers() |
329
|
|
|
{ |
330
|
|
|
$config = array_merge($this->config, [ |
331
|
|
|
'http_client_handler' => new FooClientInterface(), |
332
|
|
|
'persistent_data_handler' => new FooPersistentDataInterface(), |
333
|
|
|
'url_detection_handler' => new FooUrlDetectionInterface(), |
334
|
|
|
'pseudo_random_string_generator' => new FooBarPseudoRandomStringGenerator(), |
335
|
|
|
]); |
336
|
|
|
$fb = new Facebook($config); |
337
|
|
|
|
338
|
|
|
$this->assertInstanceOf( |
339
|
|
|
'Facebook\Tests\FooClientInterface', |
340
|
|
|
$fb->getClient()->getHttpClientHandler() |
341
|
|
|
); |
342
|
|
|
$this->assertInstanceOf( |
343
|
|
|
'Facebook\Tests\FooPersistentDataInterface', |
344
|
|
|
$fb->getRedirectLoginHelper()->getPersistentDataHandler() |
345
|
|
|
); |
346
|
|
|
$this->assertInstanceOf( |
347
|
|
|
'Facebook\Tests\FooUrlDetectionInterface', |
348
|
|
|
$fb->getRedirectLoginHelper()->getUrlDetectionHandler() |
349
|
|
|
); |
350
|
|
|
$this->assertInstanceOf( |
351
|
|
|
'Facebook\Tests\FooBarPseudoRandomStringGenerator', |
352
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
public function testPaginationReturnsProperResponse() |
357
|
|
|
{ |
358
|
|
|
$config = array_merge($this->config, [ |
359
|
|
|
'http_client_handler' => new FooClientInterface(), |
360
|
|
|
]); |
361
|
|
|
$fb = new Facebook($config); |
362
|
|
|
|
363
|
|
|
$request = new FacebookRequest($fb->getApp(), 'foo_token', 'GET'); |
364
|
|
|
$graphEdge = new GraphEdge( |
365
|
|
|
$request, |
366
|
|
|
[], |
367
|
|
|
[ |
368
|
|
|
'paging' => [ |
369
|
|
|
'cursors' => [ |
370
|
|
|
'after' => 'bar_after_cursor', |
371
|
|
|
'before' => 'bar_before_cursor', |
372
|
|
|
], |
373
|
|
|
'previous' => 'previous_url', |
374
|
|
|
'next' => 'next_url', |
375
|
|
|
] |
376
|
|
|
], |
377
|
|
|
'/1337/photos', |
378
|
|
|
'\Facebook\GraphNodes\GraphUser' |
379
|
|
|
); |
380
|
|
|
|
381
|
|
|
$nextPage = $fb->next($graphEdge); |
382
|
|
|
$this->assertInstanceOf('Facebook\GraphNodes\GraphEdge', $nextPage); |
383
|
|
|
$this->assertInstanceOf('Facebook\GraphNodes\GraphUser', $nextPage[0]); |
384
|
|
|
$this->assertEquals('Foo', $nextPage[0]['name']); |
385
|
|
|
|
386
|
|
|
$lastResponse = $fb->getLastResponse(); |
387
|
|
|
$this->assertInstanceOf('Facebook\FacebookResponse', $lastResponse); |
388
|
|
|
$this->assertEquals(1337, $lastResponse->getHttpStatusCode()); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function testCanGetSuccessfulTransferWithMaxTries() |
392
|
|
|
{ |
393
|
|
|
$config = array_merge($this->config, [ |
394
|
|
|
'http_client_handler' => new FakeGraphApiForResumableUpload(), |
395
|
|
|
]); |
396
|
|
|
$fb = new Facebook($config); |
397
|
|
|
$response = $fb->uploadVideo('me', __DIR__.'/foo.txt', [], 'foo-token', 3); |
398
|
|
|
$this->assertEquals([ |
399
|
|
|
'video_id' => '1337', |
400
|
|
|
'success' => true, |
401
|
|
|
], $response); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @expectedException \Facebook\Exceptions\FacebookResponseException |
406
|
|
|
*/ |
407
|
|
|
public function testMaxingOutRetriesWillThrow() |
408
|
|
|
{ |
409
|
|
|
$client = new FakeGraphApiForResumableUpload(); |
410
|
|
|
$client->failOnTransfer(); |
411
|
|
|
|
412
|
|
|
$config = array_merge($this->config, [ |
413
|
|
|
'http_client_handler' => $client, |
414
|
|
|
]); |
415
|
|
|
$fb = new Facebook($config); |
416
|
|
|
$fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.