PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/facebook/tests/tests.php

https://github.com/headshota/shutkizator
PHP | 736 lines | 635 code | 73 blank | 28 comment | 3 complexity | 54f00280cfc19e1504d7e56e7329279c MD5 | raw file
  1. <?php
  2. /**
  3. * @owner naitik
  4. * @emails naitik@facebook.com, platform-tests@lists.facebook.com
  5. */
  6. class FacebookTest extends PHPUnit_Framework_TestCase
  7. {
  8. const APP_ID = '117743971608120';
  9. const SECRET = '943716006e74d9b9283d4d5d8ab93204';
  10. const MIGRATED_APP_ID = '148931871805121';
  11. const MIGRATED_SECRET = 'bb9b2bb536647ed3b92c1c9a8969ef7c';
  12. private static $VALID_EXPIRED_SESSION = array(
  13. 'access_token' => '117743971608120|2.vdCKd4ZIEJlHwwtrkilgKQ__.86400.1281049200-1677846385|NF_2DDNxFBznj2CuwiwabHhTAHc.',
  14. 'expires' => '1281049200',
  15. 'secret' => 'u0QiRGAwaPCyQ7JE_hiz1w__',
  16. 'session_key' => '2.vdCKd4ZIEJlHwwtrkilgKQ__.86400.1281049200-1677846385',
  17. 'sig' => '7a9b063de0bef334637832166948dcad',
  18. 'uid' => '1677846385',
  19. );
  20. private static $VALID_SIGNED_REQUEST = '1sxR88U4SW9m6QnSxwCEw_CObqsllXhnpP5j2pxD97c.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyODEwNTI4MDAsIm9hdXRoX3Rva2VuIjoiMTE3NzQzOTcxNjA4MTIwfDIuVlNUUWpub3hYVVNYd1RzcDB1U2g5d19fLjg2NDAwLjEyODEwNTI4MDAtMTY3Nzg0NjM4NXx4NURORHBtcy1nMUM0dUJHQVYzSVdRX2pYV0kuIiwidXNlcl9pZCI6IjE2Nzc4NDYzODUifQ';
  21. private static $NON_TOSSED_SIGNED_REQUEST = 'c0Ih6vYvauDwncv0n0pndr0hP0mvZaJPQDPt6Z43O0k.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9';
  22. public function testConstructor() {
  23. $facebook = new Facebook(array(
  24. 'appId' => self::APP_ID,
  25. 'secret' => self::SECRET,
  26. ));
  27. $this->assertEquals($facebook->getAppId(), self::APP_ID,
  28. 'Expect the App ID to be set.');
  29. $this->assertEquals($facebook->getApiSecret(), self::SECRET,
  30. 'Expect the API secret to be set.');
  31. $this->assertFalse($facebook->useCookieSupport(),
  32. 'Expect Cookie support to be off.');
  33. }
  34. public function testConstructorWithCookie() {
  35. $facebook = new Facebook(array(
  36. 'appId' => self::APP_ID,
  37. 'secret' => self::SECRET,
  38. 'cookie' => true,
  39. ));
  40. $this->assertEquals($facebook->getAppId(), self::APP_ID,
  41. 'Expect the App ID to be set.');
  42. $this->assertEquals($facebook->getApiSecret(), self::SECRET,
  43. 'Expect the API secret to be set.');
  44. $this->assertTrue($facebook->useCookieSupport(),
  45. 'Expect Cookie support to be on.');
  46. }
  47. public function testConstructorWithFileUpload() {
  48. $facebook = new Facebook(array(
  49. 'appId' => self::APP_ID,
  50. 'secret' => self::SECRET,
  51. 'fileUpload' => true,
  52. ));
  53. $this->assertEquals($facebook->getAppId(), self::APP_ID,
  54. 'Expect the App ID to be set.');
  55. $this->assertEquals($facebook->getApiSecret(), self::SECRET,
  56. 'Expect the API secret to be set.');
  57. $this->assertTrue($facebook->useFileUploadSupport(),
  58. 'Expect file upload support to be on.');
  59. }
  60. public function testSetAppId() {
  61. $facebook = new Facebook(array(
  62. 'appId' => self::APP_ID,
  63. 'secret' => self::SECRET,
  64. ));
  65. $facebook->setAppId('dummy');
  66. $this->assertEquals($facebook->getAppId(), 'dummy',
  67. 'Expect the App ID to be dummy.');
  68. }
  69. public function testSetAPISecret() {
  70. $facebook = new Facebook(array(
  71. 'appId' => self::APP_ID,
  72. 'secret' => self::SECRET,
  73. ));
  74. $facebook->setApiSecret('dummy');
  75. $this->assertEquals($facebook->getApiSecret(), 'dummy',
  76. 'Expect the API secret to be dummy.');
  77. }
  78. public function testDefaultBaseDomain() {
  79. $facebook = new Facebook(array(
  80. 'appId' => self::APP_ID,
  81. 'secret' => self::SECRET,
  82. 'domain' => 'fbrell.com',
  83. ));
  84. $this->assertEquals($facebook->getBaseDomain(), 'fbrell.com');
  85. }
  86. public function testSetCookieSupport() {
  87. $facebook = new Facebook(array(
  88. 'appId' => self::APP_ID,
  89. 'secret' => self::SECRET,
  90. ));
  91. $this->assertFalse($facebook->useCookieSupport(),
  92. 'Expect Cookie support to be off.');
  93. $facebook->setCookieSupport(true);
  94. $this->assertTrue($facebook->useCookieSupport(),
  95. 'Expect Cookie support to be on.');
  96. }
  97. public function testIgnoreDeleteSetCookie() {
  98. $facebook = new Facebook(array(
  99. 'appId' => self::APP_ID,
  100. 'secret' => self::SECRET,
  101. 'cookie' => true,
  102. ));
  103. $cookieName = 'fbs_' . self::APP_ID;
  104. $this->assertFalse(isset($_COOKIE[$cookieName]),
  105. 'Expect Cookie to not exist.');
  106. $facebook->setSession(null);
  107. $this->assertFalse(isset($_COOKIE[$cookieName]),
  108. 'Expect Cookie to not exist.');
  109. }
  110. public function testSetFileUploadSupport() {
  111. $facebook = new Facebook(array(
  112. 'appId' => self::APP_ID,
  113. 'secret' => self::SECRET,
  114. ));
  115. $this->assertFalse($facebook->useFileUploadSupport(),
  116. 'Expect file upload support to be off.');
  117. $facebook->setFileUploadSupport(true);
  118. $this->assertTrue($facebook->useFileUploadSupport(),
  119. 'Expect file upload support to be on.');
  120. }
  121. public function testSetNullSession() {
  122. $facebook = new Facebook(array(
  123. 'appId' => self::APP_ID,
  124. 'secret' => self::SECRET,
  125. ));
  126. $facebook->setSession(null);
  127. $this->assertTrue($facebook->getSession() === null,
  128. 'Expect null session back.');
  129. }
  130. public function testNonUserAccessToken() {
  131. $facebook = new Facebook(array(
  132. 'appId' => self::APP_ID,
  133. 'secret' => self::SECRET,
  134. 'cookie' => true,
  135. ));
  136. $this->assertTrue($facebook->getAccessToken() ==
  137. self::APP_ID.'|'.self::SECRET,
  138. 'Expect appId|secret.');
  139. }
  140. public function testSetSession() {
  141. $facebook = new Facebook(array(
  142. 'appId' => self::APP_ID,
  143. 'secret' => self::SECRET,
  144. 'cookie' => true,
  145. ));
  146. $facebook->setSession(self::$VALID_EXPIRED_SESSION);
  147. $this->assertTrue($facebook->getUser() ==
  148. self::$VALID_EXPIRED_SESSION['uid'],
  149. 'Expect uid back.');
  150. $this->assertTrue($facebook->getAccessToken() ==
  151. self::$VALID_EXPIRED_SESSION['access_token'],
  152. 'Expect access token back.');
  153. }
  154. public function testGetSessionFromCookie() {
  155. $cookieName = 'fbs_' . self::APP_ID;
  156. $session = self::$VALID_EXPIRED_SESSION;
  157. $_COOKIE[$cookieName] = '"' . http_build_query($session) . '"';
  158. $facebook = new Facebook(array(
  159. 'appId' => self::APP_ID,
  160. 'secret' => self::SECRET,
  161. 'cookie' => true,
  162. ));
  163. // since we're serializing and deserializing the array, we cannot rely on
  164. // positions being the same, so we do a ksort before comparison
  165. $loaded_session = $facebook->getSession();
  166. ksort($loaded_session);
  167. ksort($session);
  168. $this->assertEquals($loaded_session, $session,
  169. 'Expect session back.');
  170. unset($_COOKIE[$cookieName]);
  171. }
  172. public function testInvalidGetSessionFromCookie() {
  173. $facebook = new Facebook(array(
  174. 'appId' => self::APP_ID,
  175. 'secret' => self::SECRET,
  176. 'cookie' => true,
  177. ));
  178. $cookieName = 'fbs_' . self::APP_ID;
  179. $session = self::$VALID_EXPIRED_SESSION;
  180. $session['uid'] = 'make me invalid';
  181. $_COOKIE[$cookieName] = http_build_query($session);
  182. $this->assertTrue($facebook->getSession() === null,
  183. 'Expect no session back.');
  184. unset($_COOKIE[$cookieName]);
  185. }
  186. public function testSessionFromQueryString() {
  187. // @style-override allow json_encode call
  188. $_REQUEST['session'] = json_encode(self::$VALID_EXPIRED_SESSION);
  189. $facebook = new Facebook(array(
  190. 'appId' => self::APP_ID,
  191. 'secret' => self::SECRET,
  192. ));
  193. $this->assertEquals($facebook->getUser(),
  194. self::$VALID_EXPIRED_SESSION['uid'],
  195. 'Expect uid back.');
  196. unset($_REQUEST['session']);
  197. }
  198. public function testInvalidSessionFromQueryString() {
  199. $facebook = new Facebook(array(
  200. 'appId' => self::APP_ID,
  201. 'secret' => self::SECRET,
  202. ));
  203. $params = array(
  204. 'fb_sig_in_iframe' => 1,
  205. 'fb_sig_user' => '1677846385',
  206. 'fb_sig_session_key' =>
  207. '2.NdKHtYIuB0EcNSHOvqAKHg__.86400.1258092000-1677846385',
  208. 'fb_sig_ss' => 'AdCOu5nhDiexxRDLwZfqnA__',
  209. 'fb_sig' => '1949f256171f37ecebe00685ce33bf17',
  210. );
  211. foreach($params as $key => $value) {
  212. $_GET[$key] = $value;
  213. }
  214. $this->assertEquals($facebook->getUser(), null,
  215. 'Expect uid back.');
  216. foreach($params as $key => $value) {
  217. unset($_GET[$key]);
  218. }
  219. }
  220. public function testGetUID() {
  221. $facebook = new Facebook(array(
  222. 'appId' => self::APP_ID,
  223. 'secret' => self::SECRET,
  224. ));
  225. $session = self::$VALID_EXPIRED_SESSION;
  226. $facebook->setSession($session);
  227. $this->assertEquals($facebook->getUser(), $session['uid'],
  228. 'Expect dummy uid back.');
  229. }
  230. public function testAPIWithoutSession() {
  231. $facebook = new Facebook(array(
  232. 'appId' => self::APP_ID,
  233. 'secret' => self::SECRET,
  234. ));
  235. $response = $facebook->api(array(
  236. 'method' => 'fql.query',
  237. 'query' => 'SELECT name FROM user WHERE uid=4',
  238. ));
  239. $this->assertEquals(count($response), 1,
  240. 'Expect one row back.');
  241. $this->assertEquals($response[0]['name'], 'Mark Zuckerberg',
  242. 'Expect the name back.');
  243. }
  244. public function testAPIWithSession() {
  245. $facebook = new Facebook(array(
  246. 'appId' => self::APP_ID,
  247. 'secret' => self::SECRET,
  248. ));
  249. $facebook->setSession(self::$VALID_EXPIRED_SESSION);
  250. // this is strange in that we are expecting a session invalid error vs a
  251. // signature invalid error. basically we're just making sure session based
  252. // signing is working, not that the api call is returning data.
  253. try {
  254. $response = $facebook->api(array(
  255. 'method' => 'fql.query',
  256. 'query' => 'SELECT name FROM profile WHERE id=4',
  257. ));
  258. $this->fail('Should not get here.');
  259. } catch(FacebookApiException $e) {
  260. $msg = 'Exception: 190: Invalid OAuth 2.0 Access Token';
  261. $this->assertEquals((string) $e, $msg,
  262. 'Expect the invalid session message.');
  263. $result = $e->getResult();
  264. $this->assertTrue(is_array($result), 'expect a result object');
  265. $this->assertEquals('190', $result['error_code'], 'expect code');
  266. }
  267. }
  268. public function testAPIGraphPublicData() {
  269. $facebook = new Facebook(array(
  270. 'appId' => self::APP_ID,
  271. 'secret' => self::SECRET,
  272. ));
  273. $response = $facebook->api('/naitik');
  274. $this->assertEquals(
  275. $response['id'], '5526183', 'should get expected id.');
  276. }
  277. public function testGraphAPIWithSession() {
  278. $facebook = new Facebook(array(
  279. 'appId' => self::APP_ID,
  280. 'secret' => self::SECRET,
  281. ));
  282. $facebook->setSession(self::$VALID_EXPIRED_SESSION);
  283. try {
  284. $response = $facebook->api('/me');
  285. $this->fail('Should not get here.');
  286. } catch(FacebookApiException $e) {
  287. // means the server got the access token
  288. $msg = 'OAuthException: Error processing access token.';
  289. $this->assertEquals($msg, (string) $e,
  290. 'Expect the invalid session message.');
  291. // also ensure the session was reset since it was invalid
  292. $this->assertEquals($facebook->getSession(), null,
  293. 'Expect the session to be reset.');
  294. }
  295. }
  296. public function testGraphAPIMethod() {
  297. $facebook = new Facebook(array(
  298. 'appId' => self::APP_ID,
  299. 'secret' => self::SECRET,
  300. ));
  301. try {
  302. $response = $facebook->api('/naitik', 'DELETE');
  303. $this->fail('Should not get here.');
  304. } catch(FacebookApiException $e) {
  305. // ProfileDelete means the server understood the DELETE
  306. $msg = 'GraphMethodException: Unsupported delete request.';
  307. $this->assertEquals($msg, (string) $e,
  308. 'Expect the invalid session message.');
  309. }
  310. }
  311. public function testGraphAPIOAuthSpecError() {
  312. $facebook = new Facebook(array(
  313. 'appId' => self::MIGRATED_APP_ID,
  314. 'secret' => self::MIGRATED_SECRET,
  315. ));
  316. try {
  317. $response = $facebook->api('/me', array(
  318. 'client_id' => self::MIGRATED_APP_ID));
  319. $this->fail('Should not get here.');
  320. } catch(FacebookApiException $e) {
  321. // means the server got the access token
  322. $msg = 'invalid_request: An active access token must be used '.
  323. 'to query information about the current user.';
  324. $this->assertEquals($msg, (string) $e,
  325. 'Expect the invalid session message.');
  326. // also ensure the session was reset since it was invalid
  327. $this->assertEquals($facebook->getSession(), null,
  328. 'Expect the session to be reset.');
  329. }
  330. }
  331. public function testGraphAPIMethodOAuthSpecError() {
  332. $facebook = new Facebook(array(
  333. 'appId' => self::MIGRATED_APP_ID,
  334. 'secret' => self::MIGRATED_SECRET,
  335. ));
  336. try {
  337. $response = $facebook->api('/naitik', 'DELETE', array(
  338. 'client_id' => self::MIGRATED_APP_ID));
  339. $this->fail('Should not get here.');
  340. } catch(FacebookApiException $e) {
  341. // ProfileDelete means the server understood the DELETE
  342. $msg = 'invalid_request: Unsupported delete request.';
  343. $this->assertEquals($msg, (string) $e,
  344. 'Expect the invalid session message.');
  345. }
  346. }
  347. public function testCurlFailure() {
  348. $facebook = new Facebook(array(
  349. 'appId' => self::APP_ID,
  350. 'secret' => self::SECRET,
  351. ));
  352. if (!defined('CURLOPT_TIMEOUT_MS')) {
  353. // can't test it if we don't have millisecond timeouts
  354. return;
  355. }
  356. try {
  357. // we dont expect facebook will ever return in 1ms
  358. Facebook::$CURL_OPTS[CURLOPT_TIMEOUT_MS] = 1;
  359. $facebook->api('/naitik');
  360. } catch(FacebookApiException $e) {
  361. unset(Facebook::$CURL_OPTS[CURLOPT_TIMEOUT_MS]);
  362. $this->assertEquals(
  363. CURLE_OPERATION_TIMEOUTED, $e->getCode(), 'expect timeout');
  364. $this->assertEquals('CurlException', $e->getType(), 'expect type');
  365. return;
  366. }
  367. $this->fail('Should not get here.');
  368. }
  369. public function testGraphAPIWithOnlyParams() {
  370. $facebook = new Facebook(array(
  371. 'appId' => self::APP_ID,
  372. 'secret' => self::SECRET,
  373. ));
  374. $response = $facebook->api('/331218348435/feed',
  375. array('limit' => 1, 'access_token' => ''));
  376. $this->assertEquals(1, count($response['data']), 'should get one entry');
  377. $this->assertTrue(
  378. strstr($response['paging']['next'], 'limit=1') !== false,
  379. 'expect the same limit back in the paging urls'
  380. );
  381. }
  382. public function testLoginURLDefaults() {
  383. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  384. $_SERVER['REQUEST_URI'] = '/examples';
  385. $facebook = new Facebook(array(
  386. 'appId' => self::APP_ID,
  387. 'secret' => self::SECRET,
  388. ));
  389. $encodedUrl = rawurlencode('http://fbrell.com/examples');
  390. $this->assertNotNull(strpos($facebook->getLoginUrl(), $encodedUrl),
  391. 'Expect the current url to exist.');
  392. unset($_SERVER['HTTP_HOST']);
  393. unset($_SERVER['REQUEST_URI']);
  394. }
  395. public function testLoginURLDefaultsDropSessionQueryParam() {
  396. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  397. $_SERVER['REQUEST_URI'] = '/examples?session=xx42xx';
  398. $facebook = new Facebook(array(
  399. 'appId' => self::APP_ID,
  400. 'secret' => self::SECRET,
  401. ));
  402. $expectEncodedUrl = rawurlencode('http://fbrell.com/examples');
  403. $this->assertTrue(strpos($facebook->getLoginUrl(), $expectEncodedUrl) > -1,
  404. 'Expect the current url to exist.');
  405. $this->assertFalse(strpos($facebook->getLoginUrl(), 'xx42xx'),
  406. 'Expect the session param to be dropped.');
  407. unset($_SERVER['HTTP_HOST']);
  408. unset($_SERVER['REQUEST_URI']);
  409. }
  410. public function testLoginURLDefaultsDropSessionQueryParamButNotOthers() {
  411. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  412. $_SERVER['REQUEST_URI'] = '/examples?session=xx42xx&do_not_drop=xx43xx';
  413. $facebook = new Facebook(array(
  414. 'appId' => self::APP_ID,
  415. 'secret' => self::SECRET,
  416. ));
  417. $expectEncodedUrl = rawurlencode('http://fbrell.com/examples');
  418. $this->assertFalse(strpos($facebook->getLoginUrl(), 'xx42xx'),
  419. 'Expect the session param to be dropped.');
  420. $this->assertTrue(strpos($facebook->getLoginUrl(), 'xx43xx') > -1,
  421. 'Expect the do_not_drop param to exist.');
  422. unset($_SERVER['HTTP_HOST']);
  423. unset($_SERVER['REQUEST_URI']);
  424. }
  425. public function testLoginURLCustomNext() {
  426. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  427. $_SERVER['REQUEST_URI'] = '/examples';
  428. $facebook = new Facebook(array(
  429. 'appId' => self::APP_ID,
  430. 'secret' => self::SECRET,
  431. ));
  432. $next = 'http://fbrell.com/custom';
  433. $loginUrl = $facebook->getLoginUrl(array(
  434. 'next' => $next,
  435. 'cancel_url' => $next
  436. ));
  437. $currentEncodedUrl = rawurlencode('http://fbrell.com/examples');
  438. $expectedEncodedUrl = rawurlencode($next);
  439. $this->assertNotNull(strpos($loginUrl, $expectedEncodedUrl),
  440. 'Expect the custom url to exist.');
  441. $this->assertFalse(strpos($loginUrl, $currentEncodedUrl),
  442. 'Expect the current url to not exist.');
  443. unset($_SERVER['HTTP_HOST']);
  444. unset($_SERVER['REQUEST_URI']);
  445. }
  446. public function testLogoutURLDefaults() {
  447. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  448. $_SERVER['REQUEST_URI'] = '/examples';
  449. $facebook = new Facebook(array(
  450. 'appId' => self::APP_ID,
  451. 'secret' => self::SECRET,
  452. ));
  453. $encodedUrl = rawurlencode('http://fbrell.com/examples');
  454. $this->assertNotNull(strpos($facebook->getLogoutUrl(), $encodedUrl),
  455. 'Expect the current url to exist.');
  456. unset($_SERVER['HTTP_HOST']);
  457. unset($_SERVER['REQUEST_URI']);
  458. }
  459. public function testLoginStatusURLDefaults() {
  460. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  461. $_SERVER['REQUEST_URI'] = '/examples';
  462. $facebook = new Facebook(array(
  463. 'appId' => self::APP_ID,
  464. 'secret' => self::SECRET,
  465. ));
  466. $encodedUrl = rawurlencode('http://fbrell.com/examples');
  467. $this->assertNotNull(strpos($facebook->getLoginStatusUrl(), $encodedUrl),
  468. 'Expect the current url to exist.');
  469. unset($_SERVER['HTTP_HOST']);
  470. unset($_SERVER['REQUEST_URI']);
  471. }
  472. public function testLoginStatusURLCustom() {
  473. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  474. $_SERVER['REQUEST_URI'] = '/examples';
  475. $facebook = new Facebook(array(
  476. 'appId' => self::APP_ID,
  477. 'secret' => self::SECRET,
  478. ));
  479. $encodedUrl1 = rawurlencode('http://fbrell.com/examples');
  480. $okUrl = 'http://fbrell.com/here1';
  481. $encodedUrl2 = rawurlencode($okUrl);
  482. $loginStatusUrl = $facebook->getLoginStatusUrl(array(
  483. 'ok_session' => $okUrl,
  484. ));
  485. $this->assertNotNull(strpos($loginStatusUrl, $encodedUrl1),
  486. 'Expect the current url to exist.');
  487. $this->assertNotNull(strpos($loginStatusUrl, $encodedUrl2),
  488. 'Expect the custom url to exist.');
  489. unset($_SERVER['HTTP_HOST']);
  490. unset($_SERVER['REQUEST_URI']);
  491. }
  492. public function testMagicQuotesQueryString() {
  493. if (!get_magic_quotes_gpc()) {
  494. // this test cannot run without get_magic_quotes_gpc(), and the setting
  495. // cannot be modified at runtime, so we're shit out of luck. thanks php.
  496. return;
  497. }
  498. // @style-override allow json_encode call
  499. $_REQUEST['session'] = addslashes(
  500. json_encode(self::$VALID_EXPIRED_SESSION));
  501. $facebook = new Facebook(array(
  502. 'appId' => self::APP_ID,
  503. 'secret' => self::SECRET,
  504. ));
  505. $this->assertEquals($facebook->getUser(),
  506. self::$VALID_EXPIRED_SESSION['uid'],
  507. 'Expect uid back.');
  508. unset($_REQUEST['session']);
  509. }
  510. public function testMagicQuotesCookie() {
  511. if (!get_magic_quotes_gpc()) {
  512. // this test cannot run without get_magic_quotes_gpc(), and the setting
  513. // cannot be modified at runtime, so we're shit out of luck. thanks php.
  514. return;
  515. }
  516. $cookieName = 'fbs_' . self::APP_ID;
  517. $session = self::$VALID_EXPIRED_SESSION;
  518. $_COOKIE[$cookieName] = addslashes('"' . http_build_query($session) . '"');
  519. $facebook = new Facebook(array(
  520. 'appId' => self::APP_ID,
  521. 'secret' => self::SECRET,
  522. 'cookie' => true,
  523. ));
  524. // since we're serializing and deserializing the array, we cannot rely on
  525. // positions being the same, so we do a ksort before comparison
  526. $loaded_session = $facebook->getSession();
  527. ksort($loaded_session);
  528. ksort($session);
  529. $this->assertEquals($loaded_session, $session,
  530. 'Expect session back.');
  531. unset($_COOKIE[$cookieName]);
  532. }
  533. public function testNonDefaultPort() {
  534. $_SERVER['HTTP_HOST'] = 'fbrell.com:8080';
  535. $_SERVER['REQUEST_URI'] = '/examples';
  536. $facebook = new Facebook(array(
  537. 'appId' => self::APP_ID,
  538. 'secret' => self::SECRET,
  539. ));
  540. $encodedUrl = rawurlencode('http://fbrell.com:8080/examples');
  541. $this->assertNotNull(strpos($facebook->getLoginUrl(), $encodedUrl),
  542. 'Expect the current url to exist.');
  543. unset($_SERVER['HTTP_HOST']);
  544. unset($_SERVER['REQUEST_URI']);
  545. }
  546. public function testSecureCurrentUrl() {
  547. $_SERVER['HTTP_HOST'] = 'fbrell.com';
  548. $_SERVER['REQUEST_URI'] = '/examples';
  549. $_SERVER['HTTPS'] = 'on';
  550. $facebook = new Facebook(array(
  551. 'appId' => self::APP_ID,
  552. 'secret' => self::SECRET,
  553. ));
  554. $encodedUrl = rawurlencode('https://fbrell.com/examples');
  555. $this->assertNotNull(strpos($facebook->getLoginUrl(), $encodedUrl),
  556. 'Expect the current url to exist.');
  557. unset($_SERVER['HTTP_HOST']);
  558. unset($_SERVER['REQUEST_URI']);
  559. unset($_SERVER['HTTPS']);
  560. }
  561. public function testSecureCurrentUrlWithNonDefaultPort() {
  562. $_SERVER['HTTP_HOST'] = 'fbrell.com:8080';
  563. $_SERVER['REQUEST_URI'] = '/examples';
  564. $_SERVER['HTTPS'] = 'on';
  565. $facebook = new Facebook(array(
  566. 'appId' => self::APP_ID,
  567. 'secret' => self::SECRET,
  568. ));
  569. $encodedUrl = rawurlencode('https://fbrell.com:8080/examples');
  570. $this->assertNotNull(strpos($facebook->getLoginUrl(), $encodedUrl),
  571. 'Expect the current url to exist.');
  572. unset($_SERVER['HTTP_HOST']);
  573. unset($_SERVER['REQUEST_URI']);
  574. unset($_SERVER['HTTPS']);
  575. }
  576. public function testIgnoreArgSeparatorForCookie() {
  577. $cookieName = 'fbs_' . self::APP_ID;
  578. $session = self::$VALID_EXPIRED_SESSION;
  579. $_COOKIE[$cookieName] = '"' . http_build_query($session) . '"';
  580. ini_set('arg_separator.output', '&amp;');
  581. // ensure we're testing what we expect
  582. $this->assertEquals(http_build_query(array('a' => 1, 'b' => 2)),
  583. 'a=1&amp;b=2');
  584. $facebook = new Facebook(array(
  585. 'appId' => self::APP_ID,
  586. 'secret' => self::SECRET,
  587. 'cookie' => true,
  588. ));
  589. // since we're serializing and deserializing the array, we cannot rely on
  590. // positions being the same, so we do a ksort before comparison
  591. $loaded_session = $facebook->getSession();
  592. ksort($loaded_session);
  593. ksort($session);
  594. $this->assertEquals($loaded_session, $session,
  595. 'Expect session back.');
  596. unset($_COOKIE[$cookieName]);
  597. ini_set('arg_separator.output', '&');
  598. }
  599. public function testAppSecretCall() {
  600. $facebook = new Facebook(array(
  601. 'appId' => self::APP_ID,
  602. 'secret' => self::SECRET,
  603. ));
  604. $response = $facebook->api('/' . self::APP_ID . '/insights');
  605. $this->assertTrue(count($response['data']) > 0,
  606. 'Expect some data back.');
  607. }
  608. public function testBase64UrlEncode() {
  609. $input = 'Facebook rocks';
  610. $output = 'RmFjZWJvb2sgcm9ja3M';
  611. $this->assertEquals(FBPublic::publicBase64UrlDecode($output), $input);
  612. }
  613. public function testSignedToken() {
  614. $facebook = new FBPublic(array(
  615. 'appId' => self::APP_ID,
  616. 'secret' => self::SECRET,
  617. ));
  618. $payload = $facebook->publicParseSignedRequest(self::$VALID_SIGNED_REQUEST);
  619. $this->assertNotNull($payload, 'Expected token to parse');
  620. $session = $facebook->publicCreateSessionFromSignedRequest($payload);
  621. $this->assertEquals($session['uid'], self::$VALID_EXPIRED_SESSION['uid']);
  622. $this->assertEquals($facebook->getSignedRequest(), null);
  623. $_REQUEST['signed_request'] = self::$VALID_SIGNED_REQUEST;
  624. $this->assertEquals($facebook->getSignedRequest(), $payload);
  625. unset($_REQUEST['signed_request']);
  626. }
  627. public function testSignedTokenInQuery() {
  628. $facebook = new Facebook(array(
  629. 'appId' => self::APP_ID,
  630. 'secret' => self::SECRET,
  631. ));
  632. $_REQUEST['signed_request'] = self::$VALID_SIGNED_REQUEST;
  633. $this->assertNotNull($facebook->getSession());
  634. unset($_REQUEST['signed_request']);
  635. }
  636. public function testNonTossedSignedtoken() {
  637. $facebook = new FBPublic(array(
  638. 'appId' => self::APP_ID,
  639. 'secret' => self::SECRET,
  640. ));
  641. $payload = $facebook->publicParseSignedRequest(
  642. self::$NON_TOSSED_SIGNED_REQUEST);
  643. $this->assertNotNull($payload, 'Expected token to parse');
  644. $session = $facebook->publicCreateSessionFromSignedRequest($payload);
  645. $this->assertNull($session);
  646. $this->assertNull($facebook->getSignedRequest());
  647. $_REQUEST['signed_request'] = self::$NON_TOSSED_SIGNED_REQUEST;
  648. $this->assertEquals($facebook->getSignedRequest(),
  649. array('algorithm' => 'HMAC-SHA256'));
  650. unset($_REQUEST['signed_request']);
  651. }
  652. }
  653. class FBPublic extends Facebook {
  654. public static function publicBase64UrlDecode($input) {
  655. return self::base64UrlDecode($input);
  656. }
  657. public function publicParseSignedRequest($intput) {
  658. return $this->parseSignedRequest($intput);
  659. }
  660. public function publicCreateSessionFromSignedRequest($payload) {
  661. return $this->createSessionFromSignedRequest($payload);
  662. }
  663. }