/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php

http://github.com/FriendsOfSymfony/FOSFacebookBundle · PHP · 105 lines · 73 code · 15 blank · 17 comment · 0 complexity · e98c1f5db18e1a9d90c0f802bd57618c MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the FOSFacebookBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\FacebookBundle\Tests\Security\EntryPoint;
  11. use FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint;
  12. class FacebookAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start
  16. */
  17. public function testThatRedirectResponseWithFacebookLoginUrlIsCreated()
  18. {
  19. $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath'));
  20. $requestMock->expects($this->once())
  21. ->method('getUriForPath')
  22. ->with($this->equalTo('/index'))
  23. ->will($this->returnValue('http://localhost/index'));
  24. $options = array('check_path' => '/index', 'redirect_to_facebook_login' => true);
  25. $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
  26. ->disableOriginalConstructor()
  27. ->setMethods(array('getLoginUrl'))
  28. ->getMock();
  29. $facebookMock->expects($this->once())
  30. ->method('getLoginUrl')
  31. ->with($this->equalTo(array(
  32. 'display' => 'page',
  33. 'scope' => 'email,user_website',
  34. 'redirect_uri' => 'http://localhost/index'
  35. )))
  36. ->will($this->returnValue('http://localhost/facebook-redirect/index'));
  37. $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array('email', 'user_website'));
  38. $response = $facebookAuthentication->start($requestMock);
  39. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response, 'RedirectResponse is returned');
  40. $this->assertEquals($response->headers->get('location'), 'http://localhost/facebook-redirect/index', 'RedirectResponse has defined expected location');
  41. }
  42. /**
  43. * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start
  44. */
  45. public function testThatRedirectResponseWithoutFacebookLoginUrlIsCreated()
  46. {
  47. $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath'));
  48. $requestMock->expects($this->never())
  49. ->method('getUriForPath');
  50. $options = array(
  51. 'check_path' => '/index',
  52. 'login_path' => '/login',
  53. 'redirect_to_facebook_login' => false
  54. );
  55. $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
  56. ->disableOriginalConstructor()
  57. ->setMethods(array('getLoginUrl'))
  58. ->getMock();
  59. $facebookMock->expects($this->never())
  60. ->method('getLoginUrl');
  61. $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array('email', 'user_website'));
  62. $response = $facebookAuthentication->start($requestMock);
  63. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response, 'RedirectResponse is returned');
  64. $this->assertEquals($response->headers->get('location'), '/login', 'RedirectResponse has defined expected location');
  65. }
  66. /**
  67. * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start
  68. */
  69. public function testThatRedirectionToFacebookLoginUrlIsCreated()
  70. {
  71. $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath'));
  72. $options = array(
  73. 'check_path' => '/index',
  74. 'server_url' => 'http://server.url',
  75. 'app_url' => 'http://app.url',
  76. 'redirect_to_facebook_login' => true
  77. );
  78. $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence')
  79. ->disableOriginalConstructor()
  80. ->setMethods(array('getLoginUrl'))
  81. ->getMock();
  82. $facebookMock->expects($this->once())
  83. ->method('getLoginUrl')
  84. ->will($this->returnValue('http://localhost/facebook-redirect/index'));
  85. $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array());
  86. $response = $facebookAuthentication->start($requestMock);
  87. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response, 'Response is returned');
  88. $this->assertRegExp('/location\.href="http:\/\/localhost\/facebook-redirect\/index/', $response->getContent(), 'Javascript redirection is in response');
  89. }
  90. }