From 677953067f2bb5502a70f0d004f1ac844b18a128 Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 16 Jan 2017 22:04:43 +0100 Subject: * Facebook support --- .../tests/Url/FacebookUrlDetectionHandlerTest.php | 134 +++++++++++++ .../tests/Url/FacebookUrlManipulatorTest.php | 217 +++++++++++++++++++++ 2 files changed, 351 insertions(+) create mode 100644 lib/facebook-graph-sdk/tests/Url/FacebookUrlDetectionHandlerTest.php create mode 100644 lib/facebook-graph-sdk/tests/Url/FacebookUrlManipulatorTest.php (limited to 'lib/facebook-graph-sdk/tests/Url') diff --git a/lib/facebook-graph-sdk/tests/Url/FacebookUrlDetectionHandlerTest.php b/lib/facebook-graph-sdk/tests/Url/FacebookUrlDetectionHandlerTest.php new file mode 100644 index 0000000..c3127ef --- /dev/null +++ b/lib/facebook-graph-sdk/tests/Url/FacebookUrlDetectionHandlerTest.php @@ -0,0 +1,134 @@ + 'foo.bar', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => '/baz?foo=123', + ]; + + $urlHandler = new FacebookUrlDetectionHandler(); + $currentUri = $urlHandler->getCurrentUrl(); + + $this->assertEquals('http://foo.bar/baz?foo=123', $currentUri); + } + + public function testProperlyGeneratesSecureUrlFromCommonScenario() + { + $_SERVER = [ + 'HTTP_HOST' => 'foo.bar', + 'SERVER_PORT' => '443', + 'REQUEST_URI' => '/baz?foo=123', + ]; + + $urlHandler = new FacebookUrlDetectionHandler(); + $currentUri = $urlHandler->getCurrentUrl(); + + $this->assertEquals('https://foo.bar/baz?foo=123', $currentUri); + } + + public function testProperlyGeneratesUrlFromProxy() + { + $_SERVER = [ + 'HTTP_X_FORWARDED_PORT' => '80', + 'HTTP_X_FORWARDED_PROTO' => 'http', + 'HTTP_HOST' => 'foo.bar', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => '/baz?foo=123', + ]; + + $urlHandler = new FacebookUrlDetectionHandler(); + $currentUri = $urlHandler->getCurrentUrl(); + + $this->assertEquals('http://foo.bar/baz?foo=123', $currentUri); + } + + public function testProperlyGeneratesSecureUrlFromProxy() + { + $_SERVER = [ + 'HTTP_X_FORWARDED_PORT' => '443', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_HOST' => 'foo.bar', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => '/baz?foo=123', + ]; + + $urlHandler = new FacebookUrlDetectionHandler(); + $currentUri = $urlHandler->getCurrentUrl(); + + $this->assertEquals('https://foo.bar/baz?foo=123', $currentUri); + } + + public function testProperlyGeneratesUrlWithCustomPort() + { + $_SERVER = [ + 'HTTP_HOST' => 'foo.bar', + 'SERVER_PORT' => '1337', + 'REQUEST_URI' => '/foo.php', + ]; + + $urlHandler = new FacebookUrlDetectionHandler(); + $currentUri = $urlHandler->getCurrentUrl(); + + $this->assertEquals('http://foo.bar:1337/foo.php', $currentUri); + } + + public function testProperlyGeneratesSecureUrlWithCustomPort() + { + $_SERVER = [ + 'HTTP_HOST' => 'foo.bar', + 'SERVER_PORT' => '1337', + 'REQUEST_URI' => '/foo.php', + 'HTTPS' => 'On', + ]; + + $urlHandler = new FacebookUrlDetectionHandler(); + $currentUri = $urlHandler->getCurrentUrl(); + + $this->assertEquals('https://foo.bar:1337/foo.php', $currentUri); + } + + public function testProperlyGeneratesUrlWithCustomPortFromProxy() + { + $_SERVER = [ + 'HTTP_X_FORWARDED_PORT' => '8888', + 'HTTP_X_FORWARDED_PROTO' => 'http', + 'HTTP_HOST' => 'foo.bar', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => '/foo.php', + ]; + + $urlHandler = new FacebookUrlDetectionHandler(); + $currentUri = $urlHandler->getCurrentUrl(); + + $this->assertEquals('http://foo.bar:8888/foo.php', $currentUri); + } +} diff --git a/lib/facebook-graph-sdk/tests/Url/FacebookUrlManipulatorTest.php b/lib/facebook-graph-sdk/tests/Url/FacebookUrlManipulatorTest.php new file mode 100644 index 0000000..c58e2b3 --- /dev/null +++ b/lib/facebook-graph-sdk/tests/Url/FacebookUrlManipulatorTest.php @@ -0,0 +1,217 @@ +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); + } +} -- cgit v1.2.3