|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2017 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
|
|
|
namespace Facebook\Tests\Url; |
|
24
|
|
|
|
|
25
|
|
|
use Facebook\Url\UrlManipulator; |
|
26
|
|
|
use PHPUnit\Framework\TestCase; |
|
27
|
|
|
|
|
28
|
|
|
class UrlManipulatorTest extends TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @dataProvider provideUris |
|
32
|
|
|
* |
|
33
|
|
|
* @param mixed $dirtyUrl |
|
34
|
|
|
* @param mixed $expectedCleanUrl |
|
35
|
|
|
*/ |
|
36
|
|
|
public function testParamsGetRemovedFromAUrl($dirtyUrl, $expectedCleanUrl) |
|
37
|
|
|
{ |
|
38
|
|
|
$removeParams = [ |
|
39
|
|
|
'state', |
|
40
|
|
|
'code', |
|
41
|
|
|
'error', |
|
42
|
|
|
'error_reason', |
|
43
|
|
|
'error_description', |
|
44
|
|
|
'error_code', |
|
45
|
|
|
]; |
|
46
|
|
|
$currentUri = UrlManipulator::removeParamsFromUrl($dirtyUrl, $removeParams); |
|
47
|
|
|
$this->assertEquals($expectedCleanUrl, $currentUri); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function provideUris() |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
[ |
|
54
|
|
|
'http://localhost/something?state=0000&foo=bar&code=abcd', |
|
55
|
|
|
'http://localhost/something?foo=bar', |
|
56
|
|
|
], |
|
57
|
|
|
[ |
|
58
|
|
|
'https://localhost/something?state=0000&foo=bar&code=abcd', |
|
59
|
|
|
'https://localhost/something?foo=bar', |
|
60
|
|
|
], |
|
61
|
|
|
[ |
|
62
|
|
|
'http://localhost/something?state=0000&foo=bar&error=abcd&error_reason=abcd&error_description=abcd&error_code=1', |
|
63
|
|
|
'http://localhost/something?foo=bar', |
|
64
|
|
|
], |
|
65
|
|
|
[ |
|
66
|
|
|
'https://localhost/something?state=0000&foo=bar&error=abcd&error_reason=abcd&error_description=abcd&error_code=1', |
|
67
|
|
|
'https://localhost/something?foo=bar', |
|
68
|
|
|
], |
|
69
|
|
|
[ |
|
70
|
|
|
'http://localhost/something?state=0000&foo=bar&error=abcd', |
|
71
|
|
|
'http://localhost/something?foo=bar', |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'https://localhost/something?state=0000&foo=bar&error=abcd', |
|
75
|
|
|
'https://localhost/something?foo=bar', |
|
76
|
|
|
], |
|
77
|
|
|
[ |
|
78
|
|
|
'https://localhost:1337/something?state=0000&foo=bar&error=abcd', |
|
79
|
|
|
'https://localhost:1337/something?foo=bar', |
|
80
|
|
|
], |
|
81
|
|
|
[ |
|
82
|
|
|
'https://localhost:1337/something?state=0000&code=foo', |
|
83
|
|
|
'https://localhost:1337/something', |
|
84
|
|
|
], |
|
85
|
|
|
[ |
|
86
|
|
|
'https://localhost/something/?state=0000&code=foo&foo=bar', |
|
87
|
|
|
'https://localhost/something/?foo=bar', |
|
88
|
|
|
], |
|
89
|
|
|
[ |
|
90
|
|
|
'https://localhost/something/?state=0000&code=foo', |
|
91
|
|
|
'https://localhost/something/', |
|
92
|
|
|
], |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testGracefullyHandlesUrlAppending() |
|
97
|
|
|
{ |
|
98
|
|
|
$params = []; |
|
99
|
|
|
$url = 'https://www.foo.com/'; |
|
100
|
|
|
$processed_url = UrlManipulator::appendParamsToUrl($url, $params); |
|
101
|
|
|
$this->assertEquals('https://www.foo.com/', $processed_url); |
|
102
|
|
|
|
|
103
|
|
|
$params = [ |
|
104
|
|
|
'access_token' => 'foo', |
|
105
|
|
|
]; |
|
106
|
|
|
$url = 'https://www.foo.com/'; |
|
107
|
|
|
$processed_url = UrlManipulator::appendParamsToUrl($url, $params); |
|
108
|
|
|
$this->assertEquals('https://www.foo.com/?access_token=foo', $processed_url); |
|
109
|
|
|
|
|
110
|
|
|
$params = [ |
|
111
|
|
|
'access_token' => 'foo', |
|
112
|
|
|
'bar' => 'baz', |
|
113
|
|
|
]; |
|
114
|
|
|
$url = 'https://www.foo.com/?foo=bar'; |
|
115
|
|
|
$processed_url = UrlManipulator::appendParamsToUrl($url, $params); |
|
116
|
|
|
$this->assertEquals('https://www.foo.com/?access_token=foo&bar=baz&foo=bar', $processed_url); |
|
117
|
|
|
|
|
118
|
|
|
$params = [ |
|
119
|
|
|
'access_token' => 'foo', |
|
120
|
|
|
]; |
|
121
|
|
|
$url = 'https://www.foo.com/?foo=bar&access_token=bar'; |
|
122
|
|
|
$processed_url = UrlManipulator::appendParamsToUrl($url, $params); |
|
123
|
|
|
$this->assertEquals('https://www.foo.com/?access_token=bar&foo=bar', $processed_url); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testSlashesAreProperlyPrepended() |
|
127
|
|
|
{ |
|
128
|
|
|
$slashTestOne = UrlManipulator::forceSlashPrefix('foo'); |
|
129
|
|
|
$slashTestTwo = UrlManipulator::forceSlashPrefix('/foo'); |
|
130
|
|
|
$slashTestThree = UrlManipulator::forceSlashPrefix('foo/bar'); |
|
131
|
|
|
$slashTestFour = UrlManipulator::forceSlashPrefix('/foo/bar'); |
|
132
|
|
|
$slashTestFive = UrlManipulator::forceSlashPrefix(null); |
|
133
|
|
|
$slashTestSix = UrlManipulator::forceSlashPrefix(''); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertEquals('/foo', $slashTestOne); |
|
136
|
|
|
$this->assertEquals('/foo', $slashTestTwo); |
|
137
|
|
|
$this->assertEquals('/foo/bar', $slashTestThree); |
|
138
|
|
|
$this->assertEquals('/foo/bar', $slashTestFour); |
|
139
|
|
|
$this->assertEquals(null, $slashTestFive); |
|
140
|
|
|
$this->assertEquals('', $slashTestSix); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testParamsCanBeReturnedAsArray() |
|
144
|
|
|
{ |
|
145
|
|
|
$paramsOne = UrlManipulator::getParamsAsArray('/foo'); |
|
146
|
|
|
$paramsTwo = UrlManipulator::getParamsAsArray('/foo?one=1&two=2'); |
|
147
|
|
|
$paramsThree = UrlManipulator::getParamsAsArray('https://www.foo.com'); |
|
148
|
|
|
$paramsFour = UrlManipulator::getParamsAsArray('https://www.foo.com/?'); |
|
149
|
|
|
$paramsFive = UrlManipulator::getParamsAsArray('https://www.foo.com/?foo=bar'); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertEquals([], $paramsOne); |
|
152
|
|
|
$this->assertEquals(['one' => '1', 'two' => '2'], $paramsTwo); |
|
153
|
|
|
$this->assertEquals([], $paramsThree); |
|
154
|
|
|
$this->assertEquals([], $paramsFour); |
|
155
|
|
|
$this->assertEquals(['foo' => 'bar'], $paramsFive); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @dataProvider provideMergableEndpoints |
|
160
|
|
|
* |
|
161
|
|
|
* @param mixed $urlOne |
|
162
|
|
|
* @param mixed $urlTwo |
|
163
|
|
|
* @param mixed $expected |
|
164
|
|
|
*/ |
|
165
|
|
|
public function testParamsCanBeMergedOntoUrlProperly($urlOne, $urlTwo, $expected) |
|
166
|
|
|
{ |
|
167
|
|
|
$result = UrlManipulator::mergeUrlParams($urlOne, $urlTwo); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertEquals($result, $expected); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function provideMergableEndpoints() |
|
173
|
|
|
{ |
|
174
|
|
|
return [ |
|
175
|
|
|
[ |
|
176
|
|
|
'https://www.foo.com/?foo=ignore_foo&dance=fun', |
|
177
|
|
|
'/me?foo=keep_foo', |
|
178
|
|
|
'/me?dance=fun&foo=keep_foo', |
|
179
|
|
|
], |
|
180
|
|
|
[ |
|
181
|
|
|
'https://www.bar.com?', |
|
182
|
|
|
'https://foo.com?foo=bar', |
|
183
|
|
|
'https://foo.com?foo=bar', |
|
184
|
|
|
], |
|
185
|
|
|
[ |
|
186
|
|
|
'you', |
|
187
|
|
|
'me', |
|
188
|
|
|
'me', |
|
189
|
|
|
], |
|
190
|
|
|
[ |
|
191
|
|
|
'/1234?swing=fun', |
|
192
|
|
|
'/1337?bar=baz&west=coast', |
|
193
|
|
|
'/1337?bar=baz&swing=fun&west=coast', |
|
194
|
|
|
], |
|
195
|
|
|
]; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testGraphUrlsCanBeTrimmed() |
|
199
|
|
|
{ |
|
200
|
|
|
$fullGraphUrl = 'https://graph.facebook.com/'; |
|
201
|
|
|
$baseGraphUrl = UrlManipulator::baseGraphUrlEndpoint($fullGraphUrl); |
|
202
|
|
|
$this->assertEquals('/', $baseGraphUrl); |
|
203
|
|
|
|
|
204
|
|
|
$fullGraphUrl = 'https://graph.facebook.com/v1.0/'; |
|
205
|
|
|
$baseGraphUrl = UrlManipulator::baseGraphUrlEndpoint($fullGraphUrl); |
|
206
|
|
|
$this->assertEquals('/', $baseGraphUrl); |
|
207
|
|
|
|
|
208
|
|
|
$fullGraphUrl = 'https://graph.facebook.com/me'; |
|
209
|
|
|
$baseGraphUrl = UrlManipulator::baseGraphUrlEndpoint($fullGraphUrl); |
|
210
|
|
|
$this->assertEquals('/me', $baseGraphUrl); |
|
211
|
|
|
|
|
212
|
|
|
$fullGraphUrl = 'https://graph.beta.facebook.com/me'; |
|
213
|
|
|
$baseGraphUrl = UrlManipulator::baseGraphUrlEndpoint($fullGraphUrl); |
|
214
|
|
|
$this->assertEquals('/me', $baseGraphUrl); |
|
215
|
|
|
|
|
216
|
|
|
$fullGraphUrl = 'https://whatever-they-want.facebook.com/v2.1/me'; |
|
217
|
|
|
$baseGraphUrl = UrlManipulator::baseGraphUrlEndpoint($fullGraphUrl); |
|
218
|
|
|
$this->assertEquals('/me', $baseGraphUrl); |
|
219
|
|
|
|
|
220
|
|
|
$fullGraphUrl = 'https://graph.facebook.com/v5.301/1233?foo=bar'; |
|
221
|
|
|
$baseGraphUrl = UrlManipulator::baseGraphUrlEndpoint($fullGraphUrl); |
|
222
|
|
|
$this->assertEquals('/1233?foo=bar', $baseGraphUrl); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|