1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
<?php
/**
* Copyright 2017 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
namespace Facebook\Tests\Url;
use Facebook\Url\FacebookUrlManipulator;
class FacebookUrlManipulatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideUris
*/
public function testParamsGetRemovedFromAUrl($dirtyUrl, $expectedCleanUrl)
{
$removeParams = [
'state',
'code',
'error',
'error_reason',
'error_description',
'error_code',
];
$currentUri = FacebookUrlManipulator::removeParamsFromUrl($dirtyUrl, $removeParams);
$this->assertEquals($expectedCleanUrl, $currentUri);
}
public function provideUris()
{
return [
[
'http://localhost/something?state=0000&foo=bar&code=abcd',
'http://localhost/something?foo=bar',
],
[
'https://localhost/something?state=0000&foo=bar&code=abcd',
'https://localhost/something?foo=bar',
],
[
'http://localhost/something?state=0000&foo=bar&error=abcd&error_reason=abcd&error_description=abcd&error_code=1',
'http://localhost/something?foo=bar',
],
[
'https://localhost/something?state=0000&foo=bar&error=abcd&error_reason=abcd&error_description=abcd&error_code=1',
'https://localhost/something?foo=bar',
],
[
'http://localhost/something?state=0000&foo=bar&error=abcd',
'http://localhost/something?foo=bar',
],
[
'https://localhost/something?state=0000&foo=bar&error=abcd',
'https://localhost/something?foo=bar',
],
[
'https://localhost:1337/something?state=0000&foo=bar&error=abcd',
'https://localhost:1337/something?foo=bar',
],
[
'https://localhost:1337/something?state=0000&code=foo',
'https://localhost:1337/something',
],
[
'https://localhost/something/?state=0000&code=foo&foo=bar',
'https://localhost/something/?foo=bar',
],
[
'https://localhost/something/?state=0000&code=foo',
'https://localhost/something/',
],
];
}
public function testGracefullyHandlesUrlAppending()
{
$params = [];
$url = 'https://www.foo.com/';
$processed_url = FacebookUrlManipulator::appendParamsToUrl($url, $params);
$this->assertEquals('https://www.foo.com/', $processed_url);
$params = [
'access_token' => 'foo',
];
$url = 'https://www.foo.com/';
$processed_url = FacebookUrlManipulator::appendParamsToUrl($url, $params);
$this->assertEquals('https://www.foo.com/?access_token=foo', $processed_url);
$params = [
'access_token' => 'foo',
'bar' => 'baz',
];
$url = 'https://www.foo.com/?foo=bar';
$processed_url = FacebookUrlManipulator::appendParamsToUrl($url, $params);
$this->assertEquals('https://www.foo.com/?access_token=foo&bar=baz&foo=bar', $processed_url);
$params = [
'access_token' => 'foo',
];
$url = 'https://www.foo.com/?foo=bar&access_token=bar';
$processed_url = FacebookUrlManipulator::appendParamsToUrl($url, $params);
$this->assertEquals('https://www.foo.com/?access_token=bar&foo=bar', $processed_url);
}
public function testSlashesAreProperlyPrepended()
{
$slashTestOne = FacebookUrlManipulator::forceSlashPrefix('foo');
$slashTestTwo = FacebookUrlManipulator::forceSlashPrefix('/foo');
$slashTestThree = FacebookUrlManipulator::forceSlashPrefix('foo/bar');
$slashTestFour = FacebookUrlManipulator::forceSlashPrefix('/foo/bar');
$slashTestFive = FacebookUrlManipulator::forceSlashPrefix(null);
$slashTestSix = FacebookUrlManipulator::forceSlashPrefix('');
$this->assertEquals('/foo', $slashTestOne);
$this->assertEquals('/foo', $slashTestTwo);
$this->assertEquals('/foo/bar', $slashTestThree);
$this->assertEquals('/foo/bar', $slashTestFour);
$this->assertEquals(null, $slashTestFive);
$this->assertEquals('', $slashTestSix);
}
public function testParamsCanBeReturnedAsArray()
{
$paramsOne = FacebookUrlManipulator::getParamsAsArray('/foo');
$paramsTwo = FacebookUrlManipulator::getParamsAsArray('/foo?one=1&two=2');
$paramsThree = FacebookUrlManipulator::getParamsAsArray('https://www.foo.com');
$paramsFour = FacebookUrlManipulator::getParamsAsArray('https://www.foo.com/?');
$paramsFive = FacebookUrlManipulator::getParamsAsArray('https://www.foo.com/?foo=bar');
$this->assertEquals([], $paramsOne);
$this->assertEquals(['one' => '1', 'two' => '2'], $paramsTwo);
$this->assertEquals([], $paramsThree);
$this->assertEquals([], $paramsFour);
$this->assertEquals(['foo' => 'bar'], $paramsFive);
}
/**
* @dataProvider provideMergableEndpoints
*/
public function testParamsCanBeMergedOntoUrlProperly($urlOne, $urlTwo, $expected)
{
$result = FacebookUrlManipulator::mergeUrlParams($urlOne, $urlTwo);
$this->assertEquals($result, $expected);
}
public function provideMergableEndpoints()
{
return [
[
'https://www.foo.com/?foo=ignore_foo&dance=fun',
'/me?foo=keep_foo',
'/me?dance=fun&foo=keep_foo',
],
[
'https://www.bar.com?',
'https://foo.com?foo=bar',
'https://foo.com?foo=bar',
],
[
'you',
'me',
'me',
],
[
'/1234?swing=fun',
'/1337?bar=baz&west=coast',
'/1337?bar=baz&swing=fun&west=coast',
],
];
}
public function testGraphUrlsCanBeTrimmed()
{
$fullGraphUrl = 'https://graph.facebook.com/';
$baseGraphUrl = FacebookUrlManipulator::baseGraphUrlEndpoint($fullGraphUrl);
$this->assertEquals('/', $baseGraphUrl);
$fullGraphUrl = 'https://graph.facebook.com/v1.0/';
$baseGraphUrl = FacebookUrlManipulator::baseGraphUrlEndpoint($fullGraphUrl);
$this->assertEquals('/', $baseGraphUrl);
$fullGraphUrl = 'https://graph.facebook.com/me';
$baseGraphUrl = FacebookUrlManipulator::baseGraphUrlEndpoint($fullGraphUrl);
$this->assertEquals('/me', $baseGraphUrl);
$fullGraphUrl = 'https://graph.beta.facebook.com/me';
$baseGraphUrl = FacebookUrlManipulator::baseGraphUrlEndpoint($fullGraphUrl);
$this->assertEquals('/me', $baseGraphUrl);
$fullGraphUrl = 'https://whatever-they-want.facebook.com/v2.1/me';
$baseGraphUrl = FacebookUrlManipulator::baseGraphUrlEndpoint($fullGraphUrl);
$this->assertEquals('/me', $baseGraphUrl);
$fullGraphUrl = 'https://graph.facebook.com/v5.301/1233?foo=bar';
$baseGraphUrl = FacebookUrlManipulator::baseGraphUrlEndpoint($fullGraphUrl);
$this->assertEquals('/1233?foo=bar', $baseGraphUrl);
}
}
|