DefaultAuthenticationSuccessHandlerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Security\Http\Tests\Authentication;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
  14. use Symfony\Component\Security\Http\HttpUtils;
  15. class DefaultAuthenticationSuccessHandlerTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider getRequestRedirections
  19. */
  20. public function testRequestRedirections(Request $request, $options, $redirectedUrl)
  21. {
  22. $urlGenerator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->getMock();
  23. $urlGenerator->expects($this->any())->method('generate')->will($this->returnValue('http://localhost/login'));
  24. $httpUtils = new HttpUtils($urlGenerator);
  25. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  26. $handler = new DefaultAuthenticationSuccessHandler($httpUtils, $options);
  27. if ($request->hasSession()) {
  28. $handler->setProviderKey('admin');
  29. }
  30. $this->assertSame('http://localhost'.$redirectedUrl, $handler->onAuthenticationSuccess($request, $token)->getTargetUrl());
  31. }
  32. public function getRequestRedirections()
  33. {
  34. $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
  35. $session->expects($this->once())->method('get')->with('_security.admin.target_path')->will($this->returnValue('/admin/dashboard'));
  36. $session->expects($this->once())->method('remove')->with('_security.admin.target_path');
  37. $requestWithSession = Request::create('/');
  38. $requestWithSession->setSession($session);
  39. return array(
  40. 'default' => array(
  41. Request::create('/'),
  42. array(),
  43. '/',
  44. ),
  45. 'forced target path' => array(
  46. Request::create('/'),
  47. array('always_use_default_target_path' => true, 'default_target_path' => '/dashboard'),
  48. '/dashboard',
  49. ),
  50. 'target path as query string' => array(
  51. Request::create('/?_target_path=/dashboard'),
  52. array(),
  53. '/dashboard',
  54. ),
  55. 'target path name as query string is customized' => array(
  56. Request::create('/?_my_target_path=/dashboard'),
  57. array('target_path_parameter' => '_my_target_path'),
  58. '/dashboard',
  59. ),
  60. 'target path name as query string is customized and nested' => array(
  61. Request::create('/?_target_path[value]=/dashboard'),
  62. array('target_path_parameter' => '_target_path[value]'),
  63. '/dashboard',
  64. ),
  65. 'target path in session' => array(
  66. $requestWithSession,
  67. array(),
  68. '/admin/dashboard',
  69. ),
  70. 'target path as referer' => array(
  71. Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
  72. array('use_referer' => true),
  73. '/dashboard',
  74. ),
  75. 'target path as referer is ignored if not configured' => array(
  76. Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
  77. array(),
  78. '/',
  79. ),
  80. 'target path as referer when referer not set' => array(
  81. Request::create('/'),
  82. array('use_referer' => true),
  83. '/',
  84. ),
  85. 'target path as referer when referer is ?' => array(
  86. Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => '?')),
  87. array('use_referer' => true),
  88. '/',
  89. ),
  90. 'target path should be different than login URL' => array(
  91. Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login')),
  92. array('use_referer' => true, 'login_path' => '/login'),
  93. '/',
  94. ),
  95. 'target path should be different than login URL (query string does not matter)' => array(
  96. Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login?t=1&p=2')),
  97. array('use_referer' => true, 'login_path' => '/login'),
  98. '/',
  99. ),
  100. 'target path should be different than login URL (login_path as a route)' => array(
  101. Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login?t=1&p=2')),
  102. array('use_referer' => true, 'login_path' => 'login_route'),
  103. '/',
  104. ),
  105. );
  106. }
  107. }