@@ -25,6 +25,7 @@ class PHPSDKTestCase extends PHPUnit_Framework_TestCase {
25
25
private static $ kExpiredAccessToken = '206492729383450|2.N4RKywNPuHAey7CK56_wmg__.3600.1304560800.1-214707|6Q14AfpYi_XJB26aRQumouzJiGA ' ;
26
26
private static $ kValidSignedRequest = '1sxR88U4SW9m6QnSxwCEw_CObqsllXhnpP5j2pxD97c.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyODEwNTI4MDAsIm9hdXRoX3Rva2VuIjoiMTE3NzQzOTcxNjA4MTIwfDIuVlNUUWpub3hYVVNYd1RzcDB1U2g5d19fLjg2NDAwLjEyODEwNTI4MDAtMTY3Nzg0NjM4NXx4NURORHBtcy1nMUM0dUJHQVYzSVdRX2pYV0kuIiwidXNlcl9pZCI6IjE2Nzc4NDYzODUifQ ' ;
27
27
private static $ kNonTosedSignedRequest = 'c0Ih6vYvauDwncv0n0pndr0hP0mvZaJPQDPt6Z43O0k.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9 ' ;
28
+ private static $ kSignedRequestWithBogusSignature = '1sxR32U4SW9m6QnSxwCEw_CObqsllXhnpP5j2pxD97c.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyODEwNTI4MDAsIm9hdXRoX3Rva2VuIjoiMTE3NzQzOTcxNjA4MTIwfDIuVlNUUWpub3hYVVNYd1RzcDB1U2g5d19fLjg2NDAwLjEyODEwNTI4MDAtMTY3Nzg0NjM4NXx4NURORHBtcy1nMUM0dUJHQVYzSVdRX2pYV0kuIiwidXNlcl9pZCI6IjE2Nzc4NDYzODUifQ ' ;
28
29
29
30
public function testConstructor () {
30
31
$ facebook = new TransientFacebook (array (
@@ -183,6 +184,39 @@ public function testGetLoginURLWithExtraParams() {
183
184
$ this ->assertEquals (strlen ($ query_map ['state ' ]), $ num_characters = 32 );
184
185
}
185
186
187
+ public function testGetLoginURLWithScopeParamsAsArray () {
188
+ $ facebook = new Facebook (array (
189
+ 'appId ' => self ::APP_ID ,
190
+ 'secret ' => self ::SECRET ,
191
+ ));
192
+
193
+ // fake the HPHP $_SERVER globals
194
+ $ _SERVER ['HTTP_HOST ' ] = 'www.test.com ' ;
195
+ $ _SERVER ['REQUEST_URI ' ] = '/unit-tests.php ' ;
196
+ $ scope_params_as_array = array ('email ' ,'sms ' ,'read_stream ' );
197
+ $ extra_params = array ('scope ' => $ scope_params_as_array ,
198
+ 'nonsense ' => 'nonsense ' );
199
+ $ login_url = parse_url ($ facebook ->getLoginUrl ($ extra_params ));
200
+ $ this ->assertEquals ($ login_url ['scheme ' ], 'https ' );
201
+ $ this ->assertEquals ($ login_url ['host ' ], 'www.facebook.com ' );
202
+ $ this ->assertEquals ($ login_url ['path ' ], '/dialog/oauth ' );
203
+ // expect api to flatten array params to comma separated list
204
+ // should do the same here before asserting to make sure API is behaving
205
+ // correctly;
206
+ $ extra_params ['scope ' ] = implode (', ' , $ scope_params_as_array );
207
+ $ expected_login_params =
208
+ array_merge (
209
+ array ('client_id ' => self ::APP_ID ,
210
+ 'redirect_uri ' => 'http://www.test.com/unit-tests.php ' ),
211
+ $ extra_params );
212
+ $ query_map = array ();
213
+ parse_str ($ login_url ['query ' ], $ query_map );
214
+ $ this ->assertIsSubset ($ expected_login_params , $ query_map );
215
+ // we don't know what the state is, but we know it's an md5 and should
216
+ // be 32 characters long.
217
+ $ this ->assertEquals (strlen ($ query_map ['state ' ]), $ num_characters = 32 );
218
+ }
219
+
186
220
public function testGetCodeWithValidCSRFState () {
187
221
$ facebook = new FBCode (array (
188
222
'appId ' => self ::APP_ID ,
@@ -234,6 +268,30 @@ public function testGetUserFromSignedRequest() {
234
268
'Failed to get user ID from a valid signed request. ' );
235
269
}
236
270
271
+ public function testGetSignedRequestFromCookie () {
272
+ $ facebook = new FBGetSignedRequestCookieFacebook (array (
273
+ 'appId ' => self ::APP_ID ,
274
+ 'secret ' => self ::SECRET ,
275
+ ));
276
+
277
+ $ _COOKIE [$ facebook ->publicGetSignedRequestCookieName ()] =
278
+ self ::$ kValidSignedRequest ;
279
+ $ this ->assertNotNull ($ facebook ->publicGetSignedRequest ());
280
+ $ this ->assertEquals ('1677846385 ' , $ facebook ->getUser (),
281
+ 'Failed to get user ID from a valid signed request. ' );
282
+ }
283
+
284
+ public function testGetSignedRequestWithIncorrectSignature () {
285
+ $ facebook = new FBGetSignedRequestCookieFacebook (array (
286
+ 'appId ' => self ::APP_ID ,
287
+ 'secret ' => self ::SECRET ,
288
+ ));
289
+
290
+ $ _COOKIE [$ facebook ->publicGetSignedRequestCookieName ()] =
291
+ self ::$ kSignedRequestWithBogusSignature ;
292
+ $ this ->assertNull ($ facebook ->publicGetSignedRequest ());
293
+ }
294
+
237
295
public function testNonUserAccessToken () {
238
296
$ facebook = new FBAccessToken (array (
239
297
'appId ' => self ::APP_ID ,
@@ -350,7 +408,7 @@ public function testGraphAPIMethod() {
350
408
} catch (FacebookApiException $ e ) {
351
409
// ProfileDelete means the server understood the DELETE
352
410
$ msg =
353
- 'OAuthException: An access token is required to request this resource. ' ;
411
+ 'OAuthException: A user access token is required to request this resource. ' ;
354
412
$ this ->assertEquals ($ msg , (string ) $ e ,
355
413
'Expect the invalid session message. ' );
356
414
}
@@ -426,13 +484,24 @@ public function testGraphAPIWithOnlyParams() {
426
484
'secret ' => self ::SECRET ,
427
485
));
428
486
429
- $ response = $ facebook ->api ('/331218348435/feed ' ,
430
- array ('limit ' => 1 , 'access_token ' => '' ));
431
- $ this ->assertEquals (1 , count ($ response ['data ' ]), 'should get one entry ' );
432
- $ this ->assertTrue (
433
- strpos ($ response ['paging ' ]['next ' ], 'limit=1 ' ) !== false ,
434
- 'expect the same limit back in the paging urls '
435
- );
487
+ $ response = $ facebook ->api ('/jerry ' );
488
+ $ this ->assertTrue (isset ($ response ['id ' ]),
489
+ 'User ID should be public. ' );
490
+ $ this ->assertTrue (isset ($ response ['name ' ]),
491
+ 'User \'s name should be public. ' );
492
+ $ this ->assertTrue (isset ($ response ['first_name ' ]),
493
+ 'User \'s first name should be public. ' );
494
+ $ this ->assertTrue (isset ($ response ['last_name ' ]),
495
+ 'User \'s last name should be public. ' );
496
+ $ this ->assertFalse (isset ($ response ['work ' ]),
497
+ 'User \'s work history should only be available with ' .
498
+ 'a valid access token. ' );
499
+ $ this ->assertFalse (isset ($ response ['education ' ]),
500
+ 'User \'s education history should only be ' .
501
+ 'available with a valid access token. ' );
502
+ $ this ->assertFalse (isset ($ response ['verified ' ]),
503
+ 'User \'s verification status should only be ' .
504
+ 'available with a valid access token. ' );
436
505
}
437
506
438
507
public function testLoginURLDefaults () {
@@ -839,3 +908,13 @@ public function publicGetCurrentUrl() {
839
908
return $ this ->getCurrentUrl ();
840
909
}
841
910
}
911
+
912
+ class FBGetSignedRequestCookieFacebook extends TransientFacebook {
913
+ public function publicGetSignedRequest () {
914
+ return $ this ->getSignedRequest ();
915
+ }
916
+
917
+ public function publicGetSignedRequestCookieName () {
918
+ return $ this ->getSignedRequestCookieName ();
919
+ }
920
+ }
0 commit comments