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\FacebookRequest; |
29
|
|
|
use Facebook\Authentication\AccessToken; |
30
|
|
|
use Facebook\GraphNodes\GraphEdge; |
31
|
|
|
use Facebook\Tests\Fixtures\FakeGraphApiForResumableUpload; |
32
|
|
|
use Facebook\Tests\Fixtures\FooBarPseudoRandomStringGenerator; |
33
|
|
|
use Facebook\Tests\Fixtures\FooClientInterface; |
34
|
|
|
use Facebook\Tests\Fixtures\FooPersistentDataInterface; |
35
|
|
|
use Facebook\Tests\Fixtures\FooUrlDetectionInterface; |
36
|
|
|
|
37
|
|
|
class FacebookTest extends \PHPUnit_Framework_TestCase |
38
|
|
|
{ |
39
|
|
|
protected $config = [ |
40
|
|
|
'app_id' => '1337', |
41
|
|
|
'app_secret' => 'foo_secret', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
public function testLoadPolyfills() |
45
|
|
|
{ |
46
|
|
|
$this->assertEquals(true, defined('FACEBOOK_SDK_POLYFILLS_LOADED')); |
47
|
|
|
$this->assertEquals(1, FACEBOOK_SDK_POLYFILLS_LOADED); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @expectedException \Facebook\Exceptions\FacebookSDKException |
52
|
|
|
*/ |
53
|
|
|
public function testInstantiatingWithoutAppIdThrows() |
54
|
|
|
{ |
55
|
|
|
// unset value so there is no fallback to test expected Exception |
56
|
|
|
putenv(Facebook::APP_ID_ENV_NAME.'='); |
57
|
|
|
$config = [ |
58
|
|
|
'app_secret' => 'foo_secret', |
59
|
|
|
]; |
60
|
|
|
new Facebook($config); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @expectedException \Facebook\Exceptions\FacebookSDKException |
65
|
|
|
*/ |
66
|
|
|
public function testInstantiatingWithoutAppSecretThrows() |
67
|
|
|
{ |
68
|
|
|
// unset value so there is no fallback to test expected Exception |
69
|
|
|
putenv(Facebook::APP_SECRET_ENV_NAME.'='); |
70
|
|
|
$config = [ |
71
|
|
|
'app_id' => 'foo_id', |
72
|
|
|
]; |
73
|
|
|
new Facebook($config); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \InvalidArgumentException |
78
|
|
|
*/ |
79
|
|
|
public function testSettingAnInvalidHttpClientHandlerThrows() |
80
|
|
|
{ |
81
|
|
|
$config = array_merge($this->config, [ |
82
|
|
|
'http_client_handler' => 'foo_handler', |
83
|
|
|
]); |
84
|
|
|
new Facebook($config); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testCurlHttpClientHandlerCanBeForced() |
88
|
|
|
{ |
89
|
|
|
if (!extension_loaded('curl')) { |
90
|
|
|
$this->markTestSkipped('cURL must be installed to test cURL client handler.'); |
91
|
|
|
} |
92
|
|
|
$config = array_merge($this->config, [ |
93
|
|
|
'http_client_handler' => 'curl' |
94
|
|
|
]); |
95
|
|
|
$fb = new Facebook($config); |
96
|
|
|
$this->assertInstanceOf( |
97
|
|
|
'Facebook\HttpClients\FacebookCurlHttpClient', |
98
|
|
|
$fb->getClient()->getHttpClientHandler() |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function testStreamHttpClientHandlerCanBeForced() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$config = array_merge($this->config, [ |
105
|
|
|
'http_client_handler' => 'stream' |
106
|
|
|
]); |
107
|
|
|
$fb = new Facebook($config); |
108
|
|
|
$this->assertInstanceOf( |
109
|
|
|
'Facebook\HttpClients\FacebookStreamHttpClient', |
110
|
|
|
$fb->getClient()->getHttpClientHandler() |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
View Code Duplication |
public function testGuzzleHttpClientHandlerCanBeForced() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$config = array_merge($this->config, [ |
117
|
|
|
'http_client_handler' => 'guzzle' |
118
|
|
|
]); |
119
|
|
|
$fb = new Facebook($config); |
120
|
|
|
$this->assertInstanceOf( |
121
|
|
|
'Facebook\HttpClients\FacebookGuzzleHttpClient', |
122
|
|
|
$fb->getClient()->getHttpClientHandler() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @expectedException \InvalidArgumentException |
128
|
|
|
*/ |
129
|
|
|
public function testSettingAnInvalidPersistentDataHandlerThrows() |
130
|
|
|
{ |
131
|
|
|
$config = array_merge($this->config, [ |
132
|
|
|
'persistent_data_handler' => 'foo_handler', |
133
|
|
|
]); |
134
|
|
|
new Facebook($config); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function testPersistentDataHandlerCanBeForced() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$config = array_merge($this->config, [ |
140
|
|
|
'persistent_data_handler' => 'memory' |
141
|
|
|
]); |
142
|
|
|
$fb = new Facebook($config); |
143
|
|
|
$this->assertInstanceOf( |
144
|
|
|
'Facebook\PersistentData\FacebookMemoryPersistentDataHandler', |
145
|
|
|
$fb->getRedirectLoginHelper()->getPersistentDataHandler() |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testSettingAnInvalidUrlHandlerThrows() |
150
|
|
|
{ |
151
|
|
|
$expectedException = (PHP_MAJOR_VERSION > 5 && class_exists('TypeError')) |
152
|
|
|
? 'TypeError' |
153
|
|
|
: 'PHPUnit_Framework_Error'; |
154
|
|
|
|
155
|
|
|
$this->setExpectedException($expectedException); |
156
|
|
|
|
157
|
|
|
$config = array_merge($this->config, [ |
158
|
|
|
'url_detection_handler' => 'foo_handler', |
159
|
|
|
]); |
160
|
|
|
new Facebook($config); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testTheUrlHandlerWillDefaultToTheFacebookImplementation() |
164
|
|
|
{ |
165
|
|
|
$fb = new Facebook($this->config); |
166
|
|
|
$this->assertInstanceOf('Facebook\Url\FacebookUrlDetectionHandler', $fb->getUrlDetectionHandler()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
View Code Duplication |
public function testAnAccessTokenCanBeSetAsAString() |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$fb = new Facebook($this->config); |
172
|
|
|
$fb->setDefaultAccessToken('foo_token'); |
173
|
|
|
$accessToken = $fb->getDefaultAccessToken(); |
174
|
|
|
|
175
|
|
|
$this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken); |
176
|
|
|
$this->assertEquals('foo_token', (string)$accessToken); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
public function testAnAccessTokenCanBeSetAsAnAccessTokenEntity() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$fb = new Facebook($this->config); |
182
|
|
|
$fb->setDefaultAccessToken(new AccessToken('bar_token')); |
183
|
|
|
$accessToken = $fb->getDefaultAccessToken(); |
184
|
|
|
|
185
|
|
|
$this->assertInstanceOf('Facebook\Authentication\AccessToken', $accessToken); |
186
|
|
|
$this->assertEquals('bar_token', (string)$accessToken); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @expectedException \InvalidArgumentException |
191
|
|
|
*/ |
192
|
|
|
public function testSettingAnInvalidPseudoRandomStringGeneratorThrows() |
193
|
|
|
{ |
194
|
|
|
$config = array_merge($this->config, [ |
195
|
|
|
'pseudo_random_string_generator' => 'foo_generator', |
196
|
|
|
]); |
197
|
|
|
new Facebook($config); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
View Code Duplication |
public function testMcryptCsprgCanBeForced() |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
if (!function_exists('mcrypt_create_iv')) { |
203
|
|
|
$this->markTestSkipped( |
204
|
|
|
'Mcrypt must be installed to test mcrypt_create_iv().' |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$config = array_merge($this->config, [ |
209
|
|
|
'persistent_data_handler' => 'memory', // To keep session errors from happening |
210
|
|
|
'pseudo_random_string_generator' => 'mcrypt' |
211
|
|
|
]); |
212
|
|
|
$fb = new Facebook($config); |
213
|
|
|
$this->assertInstanceOf( |
214
|
|
|
'Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator', |
215
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
View Code Duplication |
public function testOpenSslCsprgCanBeForced() |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
if (!function_exists('openssl_random_pseudo_bytes')) { |
222
|
|
|
$this->markTestSkipped( |
223
|
|
|
'The OpenSSL extension must be enabled to test openssl_random_pseudo_bytes().' |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$config = array_merge($this->config, [ |
228
|
|
|
'persistent_data_handler' => 'memory', // To keep session errors from happening |
229
|
|
|
'pseudo_random_string_generator' => 'openssl' |
230
|
|
|
]); |
231
|
|
|
$fb = new Facebook($config); |
232
|
|
|
$this->assertInstanceOf( |
233
|
|
|
'Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator', |
234
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
View Code Duplication |
public function testUrandomCsprgCanBeForced() |
|
|
|
|
239
|
|
|
{ |
240
|
|
|
if (ini_get('open_basedir')) { |
241
|
|
|
$this->markTestSkipped( |
242
|
|
|
'Cannot test /dev/urandom generator due to open_basedir constraint.' |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
if (!is_readable('/dev/urandom')) { |
247
|
|
|
$this->markTestSkipped( |
248
|
|
|
'/dev/urandom not found or is not readable.' |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$config = array_merge($this->config, [ |
253
|
|
|
'persistent_data_handler' => 'memory', // To keep session errors from happening |
254
|
|
|
'pseudo_random_string_generator' => 'urandom' |
255
|
|
|
]); |
256
|
|
|
$fb = new Facebook($config); |
257
|
|
|
$this->assertInstanceOf( |
258
|
|
|
'Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator', |
259
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @expectedException \InvalidArgumentException |
265
|
|
|
*/ |
266
|
|
|
public function testSettingAnAccessThatIsNotStringOrAccessTokenThrows() |
267
|
|
|
{ |
268
|
|
|
$config = array_merge($this->config, [ |
269
|
|
|
'default_access_token' => 123, |
270
|
|
|
]); |
271
|
|
|
new Facebook($config); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function testCreatingANewRequestWillDefaultToTheProperConfig() |
275
|
|
|
{ |
276
|
|
|
$config = array_merge($this->config, [ |
277
|
|
|
'default_access_token' => 'foo_token', |
278
|
|
|
'enable_beta_mode' => true, |
279
|
|
|
'default_graph_version' => 'v1337', |
280
|
|
|
]); |
281
|
|
|
$fb = new Facebook($config); |
282
|
|
|
|
283
|
|
|
$request = $fb->request('FOO_VERB', '/foo'); |
284
|
|
|
$this->assertEquals('1337', $request->getApp()->getId()); |
285
|
|
|
$this->assertEquals('foo_secret', $request->getApp()->getSecret()); |
286
|
|
|
$this->assertEquals('foo_token', (string)$request->getAccessToken()); |
287
|
|
|
$this->assertEquals('v1337', $request->getGraphVersion()); |
288
|
|
|
$this->assertEquals( |
289
|
|
|
FacebookClient::BASE_GRAPH_URL_BETA, |
290
|
|
|
$fb->getClient()->getBaseGraphUrl() |
291
|
|
|
); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function testCanInjectCustomHandlers() |
295
|
|
|
{ |
296
|
|
|
$config = array_merge($this->config, [ |
297
|
|
|
'http_client_handler' => new FooClientInterface(), |
298
|
|
|
'persistent_data_handler' => new FooPersistentDataInterface(), |
299
|
|
|
'url_detection_handler' => new FooUrlDetectionInterface(), |
300
|
|
|
'pseudo_random_string_generator' => new FooBarPseudoRandomStringGenerator(), |
301
|
|
|
]); |
302
|
|
|
$fb = new Facebook($config); |
303
|
|
|
|
304
|
|
|
$this->assertInstanceOf( |
305
|
|
|
'Facebook\Tests\Fixtures\FooClientInterface', |
306
|
|
|
$fb->getClient()->getHttpClientHandler() |
307
|
|
|
); |
308
|
|
|
$this->assertInstanceOf( |
309
|
|
|
'Facebook\Tests\Fixtures\FooPersistentDataInterface', |
310
|
|
|
$fb->getRedirectLoginHelper()->getPersistentDataHandler() |
311
|
|
|
); |
312
|
|
|
$this->assertInstanceOf( |
313
|
|
|
'Facebook\Tests\Fixtures\FooUrlDetectionInterface', |
314
|
|
|
$fb->getRedirectLoginHelper()->getUrlDetectionHandler() |
315
|
|
|
); |
316
|
|
|
$this->assertInstanceOf( |
317
|
|
|
'Facebook\Tests\Fixtures\FooBarPseudoRandomStringGenerator', |
318
|
|
|
$fb->getRedirectLoginHelper()->getPseudoRandomStringGenerator() |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function testPaginationReturnsProperResponse() |
323
|
|
|
{ |
324
|
|
|
$config = array_merge($this->config, [ |
325
|
|
|
'http_client_handler' => new FooClientInterface(), |
326
|
|
|
]); |
327
|
|
|
$fb = new Facebook($config); |
328
|
|
|
|
329
|
|
|
$request = new FacebookRequest($fb->getApp(), 'foo_token', 'GET'); |
330
|
|
|
$graphEdge = new GraphEdge( |
331
|
|
|
$request, |
332
|
|
|
[], |
333
|
|
|
[ |
334
|
|
|
'paging' => [ |
335
|
|
|
'cursors' => [ |
336
|
|
|
'after' => 'bar_after_cursor', |
337
|
|
|
'before' => 'bar_before_cursor', |
338
|
|
|
], |
339
|
|
|
'previous' => 'previous_url', |
340
|
|
|
'next' => 'next_url', |
341
|
|
|
] |
342
|
|
|
], |
343
|
|
|
'/1337/photos', |
344
|
|
|
'\Facebook\GraphNodes\GraphUser' |
345
|
|
|
); |
346
|
|
|
|
347
|
|
|
$nextPage = $fb->next($graphEdge); |
348
|
|
|
$this->assertInstanceOf('Facebook\GraphNodes\GraphEdge', $nextPage); |
349
|
|
|
$this->assertInstanceOf('Facebook\GraphNodes\GraphUser', $nextPage[0]); |
350
|
|
|
$this->assertEquals('Foo', $nextPage[0]['name']); |
351
|
|
|
|
352
|
|
|
$lastResponse = $fb->getLastResponse(); |
353
|
|
|
$this->assertInstanceOf('Facebook\FacebookResponse', $lastResponse); |
354
|
|
|
$this->assertEquals(1337, $lastResponse->getHttpStatusCode()); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function testCanGetSuccessfulTransferWithMaxTries() |
358
|
|
|
{ |
359
|
|
|
$config = array_merge($this->config, [ |
360
|
|
|
'http_client_handler' => new FakeGraphApiForResumableUpload(), |
361
|
|
|
]); |
362
|
|
|
$fb = new Facebook($config); |
363
|
|
|
$response = $fb->uploadVideo('me', __DIR__.'/foo.txt', [], 'foo-token', 3); |
364
|
|
|
$this->assertEquals([ |
365
|
|
|
'video_id' => '1337', |
366
|
|
|
'success' => true, |
367
|
|
|
], $response); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @expectedException \Facebook\Exceptions\FacebookResponseException |
372
|
|
|
*/ |
373
|
|
|
public function testMaxingOutRetriesWillThrow() |
374
|
|
|
{ |
375
|
|
|
$client = new FakeGraphApiForResumableUpload(); |
376
|
|
|
$client->failOnTransfer(); |
377
|
|
|
|
378
|
|
|
$config = array_merge($this->config, [ |
379
|
|
|
'http_client_handler' => $client, |
380
|
|
|
]); |
381
|
|
|
$fb = new Facebook($config); |
382
|
|
|
$fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3); |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|
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.