From b3c1b81f87a36166bf5a00988e2fb522f74ed531 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 3 Jul 2012 22:50:46 -0430 Subject: [PATCH 01/30] fixing firewall only response to the entry point if there is a query string for facebook auth --- Security/Firewall/FacebookListener.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Security/Firewall/FacebookListener.php b/Security/Firewall/FacebookListener.php index 00ef0ea..a387113 100644 --- a/Security/Firewall/FacebookListener.php +++ b/Security/Firewall/FacebookListener.php @@ -22,6 +22,7 @@ class FacebookListener extends AbstractAuthenticationListener { protected function attemptAuthentication(Request $request) { - return $this->authenticationManager->authenticate(new FacebookUserToken($this->providerKey)); + if(!$request->getQueryString()) + return $this->authenticationManager->authenticate(new FacebookUserToken($this->providerKey)); } } From 960144fa535536c7a4ef8fc270d0ea0981d6c52a Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 5 Aug 2012 15:09:54 -0430 Subject: [PATCH 02/30] upgrading to 2.1 --- Facebook/FacebookSessionPersistence.php | 4 +- README.md | 46 ++++++++----------- Security/Firewall/FacebookListener.php | 4 +- .../Firewall/FacebookListenerTest.php | 4 +- composer.json | 4 +- 5 files changed, 28 insertions(+), 34 deletions(-) diff --git a/Facebook/FacebookSessionPersistence.php b/Facebook/FacebookSessionPersistence.php index a724e90..56c3077 100644 --- a/Facebook/FacebookSessionPersistence.php +++ b/Facebook/FacebookSessionPersistence.php @@ -13,8 +13,8 @@ class FacebookSessionPersistence extends \BaseFacebook { const PREFIX = '_fos_facebook_'; - private $session; - private $prefix; + protected $session; + protected $prefix; protected static $kSupportedKeys = array('state', 'code', 'access_token', 'user_id'); /** diff --git a/README.md b/README.md index 1364948..5ff3133 100644 --- a/README.md +++ b/README.md @@ -32,40 +32,32 @@ http://symfony.com/doc/current/book/security.html [![Build Status](https://secure.travis-ci.org/FriendsOfSymfony/FOSFacebookBundle.png?branch=master)](http://travis-ci.org/FriendsOfSymfony/FOSFacebookBundle) -Installation +Prerequisites ============ - 1. Add this bundle and the Facebook PHP SDK to your ``vendor/`` dir: - * Using the vendors script. - - Add the following lines in your ``deps`` file:: - - [FOSFacebookBundle] - git=git://github.com/FriendsOfSymfony/FOSFacebookBundle.git - target=/bundles/FOS/FacebookBundle - version=origin/master - - [FacebookSDK] - git=git://github.com/facebook/facebook-php-sdk.git - target=/facebook +This version requires Symfony 2.1 - Run the vendors script: - php bin/vendors install +Installation +============ - * Using git submodules. + 1. Add the following lines in your composer.json: - $ git submodule add git://github.com/FriendsOfSymfony/FOSFacebookBundle.git vendor/bundles/FOS/FacebookBundle - $ git submodule add git://github.com/facebook/facebook-php-sdk.git vendor/facebook + ``` +{ + "require": { + "friendsofsymfony/facebook-bundle": "dev-master" + } +} - 2. Add the FOS namespace to your autoloader: +``` + 2. Run the composer to download the bundle - // app/autoload.php - $loader->registerNamespaces(array( - 'FOS' => __DIR__.'/../vendor/bundles', - // your other namespaces - )); +``` bash +$ php composer.phar update friendsofsymfony/facebook-bundle +``` + 3. Add this bundle to your application's kernel: // app/ApplicationKernel.php @@ -157,8 +149,8 @@ Installation fos_facebook: app_url: "http://apps.facebook.com/appName/" server_url: "http://localhost/facebookApp/" - login_path: ^/login - check_path: ^/login_check$ + login_path: /login + check_path: /login_check default_target_path: / provider: my_fos_facebook_provider anonymous: true diff --git a/Security/Firewall/FacebookListener.php b/Security/Firewall/FacebookListener.php index a387113..9cdc4d4 100644 --- a/Security/Firewall/FacebookListener.php +++ b/Security/Firewall/FacebookListener.php @@ -22,7 +22,7 @@ class FacebookListener extends AbstractAuthenticationListener { protected function attemptAuthentication(Request $request) { - if(!$request->getQueryString()) - return $this->authenticationManager->authenticate(new FacebookUserToken($this->providerKey)); + if(!$request->getQueryString()) + return $this->authenticationManager->authenticate(new FacebookUserToken($this->providerKey)); } } diff --git a/Tests/Security/Firewall/FacebookListenerTest.php b/Tests/Security/Firewall/FacebookListenerTest.php index cb6cd10..88a5399 100644 --- a/Tests/Security/Firewall/FacebookListenerTest.php +++ b/Tests/Security/Firewall/FacebookListenerTest.php @@ -25,7 +25,9 @@ public function testThatCanAttemptAuthenticationWithFacebook() $this->getAuthenticationManager(), $this->getMock('Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface'), $this->getHttpUtils(), - 'providerKey' + 'providerKey', + $this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface'), + $this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface') ); $listener->handle($this->getResponseEvent()); } diff --git a/composer.json b/composer.json index d0ebb75..7295c28 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "friendsofsymfony/facebook-bundle", + "name": "bitgandtter/facebook-bundle", "type": "symfony-bundle", "description": "Integrate the Facebook Platform into your Symfony2 application.", "keywords": ["facebook"], @@ -16,7 +16,7 @@ "homepage": "https://github.com/friendsofsymfony/FOSFacebookBundle/contributors" } ], - + "minimum-stability": "dev", "require": { "php": ">=5.3.2", "symfony/framework-bundle": "2.1.*", From e8592bec18a16bb2407b88b0ecfd4109c6087b35 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 8 Oct 2012 22:14:19 -0430 Subject: [PATCH 03/30] allowing only facebook autentication request --- Security/Firewall/FacebookListener.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Security/Firewall/FacebookListener.php b/Security/Firewall/FacebookListener.php index 9cdc4d4..e9982d4 100644 --- a/Security/Firewall/FacebookListener.php +++ b/Security/Firewall/FacebookListener.php @@ -10,7 +10,6 @@ */ namespace FOS\FacebookBundle\Security\Firewall; - use FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken; use Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener; use Symfony\Component\HttpFoundation\Request; @@ -20,9 +19,10 @@ */ class FacebookListener extends AbstractAuthenticationListener { - protected function attemptAuthentication(Request $request) - { - if(!$request->getQueryString()) - return $this->authenticationManager->authenticate(new FacebookUserToken($this->providerKey)); - } + + protected function attemptAuthentication( Request $request ) + { + if ( !$request->getQueryString( ) ) + return $this->authenticationManager->authenticate( new FacebookUserToken( $this->providerKey) ); + } } From 2bb79b2243772b45fd9a1bc9265a5781b304327f Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Tue, 5 Mar 2013 11:12:31 -0430 Subject: [PATCH 04/30] updating composer --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 7295c28..75a4490 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,9 @@ "minimum-stability": "dev", "require": { "php": ">=5.3.2", - "symfony/framework-bundle": "2.1.*", - "symfony/security-bundle": "2.1.*", - "symfony/twig-bundle": "2.1.*", + "symfony/framework-bundle": "~2.1", + "symfony/security-bundle": "~2.1", + "symfony/twig-bundle": "~2.1", "facebook/php-sdk": "dev-master" }, From 52c3e300afeebfcccc34eccd5d41a178af47be2b Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 13 May 2013 15:30:33 -0430 Subject: [PATCH 05/30] updating composer --- composer.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 75a4490..8d79696 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,11 @@ "homepage": "https://github.com/friendsofsymfony/FOSFacebookBundle/contributors" } ], - "minimum-stability": "dev", "require": { "php": ">=5.3.2", - "symfony/framework-bundle": "~2.1", - "symfony/security-bundle": "~2.1", - "symfony/twig-bundle": "~2.1", + "symfony/framework-bundle": "*", + "symfony/security-bundle": "*", + "symfony/twig-bundle": "*", "facebook/php-sdk": "dev-master" }, From ddd7dc936be3b5a820ab16d80e241dcd0e2dec50 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Tue, 14 May 2013 10:34:29 -0430 Subject: [PATCH 06/30] updating composer --- composer.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 8d79696..ebe7034 100644 --- a/composer.json +++ b/composer.json @@ -18,15 +18,13 @@ ], "require": { "php": ">=5.3.2", - "symfony/framework-bundle": "*", - "symfony/security-bundle": "*", - "symfony/twig-bundle": "*", + "symfony/framework-bundle": ">=2.1", + "symfony/security-bundle": ">=2.1", + "symfony/twig-bundle": ">=2.1", "facebook/php-sdk": "dev-master" }, - "autoload": { "psr-0": { "FOS\\FacebookBundle": "" } }, - "target-dir": "FOS/FacebookBundle" } From 9ea56a2a78a13d98fe50c89f2d49ce3ab6297f50 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 20 May 2013 00:28:02 -0430 Subject: [PATCH 07/30] returning all facebook data --- DependencyInjection/Configuration.php | 62 ++- DependencyInjection/FOSFacebookExtension.php | 106 ++--- .../Security/Factory/FacebookFactory.php | 105 +++-- FOSFacebookBundle.php | 16 +- Facebook/FacebookSessionPersistence.php | 193 +++++---- Resources/config/facebook.xml | 50 +-- Resources/config/security.xml | 46 +- Resources/views/initialize.html.php | 29 +- Resources/views/initialize.html.twig | 3 +- Resources/views/loginButton.html.php | 5 +- Resources/views/loginButton.html.twig | 3 +- .../Provider/FacebookProvider.php | 175 ++++---- .../Token/FacebookUserToken.php | 68 +-- .../FacebookAuthenticationEntryPoint.php | 76 ++-- Security/Firewall/FacebookListener.php | 2 +- Security/Logout/FacebookHandler.php | 23 +- Security/User/UserManagerInterface.php | 6 +- Templating/Helper/FacebookHelper.php | 144 +++---- .../DependencyInjection/ConfigurationTest.php | 14 +- .../FOSFacebookExtensionTest.php | 134 +++--- .../Security/Factory/FacebookFactoryTest.php | 180 ++++---- Tests/FacebookApiException.php | 3 +- Tests/Kernel.php | 98 ++--- .../Provider/FacebookProviderTest.php | 397 ++++++++---------- .../Token/FacebookUserTokenTest.php | 59 ++- .../FacebookAuthenticationEntryPointTest.php | 110 ++--- .../Firewall/FacebookListenerTest.php | 141 +++---- .../Templating/Helper/FacebookHelperTest.php | 115 ++--- .../Twig/Extension/FacebookExtensionTest.php | 119 +++--- Tests/bootstrap.php | 7 +- Twig/Extension/FacebookExtension.php | 128 +++--- 31 files changed, 1281 insertions(+), 1336 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index b37049c..e27932b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -10,9 +10,7 @@ */ namespace FOS\FacebookBundle\DependencyInjection; - -use Symfony\Component\Config\Definition\Builder\TreeBuilder, - Symfony\Component\Config\Definition\ConfigurationInterface; +use Symfony\Component\Config\Definition\Builder\TreeBuilder, Symfony\Component\Config\Definition\ConfigurationInterface; /** * This class contains the configuration information for the bundle @@ -24,38 +22,28 @@ */ class Configuration implements ConfigurationInterface { - /** - * Generates the configuration tree. - * - * @return TreeBuilder - */ - public function getConfigTreeBuilder() - { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('fos_facebook'); - - $rootNode - ->fixXmlConfig('permission', 'permissions') - ->children() - ->scalarNode('app_id')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('secret')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('file')->defaultNull()->end() - ->scalarNode('cookie')->defaultFalse()->end() - ->scalarNode('domain')->defaultNull()->end() - ->scalarNode('alias')->defaultNull()->end() - ->scalarNode('logging')->defaultValue('%kernel.debug%')->end() - ->scalarNode('culture')->defaultValue('en_US')->end() - ->arrayNode('class') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('api')->defaultValue('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')->end() - ->scalarNode('helper')->defaultValue('FOS\FacebookBundle\Templating\Helper\FacebookHelper')->end() - ->scalarNode('twig')->defaultValue('FOS\FacebookBundle\Twig\Extension\FacebookExtension')->end() - ->end() - ->end() - ->arrayNode('permissions')->prototype('scalar')->end() - ->end(); - - return $treeBuilder; - } + /** + * Generates the configuration tree. + * + * @return TreeBuilder + */ + + public function getConfigTreeBuilder( ) + { + $treeBuilder = new TreeBuilder( ); + $rootNode = $treeBuilder->root( 'fos_facebook' ); + + $rootNode->fixXmlConfig( 'permission', 'permissions' )->children( )->scalarNode( 'app_id' )->isRequired( ) + ->cannotBeEmpty( )->end( )->scalarNode( 'secret' )->isRequired( )->cannotBeEmpty( )->end( )->scalarNode( 'file' ) + ->defaultNull( )->end( )->scalarNode( 'cookie' )->defaultFalse( )->end( )->scalarNode( 'domain' ) + ->defaultNull( )->end( )->scalarNode( 'alias' )->defaultNull( )->end( )->scalarNode( 'logging' ) + ->defaultValue( '%kernel.debug%' )->end( )->scalarNode( 'culture' )->defaultValue( 'en_US' )->end( ) + ->arrayNode( 'class' )->addDefaultsIfNotSet( )->children( )->scalarNode( 'api' ) + ->defaultValue( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' )->end( )->scalarNode( 'helper' ) + ->defaultValue( 'FOS\FacebookBundle\Templating\Helper\FacebookHelper' )->end( )->scalarNode( 'twig' ) + ->defaultValue( 'FOS\FacebookBundle\Twig\Extension\FacebookExtension' )->end( )->end( )->end( ) + ->arrayNode( 'permissions' )->prototype( 'scalar' )->end( )->end( ); + + return $treeBuilder; + } } diff --git a/DependencyInjection/FOSFacebookExtension.php b/DependencyInjection/FOSFacebookExtension.php index da243b0..9743492 100644 --- a/DependencyInjection/FOSFacebookExtension.php +++ b/DependencyInjection/FOSFacebookExtension.php @@ -10,7 +10,6 @@ */ namespace FOS\FacebookBundle\DependencyInjection; - use Symfony\Component\Config\Definition\Processor; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; @@ -19,62 +18,67 @@ class FOSFacebookExtension extends Extension { - protected $resources = array( - 'facebook' => 'facebook.xml', - 'security' => 'security.xml', - ); - - public function load(array $configs, ContainerBuilder $container) + protected $resources = array( 'facebook' => 'facebook.xml', 'security' => 'security.xml', ); + + public function load( array $configs, ContainerBuilder $container ) + { + $processor = new Processor( ); + $configuration = new Configuration( ); + $config = $processor->processConfiguration( $configuration, $configs ); + + $this->loadDefaults( $container ); + + if ( isset( $config[ 'alias' ] ) ) { - $processor = new Processor(); - $configuration = new Configuration(); - $config = $processor->processConfiguration($configuration, $configs); - - $this->loadDefaults($container); - - if (isset($config['alias'])) { - $container->setAlias($config['alias'], 'fos_facebook.api'); - } - - foreach (array('api', 'helper', 'twig') as $attribute) { - $container->setParameter('fos_facebook.'.$attribute.'.class', $config['class'][$attribute]); - } - - foreach (array('app_id', 'secret', 'cookie', 'domain', 'logging', 'culture', 'permissions') as $attribute) { - $container->setParameter('fos_facebook.'.$attribute, $config[$attribute]); - } - - if (isset($config['file']) && $container->hasDefinition('fos_facebook.api')) { - $facebookApi = $container->getDefinition('fos_facebook.api'); - $facebookApi->setFile($config['file']); - } + $container->setAlias( $config[ 'alias' ], 'fos_facebook.api' ); } - - /** - * @codeCoverageIgnore - */ - public function getXsdValidationBasePath() + + foreach ( array( 'api', 'helper', 'twig' ) as $attribute ) { - return __DIR__ . '/../Resources/config/schema'; + $container->setParameter( 'fos_facebook.' . $attribute . '.class', $config[ 'class' ][ $attribute ] ); } - - /** - * @codeCoverageIgnore - */ - public function getNamespace() + + foreach ( array( 'app_id', 'secret', 'cookie', 'domain', 'logging', 'culture', 'permissions' ) as $attribute ) { - return 'http://symfony.com/schema/dic/fos_facebook'; + $container->setParameter( 'fos_facebook.' . $attribute, $config[ $attribute ] ); } - - /** - * @codeCoverageIgnore - */ - protected function loadDefaults($container) + + if ( isset( $config[ 'file' ] ) && $container->hasDefinition( 'fos_facebook.api' ) ) { - $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - - foreach ($this->resources as $resource) { - $loader->load($resource); - } + $facebookApi = $container->getDefinition( 'fos_facebook.api' ); + $facebookApi->setFile( $config[ 'file' ] ); + } + } + + /** + * @codeCoverageIgnore + */ + + public function getXsdValidationBasePath( ) + { + return __DIR__ . '/../Resources/config/schema'; + } + + /** + * @codeCoverageIgnore + */ + + public function getNamespace( ) + { + return 'http://symfony.com/schema/dic/fos_facebook'; + } + + /** + * @codeCoverageIgnore + */ + + protected function loadDefaults( $container ) + { + $loader = new XmlFileLoader( $container, new FileLocator( __DIR__ . '/../Resources/config')); + + foreach ( $this->resources as $resource ) + { + $loader->load( $resource ); } + } } diff --git a/DependencyInjection/Security/Factory/FacebookFactory.php b/DependencyInjection/Security/Factory/FacebookFactory.php index 67b10cc..5885c7d 100644 --- a/DependencyInjection/Security/Factory/FacebookFactory.php +++ b/DependencyInjection/Security/Factory/FacebookFactory.php @@ -18,60 +18,57 @@ class FacebookFactory extends AbstractFactory { - public function __construct() + + public function __construct( ) + { + $this->addOption( 'display', 'page' ); + $this->addOption( 'app_url' ); + $this->addOption( 'server_url' ); + $this->addOption( 'create_user_if_not_exists', false ); + } + + public function getPosition( ) + { + return 'pre_auth'; + } + + public function getKey( ) + { + return 'fos_facebook'; + } + + protected function getListenerId( ) + { + return 'fos_facebook.security.authentication.listener'; + } + + protected function createAuthProvider( ContainerBuilder $container, $id, $config, $userProviderId ) + { + $authProviderId = 'fos_facebook.auth.' . $id; + + $definition = $container->setDefinition( $authProviderId, new DefinitionDecorator( 'fos_facebook.auth') ) + ->replaceArgument( 0, $id ); + + // with user provider + if ( isset( $config[ 'provider' ] ) ) { - $this->addOption('display', 'page'); - $this->addOption('app_url'); - $this->addOption('server_url'); - $this->addOption('create_user_if_not_exists', false); - } - - public function getPosition() - { - return 'pre_auth'; - } - - public function getKey() - { - return 'fos_facebook'; - } - - protected function getListenerId() - { - return 'fos_facebook.security.authentication.listener'; - } - - protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId) - { - $authProviderId = 'fos_facebook.auth.'.$id; - - $definition = $container - ->setDefinition($authProviderId, new DefinitionDecorator('fos_facebook.auth')) - ->replaceArgument(0, $id); - - // with user provider - if (isset($config['provider'])) { - $definition - ->addArgument(new Reference($userProviderId)) - ->addArgument(new Reference('security.user_checker')) - ->addArgument($config['create_user_if_not_exists']) - ; - } - - return $authProviderId; - } - - protected function createEntryPoint($container, $id, $config, $defaultEntryPointId) - { - $entryPointId = 'fos_facebook.security.authentication.entry_point.'.$id; - $container - ->setDefinition($entryPointId, new DefinitionDecorator('fos_facebook.security.authentication.entry_point')) - ->replaceArgument(1, $config) - ; - - // set options to container for use by other classes - $container->setParameter('fos_facebook.options.'.$id, $config); - - return $entryPointId; + $definition->addArgument( new Reference( $userProviderId) )->addArgument( new Reference( 'security.user_checker') ) + ->addArgument( $config[ 'create_user_if_not_exists' ] ); } + + return $authProviderId; + } + + protected function createEntryPoint( $container, $id, $config, $defaultEntryPointId ) + { + $entryPointId = 'fos_facebook.security.authentication.entry_point.' . $id; + $container + ->setDefinition( $entryPointId, new DefinitionDecorator( 'fos_facebook.security.authentication.entry_point') ) + ->replaceArgument( 1, $config ); + + // set options to container for use by other classes + $container->setParameter( 'fos_facebook.options.' . $id, $config ); + + return $entryPointId; + } } diff --git a/FOSFacebookBundle.php b/FOSFacebookBundle.php index ab9904b..3105194 100644 --- a/FOSFacebookBundle.php +++ b/FOSFacebookBundle.php @@ -10,18 +10,18 @@ */ namespace FOS\FacebookBundle; - use FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; class FOSFacebookBundle extends Bundle { - public function build(ContainerBuilder $container) - { - parent::build($container); - - $extension = $container->getExtension('security'); - $extension->addSecurityListenerFactory(new FacebookFactory()); - } + + public function build( ContainerBuilder $container ) + { + parent::build( $container ); + + $extension = $container->getExtension( 'security' ); + $extension->addSecurityListenerFactory( new FacebookFactory( ) ); + } } diff --git a/Facebook/FacebookSessionPersistence.php b/Facebook/FacebookSessionPersistence.php index 56c3077..96751dd 100644 --- a/Facebook/FacebookSessionPersistence.php +++ b/Facebook/FacebookSessionPersistence.php @@ -1,7 +1,6 @@ session = $session; - $this->prefix = $prefix; - $this->session->start(); - - parent::__construct($config); - } - - /** - * Stores the given ($key, $value) pair, so that future calls to - * getPersistentData($key) return $value. This call may be in another request. - * - * @param string $key - * @param array $value - * - * @return void - */ - protected function setPersistentData($key, $value) + const PREFIX = '_fos_facebook_'; + + protected $session; + protected $prefix; + protected static $kSupportedKeys = array( 'state', 'code', 'access_token', 'user_id' ); + + /** + * @param array $config the application configuration. + * @see BaseFacebook::__construct in facebook.php + */ + + public function __construct( $config, Session $session, $prefix = self::PREFIX ) + { + $this->session = $session; + $this->prefix = $prefix; + $this->session->start( ); + + parent::__construct( $config ); + } + + /** + * Stores the given ($key, $value) pair, so that future calls to + * getPersistentData($key) return $value. This call may be in another request. + * + * @param string $key + * @param array $value + * + * @return void + */ + + protected function setPersistentData( $key, $value ) + { + if ( !in_array( $key, self::$kSupportedKeys ) ) { - if (!in_array($key, self::$kSupportedKeys)) { - self::errorLog('Unsupported key passed to setPersistentData.'); - return; - } - - $this->session->set($this->constructSessionVariableName($key), $value); + self::errorLog( 'Unsupported key passed to setPersistentData.' ); + return; } - - /** - * Get the data for $key, persisted by BaseFacebook::setPersistentData() - * - * @param string $key The key of the data to retrieve - * @param boolean $default The default value to return if $key is not found - * - * @return mixed - */ - protected function getPersistentData($key, $default = false) + + $this->session->set( $this->constructSessionVariableName( $key ), $value ); + } + + /** + * Get the data for $key, persisted by BaseFacebook::setPersistentData() + * + * @param string $key The key of the data to retrieve + * @param boolean $default The default value to return if $key is not found + * + * @return mixed + */ + + protected function getPersistentData( $key, $default = false ) + { + if ( !in_array( $key, self::$kSupportedKeys ) ) { - if (!in_array($key, self::$kSupportedKeys)) { - self::errorLog('Unsupported key passed to getPersistentData.'); - return $default; - } - - $sessionVariableName = $this->constructSessionVariableName($key); - if ($this->session->has($sessionVariableName)) { - return $this->session->get($sessionVariableName); - } - - return $default; - + self::errorLog( 'Unsupported key passed to getPersistentData.' ); + return $default; } - - /** - * Clear the data with $key from the persistent storage - * - * @param string $key - * @return void - */ - protected function clearPersistentData($key) + + $sessionVariableName = $this->constructSessionVariableName( $key ); + if ( $this->session->has( $sessionVariableName ) ) { - if (!in_array($key, self::$kSupportedKeys)) { - self::errorLog('Unsupported key passed to clearPersistentData.'); - return; - } - - $this->session->remove($this->constructSessionVariableName($key)); + return $this->session->get( $sessionVariableName ); } - - /** - * Clear all data from the persistent storage - * - * @return void - */ - protected function clearAllPersistentData() + + return $default; + + } + + /** + * Clear the data with $key from the persistent storage + * + * @param string $key + * @return void + */ + + protected function clearPersistentData( $key ) + { + if ( !in_array( $key, self::$kSupportedKeys ) ) { - foreach ($this->session->all() as $k => $v) { - if (0 !== strpos($k, $this->prefix)) { - continue; - } - - $this->session->remove($k); - } + self::errorLog( 'Unsupported key passed to clearPersistentData.' ); + return; } - - protected function constructSessionVariableName($key) + + $this->session->remove( $this->constructSessionVariableName( $key ) ); + } + + /** + * Clear all data from the persistent storage + * + * @return void + */ + + protected function clearAllPersistentData( ) + { + foreach ( $this->session->all( ) as $k => $v ) { - return $this->prefix.implode('_', array( - 'fb', - $this->getAppId(), - $key, - )); + if ( 0 !== strpos( $k, $this->prefix ) ) + { + continue; + } + + $this->session->remove( $k ); } + } + + protected function constructSessionVariableName( $key ) + { + return $this->prefix + . implode( '_', array( 'fb', $this->getAppId( ), $key, ) ); + } } diff --git a/Resources/config/facebook.xml b/Resources/config/facebook.xml index a376d91..4cbcf2e 100644 --- a/Resources/config/facebook.xml +++ b/Resources/config/facebook.xml @@ -1,34 +1,34 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - + - - - %fos_facebook.app_id% - %fos_facebook.secret% - %fos_facebook.cookie% - %fos_facebook.domain% - - - + + + %fos_facebook.app_id% + %fos_facebook.secret% + %fos_facebook.cookie% + %fos_facebook.domain% + + + - - - - %fos_facebook.logging% - %fos_facebook.culture% - %fos_facebook.permissions% - - + + + + %fos_facebook.logging% + %fos_facebook.culture% + %fos_facebook.permissions% + + - - - - + + + + - + diff --git a/Resources/config/security.xml b/Resources/config/security.xml index 7057ecf..50e85ab 100644 --- a/Resources/config/security.xml +++ b/Resources/config/security.xml @@ -1,30 +1,34 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - - - - + + + + + - - - + + + - - + + - - - - %fos_facebook.permissions% - + + + + %fos_facebook.permissions% + - + diff --git a/Resources/views/initialize.html.php b/Resources/views/initialize.html.php index dcbf54c..40e3220 100644 --- a/Resources/views/initialize.html.php +++ b/Resources/views/initialize.html.php @@ -1,25 +1,26 @@
- - + + + {% endif %} '); - } - - return new RedirectResponse($loginUrl); + return new Response( ''); } + + return new RedirectResponse( $loginUrl); + } } diff --git a/Security/Firewall/FacebookListener.php b/Security/Firewall/FacebookListener.php index e9982d4..bb053c6 100644 --- a/Security/Firewall/FacebookListener.php +++ b/Security/Firewall/FacebookListener.php @@ -19,7 +19,7 @@ */ class FacebookListener extends AbstractAuthenticationListener { - + protected function attemptAuthentication( Request $request ) { if ( !$request->getQueryString( ) ) diff --git a/Security/Logout/FacebookHandler.php b/Security/Logout/FacebookHandler.php index 0d73ce0..cb6cd3c 100644 --- a/Security/Logout/FacebookHandler.php +++ b/Security/Logout/FacebookHandler.php @@ -10,7 +10,6 @@ */ namespace FOS\FacebookBundle\Security\Logout; - use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; @@ -23,15 +22,15 @@ */ class FacebookHandler implements LogoutHandlerInterface { - private $facebook; - - public function __construct(\BaseFacebook $facebook) - { - $this->facebook = $facebook; - } - - public function logout(Request $request, Response $response, TokenInterface $token) - { - $this->facebook->destroySession(); - } + private $facebook; + + public function __construct( \BaseFacebook $facebook ) + { + $this->facebook = $facebook; + } + + public function logout( Request $request, Response $response, TokenInterface $token ) + { + $this->facebook->destroySession( ); + } } diff --git a/Security/User/UserManagerInterface.php b/Security/User/UserManagerInterface.php index 89e3288..0858bcd 100644 --- a/Security/User/UserManagerInterface.php +++ b/Security/User/UserManagerInterface.php @@ -1,10 +1,10 @@ templating = $templating; - $this->logging = $logging; - $this->culture = $culture; - $this->scope = $scope; - $this->facebook = $facebook; - } - - /** - * Returns the HTML necessary for initializing the JavaScript SDK. - * - * The default template includes the following parameters: - * - * * async - * * fbAsyncInit - * * appId - * * xfbml - * * oauth - * * status - * * cookie - * * logging - * * culture - * - * @param array $parameters An array of parameters for the initialization template - * @param string $name A template name - * - * @return string An HTML string - */ - public function initialize($parameters = array(), $name = null) - { - $name = $name ?: 'FOSFacebookBundle::initialize.html.php'; - return $this->templating->render($name, $parameters + array( - 'async' => true, - 'fbAsyncInit' => '', - 'appId' => (string) $this->facebook->getAppId(), - 'xfbml' => false, - 'oauth' => true, - 'status' => false, - 'cookie' => true, - 'logging' => $this->logging, - 'culture' => $this->culture, - )); - } - - public function loginButton($parameters = array(), $name = null) - { - $name = $name ?: 'FOSFacebookBundle::loginButton.html.php'; - return $this->templating->render($name, $parameters + array( - 'autologoutlink' => 'false', - 'label' => '', - 'scope' => implode(',', $this->scope), - )); - } - - public function logoutUrl($parameters = array(), $name = null) - { - return $this->facebook->getLogoutUrl($parameters); - } - - /** - * @codeCoverageIgnore - */ - public function getName() - { - return 'facebook'; - } + protected $templating; + protected $logging; + protected $culture; + protected $scope; + protected $facebook; + + public function __construct( EngineInterface $templating, \BaseFacebook $facebook, $logging = true, + $culture = 'en_US', array $scope = array( ) ) + { + $this->templating = $templating; + $this->logging = $logging; + $this->culture = $culture; + $this->scope = $scope; + $this->facebook = $facebook; + } + + /** + * Returns the HTML necessary for initializing the JavaScript SDK. + * + * The default template includes the following parameters: + * + * * async + * * fbAsyncInit + * * appId + * * xfbml + * * oauth + * * status + * * cookie + * * logging + * * culture + * + * @param array $parameters An array of parameters for the initialization template + * @param string $name A template name + * + * @return string An HTML string + */ + + public function initialize( $parameters = array( ), $name = null ) + { + $name = $name ? : 'FOSFacebookBundle::initialize.html.php'; + return $this->templating + ->render( $name, + $parameters + + array( 'async' => true, 'fbAsyncInit' => '', 'appId' => ( string ) $this->facebook->getAppId( ), + 'xfbml' => false, 'oauth' => true, 'status' => false, 'cookie' => true, + 'logging' => $this->logging, 'culture' => $this->culture, ) ); + } + + public function loginButton( $parameters = array( ), $name = null ) + { + $name = $name ? : 'FOSFacebookBundle::loginButton.html.php'; + return $this->templating + ->render( $name, + $parameters + + array( 'autologoutlink' => 'false', 'label' => '', 'scope' => implode( ',', $this->scope ), ) ); + } + + public function logoutUrl( $parameters = array( ), $name = null ) + { + return $this->facebook->getLogoutUrl( $parameters ); + } + + /** + * @codeCoverageIgnore + */ + + public function getName( ) + { + return 'facebook'; + } } diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 2ee2943..5238f2e 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -10,15 +10,17 @@ */ namespace FOS\FacebookBundle\Tests\DependencyInjection; - use Symfony\Component\Config\Definition\ConfigurationInterface; use FOS\FacebookBundle\DependencyInjection\Configuration; class ConfigurationTest extends \PHPUnit_Framework_TestCase { - public function testThatCanGetConfigTreeBuilder() - { - $configuration = new Configuration(); - $this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\TreeBuilder', $configuration->getConfigTreeBuilder()); - } + + public function testThatCanGetConfigTreeBuilder( ) + { + $configuration = new Configuration( ); + $this + ->assertInstanceOf( 'Symfony\Component\Config\Definition\Builder\TreeBuilder', + $configuration->getConfigTreeBuilder( ) ); + } } diff --git a/Tests/DependencyInjection/FOSFacebookExtensionTest.php b/Tests/DependencyInjection/FOSFacebookExtensionTest.php index 89b1cef..746a5b3 100644 --- a/Tests/DependencyInjection/FOSFacebookExtensionTest.php +++ b/Tests/DependencyInjection/FOSFacebookExtensionTest.php @@ -10,88 +10,62 @@ */ namespace FOS\FacebookBundle\Tests\DependencyInjection; - use FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension; class FOSFacebookExtensionTest extends \PHPUnit_Framework_TestCase { - /** - * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load - */ - public function testLoadFailure() - { - $container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder') - ->disableOriginalConstructor() - ->getMock(); - - $extension = $this->getMockBuilder('FOS\\FacebookBundle\\DependencyInjection\\FOSFacebookExtension') - ->getMock(); - - $extension->load(array(array()), $container); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load - */ - public function testLoadSetParameters() - { - $container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder') - ->disableOriginalConstructor() - ->getMock(); - - $parameterBag = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag') - ->disableOriginalConstructor() - ->getMock(); - - $parameterBag - ->expects($this->any()) - ->method('add'); - - $container - ->expects($this->any()) - ->method('getParameterBag') - ->will($this->returnValue($parameterBag)); - - $extension = new FOSFacebookExtension(); - $configs = array( - array('class' => array('api' => 'foo')), - array('file' => 'foo'), - array('app_id' => 'foo'), - array('secret' => 'foo'), - array('cookie' => 'foo'), - array('domain' => 'foo'), - array('logging' => 'foo'), - array('culture' => 'foo'), - array('permissions' => array('email')), - ); - $extension->load($configs, $container); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load - */ - public function testThatCanSetContainerAlias() - { - $container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder') - ->disableOriginalConstructor() - ->getMock(); - $container->expects($this->once()) - ->method('setAlias') - ->with($this->equalTo('facebook_alias'), $this->equalTo('fos_facebook.api')); - - $configs = array( - array('class' => array('api' => 'foo')), - array('file' => 'foo'), - array('app_id' => 'foo'), - array('secret' => 'foo'), - array('cookie' => 'foo'), - array('domain' => 'foo'), - array('logging' => 'foo'), - array('culture' => 'foo'), - array('permissions' => array('email')), - array('alias' => 'facebook_alias') - ); - $extension = new FOSFacebookExtension(); - $extension->load($configs, $container); - } + /** + * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load + */ + + public function testLoadFailure( ) + { + $container = $this->getMockBuilder( 'Symfony\\Component\\DependencyInjection\\ContainerBuilder' ) + ->disableOriginalConstructor( )->getMock( ); + + $extension = $this->getMockBuilder( 'FOS\\FacebookBundle\\DependencyInjection\\FOSFacebookExtension' )->getMock( ); + + $extension->load( array( array( ) ), $container ); + } + + /** + * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load + */ + + public function testLoadSetParameters( ) + { + $container = $this->getMockBuilder( 'Symfony\\Component\\DependencyInjection\\ContainerBuilder' ) + ->disableOriginalConstructor( )->getMock( ); + + $parameterBag = $this->getMockBuilder( 'Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag' ) + ->disableOriginalConstructor( )->getMock( ); + + $parameterBag->expects( $this->any( ) )->method( 'add' ); + + $container->expects( $this->any( ) )->method( 'getParameterBag' )->will( $this->returnValue( $parameterBag ) ); + + $extension = new FOSFacebookExtension( ); + $configs = array( array( 'class' => array( 'api' => 'foo' ) ), array( 'file' => 'foo' ), array( 'app_id' => 'foo' ), + array( 'secret' => 'foo' ), array( 'cookie' => 'foo' ), array( 'domain' => 'foo' ), array( 'logging' => 'foo' ), + array( 'culture' => 'foo' ), array( 'permissions' => array( 'email' ) ), ); + $extension->load( $configs, $container ); + } + + /** + * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load + */ + + public function testThatCanSetContainerAlias( ) + { + $container = $this->getMockBuilder( 'Symfony\\Component\\DependencyInjection\\ContainerBuilder' ) + ->disableOriginalConstructor( )->getMock( ); + $container->expects( $this->once( ) )->method( 'setAlias' ) + ->with( $this->equalTo( 'facebook_alias' ), $this->equalTo( 'fos_facebook.api' ) ); + + $configs = array( array( 'class' => array( 'api' => 'foo' ) ), array( 'file' => 'foo' ), array( 'app_id' => 'foo' ), + array( 'secret' => 'foo' ), array( 'cookie' => 'foo' ), array( 'domain' => 'foo' ), array( 'logging' => 'foo' ), + array( 'culture' => 'foo' ), array( 'permissions' => array( 'email' ) ), array( 'alias' => 'facebook_alias' ) ); + $extension = new FOSFacebookExtension( ); + $extension->load( $configs, $container ); + } } diff --git a/Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php b/Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php index c0c6b5b..127a9f2 100644 --- a/Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php +++ b/Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php @@ -10,97 +10,99 @@ */ namespace FOS\FacebookBundle\Tests\DependencyInjection\Security\Factory; - use FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory; class FacebookFactoryTest extends \PHPUnit_Framework_TestCase { - /** - * @var FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory - */ - private $factory = null; - - public function setUp() - { - $this->factory = new FacebookFactory(); - } - - public function testThatCanGetPosition() - { - $this->assertEquals('pre_auth', $this->factory->getPosition()); - } - - public function testThatCanGetKey() - { - $this->assertEquals('fos_facebook', $this->factory->getKey()); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider - */ - public function testThatCreateUserAuthProviderWhenDefinedInConfig() - { - $idsArray = $this->facebookFactoryCreate(array('provider' => true, 'remember_me' => false, 'create_user_if_not_exists' => false)); - $this->assertEquals('fos_facebook.auth.l3l0', $idsArray[0]); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider - */ - public function testThatCreateUserAuthProviderEvenWhenNotDefinedInConfig() - { - $idsArray = $this->facebookFactoryCreate(array('remember_me' => false)); - $this->assertEquals('fos_facebook.auth.l3l0', $idsArray[0]); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider - */ - public function testThatCreateDifferentUserAuthProviderForDifferentFirewalls() - { - $idsArray = $this->facebookFactoryCreate(array('remember_me' => false)); - $this->assertEquals('fos_facebook.auth.l3l0', $idsArray[0]); - - $idsArray = $this->facebookFactoryCreate(array('remember_me' => false), 'main'); - $this->assertEquals('fos_facebook.auth.main', $idsArray[0]); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createEntryPoint - */ - public function testThatCreateEntryPoint() - { - $idsArray = $this->facebookFactoryCreate(array('remember_me' => false)); - $this->assertEquals('fos_facebook.security.authentication.entry_point.l3l0', $idsArray[2]); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::getListenerId - */ - public function testThatListenerForListenerId() - { - $idsArray = $this->facebookFactoryCreate(array('remember_me' => false)); - $this->assertEquals('fos_facebook.security.authentication.listener.l3l0', $idsArray[1]); - } - - /** - * @param array $config - * @return array - */ - private function facebookFactoryCreate($config = array(), $id = 'l3l0') - { - $definition = $this->getMock('Symfony\Component\DependencyInjection\Definition', array('addArgument', 'replaceArgument')); - $definition->expects($this->any()) - ->method('replaceArgument') - ->will($this->returnValue($definition)); - $definition->expects($this->any()) - ->method('addArgument') - ->will($this->returnValue($definition)); - $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder', array('setDefinition')); - $container->expects($this->any()) - ->method('setDefinition') - ->will($this->returnValue($definition)); - - return $this->factory->create($container, $id, $config, 'l3l0.user.provider', null); - } + /** + * @var FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory + */ + private $factory = null; + + public function setUp( ) + { + $this->factory = new FacebookFactory( ); + } + + public function testThatCanGetPosition( ) + { + $this->assertEquals( 'pre_auth', $this->factory->getPosition( ) ); + } + + public function testThatCanGetKey( ) + { + $this->assertEquals( 'fos_facebook', $this->factory->getKey( ) ); + } + + /** + * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider + */ + + public function testThatCreateUserAuthProviderWhenDefinedInConfig( ) + { + $idsArray = $this + ->facebookFactoryCreate( + array( 'provider' => true, 'remember_me' => false, 'create_user_if_not_exists' => false ) ); + $this->assertEquals( 'fos_facebook.auth.l3l0', $idsArray[ 0 ] ); + } + + /** + * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider + */ + + public function testThatCreateUserAuthProviderEvenWhenNotDefinedInConfig( ) + { + $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); + $this->assertEquals( 'fos_facebook.auth.l3l0', $idsArray[ 0 ] ); + } + + /** + * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider + */ + + public function testThatCreateDifferentUserAuthProviderForDifferentFirewalls( ) + { + $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); + $this->assertEquals( 'fos_facebook.auth.l3l0', $idsArray[ 0 ] ); + + $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ), 'main' ); + $this->assertEquals( 'fos_facebook.auth.main', $idsArray[ 0 ] ); + } + + /** + * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createEntryPoint + */ + + public function testThatCreateEntryPoint( ) + { + $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); + $this->assertEquals( 'fos_facebook.security.authentication.entry_point.l3l0', $idsArray[ 2 ] ); + } + + /** + * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::getListenerId + */ + + public function testThatListenerForListenerId( ) + { + $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); + $this->assertEquals( 'fos_facebook.security.authentication.listener.l3l0', $idsArray[ 1 ] ); + } + + /** + * @param array $config + * @return array + */ + + private function facebookFactoryCreate( $config = array( ), $id = 'l3l0' ) + { + $definition = $this + ->getMock( 'Symfony\Component\DependencyInjection\Definition', array( 'addArgument', 'replaceArgument' ) ); + $definition->expects( $this->any( ) )->method( 'replaceArgument' )->will( $this->returnValue( $definition ) ); + $definition->expects( $this->any( ) )->method( 'addArgument' )->will( $this->returnValue( $definition ) ); + $container = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerBuilder', array( 'setDefinition' ) ); + $container->expects( $this->any( ) )->method( 'setDefinition' )->will( $this->returnValue( $definition ) ); + + return $this->factory->create( $container, $id, $config, 'l3l0.user.provider', null ); + } } diff --git a/Tests/FacebookApiException.php b/Tests/FacebookApiException.php index 6e8eb88..272863f 100644 --- a/Tests/FacebookApiException.php +++ b/Tests/FacebookApiException.php @@ -9,5 +9,6 @@ * file that was distributed with this source code. */ -class FacebookApiException extends \Exception { +class FacebookApiException extends \Exception +{ } diff --git a/Tests/Kernel.php b/Tests/Kernel.php index cca89fe..3e04453 100644 --- a/Tests/Kernel.php +++ b/Tests/Kernel.php @@ -10,7 +10,6 @@ */ namespace FOS\FacebookBundle\Tests; - use Symfony\Component\HttpKernel\Kernel as BaseKernel; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Filesystem\Filesystem; @@ -18,54 +17,59 @@ class Kernel extends BaseKernel { - public function __construct() + + public function __construct( ) + { + $this->tmpDir = sys_get_temp_dir( ) . '/sf2_' . rand( 1, 9999 ); + if ( !is_dir( $this->tmpDir ) ) { - $this->tmpDir = sys_get_temp_dir().'/sf2_'.rand(1, 9999); - if (!is_dir($this->tmpDir)) { - if (false === @mkdir($this->tmpDir)) { - die(sprintf('Unable to create a temporary directory (%s)', $this->tmpDir)); - } - } elseif (!is_writable($this->tmpDir)) { - die(sprintf('Unable to write in a temporary directory (%s)', $this->tmpDir)); - } - - parent::__construct('env', true); - - require_once __DIR__.'/FacebookApiException.php'; - - $loader = new UniversalClassLoader(); - $loader->loadClass('\FacebookApiException'); - $loader->register(); + if ( false === @mkdir( $this->tmpDir ) ) + { + die( sprintf( 'Unable to create a temporary directory (%s)', $this->tmpDir ) ); + } } - - public function __destruct() - { - $fs = new Filesystem(); - $fs->remove($this->tmpDir); - } - - public function registerRootDir() - { - return $this->tmpDir; - } - - public function registerBundles() - { - return array( - new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), - ); - } - - public function registerBundleDirs() - { - return array( - ); - } - - public function registerContainerConfiguration(LoaderInterface $loader) + elseif ( !is_writable( $this->tmpDir ) ) { - $loader->load(function ($container) { - $container->setParameter('kernel.compiled_classes', array()); - }); + die( sprintf( 'Unable to write in a temporary directory (%s)', $this->tmpDir ) ); } + + parent::__construct( 'env', true ); + + require_once __DIR__ . '/FacebookApiException.php'; + + $loader = new UniversalClassLoader( ); + $loader->loadClass( '\FacebookApiException' ); + $loader->register( ); + } + + public function __destruct( ) + { + $fs = new Filesystem( ); + $fs->remove( $this->tmpDir ); + } + + public function registerRootDir( ) + { + return $this->tmpDir; + } + + public function registerBundles( ) + { + return array( new \Symfony\Bundle\FrameworkBundle\FrameworkBundle( ), ); + } + + public function registerBundleDirs( ) + { + return array( ); + } + + public function registerContainerConfiguration( LoaderInterface $loader ) + { + $loader + ->load( + function ($container) + { + $container->setParameter( 'kernel.compiled_classes', array( ) ); + } ); + } } diff --git a/Tests/Security/Authentication/Provider/FacebookProviderTest.php b/Tests/Security/Authentication/Provider/FacebookProviderTest.php index 55c3bea..31c3e6a 100644 --- a/Tests/Security/Authentication/Provider/FacebookProviderTest.php +++ b/Tests/Security/Authentication/Provider/FacebookProviderTest.php @@ -10,221 +10,194 @@ */ namespace FOS\FacebookBundle\Tests\Security\Authentication\Provider; - use FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider; use Symfony\Component\Security\Core\Exception\AuthenticationException; class FacebookProviderTest extends \PHPUnit_Framework_TestCase { - /** - * @expectedException \InvalidArgumentException - */ - public function testThatUserCheckerCannotBeNullWhenUserProviderIsNotNull() - { - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->getMock(); - new FacebookProvider('main', $facebookMock, $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface')); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - */ - public function testThatCannotAuthenticateWhenTokenIsNotFacebookUserToken() - { - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->getMock(); - $facebookProvider = new FacebookProvider('main', $facebookMock); - $this->assertNull($facebookProvider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'))); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports - */ - public function testThatCannotAuthenticateWhenTokenFromOtherFirewall() - { - $providerKeyForProvider = 'main'; - $providerKeyForToken = 'connect'; - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->getMock(); - $facebookProvider = new FacebookProvider($providerKeyForProvider, $facebookMock); - - $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKeyForToken)); - $tokenMock->expects($this->any()) - ->method('getProviderKey') - ->will($this->returnValue($providerKeyForToken)); - - $this->assertFalse($facebookProvider->supports($tokenMock)); - $this->assertNull($facebookProvider->authenticate($tokenMock)); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken - */ - public function testThatCanAuthenticateUserWithoutUserProvider() - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getUser')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getUser') - ->will($this->returnValue('123')); - - $facebookProvider = new FacebookProvider($providerKey, $facebookMock); - - $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getAttributes', 'getProviderKey'), array($providerKey)); - $tokenMock->expects($this->once()) - ->method('getAttributes') - ->will($this->returnValue(array())); - $tokenMock->expects($this->any()) - ->method('getProviderKey') - ->will($this->returnValue($providerKey)); - - $this->assertTrue($facebookProvider->supports($tokenMock)); - $this->assertEquals('123', $facebookProvider->authenticate($tokenMock)->getUser()); - } - - /** - * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException - */ - public function testThatCannotAuthenticateWhenUserProviderThrowsAuthenticationException() - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getUser')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getUser') - ->will($this->returnValue('123')); - - $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); - $userProviderMock->expects($this->once()) - ->method('loadUserByUsername') - ->with('123') - ->will($this->throwException(new AuthenticationException('test'))); - - $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); - $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKey)); - $tokenMock->expects($this->any()) - ->method('getProviderKey') - ->will($this->returnValue($providerKey)); - - $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $facebookProvider->authenticate($tokenMock); - } - - /** - * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException - */ - public function testThatCannotAuthenticateWhenUserProviderDoesNotReturnUsetInterface() - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getUser')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getUser') - ->will($this->returnValue('123')); - - $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); - $userProviderMock->expects($this->once()) - ->method('loadUserByUsername') - ->with('123') - ->will($this->returnValue('234')); - - $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); - $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKey)); - $tokenMock->expects($this->any()) - ->method('getProviderKey') - ->will($this->returnValue($providerKey)); - - $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $facebookProvider->authenticate($tokenMock); - } - - /** - * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException - */ - public function testThatCannotAuthenticateWhenCannotRetrieveFacebookUserFromSession() - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getUser')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getUser') - ->will($this->returnValue(false)); - - $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); - $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); - - $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getProviderKey'), array($providerKey)); - $tokenMock->expects($this->any()) - ->method('getProviderKey') - ->will($this->returnValue($providerKey)); - - $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $facebookProvider->authenticate($tokenMock); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken - */ - public function testThatCanAutenticateUsingUserProvider() - { - $providerKey = 'main'; - - $userMock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); - $userMock->expects($this->once()) - ->method('getUsername') - ->will($this->returnValue('l3l0')); - $userMock->expects($this->once()) - ->method('getRoles') - ->will($this->returnValue(array())); - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getUser')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getUser') - ->will($this->returnValue('123')); - - $userProviderMock = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface'); - $userProviderMock->expects($this->once()) - ->method('loadUserByUsername') - ->with('123') - ->will($this->returnValue($userMock)); - - $userCheckerMock = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface'); - $userCheckerMock->expects($this->once()) - ->method('checkPostAuth'); - - $tokenMock = $this->getMock('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array('getAttributes', 'getProviderKey'), array($providerKey)); - $tokenMock->expects($this->once()) - ->method('getAttributes') - ->will($this->returnValue(array())); - $tokenMock->expects($this->any()) - ->method('getProviderKey') - ->will($this->returnValue($providerKey)); - - $facebookProvider = new FacebookProvider($providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $this->assertEquals('l3l0', $facebookProvider->authenticate($tokenMock)->getUsername()); - } + /** + * @expectedException \InvalidArgumentException + */ + + public function testThatUserCheckerCannotBeNullWhenUserProviderIsNotNull( ) + { + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->getMock( ); + new FacebookProvider( 'main', $facebookMock, + $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' )); + } + + /** + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate + */ + + public function testThatCannotAuthenticateWhenTokenIsNotFacebookUserToken( ) + { + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->getMock( ); + $facebookProvider = new FacebookProvider( 'main', $facebookMock); + $this + ->assertNull( + $facebookProvider + ->authenticate( $this->getMock( 'Symfony\Component\Security\Core\Authentication\Token\TokenInterface' ) ) ); + } + + /** + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports + */ + + public function testThatCannotAuthenticateWhenTokenFromOtherFirewall( ) + { + $providerKeyForProvider = 'main'; + $providerKeyForToken = 'connect'; + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->getMock( ); + $facebookProvider = new FacebookProvider( $providerKeyForProvider, $facebookMock); + + $tokenMock = $this + ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), + array( $providerKeyForToken ) ); + $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKeyForToken ) ); + + $this->assertFalse( $facebookProvider->supports( $tokenMock ) ); + $this->assertNull( $facebookProvider->authenticate( $tokenMock ) ); + } + + /** + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken + */ + + public function testThatCanAuthenticateUserWithoutUserProvider( ) + { + $providerKey = 'main'; + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); + + $facebookProvider = new FacebookProvider( $providerKey, $facebookMock); + + $tokenMock = $this + ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', + array( 'getAttributes', 'getProviderKey' ), array( $providerKey ) ); + $tokenMock->expects( $this->once( ) )->method( 'getAttributes' )->will( $this->returnValue( array( ) ) ); + $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); + + $this->assertTrue( $facebookProvider->supports( $tokenMock ) ); + $this->assertEquals( '123', $facebookProvider->authenticate( $tokenMock )->getUser( ) ); + } + + /** + * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException + */ + + public function testThatCannotAuthenticateWhenUserProviderThrowsAuthenticationException( ) + { + $providerKey = 'main'; + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); + + $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); + $userProviderMock->expects( $this->once( ) )->method( 'loadUserByUsername' )->with( '123' ) + ->will( $this->throwException( new AuthenticationException( 'test') ) ); + + $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); + $tokenMock = $this + ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), + array( $providerKey ) ); + $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); + + $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); + $facebookProvider->authenticate( $tokenMock ); + } + + /** + * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException + */ + + public function testThatCannotAuthenticateWhenUserProviderDoesNotReturnUsetInterface( ) + { + $providerKey = 'main'; + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); + + $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); + $userProviderMock->expects( $this->once( ) )->method( 'loadUserByUsername' )->with( '123' ) + ->will( $this->returnValue( '234' ) ); + + $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); + $tokenMock = $this + ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), + array( $providerKey ) ); + $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); + + $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); + $facebookProvider->authenticate( $tokenMock ); + } + + /** + * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException + */ + + public function testThatCannotAuthenticateWhenCannotRetrieveFacebookUserFromSession( ) + { + $providerKey = 'main'; + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( false ) ); + + $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); + $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); + + $tokenMock = $this + ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), + array( $providerKey ) ); + $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); + + $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); + $facebookProvider->authenticate( $tokenMock ); + } + + /** + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate + * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken + */ + + public function testThatCanAutenticateUsingUserProvider( ) + { + $providerKey = 'main'; + + $userMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserInterface' ); + $userMock->expects( $this->once( ) )->method( 'getUsername' )->will( $this->returnValue( 'l3l0' ) ); + $userMock->expects( $this->once( ) )->method( 'getRoles' )->will( $this->returnValue( array( ) ) ); + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); + + $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); + $userProviderMock->expects( $this->once( ) )->method( 'loadUserByUsername' )->with( '123' ) + ->will( $this->returnValue( $userMock ) ); + + $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); + $userCheckerMock->expects( $this->once( ) )->method( 'checkPostAuth' ); + + $tokenMock = $this + ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', + array( 'getAttributes', 'getProviderKey' ), array( $providerKey ) ); + $tokenMock->expects( $this->once( ) )->method( 'getAttributes' )->will( $this->returnValue( array( ) ) ); + $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); + + $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); + $this->assertEquals( 'l3l0', $facebookProvider->authenticate( $tokenMock )->getUsername( ) ); + } } diff --git a/Tests/Security/Authentication/Token/FacebookUserTokenTest.php b/Tests/Security/Authentication/Token/FacebookUserTokenTest.php index f01f00c..35bac3d 100644 --- a/Tests/Security/Authentication/Token/FacebookUserTokenTest.php +++ b/Tests/Security/Authentication/Token/FacebookUserTokenTest.php @@ -10,39 +10,36 @@ */ namespace FOS\FacebookBundle\Tests\Security\Authentication\Token; - use FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken; class FacebookUserTokenTest extends \PHPUnit_Framework_TestCase { - /** - * @dataProvider provider - */ - public function testThatAlwaysReturnEmptyCredentials($uid, $roles) - { - $token = new FacebookUserToken('main', $uid, $roles); - - $this->assertEquals('', $token->getCredentials()); - } - - /** - * @return array - */ - public static function provider() - { - return array( - array('', array()), - array('l3l0', array()), - array('', array('role1', 'role2')), - array('l3l0', array('role1', 'role2')) - ); - } - - public function testThatProviderKeyIsNotEmptyAfterDeserialization() - { - $providerKey = 'main'; - $token = unserialize(serialize(new FacebookUserToken($providerKey))); - - $this->assertEquals($providerKey, $token->getProviderKey()); - } + /** + * @dataProvider provider + */ + + public function testThatAlwaysReturnEmptyCredentials( $uid, $roles ) + { + $token = new FacebookUserToken( 'main', $uid, $roles); + + $this->assertEquals( '', $token->getCredentials( ) ); + } + + /** + * @return array + */ + + public static function provider( ) + { + return array( array( '', array( ) ), array( 'l3l0', array( ) ), array( '', array( 'role1', 'role2' ) ), + array( 'l3l0', array( 'role1', 'role2' ) ) ); + } + + public function testThatProviderKeyIsNotEmptyAfterDeserialization( ) + { + $providerKey = 'main'; + $token = unserialize( serialize( new FacebookUserToken( $providerKey) ) ); + + $this->assertEquals( $providerKey, $token->getProviderKey( ) ); + } } diff --git a/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php b/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php index 9e6dc69..1aac44a 100644 --- a/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php +++ b/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php @@ -10,63 +10,63 @@ */ namespace FOS\FacebookBundle\Tests\Security\EntryPoint; - use FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint; class FacebookAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase { - /** - * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start - */ - public function testThatRedirectResponseWithFacebookLoginUrlIsCreated() - { - $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath')); - $requestMock->expects($this->once()) - ->method('getUriForPath') - ->with($this->equalTo('/index')) - ->will($this->returnValue('http://localhost/index')); - - $options = array('check_path' => '/index'); - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getLoginUrl')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getLoginUrl') - ->with($this->equalTo(array( - 'display' => 'page', - 'scope' => 'email,user_website', - 'redirect_uri' => 'http://localhost/index' - ))) - ->will($this->returnValue('http://localhost/facebook-redirect/index')); - - $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array('email', 'user_website')); - $response = $facebookAuthentication->start($requestMock); - - $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response, 'RedirectResponse is returned'); - $this->assertEquals($response->headers->get('location'), 'http://localhost/facebook-redirect/index', 'RedirectResponse has defined expected location'); - } - - /** - * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start - */ - public function testThatRedirectionToFacebookLoginUrlIsCreated() - { - $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath')); - - $options = array('check_path' => '/index', 'server_url' => 'http://server.url', 'app_url' => 'http://app.url'); - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getLoginUrl')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getLoginUrl') - ->will($this->returnValue('http://localhost/facebook-redirect/index')); - - $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array()); - $response = $facebookAuthentication->start($requestMock); - - $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response, 'Response is returned'); - $this->assertRegExp('/location\.href="http:\/\/localhost\/facebook-redirect\/index/', $response->getContent(), 'Javascript redirection is in response'); - } + /** + * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start + */ + + public function testThatRedirectResponseWithFacebookLoginUrlIsCreated( ) + { + $requestMock = $this->getMock( 'Symfony\Component\HttpFoundation\Request', array( 'getUriForPath' ) ); + $requestMock->expects( $this->once( ) )->method( 'getUriForPath' )->with( $this->equalTo( '/index' ) ) + ->will( $this->returnValue( 'http://localhost/index' ) ); + + $options = array( 'check_path' => '/index' ); + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getLoginUrl' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getLoginUrl' ) + ->with( + $this + ->equalTo( + array( 'display' => 'page', 'scope' => 'email,user_website', + 'redirect_uri' => 'http://localhost/index' ) ) ) + ->will( $this->returnValue( 'http://localhost/facebook-redirect/index' ) ); + + $facebookAuthentication = new FacebookAuthenticationEntryPoint( $facebookMock, $options, + array( 'email', 'user_website' )); + $response = $facebookAuthentication->start( $requestMock ); + + $this + ->assertInstanceOf( 'Symfony\Component\HttpFoundation\RedirectResponse', $response, + 'RedirectResponse is returned' ); + $this + ->assertEquals( $response->headers->get( 'location' ), 'http://localhost/facebook-redirect/index', + 'RedirectResponse has defined expected location' ); + } + + /** + * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start + */ + + public function testThatRedirectionToFacebookLoginUrlIsCreated( ) + { + $requestMock = $this->getMock( 'Symfony\Component\HttpFoundation\Request', array( 'getUriForPath' ) ); + + $options = array( 'check_path' => '/index', 'server_url' => 'http://server.url', 'app_url' => 'http://app.url' ); + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getLoginUrl' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getLoginUrl' ) + ->will( $this->returnValue( 'http://localhost/facebook-redirect/index' ) ); + + $facebookAuthentication = new FacebookAuthenticationEntryPoint( $facebookMock, $options, array( )); + $response = $facebookAuthentication->start( $requestMock ); + + $this->assertInstanceOf( 'Symfony\Component\HttpFoundation\Response', $response, 'Response is returned' ); + $this + ->assertRegExp( '/location\.href="http:\/\/localhost\/facebook-redirect\/index/', $response->getContent( ), + 'Javascript redirection is in response' ); + } } diff --git a/Tests/Security/Firewall/FacebookListenerTest.php b/Tests/Security/Firewall/FacebookListenerTest.php index 88a5399..fe362f0 100644 --- a/Tests/Security/Firewall/FacebookListenerTest.php +++ b/Tests/Security/Firewall/FacebookListenerTest.php @@ -10,82 +10,75 @@ */ namespace FOS\FacebookBundle\Tests\Security\Firewall\FacebookListener; - use FOS\FacebookBundle\Security\Firewall\FacebookListener; class FacebookListenerTest extends \PHPUnit_Framework_TestCase { - /** - * @covers FOS\FacebookBundle\Security\Firewall\FacebookListener::attemptAuthentication - */ - public function testThatCanAttemptAuthenticationWithFacebook() - { - $listener = new FacebookListener( - $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'), - $this->getAuthenticationManager(), - $this->getMock('Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface'), - $this->getHttpUtils(), - 'providerKey', - $this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface'), - $this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface') - ); - $listener->handle($this->getResponseEvent()); - } - - /** - * @return Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface - */ - private function getAuthenticationManager() - { - $authenticationManagerMock = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface'); - $authenticationManagerMock->expects($this->once()) - ->method('authenticate') - ->with($this->isInstanceOf('FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken')); - - return $authenticationManagerMock; - } - - /** - * @return Symfony\Component\Security\Http\HttpUtils - */ - private function getHttpUtils() - { - $httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils'); - $httpUtils->expects($this->once()) - ->method('checkRequestPath') - ->will($this->returnValue(true)); - - return $httpUtils; - } - - /** - * @return Symfony\Component\HttpKernel\Event\GetResponseEvent - */ - private function getResponseEvent() - { - $responseEventMock = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array('getRequest'), array(), '', false); - $responseEventMock->expects($this->any()) - ->method('getRequest') - ->will($this->returnValue($this->getRequest())); - - return $responseEventMock; - } - - /** - * @return Symfony\Component\HttpFoundation\Request - */ - private function getRequest() - { - $requestMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') - ->disableOriginalClone() - ->getMock(); - $requestMock->expects($this->any()) - ->method('hasSession') - ->will($this->returnValue('true')); - $requestMock->expects($this->any()) - ->method('hasPreviousSession') - ->will($this->returnValue('true')); - - return $requestMock; - } + /** + * @covers FOS\FacebookBundle\Security\Firewall\FacebookListener::attemptAuthentication + */ + + public function testThatCanAttemptAuthenticationWithFacebook( ) + { + $listener = new FacebookListener( $this->getMock( 'Symfony\Component\Security\Core\SecurityContextInterface' ), + $this->getAuthenticationManager( ), + $this->getMock( 'Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface' ), + $this->getHttpUtils( ), 'providerKey', + $this->getMock( 'Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface' ), + $this->getMock( 'Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface' )); + $listener->handle( $this->getResponseEvent( ) ); + } + + /** + * @return Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface + */ + + private function getAuthenticationManager( ) + { + $authenticationManagerMock = $this + ->getMock( 'Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface' ); + $authenticationManagerMock->expects( $this->once( ) )->method( 'authenticate' ) + ->with( $this->isInstanceOf( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken' ) ); + + return $authenticationManagerMock; + } + + /** + * @return Symfony\Component\Security\Http\HttpUtils + */ + + private function getHttpUtils( ) + { + $httpUtils = $this->getMock( 'Symfony\Component\Security\Http\HttpUtils' ); + $httpUtils->expects( $this->once( ) )->method( 'checkRequestPath' )->will( $this->returnValue( true ) ); + + return $httpUtils; + } + + /** + * @return Symfony\Component\HttpKernel\Event\GetResponseEvent + */ + + private function getResponseEvent( ) + { + $responseEventMock = $this + ->getMock( 'Symfony\Component\HttpKernel\Event\GetResponseEvent', array( 'getRequest' ), array( ), '', false ); + $responseEventMock->expects( $this->any( ) )->method( 'getRequest' )->will( $this->returnValue( $this->getRequest( ) ) ); + + return $responseEventMock; + } + + /** + * @return Symfony\Component\HttpFoundation\Request + */ + + private function getRequest( ) + { + $requestMock = $this->getMockBuilder( 'Symfony\Component\HttpFoundation\Request' )->disableOriginalClone( ) + ->getMock( ); + $requestMock->expects( $this->any( ) )->method( 'hasSession' )->will( $this->returnValue( 'true' ) ); + $requestMock->expects( $this->any( ) )->method( 'hasPreviousSession' )->will( $this->returnValue( 'true' ) ); + + return $requestMock; + } } diff --git a/Tests/Templating/Helper/FacebookHelperTest.php b/Tests/Templating/Helper/FacebookHelperTest.php index 83b5603..5c9fc62 100644 --- a/Tests/Templating/Helper/FacebookHelperTest.php +++ b/Tests/Templating/Helper/FacebookHelperTest.php @@ -10,77 +10,54 @@ */ namespace FOS\FacebookBundle\Tests\Templating\Helper; - use FOS\FacebookBundle\Templating\Helper\FacebookHelper; class FacebookHelperTest extends \PHPUnit_Framework_TestCase { - /** - * @covers FOS\FacebookBundle\Templating\Helper\FacebookHelper::initialize - */ - public function testInitialize() - { - $expected = new \stdClass(); - - $templating = $this->getMockBuilder('Symfony\Component\Templating\DelegatingEngine') - ->disableOriginalConstructor() - ->getMock(); - $templating - ->expects($this->once()) - ->method('render') - ->with('FOSFacebookBundle::initialize.html.php', array( - 'appId' => 123, - 'async' => true, - 'cookie' => false, - 'culture' => 'en_US', - 'fbAsyncInit' => '', - 'logging' => true, - 'oauth' => true, - 'status' => false, - 'xfbml' => false, - )) - ->will($this->returnValue($expected)); - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getAppId')) - ->getMock(); - $facebookMock->expects($this->once()) - ->method('getAppId') - ->will($this->returnValue('123')); - - $helper = new FacebookHelper($templating, $facebookMock); - $this->assertSame($expected, $helper->initialize(array('cookie' => false))); - } - - /** - * @covers FOS\FacebookBundle\Templating\Helper\FacebookHelper::loginButton - */ - public function testLoginButton() - { - $expected = new \stdClass(); - - $templating = $this->getMockBuilder('Symfony\Component\Templating\DelegatingEngine') - ->disableOriginalConstructor() - ->getMock(); - $templating - ->expects($this->once()) - ->method('render') - ->with('FOSFacebookBundle::loginButton.html.php', array( - 'autologoutlink' => 'false', - 'label' => 'testLabel', - 'scope' => '1,2,3', - )) - ->will($this->returnValue($expected)); - - $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') - ->disableOriginalConstructor() - ->setMethods(array('getAppId')) - ->getMock(); - $facebookMock->expects($this->any()) - ->method('getAppId'); - - $helper = new FacebookHelper($templating, $facebookMock, true, 'en_US', array(1,2,3) ); - $this->assertSame($expected, $helper->loginButton(array('label' => 'testLabel'))); - } + /** + * @covers FOS\FacebookBundle\Templating\Helper\FacebookHelper::initialize + */ + + public function testInitialize( ) + { + $expected = new \stdClass( ); + + $templating = $this->getMockBuilder( 'Symfony\Component\Templating\DelegatingEngine' ) + ->disableOriginalConstructor( )->getMock( ); + $templating->expects( $this->once( ) )->method( 'render' ) + ->with( 'FOSFacebookBundle::initialize.html.php', + array( 'appId' => 123, 'async' => true, 'cookie' => false, 'culture' => 'en_US', 'fbAsyncInit' => '', + 'logging' => true, 'oauth' => true, 'status' => false, 'xfbml' => false, ) ) + ->will( $this->returnValue( $expected ) ); + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getAppId' ) )->getMock( ); + $facebookMock->expects( $this->once( ) )->method( 'getAppId' )->will( $this->returnValue( '123' ) ); + + $helper = new FacebookHelper( $templating, $facebookMock); + $this->assertSame( $expected, $helper->initialize( array( 'cookie' => false ) ) ); + } + + /** + * @covers FOS\FacebookBundle\Templating\Helper\FacebookHelper::loginButton + */ + + public function testLoginButton( ) + { + $expected = new \stdClass( ); + + $templating = $this->getMockBuilder( 'Symfony\Component\Templating\DelegatingEngine' ) + ->disableOriginalConstructor( )->getMock( ); + $templating->expects( $this->once( ) )->method( 'render' ) + ->with( 'FOSFacebookBundle::loginButton.html.php', + array( 'autologoutlink' => 'false', 'label' => 'testLabel', 'scope' => '1,2,3', ) ) + ->will( $this->returnValue( $expected ) ); + + $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) + ->disableOriginalConstructor( )->setMethods( array( 'getAppId' ) )->getMock( ); + $facebookMock->expects( $this->any( ) )->method( 'getAppId' ); + + $helper = new FacebookHelper( $templating, $facebookMock, true, 'en_US', array( 1, 2, 3 )); + $this->assertSame( $expected, $helper->loginButton( array( 'label' => 'testLabel' ) ) ); + } } diff --git a/Tests/Twig/Extension/FacebookExtensionTest.php b/Tests/Twig/Extension/FacebookExtensionTest.php index 9fe393e..7ede605 100644 --- a/Tests/Twig/Extension/FacebookExtensionTest.php +++ b/Tests/Twig/Extension/FacebookExtensionTest.php @@ -10,73 +10,66 @@ */ namespace FOS\FacebookBundle\Tests\Twig\Extension; - use FOS\FacebookBundle\Twig\Extension\FacebookExtension; use FOS\FacebookBundle\Templating\Helper\FacebookHelper; class FacebookExtensionTest extends \PHPUnit_Framework_TestCase { - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::getName - */ - public function testGetName() - { - $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - $extension = new FacebookExtension($containerMock); - $this->assertSame('facebook', $extension->getName()); - } - - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::getFunctions - */ - public function testGetFunctions() - { - $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - $extension = new FacebookExtension($containerMock); - $functions = $extension->getFunctions(); - $this->assertInstanceOf('\Twig_Function_Method', $functions['facebook_initialize']); - $this->assertInstanceOf('\Twig_Function_Method', $functions['facebook_login_button']); - } - - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::renderInitialize - */ - public function testRenderInitialize() - { - $helperMock = $this->getMockBuilder('FOS\FacebookBundle\Templating\Helper\FacebookHelper') - ->disableOriginalConstructor() - ->getMock(); - $helperMock->expects($this->once()) - ->method('initialize') - ->will($this->returnValue('returnedValue')); - $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - $containerMock->expects($this->once()) - ->method('get') - ->with('fos_facebook.helper') - ->will($this->returnValue($helperMock)); - - $extension = new FacebookExtension($containerMock); - $this->assertSame('returnedValue', $extension->renderInitialize()); - } + /** + * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::getName + */ + + public function testGetName( ) + { + $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); + $extension = new FacebookExtension( $containerMock); + $this->assertSame( 'facebook', $extension->getName( ) ); + } + + /** + * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::getFunctions + */ + + public function testGetFunctions( ) + { + $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); + $extension = new FacebookExtension( $containerMock); + $functions = $extension->getFunctions( ); + $this->assertInstanceOf( '\Twig_Function_Method', $functions[ 'facebook_initialize' ] ); + $this->assertInstanceOf( '\Twig_Function_Method', $functions[ 'facebook_login_button' ] ); + } + + /** + * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::renderInitialize + */ + + public function testRenderInitialize( ) + { + $helperMock = $this->getMockBuilder( 'FOS\FacebookBundle\Templating\Helper\FacebookHelper' ) + ->disableOriginalConstructor( )->getMock( ); + $helperMock->expects( $this->once( ) )->method( 'initialize' )->will( $this->returnValue( 'returnedValue' ) ); + $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); + $containerMock->expects( $this->once( ) )->method( 'get' )->with( 'fos_facebook.helper' ) + ->will( $this->returnValue( $helperMock ) ); + + $extension = new FacebookExtension( $containerMock); + $this->assertSame( 'returnedValue', $extension->renderInitialize( ) ); + } + + /** + * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::renderloginButton + */ + + public function testRenderLoginButton( ) + { + $helperMock = $this->getMockBuilder( 'FOS\FacebookBundle\Templating\Helper\FacebookHelper' ) + ->disableOriginalConstructor( )->getMock( ); + $helperMock->expects( $this->once( ) )->method( 'loginButton' )->will( $this->returnValue( 'returnedValueLogin' ) ); + $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); + $containerMock->expects( $this->once( ) )->method( 'get' )->with( 'fos_facebook.helper' ) + ->will( $this->returnValue( $helperMock ) ); - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::renderloginButton - */ - public function testRenderLoginButton() - { - $helperMock = $this->getMockBuilder('FOS\FacebookBundle\Templating\Helper\FacebookHelper') - ->disableOriginalConstructor() - ->getMock(); - $helperMock->expects($this->once()) - ->method('loginButton') - ->will($this->returnValue('returnedValueLogin')); - $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - $containerMock->expects($this->once()) - ->method('get') - ->with('fos_facebook.helper') - ->will($this->returnValue($helperMock)); - - $extension = new FacebookExtension($containerMock); - $this->assertSame('returnedValueLogin', $extension->renderLoginButton()); - } + $extension = new FacebookExtension( $containerMock); + $this->assertSame( 'returnedValueLogin', $extension->renderLoginButton( ) ); + } } diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index e0640ab..cd20de3 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -9,9 +9,10 @@ * file that was distributed with this source code. */ -$file = __DIR__.'/../vendor/autoload.php'; -if (!file_exists($file)) { - throw new RuntimeException('Install dependencies to run test suite.'); +$file = __DIR__ . '/../vendor/autoload.php'; +if ( !file_exists( $file ) ) +{ + throw new RuntimeException( 'Install dependencies to run test suite.'); } $autoload = require_once $file; diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index 7a151dd..3378b48 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -10,69 +10,77 @@ */ namespace FOS\FacebookBundle\Twig\Extension; - use FOS\FacebookBundle\Templating\Helper\FacebookHelper; use Symfony\Component\DependencyInjection\ContainerInterface; class FacebookExtension extends \Twig_Extension { - protected $container; - - /** - * Constructor. - * - * @param ContainerInterface $container - */ - public function __construct(ContainerInterface $container) - { - $this->container = $container; - } - - /** - * Returns a list of global functions to add to the existing list. - * - * @return array An array of global functions - */ - public function getFunctions() - { - return array( - 'facebook_initialize' => new \Twig_Function_Method($this, 'renderInitialize', array('is_safe' => array('html'))), - 'facebook_login_button' => new \Twig_Function_Method($this, 'renderLoginButton', array('is_safe' => array('html'))), - 'facebook_logout_url' => new \Twig_Function_Method($this, 'renderLogoutUrl', array('is_safe' => array('html'))), - ); - } - - /** - * Returns the name of the extension. - * - * @return string The extension name - */ - public function getName() - { - return 'facebook'; - } - - /** - * @see FacebookHelper::initialize() - */ - public function renderInitialize($parameters = array(), $name = null) - { - return $this->container->get('fos_facebook.helper')->initialize($parameters, $name ?: 'FOSFacebookBundle::initialize.html.twig'); - } - - /** - * @see FacebookHelper::loginButton() - */ - public function renderLoginButton($parameters = array(), $name = null) - { - return $this->container->get('fos_facebook.helper')->loginButton($parameters, $name ?: 'FOSFacebookBundle::loginButton.html.twig'); - } - - /** - * @see FacebookHelper::logoutUrl() - */ - public function renderLogoutUrl($parameters = array(), $name = null) - { - return $this->container->get('fos_facebook.helper')->logoutUrl($parameters); - } + protected $container; + + /** + * Constructor. + * + * @param ContainerInterface $container + */ + + public function __construct( ContainerInterface $container ) + { + $this->container = $container; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + + public function getFunctions( ) + { + return array( + 'facebook_initialize' => new \Twig_Function_Method( $this, 'renderInitialize', + array( 'is_safe' => array( 'html' ) )), + 'facebook_login_button' => new \Twig_Function_Method( $this, 'renderLoginButton', + array( 'is_safe' => array( 'html' ) )), + 'facebook_logout_url' => new \Twig_Function_Method( $this, 'renderLogoutUrl', array( 'is_safe' => array( 'html' ) )), ); + } + + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + + public function getName( ) + { + return 'facebook'; + } + + /** + * @see FacebookHelper::initialize() + */ + + public function renderInitialize( $parameters = array( ), $name = null ) + { + return $this->container->get( 'fos_facebook.helper' ) + ->initialize( $parameters, $name ? : 'FOSFacebookBundle::initialize.html.twig' ); + } + + /** + * @see FacebookHelper::loginButton() + */ + + public function renderLoginButton( $parameters = array( ), $name = null ) + { + return $this->container->get( 'fos_facebook.helper' ) + ->loginButton( $parameters, $name ? : 'FOSFacebookBundle::loginButton.html.twig' ); + } + + /** + * @see FacebookHelper::logoutUrl() + */ + + public function renderLogoutUrl( $parameters = array( ), $name = null ) + { + return $this->container->get( 'fos_facebook.helper' )->logoutUrl( $parameters ); + } } From ddaecfeba52df92e5ca8a520845ddcf8a5a5e473 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 20 May 2013 19:13:56 -0430 Subject: [PATCH 08/30] adding trigger login twig function --- Resources/views/loginButton.html.twig | 3 +-- Resources/views/triggerLogin.html.twig | 1 + Templating/Helper/FacebookHelper.php | 8 ++++++-- Twig/Extension/FacebookExtension.php | 16 ++++++++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 Resources/views/triggerLogin.html.twig diff --git a/Resources/views/loginButton.html.twig b/Resources/views/loginButton.html.twig index 4f20348..cd23b1e 100644 --- a/Resources/views/loginButton.html.twig +++ b/Resources/views/loginButton.html.twig @@ -1,2 +1 @@ -{{ label }} +{{ label }} diff --git a/Resources/views/triggerLogin.html.twig b/Resources/views/triggerLogin.html.twig new file mode 100644 index 0000000..7935485 --- /dev/null +++ b/Resources/views/triggerLogin.html.twig @@ -0,0 +1 @@ +onclick="fbLogin();" \ No newline at end of file diff --git a/Templating/Helper/FacebookHelper.php b/Templating/Helper/FacebookHelper.php index c56812b..3d7af7e 100644 --- a/Templating/Helper/FacebookHelper.php +++ b/Templating/Helper/FacebookHelper.php @@ -68,8 +68,12 @@ public function loginButton( $parameters = array( ), $name = null ) $name = $name ? : 'FOSFacebookBundle::loginButton.html.php'; return $this->templating ->render( $name, - $parameters - + array( 'autologoutlink' => 'false', 'label' => '', 'scope' => implode( ',', $this->scope ), ) ); + $parameters + array( 'autologoutlink' => 'false', 'label' => '', 'scope' => implode( ',', $this->scope ), ) ); + } + + public function renderOnClicLogin( ) + { + return $this->templating->render( 'FOSFacebookBundle::triggerLogin.html.php' ); } public function logoutUrl( $parameters = array( ), $name = null ) diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index 3378b48..25f0bbd 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -41,7 +41,10 @@ public function getFunctions( ) array( 'is_safe' => array( 'html' ) )), 'facebook_login_button' => new \Twig_Function_Method( $this, 'renderLoginButton', array( 'is_safe' => array( 'html' ) )), - 'facebook_logout_url' => new \Twig_Function_Method( $this, 'renderLogoutUrl', array( 'is_safe' => array( 'html' ) )), ); + 'facebook_trigger_login' => new \Twig_Function_Method( $this, 'renderOnClicLogin', + array( 'is_safe' => array( 'html' ) )), + 'facebook_logout_url' => new \Twig_Function_Method( $this, 'renderLogoutUrl', + array( 'is_safe' => array( 'html' ) )), ); } /** @@ -70,9 +73,18 @@ public function renderInitialize( $parameters = array( ), $name = null ) */ public function renderLoginButton( $parameters = array( ), $name = null ) + { + return $this->container->get( 'fos_facebook.helper' )->loginButton( ); + } + + /** + * @see FacebookHelper::renderOnClicLogin() + */ + + public function renderOnClicLogin( $parameters = array( ), $name = null ) { return $this->container->get( 'fos_facebook.helper' ) - ->loginButton( $parameters, $name ? : 'FOSFacebookBundle::loginButton.html.twig' ); + ->renderOnClicLogin( $parameters, $name ? : 'FOSFacebookBundle::triggerLogin.html.twig' ); } /** From bbcee1a644e598aeb9d3e5c4a8d3277e0a075b09 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 20 May 2013 20:12:10 -0430 Subject: [PATCH 09/30] adding trigger login twig function --- Resources/views/loginButton.html.php | 5 +---- Resources/views/triggerLogin.html.php | 4 ++++ 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 Resources/views/triggerLogin.html.php diff --git a/Resources/views/loginButton.html.php b/Resources/views/loginButton.html.php index 574cc38..7935485 100644 --- a/Resources/views/loginButton.html.php +++ b/Resources/views/loginButton.html.php @@ -1,4 +1 @@ - - - +onclick="fbLogin();" \ No newline at end of file diff --git a/Resources/views/triggerLogin.html.php b/Resources/views/triggerLogin.html.php new file mode 100644 index 0000000..574cc38 --- /dev/null +++ b/Resources/views/triggerLogin.html.php @@ -0,0 +1,4 @@ + + + From a60984dacafef120441b897a3c41539e4f95ec84 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 20 May 2013 20:20:51 -0430 Subject: [PATCH 10/30] adding trigger login twig function --- Templating/Helper/FacebookHelper.php | 2 +- Twig/Extension/FacebookExtension.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Templating/Helper/FacebookHelper.php b/Templating/Helper/FacebookHelper.php index 3d7af7e..103946d 100644 --- a/Templating/Helper/FacebookHelper.php +++ b/Templating/Helper/FacebookHelper.php @@ -73,7 +73,7 @@ public function loginButton( $parameters = array( ), $name = null ) public function renderOnClicLogin( ) { - return $this->templating->render( 'FOSFacebookBundle::triggerLogin.html.php' ); + return $this->templating->render( 'FOSFacebookBundle::triggerLogin.html.twig' ); } public function logoutUrl( $parameters = array( ), $name = null ) diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index 25f0bbd..3a622d5 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -74,7 +74,8 @@ public function renderInitialize( $parameters = array( ), $name = null ) public function renderLoginButton( $parameters = array( ), $name = null ) { - return $this->container->get( 'fos_facebook.helper' )->loginButton( ); + return $this->container->get( 'fos_facebook.helper' ) + ->loginButton( $parameters, $name ? : 'FOSFacebookBundle::loginButton.html.twig' ); } /** @@ -83,8 +84,7 @@ public function renderLoginButton( $parameters = array( ), $name = null ) public function renderOnClicLogin( $parameters = array( ), $name = null ) { - return $this->container->get( 'fos_facebook.helper' ) - ->renderOnClicLogin( $parameters, $name ? : 'FOSFacebookBundle::triggerLogin.html.twig' ); + return $this->container->get( 'fos_facebook.helper' )->renderOnClicLogin( ); } /** From 60fe2216576df58a4e3a45e2ff7c9cb937191c38 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 20 May 2013 20:51:19 -0430 Subject: [PATCH 11/30] adding trigger login twig function --- Resources/views/triggerLogin.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/triggerLogin.html.twig b/Resources/views/triggerLogin.html.twig index 7935485..b013b8c 100644 --- a/Resources/views/triggerLogin.html.twig +++ b/Resources/views/triggerLogin.html.twig @@ -1 +1 @@ -onclick="fbLogin();" \ No newline at end of file +onclick="FB.login(function(response){}, {scope: 'email,user_birthday,user_location'});" \ No newline at end of file From 11d660160ea5a060fcaa0f2934f480e9cd5ec1b9 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 20 May 2013 21:43:23 -0430 Subject: [PATCH 12/30] fixing error of throwing AuthError --- Resources/views/triggerLogin.html.php | 4 ---- Security/Authentication/Provider/FacebookProvider.php | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 Resources/views/triggerLogin.html.php diff --git a/Resources/views/triggerLogin.html.php b/Resources/views/triggerLogin.html.php deleted file mode 100644 index 574cc38..0000000 --- a/Resources/views/triggerLogin.html.php +++ /dev/null @@ -1,4 +0,0 @@ - - - diff --git a/Security/Authentication/Provider/FacebookProvider.php b/Security/Authentication/Provider/FacebookProvider.php index 5470c9e..644e624 100644 --- a/Security/Authentication/Provider/FacebookProvider.php +++ b/Security/Authentication/Provider/FacebookProvider.php @@ -89,7 +89,7 @@ public function authenticate( TokenInterface $token ) } catch ( \Exception $failed ) { - throw new AuthenticationException( $failed->getMessage( ), null, ( int ) $failed->getCode( ), $failed); + throw new AuthenticationException( $failed->getMessage( ), ( int ) $failed->getCode( ), $failed); } throw new AuthenticationException( 'The Facebook user could not be retrieved from the session.'); From fd5c2ab028a51535494b5bec3ebaf795a1d04f68 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 20 May 2013 22:31:31 -0430 Subject: [PATCH 13/30] updating trigger login code --- Resources/views/triggerLogin.html.twig | 2 +- Templating/Helper/FacebookHelper.php | 5 +++-- Twig/Extension/FacebookExtension.php | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Resources/views/triggerLogin.html.twig b/Resources/views/triggerLogin.html.twig index b013b8c..56a4660 100644 --- a/Resources/views/triggerLogin.html.twig +++ b/Resources/views/triggerLogin.html.twig @@ -1 +1 @@ -onclick="FB.login(function(response){}, {scope: 'email,user_birthday,user_location'});" \ No newline at end of file +onclick="FB.login(function(response){}, {scope: '{{ scope }}'});" \ No newline at end of file diff --git a/Templating/Helper/FacebookHelper.php b/Templating/Helper/FacebookHelper.php index 103946d..735b2ab 100644 --- a/Templating/Helper/FacebookHelper.php +++ b/Templating/Helper/FacebookHelper.php @@ -71,9 +71,10 @@ public function loginButton( $parameters = array( ), $name = null ) $parameters + array( 'autologoutlink' => 'false', 'label' => '', 'scope' => implode( ',', $this->scope ), ) ); } - public function renderOnClicLogin( ) + public function renderOnClickLogin( ) { - return $this->templating->render( 'FOSFacebookBundle::triggerLogin.html.twig' ); + return $this->templating + ->render( 'FOSFacebookBundle::triggerLogin.html.twig', array( 'scope' => implode( ',', $this->scope ) ) ); } public function logoutUrl( $parameters = array( ), $name = null ) diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index 3a622d5..92a53f1 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -41,7 +41,7 @@ public function getFunctions( ) array( 'is_safe' => array( 'html' ) )), 'facebook_login_button' => new \Twig_Function_Method( $this, 'renderLoginButton', array( 'is_safe' => array( 'html' ) )), - 'facebook_trigger_login' => new \Twig_Function_Method( $this, 'renderOnClicLogin', + 'facebook_trigger_login' => new \Twig_Function_Method( $this, 'renderOnClickLogin', array( 'is_safe' => array( 'html' ) )), 'facebook_logout_url' => new \Twig_Function_Method( $this, 'renderLogoutUrl', array( 'is_safe' => array( 'html' ) )), ); @@ -82,7 +82,7 @@ public function renderLoginButton( $parameters = array( ), $name = null ) * @see FacebookHelper::renderOnClicLogin() */ - public function renderOnClicLogin( $parameters = array( ), $name = null ) + public function renderOnClickLogin( $parameters = array( ), $name = null ) { return $this->container->get( 'fos_facebook.helper' )->renderOnClicLogin( ); } From 9713022a928ba76047bd0a337c2340e86f8d90c3 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Tue, 21 May 2013 22:12:59 -0430 Subject: [PATCH 14/30] updating trigger login code --- Twig/Extension/FacebookExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index 92a53f1..e0a8e37 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -84,7 +84,7 @@ public function renderLoginButton( $parameters = array( ), $name = null ) public function renderOnClickLogin( $parameters = array( ), $name = null ) { - return $this->container->get( 'fos_facebook.helper' )->renderOnClicLogin( ); + return $this->container->get( 'fos_facebook.helper' )->renderOnClickLogin( ); } /** From 8dfa8e6b76ed9a3647eb617bf999bc10db6a22b6 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Wed, 22 May 2013 23:25:14 -0430 Subject: [PATCH 15/30] adding option to include only code on template functions --- Resources/views/initialize.html.php | 6 ++++++ Resources/views/initialize.html.twig | 4 ++++ Resources/views/triggerLogin.html.twig | 2 +- Templating/Helper/FacebookHelper.php | 11 +++++++---- Twig/Extension/FacebookExtension.php | 4 ++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Resources/views/initialize.html.php b/Resources/views/initialize.html.php index 40e3220..82b63f5 100644 --- a/Resources/views/initialize.html.php +++ b/Resources/views/initialize.html.php @@ -1,3 +1,5 @@ +
@@ -5,6 +7,7 @@ src="http://connect.facebook.net//all.js"> + diff --git a/Resources/views/initialize.html.twig b/Resources/views/initialize.html.twig index f19093a..edd507d 100644 --- a/Resources/views/initialize.html.twig +++ b/Resources/views/initialize.html.twig @@ -1,9 +1,11 @@ +{% if not onlycode %}
{% if not async %} {% endif %} +{% endif %} \ No newline at end of file diff --git a/Resources/views/triggerLogin.html.twig b/Resources/views/triggerLogin.html.twig index 56a4660..218b9db 100644 --- a/Resources/views/triggerLogin.html.twig +++ b/Resources/views/triggerLogin.html.twig @@ -1 +1 @@ -onclick="FB.login(function(response){}, {scope: '{{ scope }}'});" \ No newline at end of file +{% if not onlycode %}onclick="{% endif %}FB.login(function(response){}, {scope: '{{ scope }}'});{% if not onlycode %}"{% endif %} \ No newline at end of file diff --git a/Templating/Helper/FacebookHelper.php b/Templating/Helper/FacebookHelper.php index 735b2ab..862131e 100644 --- a/Templating/Helper/FacebookHelper.php +++ b/Templating/Helper/FacebookHelper.php @@ -60,7 +60,7 @@ public function initialize( $parameters = array( ), $name = null ) $parameters + array( 'async' => true, 'fbAsyncInit' => '', 'appId' => ( string ) $this->facebook->getAppId( ), 'xfbml' => false, 'oauth' => true, 'status' => false, 'cookie' => true, - 'logging' => $this->logging, 'culture' => $this->culture, ) ); + 'logging' => $this->logging, 'culture' => $this->culture, 'onlycode' => false ) ); } public function loginButton( $parameters = array( ), $name = null ) @@ -68,13 +68,16 @@ public function loginButton( $parameters = array( ), $name = null ) $name = $name ? : 'FOSFacebookBundle::loginButton.html.php'; return $this->templating ->render( $name, - $parameters + array( 'autologoutlink' => 'false', 'label' => '', 'scope' => implode( ',', $this->scope ), ) ); + $parameters + + array( 'autologoutlink' => 'false', 'label' => '', 'scope' => implode( ',', $this->scope ), + 'onlycode' => false ) ); } - public function renderOnClickLogin( ) + public function renderOnClickLogin( $onlycode = false ) { return $this->templating - ->render( 'FOSFacebookBundle::triggerLogin.html.twig', array( 'scope' => implode( ',', $this->scope ) ) ); + ->render( 'FOSFacebookBundle::triggerLogin.html.twig', + array( 'scope' => implode( ',', $this->scope ), 'onlycode' => false ) ); } public function logoutUrl( $parameters = array( ), $name = null ) diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index e0a8e37..bc29e76 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -82,9 +82,9 @@ public function renderLoginButton( $parameters = array( ), $name = null ) * @see FacebookHelper::renderOnClicLogin() */ - public function renderOnClickLogin( $parameters = array( ), $name = null ) + public function renderOnClickLogin( $onlycode = false, $name = null ) { - return $this->container->get( 'fos_facebook.helper' )->renderOnClickLogin( ); + return $this->container->get( 'fos_facebook.helper' )->renderOnClickLogin( $onlycode ); } /** From aa52fe7904e198b54ad2553b4e80c2d3240e1f60 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Wed, 22 May 2013 23:40:25 -0430 Subject: [PATCH 16/30] adding option to include only code on template functions --- Templating/Helper/FacebookHelper.php | 2 +- Twig/Extension/FacebookExtension.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Templating/Helper/FacebookHelper.php b/Templating/Helper/FacebookHelper.php index 862131e..6d9ec6d 100644 --- a/Templating/Helper/FacebookHelper.php +++ b/Templating/Helper/FacebookHelper.php @@ -77,7 +77,7 @@ public function renderOnClickLogin( $onlycode = false ) { return $this->templating ->render( 'FOSFacebookBundle::triggerLogin.html.twig', - array( 'scope' => implode( ',', $this->scope ), 'onlycode' => false ) ); + array( 'scope' => implode( ',', $this->scope ), 'onlycode' => $onlycode ) ); } public function logoutUrl( $parameters = array( ), $name = null ) diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index bc29e76..75d202a 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -82,7 +82,7 @@ public function renderLoginButton( $parameters = array( ), $name = null ) * @see FacebookHelper::renderOnClicLogin() */ - public function renderOnClickLogin( $onlycode = false, $name = null ) + public function renderOnClickLogin( $onlycode = false ) { return $this->container->get( 'fos_facebook.helper' )->renderOnClickLogin( $onlycode ); } From 31245da568530be15961b23ddca3a63ce1a5a68a Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 26 May 2013 11:57:16 -0430 Subject: [PATCH 17/30] updating composer --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ebe7034..95a9b49 100644 --- a/composer.json +++ b/composer.json @@ -26,5 +26,6 @@ "autoload": { "psr-0": { "FOS\\FacebookBundle": "" } }, - "target-dir": "FOS/FacebookBundle" + "target-dir": "FOS/FacebookBundle", + "minimum-stability": "stable" } From fd5058339ca59f509c61434b73f0c0dcad68f4ba Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 26 May 2013 12:47:04 -0430 Subject: [PATCH 18/30] updating composer --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 95a9b49..c4fc44a 100644 --- a/composer.json +++ b/composer.json @@ -21,11 +21,10 @@ "symfony/framework-bundle": ">=2.1", "symfony/security-bundle": ">=2.1", "symfony/twig-bundle": ">=2.1", - "facebook/php-sdk": "dev-master" + "facebook/php-sdk": ">=v3.2.2" }, "autoload": { "psr-0": { "FOS\\FacebookBundle": "" } }, - "target-dir": "FOS/FacebookBundle", - "minimum-stability": "stable" + "target-dir": "FOS/FacebookBundle" } From 83e18cfec9de8dbfdae5d2b78d6ff7192b2ac448 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 26 May 2013 21:13:58 -0430 Subject: [PATCH 19/30] updating new functions --- Resources/views/loginButton.html.php | 2 +- Resources/views/scope.html.php | 1 + Resources/views/scope.html.twig | 1 + Resources/views/triggerLogin.html.twig | 1 - Security/Firewall/FacebookListener.php | 2 +- Templating/Helper/FacebookHelper.php | 13 +++++++++---- Twig/Extension/FacebookExtension.php | 19 ++++++++++++++----- 7 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 Resources/views/scope.html.php create mode 100644 Resources/views/scope.html.twig delete mode 100644 Resources/views/triggerLogin.html.twig diff --git a/Resources/views/loginButton.html.php b/Resources/views/loginButton.html.php index 7935485..51e7f49 100644 --- a/Resources/views/loginButton.html.php +++ b/Resources/views/loginButton.html.php @@ -1 +1 @@ -onclick="fbLogin();" \ No newline at end of file + diff --git a/Resources/views/scope.html.php b/Resources/views/scope.html.php new file mode 100644 index 0000000..ad41df8 --- /dev/null +++ b/Resources/views/scope.html.php @@ -0,0 +1 @@ +"" \ No newline at end of file diff --git a/Resources/views/scope.html.twig b/Resources/views/scope.html.twig new file mode 100644 index 0000000..75f6840 --- /dev/null +++ b/Resources/views/scope.html.twig @@ -0,0 +1 @@ +'{{ scope }}' \ No newline at end of file diff --git a/Resources/views/triggerLogin.html.twig b/Resources/views/triggerLogin.html.twig deleted file mode 100644 index 218b9db..0000000 --- a/Resources/views/triggerLogin.html.twig +++ /dev/null @@ -1 +0,0 @@ -{% if not onlycode %}onclick="{% endif %}FB.login(function(response){}, {scope: '{{ scope }}'});{% if not onlycode %}"{% endif %} \ No newline at end of file diff --git a/Security/Firewall/FacebookListener.php b/Security/Firewall/FacebookListener.php index bb053c6..56a21d5 100644 --- a/Security/Firewall/FacebookListener.php +++ b/Security/Firewall/FacebookListener.php @@ -22,7 +22,7 @@ class FacebookListener extends AbstractAuthenticationListener protected function attemptAuthentication( Request $request ) { - if ( !$request->getQueryString( ) ) + if ( $request->get( "facebook", null ) ) return $this->authenticationManager->authenticate( new FacebookUserToken( $this->providerKey) ); } } diff --git a/Templating/Helper/FacebookHelper.php b/Templating/Helper/FacebookHelper.php index 6d9ec6d..8bc340d 100644 --- a/Templating/Helper/FacebookHelper.php +++ b/Templating/Helper/FacebookHelper.php @@ -73,11 +73,16 @@ public function loginButton( $parameters = array( ), $name = null ) 'onlycode' => false ) ); } - public function renderOnClickLogin( $onlycode = false ) + public function scope( $name = null ) { - return $this->templating - ->render( 'FOSFacebookBundle::triggerLogin.html.twig', - array( 'scope' => implode( ',', $this->scope ), 'onlycode' => $onlycode ) ); + $name = $name ? : 'FOSFacebookBundle::scope.html.twig'; + return $this->templating->render( $name, array( 'scope' => implode( ',', $this->scope ) ) ); + } + + public function loginUrl( $redirectUrl, $parameters = array( ) ) + { + $parameters[ "redirect_uri" ] = $redirectUrl . "?facebook=true"; + return $this->facebook->getLoginUrl( $parameters + array( 'scope' => implode( ',', $this->scope ) ) ); } public function logoutUrl( $parameters = array( ), $name = null ) diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index 75d202a..a686587 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -41,8 +41,8 @@ public function getFunctions( ) array( 'is_safe' => array( 'html' ) )), 'facebook_login_button' => new \Twig_Function_Method( $this, 'renderLoginButton', array( 'is_safe' => array( 'html' ) )), - 'facebook_trigger_login' => new \Twig_Function_Method( $this, 'renderOnClickLogin', - array( 'is_safe' => array( 'html' ) )), + 'facebook_scope' => new \Twig_Function_Method( $this, 'renderScope', array( 'is_safe' => array( 'html' ) )), + 'facebook_login_url' => new \Twig_Function_Method( $this, 'renderLoginUrl', array( 'is_safe' => array( 'html' ) )), 'facebook_logout_url' => new \Twig_Function_Method( $this, 'renderLogoutUrl', array( 'is_safe' => array( 'html' ) )), ); } @@ -79,12 +79,21 @@ public function renderLoginButton( $parameters = array( ), $name = null ) } /** - * @see FacebookHelper::renderOnClicLogin() + * @see FacebookHelper::scope() + */ + + public function renderScope( ) + { + return $this->container->get( 'fos_facebook.helper' )->scope( ); + } + + /** + * @see FacebookHelper::authUrl() */ - public function renderOnClickLogin( $onlycode = false ) + public function renderLoginUrl( $redirectUtl, $parameters = array( ) ) { - return $this->container->get( 'fos_facebook.helper' )->renderOnClickLogin( $onlycode ); + return $this->container->get( 'fos_facebook.helper' )->loginUrl( $redirectUtl, $parameters ); } /** From 1866e7aeb9ff0e4ab104a7bf6f7cbcb27603e0c8 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 2 Jun 2013 12:20:48 -0430 Subject: [PATCH 20/30] adding new login js function --- Controller/AuthController.php | 16 ++++++++++ Resources/config/routing.yml | 4 +++ Resources/config/security.xml | 1 + Resources/views/loginFunction.html.twig | 29 +++++++++++++++++++ .../Provider/FacebookProvider.php | 25 +++++++++------- Templating/Helper/FacebookHelper.php | 6 ++++ Twig/Extension/FacebookExtension.php | 14 ++++++++- 7 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 Controller/AuthController.php create mode 100644 Resources/config/routing.yml create mode 100644 Resources/views/loginFunction.html.twig diff --git a/Controller/AuthController.php b/Controller/AuthController.php new file mode 100644 index 0000000..3d2b18c --- /dev/null +++ b/Controller/AuthController.php @@ -0,0 +1,16 @@ +getRequest( )->get( "accessToken" ); + $this->get( "session" )->set( "fb.accessToken", $accessToken ); + return new JsonResponse( array( "accessToken" => $accessToken )); + } +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml new file mode 100644 index 0000000..13c9d1b --- /dev/null +++ b/Resources/config/routing.yml @@ -0,0 +1,4 @@ +fbWriteAuthInSession: + pattern: /fb/writeAuthInSession + defaults: { _controller: FOSFacebookBundle:Auth:writeAuthInSession } + methods: [POST] \ No newline at end of file diff --git a/Resources/config/security.xml b/Resources/config/security.xml index 50e85ab..c9318bf 100644 --- a/Resources/config/security.xml +++ b/Resources/config/security.xml @@ -10,6 +10,7 @@ public="false"> + providerKey = $providerKey; $this->facebook = $facebook; + $this->session = $session; $this->userProvider = $userProvider; $this->userChecker = $userChecker; $this->createIfNotExists = $createIfNotExists; + + // workaraound to use a previous login valid js access token setted in the session + $sessionAccessToken = $this->session->get( "fb.accessToken" ); + $apiAccessToken = $this->facebook->getAccessToken( ); + $appAccessToken = $this->facebook->getAppId( ) . '|' . $this->facebook->getAppSecret( ); + + if ( $sessionAccessToken !== $apiAccessToken && $apiAccessToken === $appAccessToken ) + $this->facebook->setAccessToken( $sessionAccessToken ); } public function authenticate( TokenInterface $token ) diff --git a/Templating/Helper/FacebookHelper.php b/Templating/Helper/FacebookHelper.php index 8bc340d..e8b15f0 100644 --- a/Templating/Helper/FacebookHelper.php +++ b/Templating/Helper/FacebookHelper.php @@ -85,6 +85,12 @@ public function loginUrl( $redirectUrl, $parameters = array( ) ) return $this->facebook->getLoginUrl( $parameters + array( 'scope' => implode( ',', $this->scope ) ) ); } + public function loginFunction( $loginCheck, $name = null ) + { + $name = $name ? : 'FOSFacebookBundle::loginFunction.html.twig'; + return $this->templating->render( $name, array( 'loginCheck' => $loginCheck ) ); + } + public function logoutUrl( $parameters = array( ), $name = null ) { return $this->facebook->getLogoutUrl( $parameters ); diff --git a/Twig/Extension/FacebookExtension.php b/Twig/Extension/FacebookExtension.php index a686587..2b541b4 100644 --- a/Twig/Extension/FacebookExtension.php +++ b/Twig/Extension/FacebookExtension.php @@ -42,7 +42,10 @@ public function getFunctions( ) 'facebook_login_button' => new \Twig_Function_Method( $this, 'renderLoginButton', array( 'is_safe' => array( 'html' ) )), 'facebook_scope' => new \Twig_Function_Method( $this, 'renderScope', array( 'is_safe' => array( 'html' ) )), - 'facebook_login_url' => new \Twig_Function_Method( $this, 'renderLoginUrl', array( 'is_safe' => array( 'html' ) )), + 'facebook_login_function' => new \Twig_Function_Method( $this, 'renderLoginFunction', + array( 'is_safe' => array( 'html' ) )), + 'facebook_login_url' => new \Twig_Function_Method( $this, 'renderLoginUrl', + array( 'is_safe' => array( 'html' ) )), 'facebook_logout_url' => new \Twig_Function_Method( $this, 'renderLogoutUrl', array( 'is_safe' => array( 'html' ) )), ); } @@ -87,6 +90,15 @@ public function renderScope( ) return $this->container->get( 'fos_facebook.helper' )->scope( ); } + /** + * @see FacebookHelper::loginFunction() + */ + + public function renderLoginFunction( $loginCheck ) + { + return $this->container->get( 'fos_facebook.helper' )->loginFunction( $loginCheck ); + } + /** * @see FacebookHelper::authUrl() */ From 59fc221d457091f9535c9c77456af1abe0267298 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 2 Jun 2013 23:20:31 -0430 Subject: [PATCH 21/30] adding oauth popup library --- Resources/views/loginFunction.html.twig | 63 ++++++++++++++++++++----- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index 994da37..d5444e1 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -1,14 +1,55 @@ -function oauthWindow(location, callbackFnc) -{ - var oauthWindow = window.open(location, 'Facebook', 'location=0,status=0,width=800,height=400'); - var oauthInterval = window.setInterval(function(){ - if (oauthWindow.closed) { - window.clearInterval(oauthInterval); - callbackFnc(); - } - }, 1000); -} - +$(document).ready(function() { + (function($){ + // inspired by DISQUS + $.oauthpopup = function(options) + { + if (!options || !options.path) { + throw new Error("options.path must not be empty"); + } + options = $.extend({ + windowName: 'ConnectWithOAuth' // should not include space for IE + , windowOptions: 'location=0,status=0,width=800,height=400' + , callback: function(){ window.location.reload(); } + }, options); + + var oauthWindow = window.open(options.path, options.windowName, options.windowOptions); + var oauthInterval = window.setInterval(function(){ + if (oauthWindow.closed) { + window.clearInterval(oauthInterval); + options.callback(); + } + }, 1000); + }; + })(jQuery); + + function fbLogin(callbackFnc){ + if (typeof (FB) != 'undefined' && FB != null) { + FB.getLoginStatus(function(response) { + if (response.status === 'connected') { + var accessToken = response.authResponse.accessToken; + $.post("{{ path('fbWriteAuthInSession') }}", { + "accessToken" : accessToken + }, function(response) { + if (response.accessToken == accessToken) { + $.oauthpopup({ + path : "{{ path(loginCheck) }}" + "?facebook=true", + callback : callbackFnc + }); + } + }); + } else if (response.status === 'not_authorized') { + // the user is logged in to Facebook, + // but has not authenticated your app + } else { + $.oauthpopup({ + path : "{{ facebook_login_url(url(loginCheck)) }}", + callback : callbackFnc + }); + } + }); + } + } +}); function fbLogin(callbackFnc){ if (typeof (FB) != 'undefined' && FB != null) { FB.getLoginStatus(function(response) { From 96c103eebadf8658258a03c00e137df3493ba07d Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 2 Jun 2013 23:25:52 -0430 Subject: [PATCH 22/30] fixing new funciton --- Resources/views/loginFunction.html.twig | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index d5444e1..51f1261 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -49,22 +49,4 @@ $(document).ready(function() { }); } } -}); -function fbLogin(callbackFnc){ - if (typeof (FB) != 'undefined' && FB != null) { - FB.getLoginStatus(function(response) { - if (response.status === 'connected') { - var accessToken = response.authResponse.accessToken; - $.post("{{ path('fbWriteAuthInSession') }}", { - "accessToken" : accessToken - }, function(response) { - if (response.accessToken == accessToken) - oauthWindow("{{ path(loginCheck) }}" + "?facebook=true", callbackFnc); - }); - } else if (response.status === 'not_authorized') { - // the user is logged in to Facebook, - // but has not authenticated your app - } else oauthWindow("{{ facebook_login_url(url(loginCheck)) }}", callbackFnc); - }); - } -} \ No newline at end of file +}); \ No newline at end of file From 266a139900ae510ac7d8c9e59d6f2cf5872040c6 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Sun, 2 Jun 2013 23:34:58 -0430 Subject: [PATCH 23/30] reverting change forcing to include this jquery plugin --- Resources/views/loginFunction.html.twig | 29 ++----------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index 51f1261..1607412 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -1,28 +1,4 @@ -$(document).ready(function() { - (function($){ - // inspired by DISQUS - $.oauthpopup = function(options) - { - if (!options || !options.path) { - throw new Error("options.path must not be empty"); - } - options = $.extend({ - windowName: 'ConnectWithOAuth' // should not include space for IE - , windowOptions: 'location=0,status=0,width=800,height=400' - , callback: function(){ window.location.reload(); } - }, options); - - var oauthWindow = window.open(options.path, options.windowName, options.windowOptions); - var oauthInterval = window.setInterval(function(){ - if (oauthWindow.closed) { - window.clearInterval(oauthInterval); - options.callback(); - } - }, 1000); - }; - })(jQuery); - - function fbLogin(callbackFnc){ +function fbLogin(callbackFnc){ if (typeof (FB) != 'undefined' && FB != null) { FB.getLoginStatus(function(response) { if (response.status === 'connected') { @@ -48,5 +24,4 @@ $(document).ready(function() { } }); } - } -}); \ No newline at end of file + } \ No newline at end of file From 1c66720f11dabd0be58168c9b7dee282591b7c19 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Thu, 6 Jun 2013 16:30:08 -0430 Subject: [PATCH 24/30] adding function to handle unauthorized --- Resources/views/loginFunction.html.twig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index 1607412..4465542 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -1,4 +1,4 @@ -function fbLogin(callbackFnc){ +function fbLogin(callbackFnc, notAuthCallbackFnc){ if (typeof (FB) != 'undefined' && FB != null) { FB.getLoginStatus(function(response) { if (response.status === 'connected') { @@ -16,6 +16,9 @@ function fbLogin(callbackFnc){ } else if (response.status === 'not_authorized') { // the user is logged in to Facebook, // but has not authenticated your app + if (typeof fnc === "undefined" || fnc===null) + notAuthCallbackFnc = function(){}; + notAuthCallbackFnc(); } else { $.oauthpopup({ path : "{{ facebook_login_url(url(loginCheck)) }}", From ef67b3b8e3cd2410a839ed3fffb42697fe76d241 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Fri, 7 Jun 2013 00:03:32 -0430 Subject: [PATCH 25/30] fixing error on login function --- Resources/views/loginFunction.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index 4465542..eccd19d 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -16,7 +16,7 @@ function fbLogin(callbackFnc, notAuthCallbackFnc){ } else if (response.status === 'not_authorized') { // the user is logged in to Facebook, // but has not authenticated your app - if (typeof fnc === "undefined" || fnc===null) + if (typeof notAuthCallbackFnc === "undefined" || notAuthCallbackFnc===null) notAuthCallbackFnc = function(){}; notAuthCallbackFnc(); } else { From 0e9aa57430613260fdfbf16ef5a44b29f34aac56 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Fri, 7 Jun 2013 00:34:08 -0430 Subject: [PATCH 26/30] updating js login funciton --- Resources/views/loginFunction.html.twig | 36 ++++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index eccd19d..be9b568 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -1,25 +1,23 @@ +function doFBLogin(accessToken, callbackFnc){ + $.post("{{ path('fbWriteAuthInSession') }}", { + "accessToken" : accessToken + }, function(response) { + if (response.accessToken == accessToken) { + $.oauthpopup({ + path : "{{ path(loginCheck) }}" + "?facebook=true", + callback : callbackFnc + }); + } + }); +} + function fbLogin(callbackFnc, notAuthCallbackFnc){ if (typeof (FB) != 'undefined' && FB != null) { FB.getLoginStatus(function(response) { - if (response.status === 'connected') { - var accessToken = response.authResponse.accessToken; - $.post("{{ path('fbWriteAuthInSession') }}", { - "accessToken" : accessToken - }, function(response) { - if (response.accessToken == accessToken) { - $.oauthpopup({ - path : "{{ path(loginCheck) }}" + "?facebook=true", - callback : callbackFnc - }); - } - }); - } else if (response.status === 'not_authorized') { - // the user is logged in to Facebook, - // but has not authenticated your app - if (typeof notAuthCallbackFnc === "undefined" || notAuthCallbackFnc===null) - notAuthCallbackFnc = function(){}; - notAuthCallbackFnc(); - } else { + if (response.status === 'connected' || response.status === 'not_authorized') { + var accessToken = response.authResponse.accessToken; + doFBLogin(accessToken, notAuthCallbackFnc); + else { $.oauthpopup({ path : "{{ facebook_login_url(url(loginCheck)) }}", callback : callbackFnc From de9fda3935577e50130055d5306ff31b0cef4b73 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Fri, 7 Jun 2013 01:35:42 -0430 Subject: [PATCH 27/30] updating js login funciton --- Resources/views/loginFunction.html.twig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index be9b568..37b24b0 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -17,6 +17,7 @@ function fbLogin(callbackFnc, notAuthCallbackFnc){ if (response.status === 'connected' || response.status === 'not_authorized') { var accessToken = response.authResponse.accessToken; doFBLogin(accessToken, notAuthCallbackFnc); + } else { $.oauthpopup({ path : "{{ facebook_login_url(url(loginCheck)) }}", @@ -25,4 +26,5 @@ function fbLogin(callbackFnc, notAuthCallbackFnc){ } }); } - } \ No newline at end of file + } +} \ No newline at end of file From 48300121f7a17989bff1a0ae502b52124e3d16e9 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Fri, 7 Jun 2013 01:37:58 -0430 Subject: [PATCH 28/30] updating js login funciton --- Resources/views/loginFunction.html.twig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index 37b24b0..d8d2f16 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -25,6 +25,5 @@ function fbLogin(callbackFnc, notAuthCallbackFnc){ }); } }); - } - } + } } \ No newline at end of file From fe0743e43f268e30b927454680552ac1b3c7b6b1 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Fri, 7 Jun 2013 01:44:15 -0430 Subject: [PATCH 29/30] updating js login funciton --- Resources/views/loginFunction.html.twig | 26 +++++++++++-------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Resources/views/loginFunction.html.twig b/Resources/views/loginFunction.html.twig index d8d2f16..53add4b 100644 --- a/Resources/views/loginFunction.html.twig +++ b/Resources/views/loginFunction.html.twig @@ -1,22 +1,18 @@ -function doFBLogin(accessToken, callbackFnc){ - $.post("{{ path('fbWriteAuthInSession') }}", { - "accessToken" : accessToken - }, function(response) { - if (response.accessToken == accessToken) { - $.oauthpopup({ - path : "{{ path(loginCheck) }}" + "?facebook=true", - callback : callbackFnc - }); - } - }); -} - function fbLogin(callbackFnc, notAuthCallbackFnc){ if (typeof (FB) != 'undefined' && FB != null) { FB.getLoginStatus(function(response) { - if (response.status === 'connected' || response.status === 'not_authorized') { + if (response.status === 'connected') { var accessToken = response.authResponse.accessToken; - doFBLogin(accessToken, notAuthCallbackFnc); + $.post("{{ path('fbWriteAuthInSession') }}", { + "accessToken" : accessToken + }, function(response) { + if (response.accessToken == accessToken) { + $.oauthpopup({ + path : "{{ path(loginCheck) }}" + "?facebook=true", + callback : callbackFnc + }); + } + }); } else { $.oauthpopup({ From 940f752a4d0253ec632ed5c8fd223bea6fedadb0 Mon Sep 17 00:00:00 2001 From: Yasmany Cubela Medina Date: Mon, 1 Jul 2013 23:48:03 -0430 Subject: [PATCH 30/30] cleaning files to prepare migration to BIT namespace --- README.md | 483 ------------------ .../DependencyInjection/ConfigurationTest.php | 26 - .../FOSFacebookExtensionTest.php | 71 --- .../Security/Factory/FacebookFactoryTest.php | 108 ---- Tests/FacebookApiException.php | 14 - Tests/Kernel.php | 75 --- .../Provider/FacebookProviderTest.php | 203 -------- .../Token/FacebookUserTokenTest.php | 45 -- .../FacebookAuthenticationEntryPointTest.php | 72 --- .../Firewall/FacebookListenerTest.php | 84 --- .../Templating/Helper/FacebookHelperTest.php | 63 --- .../Twig/Extension/FacebookExtensionTest.php | 75 --- Tests/bootstrap.php | 18 - phpunit.xml.dist | 21 - 14 files changed, 1358 deletions(-) delete mode 100644 README.md delete mode 100644 Tests/DependencyInjection/ConfigurationTest.php delete mode 100644 Tests/DependencyInjection/FOSFacebookExtensionTest.php delete mode 100644 Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php delete mode 100644 Tests/FacebookApiException.php delete mode 100644 Tests/Kernel.php delete mode 100644 Tests/Security/Authentication/Provider/FacebookProviderTest.php delete mode 100644 Tests/Security/Authentication/Token/FacebookUserTokenTest.php delete mode 100644 Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php delete mode 100644 Tests/Security/Firewall/FacebookListenerTest.php delete mode 100644 Tests/Templating/Helper/FacebookHelperTest.php delete mode 100644 Tests/Twig/Extension/FacebookExtensionTest.php delete mode 100644 Tests/bootstrap.php delete mode 100644 phpunit.xml.dist diff --git a/README.md b/README.md deleted file mode 100644 index 5ff3133..0000000 --- a/README.md +++ /dev/null @@ -1,483 +0,0 @@ -Introduction -============ - -This Bundle enables integration of the Facebook PHP and JS SDK's. Furthermore it -also provides a Symfony2 authentication provider so that users can login to a -Symfony2 application via Facebook. Furthermore via custom user provider support -the Facebook login can also be integrated with other data sources like the -database based solution provided by FOSUserBundle. - -Note that logging in a user requires multiple steps: - - 1. the user must be logged into Facebook - 2. the user must connect his Facebook account to your site - 3. once the user has done 1. and 2. you must trigger the login - -For step 1. and 2. there are two options: - - 1. Select "OAuth Migration" in the Facebook application settings. - 2. Add a Facebook login button, this approach requires JS code to handle step 3. - 3. Letting FOSFacebookBundle redirect to the Facebook login page. - -Note that the later happens automatically if the first provider in your first -firewall configuration is configured to FOSFacebookBundle and the user accesses -a page that requires authentication without being authenticated. - -Please also refer to the Facebook documentation: -https://developers.facebook.com/docs/guides/web/ - -Please also refer to the official documentation of the SecurityBundle, especially -for details on the configuration: -http://symfony.com/doc/current/book/security.html - -[![Build Status](https://secure.travis-ci.org/FriendsOfSymfony/FOSFacebookBundle.png?branch=master)](http://travis-ci.org/FriendsOfSymfony/FOSFacebookBundle) - -Prerequisites -============ - -This version requires Symfony 2.1 - - -Installation -============ - - 1. Add the following lines in your composer.json: - - ``` -{ - "require": { - "friendsofsymfony/facebook-bundle": "dev-master" - } -} - -``` - 2. Run the composer to download the bundle - -``` bash -$ php composer.phar update friendsofsymfony/facebook-bundle -``` - - - 3. Add this bundle to your application's kernel: - - // app/ApplicationKernel.php - public function registerBundles() - { - return array( - // ... - new FOS\FacebookBundle\FOSFacebookBundle(), - // ... - ); - } - - 4. Add the following routes to your application and point them at actual controller actions - - #application/config/routing.yml - _security_check: - pattern: /login_check - _security_logout: - pattern: /logout - - #application/config/routing.xml - - - - 5. Configure the `facebook` service in your config: - - # application/config/config.yml - fos_facebook: - file: %kernel.root_dir%/../vendor/facebook/src/base_facebook.php - alias: facebook - app_id: 123456879 - secret: s3cr3t - cookie: true - permissions: [email, user_birthday, user_location] - - # application/config/config.xml - - email - user_birthday - user_location - - - If you do not include a `file` value in the config you will have to - configure your application to autoload the `BaseFacebook` class. - - 6. Add this configuration if you want to use the `security component`: - - # application/config/config.yml - security: - firewalls: - public: - # since anonymous is allowed users will not be forced to login - pattern: ^/.* - fos_facebook: - app_url: "http://apps.facebook.com/appName/" - server_url: "http://localhost/facebookApp/" - anonymous: true - logout: - handlers: ["fos_facebook.logout_handler"] - - access_control: - - { path: ^/secured/.*, role: [IS_AUTHENTICATED_FULLY] } # This is the route secured with fos_facebook - - { path: ^/.*, role: [IS_AUTHENTICATED_ANONYMOUSLY] } - - You have to add `/secured/` in your routing for this to work. An example would be... - - _facebook_secured: - pattern: /secured/ - defaults: { _controller: AcmeDemoBundle:Welcome:index } - - 7. Optionally define a custom user provider class and use it as the provider or define path for login - - # application/config/config.yml - security: - providers: - # choose the provider name freely - my_fos_facebook_provider: - id: my.facebook.user # see "Example Custom User Provider using the FOS\UserBundle" chapter further down - - firewalls: - public: - pattern: ^/.* - fos_facebook: - app_url: "http://apps.facebook.com/appName/" - server_url: "http://localhost/facebookApp/" - login_path: /login - check_path: /login_check - default_target_path: / - provider: my_fos_facebook_provider - anonymous: true - logout: - handlers: ["fos_facebook.logout_handler"] - - # application/config/config_dev.yml - security: - firewalls: - public: - fos_facebook: - app_url: "http://apps.facebook.com/appName/" - server_url: "http://localhost/facebookApp/app_dev.php/" - - 8. Optionally use access control to secure specific URLs - - - # application/config/config.yml - security: - # ... - - access_control: - - { path: ^/facebook/, role: [ROLE_FACEBOOK] } - - { path: ^/.*, role: [IS_AUTHENTICATED_ANONYMOUSLY] } - - The role `ROLE_FACEBOOK` has to be added in your User class (see Acme\MyBundle\Entity\User::setFBData() below). - > Note that the order of access control rules matters! - -Setting up the JavaScript SDK ------------------------------ - -A templating helper is included for loading the Facebook JavaScript SDK and -initializing it with parameters from your service container. To setup the -Facebook JavaScript environment, add the following to your layout just after -the opening `body` tag: - - - - initialize(array('xfbml' => true, 'fbAsyncInit' => 'onFbInit();')) ?> - - {{ facebook_initialize({'xfbml': true, 'fbAsyncInit': 'onFbInit();'}) }} - -Note that `fbAsyncInit` is a parameter helping you to execute JavaScript within -the function initializing the connection with Facebook, just after the `FB.init();` -call. `onFbInit();` is a JavaScript function defined furthermore to execute functions -which need `FB` initialized. - -If you will be adding XFBML markup to your site you may also declare the -namespace, perhaps in the opening `html` tag: - - - -Include the login button in your templates ------------------------------------------- - -Just add the following code in one of your templates: - - - loginButton(array('autologoutlink' => true)) ?> - - {{ facebook_login_button({'autologoutlink': true}) }} - -Note that with this approach only the login and connecting with Facebook will -be handled. The step of logging in the user into your Symfony2 application -still needs to be triggered. To do this you will in most cases simply subscribe -to the `auth.statusChange` event and then redirect to the `check_path`: - - - -Note that we wait 500ms before redirecting to let the browser dealing with the -Facebook cookie. You can avoid this step but you might get this error message: -*"The Facebook user could not be retrieved from the session."* - -The "_security_check" route would need to point to a "/login_check" pattern -to match the above configuration. - -Also, you need to trigger the logout action, so, using the same event (`auth.statusChange`), add a simple -check for `response.session` to redirect to the "logout" route: - - - - -Example Custom User Provider using the FOS\UserBundle -------------------------------------------------------- - -This requires adding a service for the custom user provider which is then set -to the provider id in the "provider" section in the config.yml: - - services: - my.facebook.user: - class: Acme\MyBundle\Security\User\Provider\FacebookProvider - arguments: - facebook: "@fos_facebook.api" - userManager: "@fos_user.user_manager" - validator: "@validator" - container: "@service_container" - - facebook = $facebook; - $this->userManager = $userManager; - $this->validator = $validator; - } - - public function supportsClass($class) - { - return $this->userManager->supportsClass($class); - } - - public function findUserByFbId($fbId) - { - return $this->userManager->findUserBy(array('facebookId' => $fbId)); - } - - public function loadUserByUsername($username) - { - $user = $this->findUserByFbId($username); - - try { - $fbdata = $this->facebook->api('/me'); - } catch (FacebookApiException $e) { - $fbdata = null; - } - - if (!empty($fbdata)) { - if (empty($user)) { - $user = $this->userManager->createUser(); - $user->setEnabled(true); - $user->setPassword(''); - } - - // TODO use http://developers.facebook.com/docs/api/realtime - $user->setFBData($fbdata); - - if (count($this->validator->validate($user, 'Facebook'))) { - // TODO: the user was found obviously, but doesnt match our expectations, do something smart - throw new UsernameNotFoundException('The facebook user could not be stored'); - } - $this->userManager->updateUser($user); - } - - if (empty($user)) { - throw new UsernameNotFoundException('The user is not authenticated on facebook'); - } - - return $user; - } - - public function refreshUser(UserInterface $user) - { - if (!$this->supportsClass(get_class($user)) || !$user->getFacebookId()) { - throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); - } - - return $this->loadUserByUsername($user->getFacebookId()); - } - } - -Finally one also needs to add a getFacebookId() and setFBData() method to the User model. -The following example also adds "firstname" and "lastname" properties, using the Doctrine ORM: - - facebookId, parent::serialize())); - } - - public function unserialize($data) - { - list($this->facebookId, $parentData) = unserialize($data); - parent::unserialize($parentData); - } - - /** - * @return string - */ - public function getFirstname() - { - return $this->firstname; - } - - /** - * @param string $firstname - */ - public function setFirstname($firstname) - { - $this->firstname = $firstname; - } - - /** - * @return string - */ - public function getLastname() - { - return $this->lastname; - } - - /** - * @param string $lastname - */ - public function setLastname($lastname) - { - $this->lastname = $lastname; - } - - /** - * Get the full name of the user (first + last name) - * @return string - */ - public function getFullName() - { - return $this->getFirstName() . ' ' . $this->getLastname(); - } - - /** - * @param string $facebookId - * @return void - */ - public function setFacebookId($facebookId) - { - $this->facebookId = $facebookId; - $this->setUsername($facebookId); - $this->salt = ''; - } - - /** - * @return string - */ - public function getFacebookId() - { - return $this->facebookId; - } - - /** - * @param Array - */ - public function setFBData($fbdata) - { - if (isset($fbdata['id'])) { - $this->setFacebookId($fbdata['id']); - $this->addRole('ROLE_FACEBOOK'); - } - if (isset($fbdata['first_name'])) { - $this->setFirstname($fbdata['first_name']); - } - if (isset($fbdata['last_name'])) { - $this->setLastname($fbdata['last_name']); - } - if (isset($fbdata['email'])) { - $this->setEmail($fbdata['email']); - } - } - } diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php deleted file mode 100644 index 5238f2e..0000000 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\DependencyInjection; -use Symfony\Component\Config\Definition\ConfigurationInterface; -use FOS\FacebookBundle\DependencyInjection\Configuration; - -class ConfigurationTest extends \PHPUnit_Framework_TestCase -{ - - public function testThatCanGetConfigTreeBuilder( ) - { - $configuration = new Configuration( ); - $this - ->assertInstanceOf( 'Symfony\Component\Config\Definition\Builder\TreeBuilder', - $configuration->getConfigTreeBuilder( ) ); - } -} diff --git a/Tests/DependencyInjection/FOSFacebookExtensionTest.php b/Tests/DependencyInjection/FOSFacebookExtensionTest.php deleted file mode 100644 index 746a5b3..0000000 --- a/Tests/DependencyInjection/FOSFacebookExtensionTest.php +++ /dev/null @@ -1,71 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\DependencyInjection; -use FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension; - -class FOSFacebookExtensionTest extends \PHPUnit_Framework_TestCase -{ - /** - * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load - */ - - public function testLoadFailure( ) - { - $container = $this->getMockBuilder( 'Symfony\\Component\\DependencyInjection\\ContainerBuilder' ) - ->disableOriginalConstructor( )->getMock( ); - - $extension = $this->getMockBuilder( 'FOS\\FacebookBundle\\DependencyInjection\\FOSFacebookExtension' )->getMock( ); - - $extension->load( array( array( ) ), $container ); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load - */ - - public function testLoadSetParameters( ) - { - $container = $this->getMockBuilder( 'Symfony\\Component\\DependencyInjection\\ContainerBuilder' ) - ->disableOriginalConstructor( )->getMock( ); - - $parameterBag = $this->getMockBuilder( 'Symfony\Component\DependencyInjection\ParameterBag\\ParameterBag' ) - ->disableOriginalConstructor( )->getMock( ); - - $parameterBag->expects( $this->any( ) )->method( 'add' ); - - $container->expects( $this->any( ) )->method( 'getParameterBag' )->will( $this->returnValue( $parameterBag ) ); - - $extension = new FOSFacebookExtension( ); - $configs = array( array( 'class' => array( 'api' => 'foo' ) ), array( 'file' => 'foo' ), array( 'app_id' => 'foo' ), - array( 'secret' => 'foo' ), array( 'cookie' => 'foo' ), array( 'domain' => 'foo' ), array( 'logging' => 'foo' ), - array( 'culture' => 'foo' ), array( 'permissions' => array( 'email' ) ), ); - $extension->load( $configs, $container ); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\FOSFacebookExtension::load - */ - - public function testThatCanSetContainerAlias( ) - { - $container = $this->getMockBuilder( 'Symfony\\Component\\DependencyInjection\\ContainerBuilder' ) - ->disableOriginalConstructor( )->getMock( ); - $container->expects( $this->once( ) )->method( 'setAlias' ) - ->with( $this->equalTo( 'facebook_alias' ), $this->equalTo( 'fos_facebook.api' ) ); - - $configs = array( array( 'class' => array( 'api' => 'foo' ) ), array( 'file' => 'foo' ), array( 'app_id' => 'foo' ), - array( 'secret' => 'foo' ), array( 'cookie' => 'foo' ), array( 'domain' => 'foo' ), array( 'logging' => 'foo' ), - array( 'culture' => 'foo' ), array( 'permissions' => array( 'email' ) ), array( 'alias' => 'facebook_alias' ) ); - $extension = new FOSFacebookExtension( ); - $extension->load( $configs, $container ); - } -} diff --git a/Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php b/Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php deleted file mode 100644 index 127a9f2..0000000 --- a/Tests/DependencyInjection/Security/Factory/FacebookFactoryTest.php +++ /dev/null @@ -1,108 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\DependencyInjection\Security\Factory; -use FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory; - -class FacebookFactoryTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory - */ - private $factory = null; - - public function setUp( ) - { - $this->factory = new FacebookFactory( ); - } - - public function testThatCanGetPosition( ) - { - $this->assertEquals( 'pre_auth', $this->factory->getPosition( ) ); - } - - public function testThatCanGetKey( ) - { - $this->assertEquals( 'fos_facebook', $this->factory->getKey( ) ); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider - */ - - public function testThatCreateUserAuthProviderWhenDefinedInConfig( ) - { - $idsArray = $this - ->facebookFactoryCreate( - array( 'provider' => true, 'remember_me' => false, 'create_user_if_not_exists' => false ) ); - $this->assertEquals( 'fos_facebook.auth.l3l0', $idsArray[ 0 ] ); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider - */ - - public function testThatCreateUserAuthProviderEvenWhenNotDefinedInConfig( ) - { - $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); - $this->assertEquals( 'fos_facebook.auth.l3l0', $idsArray[ 0 ] ); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createAuthProvider - */ - - public function testThatCreateDifferentUserAuthProviderForDifferentFirewalls( ) - { - $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); - $this->assertEquals( 'fos_facebook.auth.l3l0', $idsArray[ 0 ] ); - - $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ), 'main' ); - $this->assertEquals( 'fos_facebook.auth.main', $idsArray[ 0 ] ); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::createEntryPoint - */ - - public function testThatCreateEntryPoint( ) - { - $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); - $this->assertEquals( 'fos_facebook.security.authentication.entry_point.l3l0', $idsArray[ 2 ] ); - } - - /** - * @covers FOS\FacebookBundle\DependencyInjection\Security\Factory\FacebookFactory::getListenerId - */ - - public function testThatListenerForListenerId( ) - { - $idsArray = $this->facebookFactoryCreate( array( 'remember_me' => false ) ); - $this->assertEquals( 'fos_facebook.security.authentication.listener.l3l0', $idsArray[ 1 ] ); - } - - /** - * @param array $config - * @return array - */ - - private function facebookFactoryCreate( $config = array( ), $id = 'l3l0' ) - { - $definition = $this - ->getMock( 'Symfony\Component\DependencyInjection\Definition', array( 'addArgument', 'replaceArgument' ) ); - $definition->expects( $this->any( ) )->method( 'replaceArgument' )->will( $this->returnValue( $definition ) ); - $definition->expects( $this->any( ) )->method( 'addArgument' )->will( $this->returnValue( $definition ) ); - $container = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerBuilder', array( 'setDefinition' ) ); - $container->expects( $this->any( ) )->method( 'setDefinition' )->will( $this->returnValue( $definition ) ); - - return $this->factory->create( $container, $id, $config, 'l3l0.user.provider', null ); - } -} diff --git a/Tests/FacebookApiException.php b/Tests/FacebookApiException.php deleted file mode 100644 index 272863f..0000000 --- a/Tests/FacebookApiException.php +++ /dev/null @@ -1,14 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -class FacebookApiException extends \Exception -{ -} diff --git a/Tests/Kernel.php b/Tests/Kernel.php deleted file mode 100644 index 3e04453..0000000 --- a/Tests/Kernel.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests; -use Symfony\Component\HttpKernel\Kernel as BaseKernel; -use Symfony\Component\Config\Loader\LoaderInterface; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\ClassLoader\UniversalClassLoader; - -class Kernel extends BaseKernel -{ - - public function __construct( ) - { - $this->tmpDir = sys_get_temp_dir( ) . '/sf2_' . rand( 1, 9999 ); - if ( !is_dir( $this->tmpDir ) ) - { - if ( false === @mkdir( $this->tmpDir ) ) - { - die( sprintf( 'Unable to create a temporary directory (%s)', $this->tmpDir ) ); - } - } - elseif ( !is_writable( $this->tmpDir ) ) - { - die( sprintf( 'Unable to write in a temporary directory (%s)', $this->tmpDir ) ); - } - - parent::__construct( 'env', true ); - - require_once __DIR__ . '/FacebookApiException.php'; - - $loader = new UniversalClassLoader( ); - $loader->loadClass( '\FacebookApiException' ); - $loader->register( ); - } - - public function __destruct( ) - { - $fs = new Filesystem( ); - $fs->remove( $this->tmpDir ); - } - - public function registerRootDir( ) - { - return $this->tmpDir; - } - - public function registerBundles( ) - { - return array( new \Symfony\Bundle\FrameworkBundle\FrameworkBundle( ), ); - } - - public function registerBundleDirs( ) - { - return array( ); - } - - public function registerContainerConfiguration( LoaderInterface $loader ) - { - $loader - ->load( - function ($container) - { - $container->setParameter( 'kernel.compiled_classes', array( ) ); - } ); - } -} diff --git a/Tests/Security/Authentication/Provider/FacebookProviderTest.php b/Tests/Security/Authentication/Provider/FacebookProviderTest.php deleted file mode 100644 index 31c3e6a..0000000 --- a/Tests/Security/Authentication/Provider/FacebookProviderTest.php +++ /dev/null @@ -1,203 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\Security\Authentication\Provider; -use FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider; -use Symfony\Component\Security\Core\Exception\AuthenticationException; - -class FacebookProviderTest extends \PHPUnit_Framework_TestCase -{ - /** - * @expectedException \InvalidArgumentException - */ - - public function testThatUserCheckerCannotBeNullWhenUserProviderIsNotNull( ) - { - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->getMock( ); - new FacebookProvider( 'main', $facebookMock, - $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' )); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - */ - - public function testThatCannotAuthenticateWhenTokenIsNotFacebookUserToken( ) - { - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->getMock( ); - $facebookProvider = new FacebookProvider( 'main', $facebookMock); - $this - ->assertNull( - $facebookProvider - ->authenticate( $this->getMock( 'Symfony\Component\Security\Core\Authentication\Token\TokenInterface' ) ) ); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports - */ - - public function testThatCannotAuthenticateWhenTokenFromOtherFirewall( ) - { - $providerKeyForProvider = 'main'; - $providerKeyForToken = 'connect'; - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->getMock( ); - $facebookProvider = new FacebookProvider( $providerKeyForProvider, $facebookMock); - - $tokenMock = $this - ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), - array( $providerKeyForToken ) ); - $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKeyForToken ) ); - - $this->assertFalse( $facebookProvider->supports( $tokenMock ) ); - $this->assertNull( $facebookProvider->authenticate( $tokenMock ) ); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::supports - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken - */ - - public function testThatCanAuthenticateUserWithoutUserProvider( ) - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); - - $facebookProvider = new FacebookProvider( $providerKey, $facebookMock); - - $tokenMock = $this - ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', - array( 'getAttributes', 'getProviderKey' ), array( $providerKey ) ); - $tokenMock->expects( $this->once( ) )->method( 'getAttributes' )->will( $this->returnValue( array( ) ) ); - $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); - - $this->assertTrue( $facebookProvider->supports( $tokenMock ) ); - $this->assertEquals( '123', $facebookProvider->authenticate( $tokenMock )->getUser( ) ); - } - - /** - * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException - */ - - public function testThatCannotAuthenticateWhenUserProviderThrowsAuthenticationException( ) - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); - - $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); - $userProviderMock->expects( $this->once( ) )->method( 'loadUserByUsername' )->with( '123' ) - ->will( $this->throwException( new AuthenticationException( 'test') ) ); - - $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); - $tokenMock = $this - ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), - array( $providerKey ) ); - $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); - - $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $facebookProvider->authenticate( $tokenMock ); - } - - /** - * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException - */ - - public function testThatCannotAuthenticateWhenUserProviderDoesNotReturnUsetInterface( ) - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); - - $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); - $userProviderMock->expects( $this->once( ) )->method( 'loadUserByUsername' )->with( '123' ) - ->will( $this->returnValue( '234' ) ); - - $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); - $tokenMock = $this - ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), - array( $providerKey ) ); - $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); - - $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $facebookProvider->authenticate( $tokenMock ); - } - - /** - * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException - */ - - public function testThatCannotAuthenticateWhenCannotRetrieveFacebookUserFromSession( ) - { - $providerKey = 'main'; - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( false ) ); - - $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); - $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); - - $tokenMock = $this - ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', array( 'getProviderKey' ), - array( $providerKey ) ); - $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); - - $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $facebookProvider->authenticate( $tokenMock ); - } - - /** - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::authenticate - * @covers FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::createAuthenticatedToken - */ - - public function testThatCanAutenticateUsingUserProvider( ) - { - $providerKey = 'main'; - - $userMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserInterface' ); - $userMock->expects( $this->once( ) )->method( 'getUsername' )->will( $this->returnValue( 'l3l0' ) ); - $userMock->expects( $this->once( ) )->method( 'getRoles' )->will( $this->returnValue( array( ) ) ); - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getUser' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getUser' )->will( $this->returnValue( '123' ) ); - - $userProviderMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserProviderInterface' ); - $userProviderMock->expects( $this->once( ) )->method( 'loadUserByUsername' )->with( '123' ) - ->will( $this->returnValue( $userMock ) ); - - $userCheckerMock = $this->getMock( 'Symfony\Component\Security\Core\User\UserCheckerInterface' ); - $userCheckerMock->expects( $this->once( ) )->method( 'checkPostAuth' ); - - $tokenMock = $this - ->getMock( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken', - array( 'getAttributes', 'getProviderKey' ), array( $providerKey ) ); - $tokenMock->expects( $this->once( ) )->method( 'getAttributes' )->will( $this->returnValue( array( ) ) ); - $tokenMock->expects( $this->any( ) )->method( 'getProviderKey' )->will( $this->returnValue( $providerKey ) ); - - $facebookProvider = new FacebookProvider( $providerKey, $facebookMock, $userProviderMock, $userCheckerMock); - $this->assertEquals( 'l3l0', $facebookProvider->authenticate( $tokenMock )->getUsername( ) ); - } -} diff --git a/Tests/Security/Authentication/Token/FacebookUserTokenTest.php b/Tests/Security/Authentication/Token/FacebookUserTokenTest.php deleted file mode 100644 index 35bac3d..0000000 --- a/Tests/Security/Authentication/Token/FacebookUserTokenTest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\Security\Authentication\Token; -use FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken; - -class FacebookUserTokenTest extends \PHPUnit_Framework_TestCase -{ - /** - * @dataProvider provider - */ - - public function testThatAlwaysReturnEmptyCredentials( $uid, $roles ) - { - $token = new FacebookUserToken( 'main', $uid, $roles); - - $this->assertEquals( '', $token->getCredentials( ) ); - } - - /** - * @return array - */ - - public static function provider( ) - { - return array( array( '', array( ) ), array( 'l3l0', array( ) ), array( '', array( 'role1', 'role2' ) ), - array( 'l3l0', array( 'role1', 'role2' ) ) ); - } - - public function testThatProviderKeyIsNotEmptyAfterDeserialization( ) - { - $providerKey = 'main'; - $token = unserialize( serialize( new FacebookUserToken( $providerKey) ) ); - - $this->assertEquals( $providerKey, $token->getProviderKey( ) ); - } -} diff --git a/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php b/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php deleted file mode 100644 index 1aac44a..0000000 --- a/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\Security\EntryPoint; -use FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint; - -class FacebookAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase -{ - /** - * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start - */ - - public function testThatRedirectResponseWithFacebookLoginUrlIsCreated( ) - { - $requestMock = $this->getMock( 'Symfony\Component\HttpFoundation\Request', array( 'getUriForPath' ) ); - $requestMock->expects( $this->once( ) )->method( 'getUriForPath' )->with( $this->equalTo( '/index' ) ) - ->will( $this->returnValue( 'http://localhost/index' ) ); - - $options = array( 'check_path' => '/index' ); - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getLoginUrl' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getLoginUrl' ) - ->with( - $this - ->equalTo( - array( 'display' => 'page', 'scope' => 'email,user_website', - 'redirect_uri' => 'http://localhost/index' ) ) ) - ->will( $this->returnValue( 'http://localhost/facebook-redirect/index' ) ); - - $facebookAuthentication = new FacebookAuthenticationEntryPoint( $facebookMock, $options, - array( 'email', 'user_website' )); - $response = $facebookAuthentication->start( $requestMock ); - - $this - ->assertInstanceOf( 'Symfony\Component\HttpFoundation\RedirectResponse', $response, - 'RedirectResponse is returned' ); - $this - ->assertEquals( $response->headers->get( 'location' ), 'http://localhost/facebook-redirect/index', - 'RedirectResponse has defined expected location' ); - } - - /** - * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start - */ - - public function testThatRedirectionToFacebookLoginUrlIsCreated( ) - { - $requestMock = $this->getMock( 'Symfony\Component\HttpFoundation\Request', array( 'getUriForPath' ) ); - - $options = array( 'check_path' => '/index', 'server_url' => 'http://server.url', 'app_url' => 'http://app.url' ); - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getLoginUrl' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getLoginUrl' ) - ->will( $this->returnValue( 'http://localhost/facebook-redirect/index' ) ); - - $facebookAuthentication = new FacebookAuthenticationEntryPoint( $facebookMock, $options, array( )); - $response = $facebookAuthentication->start( $requestMock ); - - $this->assertInstanceOf( 'Symfony\Component\HttpFoundation\Response', $response, 'Response is returned' ); - $this - ->assertRegExp( '/location\.href="http:\/\/localhost\/facebook-redirect\/index/', $response->getContent( ), - 'Javascript redirection is in response' ); - } -} diff --git a/Tests/Security/Firewall/FacebookListenerTest.php b/Tests/Security/Firewall/FacebookListenerTest.php deleted file mode 100644 index fe362f0..0000000 --- a/Tests/Security/Firewall/FacebookListenerTest.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\Security\Firewall\FacebookListener; -use FOS\FacebookBundle\Security\Firewall\FacebookListener; - -class FacebookListenerTest extends \PHPUnit_Framework_TestCase -{ - /** - * @covers FOS\FacebookBundle\Security\Firewall\FacebookListener::attemptAuthentication - */ - - public function testThatCanAttemptAuthenticationWithFacebook( ) - { - $listener = new FacebookListener( $this->getMock( 'Symfony\Component\Security\Core\SecurityContextInterface' ), - $this->getAuthenticationManager( ), - $this->getMock( 'Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface' ), - $this->getHttpUtils( ), 'providerKey', - $this->getMock( 'Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface' ), - $this->getMock( 'Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface' )); - $listener->handle( $this->getResponseEvent( ) ); - } - - /** - * @return Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface - */ - - private function getAuthenticationManager( ) - { - $authenticationManagerMock = $this - ->getMock( 'Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface' ); - $authenticationManagerMock->expects( $this->once( ) )->method( 'authenticate' ) - ->with( $this->isInstanceOf( 'FOS\FacebookBundle\Security\Authentication\Token\FacebookUserToken' ) ); - - return $authenticationManagerMock; - } - - /** - * @return Symfony\Component\Security\Http\HttpUtils - */ - - private function getHttpUtils( ) - { - $httpUtils = $this->getMock( 'Symfony\Component\Security\Http\HttpUtils' ); - $httpUtils->expects( $this->once( ) )->method( 'checkRequestPath' )->will( $this->returnValue( true ) ); - - return $httpUtils; - } - - /** - * @return Symfony\Component\HttpKernel\Event\GetResponseEvent - */ - - private function getResponseEvent( ) - { - $responseEventMock = $this - ->getMock( 'Symfony\Component\HttpKernel\Event\GetResponseEvent', array( 'getRequest' ), array( ), '', false ); - $responseEventMock->expects( $this->any( ) )->method( 'getRequest' )->will( $this->returnValue( $this->getRequest( ) ) ); - - return $responseEventMock; - } - - /** - * @return Symfony\Component\HttpFoundation\Request - */ - - private function getRequest( ) - { - $requestMock = $this->getMockBuilder( 'Symfony\Component\HttpFoundation\Request' )->disableOriginalClone( ) - ->getMock( ); - $requestMock->expects( $this->any( ) )->method( 'hasSession' )->will( $this->returnValue( 'true' ) ); - $requestMock->expects( $this->any( ) )->method( 'hasPreviousSession' )->will( $this->returnValue( 'true' ) ); - - return $requestMock; - } -} diff --git a/Tests/Templating/Helper/FacebookHelperTest.php b/Tests/Templating/Helper/FacebookHelperTest.php deleted file mode 100644 index 5c9fc62..0000000 --- a/Tests/Templating/Helper/FacebookHelperTest.php +++ /dev/null @@ -1,63 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\Templating\Helper; -use FOS\FacebookBundle\Templating\Helper\FacebookHelper; - -class FacebookHelperTest extends \PHPUnit_Framework_TestCase -{ - /** - * @covers FOS\FacebookBundle\Templating\Helper\FacebookHelper::initialize - */ - - public function testInitialize( ) - { - $expected = new \stdClass( ); - - $templating = $this->getMockBuilder( 'Symfony\Component\Templating\DelegatingEngine' ) - ->disableOriginalConstructor( )->getMock( ); - $templating->expects( $this->once( ) )->method( 'render' ) - ->with( 'FOSFacebookBundle::initialize.html.php', - array( 'appId' => 123, 'async' => true, 'cookie' => false, 'culture' => 'en_US', 'fbAsyncInit' => '', - 'logging' => true, 'oauth' => true, 'status' => false, 'xfbml' => false, ) ) - ->will( $this->returnValue( $expected ) ); - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getAppId' ) )->getMock( ); - $facebookMock->expects( $this->once( ) )->method( 'getAppId' )->will( $this->returnValue( '123' ) ); - - $helper = new FacebookHelper( $templating, $facebookMock); - $this->assertSame( $expected, $helper->initialize( array( 'cookie' => false ) ) ); - } - - /** - * @covers FOS\FacebookBundle\Templating\Helper\FacebookHelper::loginButton - */ - - public function testLoginButton( ) - { - $expected = new \stdClass( ); - - $templating = $this->getMockBuilder( 'Symfony\Component\Templating\DelegatingEngine' ) - ->disableOriginalConstructor( )->getMock( ); - $templating->expects( $this->once( ) )->method( 'render' ) - ->with( 'FOSFacebookBundle::loginButton.html.php', - array( 'autologoutlink' => 'false', 'label' => 'testLabel', 'scope' => '1,2,3', ) ) - ->will( $this->returnValue( $expected ) ); - - $facebookMock = $this->getMockBuilder( 'FOS\FacebookBundle\Facebook\FacebookSessionPersistence' ) - ->disableOriginalConstructor( )->setMethods( array( 'getAppId' ) )->getMock( ); - $facebookMock->expects( $this->any( ) )->method( 'getAppId' ); - - $helper = new FacebookHelper( $templating, $facebookMock, true, 'en_US', array( 1, 2, 3 )); - $this->assertSame( $expected, $helper->loginButton( array( 'label' => 'testLabel' ) ) ); - } -} diff --git a/Tests/Twig/Extension/FacebookExtensionTest.php b/Tests/Twig/Extension/FacebookExtensionTest.php deleted file mode 100644 index 7ede605..0000000 --- a/Tests/Twig/Extension/FacebookExtensionTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\FacebookBundle\Tests\Twig\Extension; -use FOS\FacebookBundle\Twig\Extension\FacebookExtension; -use FOS\FacebookBundle\Templating\Helper\FacebookHelper; - -class FacebookExtensionTest extends \PHPUnit_Framework_TestCase -{ - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::getName - */ - - public function testGetName( ) - { - $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); - $extension = new FacebookExtension( $containerMock); - $this->assertSame( 'facebook', $extension->getName( ) ); - } - - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::getFunctions - */ - - public function testGetFunctions( ) - { - $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); - $extension = new FacebookExtension( $containerMock); - $functions = $extension->getFunctions( ); - $this->assertInstanceOf( '\Twig_Function_Method', $functions[ 'facebook_initialize' ] ); - $this->assertInstanceOf( '\Twig_Function_Method', $functions[ 'facebook_login_button' ] ); - } - - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::renderInitialize - */ - - public function testRenderInitialize( ) - { - $helperMock = $this->getMockBuilder( 'FOS\FacebookBundle\Templating\Helper\FacebookHelper' ) - ->disableOriginalConstructor( )->getMock( ); - $helperMock->expects( $this->once( ) )->method( 'initialize' )->will( $this->returnValue( 'returnedValue' ) ); - $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); - $containerMock->expects( $this->once( ) )->method( 'get' )->with( 'fos_facebook.helper' ) - ->will( $this->returnValue( $helperMock ) ); - - $extension = new FacebookExtension( $containerMock); - $this->assertSame( 'returnedValue', $extension->renderInitialize( ) ); - } - - /** - * @covers FOS\FacebookBundle\Twig\Extension\FacebookExtension::renderloginButton - */ - - public function testRenderLoginButton( ) - { - $helperMock = $this->getMockBuilder( 'FOS\FacebookBundle\Templating\Helper\FacebookHelper' ) - ->disableOriginalConstructor( )->getMock( ); - $helperMock->expects( $this->once( ) )->method( 'loginButton' )->will( $this->returnValue( 'returnedValueLogin' ) ); - $containerMock = $this->getMock( 'Symfony\Component\DependencyInjection\ContainerInterface' ); - $containerMock->expects( $this->once( ) )->method( 'get' )->with( 'fos_facebook.helper' ) - ->will( $this->returnValue( $helperMock ) ); - - $extension = new FacebookExtension( $containerMock); - $this->assertSame( 'returnedValueLogin', $extension->renderLoginButton( ) ); - } -} diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php deleted file mode 100644 index cd20de3..0000000 --- a/Tests/bootstrap.php +++ /dev/null @@ -1,18 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -$file = __DIR__ . '/../vendor/autoload.php'; -if ( !file_exists( $file ) ) -{ - throw new RuntimeException( 'Install dependencies to run test suite.'); -} - -$autoload = require_once $file; diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 526dddb..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - ./Tests - - - - - - ./ - - ./Resources - ./Tests - ./vendor - - - -