@@ -94,8 +94,44 @@ public function testSetFileUploadSupport() {
94
94
'Expect file upload support to be on. ' );
95
95
}
96
96
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
+
97
133
public function testGetLoginURL () {
98
- $ facebook = new TransientFacebook (array (
134
+ $ facebook = new Facebook (array (
99
135
'appId ' => self ::APP_ID ,
100
136
'secret ' => self ::SECRET ,
101
137
));
@@ -120,7 +156,7 @@ public function testGetLoginURL() {
120
156
}
121
157
122
158
public function testGetLoginURLWithExtraParams () {
123
- $ facebook = new TransientFacebook (array (
159
+ $ facebook = new Facebook (array (
124
160
'appId ' => self ::APP_ID ,
125
161
'secret ' => self ::SECRET ,
126
162
));
@@ -148,30 +184,28 @@ public function testGetLoginURLWithExtraParams() {
148
184
}
149
185
150
186
public function testGetCodeWithValidCSRFState () {
151
- $ csrf_cookie_name = FBCode::constructCSRFTokenCookieName (self ::APP_ID );
152
- $ _COOKIE [$ csrf_cookie_name ] = $ this ->generateMD5HashOfRandomValue ();
153
187
$ facebook = new FBCode (array (
154
188
'appId ' => self ::APP_ID ,
155
189
'secret ' => self ::SECRET ,
156
190
));
157
191
192
+ $ facebook ->setCSRFStateToken ();
158
193
$ code = $ _REQUEST ['code ' ] = $ this ->generateMD5HashOfRandomValue ();
159
- $ _REQUEST ['state ' ] = $ _COOKIE [ $ csrf_cookie_name ] ;
194
+ $ _REQUEST ['state ' ] = $ facebook -> getCSRFStateToken () ;
160
195
$ this ->assertEquals ($ code ,
161
196
$ facebook ->publicGetCode (),
162
197
'Expect code to be pulled from $_REQUEST[ \'code \'] ' );
163
198
}
164
199
165
200
public function testGetCodeWithInvalidCSRFState () {
166
- $ csrf_cookie_name = FBCode::constructCSRFTokenCookieName (self ::APP_ID );
167
- $ _COOKIE [$ csrf_cookie_name ] = $ this ->generateMD5HashOfRandomValue ();
168
201
$ facebook = new FBCode (array (
169
202
'appId ' => self ::APP_ID ,
170
203
'secret ' => self ::SECRET ,
171
204
));
172
205
206
+ $ facebook ->setCSRFStateToken ();
173
207
$ code = $ _REQUEST ['code ' ] = $ this ->generateMD5HashOfRandomValue ();
174
- $ _REQUEST ['state ' ] = $ _COOKIE [ $ csrf_cookie_name ]. " forgery!!! " ;
208
+ $ _REQUEST ['state ' ] = $ facebook -> getCSRFStateToken (). ' forgery!!! ' ;
175
209
$ this ->assertFalse ($ facebook ->publicGetCode (),
176
210
'Expect getCode to fail, CSRF state should not match. ' );
177
211
}
@@ -183,7 +217,7 @@ public function testGetCodeWithMissingCSRFState() {
183
217
));
184
218
185
219
$ code = $ _REQUEST ['code ' ] = $ this ->generateMD5HashOfRandomValue ();
186
- // don't set $_REQUEST['fbcsrf_<app-id>']
220
+ // intentionally don't set CSRF token at all
187
221
$ this ->assertFalse ($ facebook ->publicGetCode (),
188
222
'Expect getCode to fail, CSRF state not sent back. ' );
189
223
@@ -562,9 +596,20 @@ public function testAppSecretCall() {
562
596
'appId ' => self ::APP_ID ,
563
597
'secret ' => self ::SECRET ,
564
598
));
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. ' );
568
613
}
569
614
570
615
public function testBase64UrlEncode () {
@@ -734,6 +779,7 @@ protected function setPersistentData($key, $value) {}
734
779
protected function getPersistentData ($ key , $ default = false ) {
735
780
return $ default ;
736
781
}
782
+ protected function clearPersistentData ($ key ) {}
737
783
protected function clearAllPersistentData () {}
738
784
}
739
785
@@ -762,18 +808,23 @@ class PersistentFBPublic extends Facebook {
762
808
public function publicParseSignedRequest ($ input ) {
763
809
return $ this ->parseSignedRequest ($ input );
764
810
}
811
+
765
812
public function publicSetPersistentData ($ key , $ value ) {
766
813
$ this ->setPersistentData ($ key , $ value );
767
814
}
768
815
}
769
816
770
- class FBCode extends TransientFacebook {
817
+ class FBCode extends Facebook {
771
818
public function publicGetCode () {
772
819
return $ this ->getCode ();
773
820
}
774
821
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 ' );
777
828
}
778
829
}
779
830
@@ -782,3 +833,9 @@ public function publicGetApplicationAccessToken() {
782
833
return $ this ->getApplicationAccessToken ();
783
834
}
784
835
}
836
+
837
+ class FBGetCurrentURLFacebook extends TransientFacebook {
838
+ public function publicGetCurrentUrl () {
839
+ return $ this ->getCurrentUrl ();
840
+ }
841
+ }
0 commit comments