Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #643 from SammyK/patch-608-force-graph-version
Browse files Browse the repository at this point in the history
Remove default Graph API version fallback
  • Loading branch information
SammyK authored Dec 23, 2016
2 parents 3e8719d + 3f9fd16 commit 0b076fb
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/Facebook/Authentication/OAuth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ class OAuth2Client
/**
* @param FacebookApp $app
* @param FacebookClient $client
* @param string|null $graphVersion The version of the Graph API to use.
* @param string $graphVersion The version of the Graph API to use.
*/
public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion = null)
public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion)
{
$this->app = $app;
$this->client = $client;
$this->graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION;
$this->graphVersion = $graphVersion;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Facebook/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ class Facebook
*/
const VERSION = '6.0-dev';

/**
* @const string Default Graph API version for requests.
*/
const DEFAULT_GRAPH_VERSION = 'v2.7';

/**
* @const string The name of the environment variable that contains the app ID.
*/
Expand Down Expand Up @@ -120,7 +115,7 @@ public function __construct(array $config = [])
$config = array_merge([
'app_id' => getenv(static::APP_ID_ENV_NAME),
'app_secret' => getenv(static::APP_SECRET_ENV_NAME),
'default_graph_version' => static::DEFAULT_GRAPH_VERSION,
'default_graph_version' => null,
'enable_beta_mode' => false,
'http_client_handler' => null,
'persistent_data_handler' => null,
Expand All @@ -133,6 +128,9 @@ public function __construct(array $config = [])
if (!$config['app_secret']) {
throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"');
}
if (!$config['default_graph_version']) {
throw new \InvalidArgumentException('Required "default_graph_version" key not supplied in config');
}

$this->app = new FacebookApp($config['app_id'], $config['app_secret']);
$this->client = new FacebookClient(
Expand All @@ -148,7 +146,6 @@ public function __construct(array $config = [])
$this->setDefaultAccessToken($config['default_access_token']);
}

// @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set
$this->defaultGraphVersion = $config['default_graph_version'];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Facebook/FacebookRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function __construct(FacebookApp $app = null, $accessToken = null, $metho
$this->setEndpoint($endpoint);
$this->setParams($params);
$this->setETag($eTag);
$this->graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION;
$this->graphVersion = $graphVersion;
}

/**
Expand Down Expand Up @@ -490,7 +490,7 @@ public function getPostParams()
/**
* The graph version used for this request.
*
* @return string
* @return string|null
*/
public function getGraphVersion()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Facebook/Helpers/FacebookPageTabHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class FacebookPageTabHelper extends FacebookCanvasHelper
*
* @param FacebookApp $app The FacebookApp entity.
* @param FacebookClient $client The client to make HTTP requests.
* @param string|null $graphVersion The version of Graph to use.
* @param string $graphVersion The version of Graph to use.
*/
public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion = null)
public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion)
{
parent::__construct($app, $client, $graphVersion);

Expand Down
5 changes: 2 additions & 3 deletions src/Facebook/Helpers/FacebookSignedRequestFromInputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ abstract class FacebookSignedRequestFromInputHelper
*
* @param FacebookApp $app The FacebookApp entity.
* @param FacebookClient $client The client to make HTTP requests.
* @param string|null $graphVersion The version of Graph to use.
* @param string $graphVersion The version of Graph to use.
*/
public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion = null)
public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion)
{
$this->app = $app;
$graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION;
$this->oAuth2Client = new OAuth2Client($this->app, $client, $graphVersion);

$this->instantiateSignedRequest();
Expand Down
4 changes: 2 additions & 2 deletions src/Facebook/Url/FacebookUrlManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ public static function mergeUrlParams($urlToStealFrom, $urlToAddTo)
*
* @param string|null $string
*
* @return string|null
* @return string
*/
public static function forceSlashPrefix($string)
{
if (!$string) {
return $string;
return '';
}

return strpos($string, '/') === 0 ? $string : '/' . $string;
Expand Down
20 changes: 8 additions & 12 deletions tests/FacebookBatchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
namespace Facebook\Tests;

use Facebook\Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
use Facebook\FacebookBatchRequest;
Expand Down Expand Up @@ -216,15 +215,14 @@ public function testBatchRequestEntitiesProperlyGetConvertedToAnArray($request,
public function requestsAndExpectedResponsesProvider()
{
$headers = $this->defaultHeaders();
$apiVersion = Facebook::DEFAULT_GRAPH_VERSION;

return [
[
new FacebookRequest(null, null, 'GET', '/foo', ['foo' => 'bar']),
[
'headers' => $headers,
'method' => 'GET',
'relative_url' => '/' . $apiVersion . '/foo?foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
'relative_url' => '/foo?foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
'name' => 'foo_name',
],
],
Expand All @@ -233,7 +231,7 @@ public function requestsAndExpectedResponsesProvider()
[
'headers' => $headers,
'method' => 'POST',
'relative_url' => '/' . $apiVersion . '/bar',
'relative_url' => '/bar',
'body' => 'bar=baz&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
'name' => 'foo_name',
],
Expand All @@ -243,7 +241,7 @@ public function requestsAndExpectedResponsesProvider()
[
'headers' => $headers,
'method' => 'DELETE',
'relative_url' => '/' . $apiVersion . '/bar?access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
'relative_url' => '/bar?access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
'name' => 'foo_name',
],
],
Expand Down Expand Up @@ -273,7 +271,7 @@ public function testBatchRequestsWithFilesGetConvertedToAnArray()
$this->assertEquals([
'headers' => $this->defaultHeaders(),
'method' => 'POST',
'relative_url' => '/' . Facebook::DEFAULT_GRAPH_VERSION . '/bar',
'relative_url' => '/bar',
'body' => 'message=foobar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
'name' => 'foo_name',
'attached_files' => $attachedFiles,
Expand All @@ -290,10 +288,9 @@ public function testPreppingABatchRequestProperlySetsThePostParams()
$params = $batchRequest->getParams();

$expectedHeaders = json_encode($this->defaultHeaders());
$version = Facebook::DEFAULT_GRAPH_VERSION;
$expectedBatchParams = [
'batch' => '[{"headers":' . $expectedHeaders . ',"method":"GET","relative_url":"\\/' . $version . '\\/foo?access_token=bar_token&appsecret_proof=2ceec40b7b9fd7d38fff1767b766bcc6b1f9feb378febac4612c156e6a8354bd","name":"foo_name"},'
. '{"headers":' . $expectedHeaders . ',"method":"POST","relative_url":"\\/' . $version . '\\/bar","body":"foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9"}]',
'batch' => '[{"headers":' . $expectedHeaders . ',"method":"GET","relative_url":"\\/foo?access_token=bar_token&appsecret_proof=2ceec40b7b9fd7d38fff1767b766bcc6b1f9feb378febac4612c156e6a8354bd","name":"foo_name"},'
. '{"headers":' . $expectedHeaders . ',"method":"POST","relative_url":"\\/bar","body":"foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9"}]',
'include_headers' => true,
'access_token' => 'foo_token',
'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
Expand All @@ -317,10 +314,9 @@ public function testPreppingABatchRequestProperlyMovesTheFiles()
$attachedFiles = implode(',', array_keys($files));

$expectedHeaders = json_encode($this->defaultHeaders());
$version = Facebook::DEFAULT_GRAPH_VERSION;
$expectedBatchParams = [
'batch' => '[{"headers":' . $expectedHeaders . ',"method":"GET","relative_url":"\\/' . $version . '\\/foo?access_token=bar_token&appsecret_proof=2ceec40b7b9fd7d38fff1767b766bcc6b1f9feb378febac4612c156e6a8354bd","name":"foo_name"},'
. '{"headers":' . $expectedHeaders . ',"method":"POST","relative_url":"\\/' . $version . '\\/me\\/photos","body":"message=foobar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9","attached_files":"' . $attachedFiles . '"}]',
'batch' => '[{"headers":' . $expectedHeaders . ',"method":"GET","relative_url":"\\/foo?access_token=bar_token&appsecret_proof=2ceec40b7b9fd7d38fff1767b766bcc6b1f9feb378febac4612c156e6a8354bd","name":"foo_name"},'
. '{"headers":' . $expectedHeaders . ',"method":"POST","relative_url":"\\/me\\/photos","body":"message=foobar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9","attached_files":"' . $attachedFiles . '"}]',
'include_headers' => true,
'access_token' => 'foo_token',
'appsecret_proof' => 'df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9',
Expand Down
3 changes: 1 addition & 2 deletions tests/FacebookClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace Facebook\Tests;

use Facebook\Exceptions\FacebookSDKException;
use Facebook\Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
use Facebook\FacebookBatchRequest;
Expand Down Expand Up @@ -165,7 +164,7 @@ public function testAFacebookBatchRequestWillProperlyBatchFiles()

list($url, $method, $headers, $body) = $this->fbClient->prepareRequestMessage($fbBatchRequest);

$this->assertEquals(FacebookClient::BASE_GRAPH_VIDEO_URL . '/' . Facebook::DEFAULT_GRAPH_VERSION, $url);
$this->assertEquals(FacebookClient::BASE_GRAPH_VIDEO_URL, $url);
$this->assertEquals('POST', $method);
$this->assertContains('multipart/form-data; boundary=', $headers['Content-Type']);
$this->assertContains('Content-Disposition: form-data; name="batch"', $body);
Expand Down
9 changes: 4 additions & 5 deletions tests/FacebookRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
namespace Facebook\Tests;

use Facebook\Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
use Facebook\FileUpload\FacebookFile;
Expand Down Expand Up @@ -125,14 +124,14 @@ public function testAProperUrlWillBeGenerated()

$getUrl = $getRequest->getUrl();
$expectedParams = 'foo=bar&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9';
$expectedUrl = '/' . Facebook::DEFAULT_GRAPH_VERSION . '/foo?' . $expectedParams;
$expectedUrl = '/foo?' . $expectedParams;

$this->assertEquals($expectedUrl, $getUrl);

$postRequest = new FacebookRequest($app, 'foo_token', 'POST', '/bar', ['foo' => 'bar']);
$postRequest = new FacebookRequest($app, 'foo_token', 'POST', '/bar', ['foo' => 'bar'], null, 'v0.0');

$postUrl = $postRequest->getUrl();
$expectedUrl = '/' . Facebook::DEFAULT_GRAPH_VERSION . '/bar';
$expectedUrl = '/v0.0/bar';

$this->assertEquals($expectedUrl, $postUrl);
}
Expand All @@ -156,7 +155,7 @@ public function testAuthenticationParamsAreStrippedAndReapplied()
$url = $request->getUrl();

$expectedParams = 'bar=baz&access_token=foo_token&appsecret_proof=df4256903ba4e23636cc142117aa632133d75c642bd2a68955be1443bd14deb9';
$expectedUrl = '/' . Facebook::DEFAULT_GRAPH_VERSION . '/foo?' . $expectedParams;
$expectedUrl = '/foo?' . $expectedParams;
$this->assertEquals($expectedUrl, $url);

$params = $request->getParams();
Expand Down
16 changes: 15 additions & 1 deletion tests/FacebookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use Facebook\Authentication\AccessToken;
use Facebook\GraphNodes\GraphEdge;
use Facebook\Tests\Fixtures\FakeGraphApiForResumableUpload;
use Facebook\Tests\Fixtures\FooBarPseudoRandomStringGenerator;
use Facebook\Tests\Fixtures\FooClientInterface;
use Facebook\Tests\Fixtures\FooPersistentDataInterface;
use Facebook\Tests\Fixtures\FooUrlDetectionInterface;
Expand All @@ -39,6 +38,7 @@ class FacebookTest extends \PHPUnit_Framework_TestCase
protected $config = [
'app_id' => '1337',
'app_secret' => 'foo_secret',
'default_graph_version' => 'v0.0',
];

/**
Expand All @@ -50,6 +50,7 @@ public function testInstantiatingWithoutAppIdThrows()
putenv(Facebook::APP_ID_ENV_NAME.'=');
$config = [
'app_secret' => 'foo_secret',
'default_graph_version' => 'v0.0',
];
new Facebook($config);
}
Expand All @@ -63,6 +64,19 @@ public function testInstantiatingWithoutAppSecretThrows()
putenv(Facebook::APP_SECRET_ENV_NAME.'=');
$config = [
'app_id' => 'foo_id',
'default_graph_version' => 'v0.0',
];
new Facebook($config);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInstantiatingWithoutDefaultGraphVersionThrows()
{
$config = [
'app_id' => 'foo_id',
'app_secret' => 'foo_secret',
];
new Facebook($config);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Helpers/FacebookCanvasHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use Facebook\FacebookApp;
use Facebook\FacebookClient;
use Facebook\Tests\FacebookTest;
use Facebook\Helpers\FacebookCanvasHelper;

class FacebookCanvasHelperTest extends \PHPUnit_Framework_TestCase
Expand All @@ -39,7 +40,7 @@ class FacebookCanvasHelperTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$app = new FacebookApp('123', 'foo_app_secret');
$this->helper = new FacebookCanvasHelper($app, new FacebookClient());
$this->helper = new FacebookCanvasHelper($app, new FacebookClient(), 'v0.0');
}

public function testSignedRequestDataCanBeRetrievedFromPostData()
Expand Down
3 changes: 2 additions & 1 deletion tests/Helpers/FacebookJavaScriptHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use Facebook\FacebookApp;
use Facebook\FacebookClient;
use Facebook\Tests\FacebookTest;
use Facebook\Helpers\FacebookJavaScriptHelper;

class FacebookJavaScriptHelperTest extends \PHPUnit_Framework_TestCase
Expand All @@ -36,7 +37,7 @@ public function testARawSignedRequestCanBeRetrievedFromCookieData()
$_COOKIE['fbsr_123'] = $this->rawSignedRequestAuthorized;

$app = new FacebookApp('123', 'foo_app_secret');
$helper = new FacebookJavaScriptHelper($app, new FacebookClient());
$helper = new FacebookJavaScriptHelper($app, new FacebookClient(), 'v0.0');

$rawSignedRequest = $helper->getRawSignedRequest();

Expand Down
3 changes: 2 additions & 1 deletion tests/Helpers/FacebookPageTabHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use Facebook\FacebookApp;
use Facebook\FacebookClient;
use Facebook\Tests\FacebookTest;
use Facebook\Helpers\FacebookPageTabHelper;

class FacebookPageTabHelperTest extends \PHPUnit_Framework_TestCase
Expand All @@ -36,7 +37,7 @@ public function testPageDataCanBeAccessed()
$_POST['signed_request'] = $this->rawSignedRequestAuthorized;

$app = new FacebookApp('123', 'foo_app_secret');
$helper = new FacebookPageTabHelper($app, new FacebookClient());
$helper = new FacebookPageTabHelper($app, new FacebookClient(), 'v0.0');

$this->assertFalse($helper->isAdmin());
$this->assertEquals('42', $helper->getPageId());
Expand Down
3 changes: 2 additions & 1 deletion tests/Helpers/FacebookSignedRequestFromInputHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace Facebook\Tests\Helpers;

use Facebook\FacebookApp;
use Facebook\Tests\FacebookTest;
use Facebook\Tests\Fixtures\FooSignedRequestHelper;
use Facebook\Tests\Fixtures\FooSignedRequestHelperFacebookClient;

Expand All @@ -41,7 +42,7 @@ class FacebookSignedRequestFromInputHelperTest extends \PHPUnit_Framework_TestCa
protected function setUp()
{
$app = new FacebookApp('123', 'foo_app_secret');
$this->helper = new FooSignedRequestHelper($app, new FooSignedRequestHelperFacebookClient());
$this->helper = new FooSignedRequestHelper($app, new FooSignedRequestHelperFacebookClient(), 'v0.0');
}

public function testSignedRequestDataCanBeRetrievedFromPostData()
Expand Down

0 comments on commit 0b076fb

Please sign in to comment.