@@ -94,8 +94,44 @@ public function testSetFileUploadSupport() {
9494 'Expect file upload support to be on. ' );
9595 }
9696
97+ public function testGetCurrentURL () {
98+ $ facebook = new FBGetCurrentURLFacebook (array (
99+ 'appId ' => self ::APP_ID ,
100+ 'secret ' => self ::SECRET ,
101+ ));
102+
103+ // fake the HPHP $_SERVER globals
104+ $ _SERVER ['HTTP_HOST ' ] = 'www.test.com ' ;
105+ $ _SERVER ['REQUEST_URI ' ] = '/unit-tests.php?one=one&two=two&three=three ' ;
106+ $ current_url = $ facebook ->publicGetCurrentUrl ();
107+ $ this ->assertEquals (
108+ 'http://www.test.com/unit-tests.php?one=one&two=two&three=three ' ,
109+ $ current_url ,
110+ 'getCurrentUrl function is changing the current URL ' );
111+
112+ // ensure structure of valueless GET params is retained (sometimes
113+ // an = sign was present, and sometimes it was not)
114+ // first test when equal signs are present
115+ $ _SERVER ['HTTP_HOST ' ] = 'www.test.com ' ;
116+ $ _SERVER ['REQUEST_URI ' ] = '/unit-tests.php?one=&two=&three= ' ;
117+ $ current_url = $ facebook ->publicGetCurrentUrl ();
118+ $ this ->assertEquals (
119+ 'http://www.test.com/unit-tests.php?one=&two=&three= ' ,
120+ $ current_url ,
121+ 'getCurrentUrl function is changing the current URL ' );
122+
123+ // now confirm that
124+ $ _SERVER ['HTTP_HOST ' ] = 'www.test.com ' ;
125+ $ _SERVER ['REQUEST_URI ' ] = '/unit-tests.php?one&two&three ' ;
126+ $ current_url = $ facebook ->publicGetCurrentUrl ();
127+ $ this ->assertEquals (
128+ 'http://www.test.com/unit-tests.php?one&two&three ' ,
129+ $ current_url ,
130+ 'getCurrentUrl function is changing the current URL ' );
131+ }
132+
97133 public function testGetLoginURL () {
98- $ facebook = new TransientFacebook (array (
134+ $ facebook = new Facebook (array (
99135 'appId ' => self ::APP_ID ,
100136 'secret ' => self ::SECRET ,
101137 ));
@@ -120,7 +156,7 @@ public function testGetLoginURL() {
120156 }
121157
122158 public function testGetLoginURLWithExtraParams () {
123- $ facebook = new TransientFacebook (array (
159+ $ facebook = new Facebook (array (
124160 'appId ' => self ::APP_ID ,
125161 'secret ' => self ::SECRET ,
126162 ));
@@ -148,30 +184,28 @@ public function testGetLoginURLWithExtraParams() {
148184 }
149185
150186 public function testGetCodeWithValidCSRFState () {
151- $ csrf_cookie_name = FBCode::constructCSRFTokenCookieName (self ::APP_ID );
152- $ _COOKIE [$ csrf_cookie_name ] = $ this ->generateMD5HashOfRandomValue ();
153187 $ facebook = new FBCode (array (
154188 'appId ' => self ::APP_ID ,
155189 'secret ' => self ::SECRET ,
156190 ));
157191
192+ $ facebook ->setCSRFStateToken ();
158193 $ code = $ _REQUEST ['code ' ] = $ this ->generateMD5HashOfRandomValue ();
159- $ _REQUEST ['state ' ] = $ _COOKIE [ $ csrf_cookie_name ] ;
194+ $ _REQUEST ['state ' ] = $ facebook -> getCSRFStateToken () ;
160195 $ this ->assertEquals ($ code ,
161196 $ facebook ->publicGetCode (),
162197 'Expect code to be pulled from $_REQUEST[ \'code \'] ' );
163198 }
164199
165200 public function testGetCodeWithInvalidCSRFState () {
166- $ csrf_cookie_name = FBCode::constructCSRFTokenCookieName (self ::APP_ID );
167- $ _COOKIE [$ csrf_cookie_name ] = $ this ->generateMD5HashOfRandomValue ();
168201 $ facebook = new FBCode (array (
169202 'appId ' => self ::APP_ID ,
170203 'secret ' => self ::SECRET ,
171204 ));
172205
206+ $ facebook ->setCSRFStateToken ();
173207 $ code = $ _REQUEST ['code ' ] = $ this ->generateMD5HashOfRandomValue ();
174- $ _REQUEST ['state ' ] = $ _COOKIE [ $ csrf_cookie_name ]. " forgery!!! " ;
208+ $ _REQUEST ['state ' ] = $ facebook -> getCSRFStateToken (). ' forgery!!! ' ;
175209 $ this ->assertFalse ($ facebook ->publicGetCode (),
176210 'Expect getCode to fail, CSRF state should not match. ' );
177211 }
@@ -183,7 +217,7 @@ public function testGetCodeWithMissingCSRFState() {
183217 ));
184218
185219 $ code = $ _REQUEST ['code ' ] = $ this ->generateMD5HashOfRandomValue ();
186- // don't set $_REQUEST['fbcsrf_<app-id>']
220+ // intentionally don't set CSRF token at all
187221 $ this ->assertFalse ($ facebook ->publicGetCode (),
188222 'Expect getCode to fail, CSRF state not sent back. ' );
189223
@@ -562,9 +596,20 @@ public function testAppSecretCall() {
562596 'appId ' => self ::APP_ID ,
563597 'secret ' => self ::SECRET ,
564598 ));
565- $ response = $ facebook ->api ('/ ' . self ::APP_ID . '/insights ' );
566- $ this ->assertTrue (count ($ response ['data ' ]) > 0 ,
567- 'Expect some data back. ' );
599+
600+ $ proper_exception_thrown = false ;
601+ try {
602+ $ response = $ facebook ->api ('/ ' . self ::APP_ID . '/insights ' );
603+ $ this ->fail ('Desktop applications need a user token for insights. ' );
604+ } catch (FacebookApiException $ e ) {
605+ $ proper_exception_thrown =
606+ strpos ($ e ->getMessage (),
607+ 'Requires session when calling from a desktop app ' ) !== false ;
608+ } catch (Exception $ e ) {}
609+
610+ $ this ->assertTrue ($ proper_exception_thrown ,
611+ 'Incorrect exception type thrown when trying to gain ' .
612+ 'insights for desktop app without a user access token. ' );
568613 }
569614
570615 public function testBase64UrlEncode () {
@@ -734,6 +779,7 @@ protected function setPersistentData($key, $value) {}
734779 protected function getPersistentData ($ key , $ default = false ) {
735780 return $ default ;
736781 }
782+ protected function clearPersistentData ($ key ) {}
737783 protected function clearAllPersistentData () {}
738784}
739785
@@ -762,18 +808,23 @@ class PersistentFBPublic extends Facebook {
762808 public function publicParseSignedRequest ($ input ) {
763809 return $ this ->parseSignedRequest ($ input );
764810 }
811+
765812 public function publicSetPersistentData ($ key , $ value ) {
766813 $ this ->setPersistentData ($ key , $ value );
767814 }
768815}
769816
770- class FBCode extends TransientFacebook {
817+ class FBCode extends Facebook {
771818 public function publicGetCode () {
772819 return $ this ->getCode ();
773820 }
774821
775- public static function constructCSRFTokenCookieName ($ app_id ) {
776- return 'fbcsrf_ ' .$ app_id ;
822+ public function setCSRFStateToken () {
823+ $ this ->establishCSRFTokenState ();
824+ }
825+
826+ public function getCSRFStateToken () {
827+ return $ this ->getPersistentData ('state ' );
777828 }
778829}
779830
@@ -782,3 +833,9 @@ public function publicGetApplicationAccessToken() {
782833 return $ this ->getApplicationAccessToken ();
783834 }
784835}
836+
837+ class FBGetCurrentURLFacebook extends TransientFacebook {
838+ public function publicGetCurrentUrl () {
839+ return $ this ->getCurrentUrl ();
840+ }
841+ }
0 commit comments