AbstractRememberMeServicesTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Tests\Component\Security\Http\RememberMe;
  11. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class AbstractRememberMeServicesTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testGetRememberMeParameter()
  17. {
  18. $service = $this->getService(null, array('remember_me_parameter' => 'foo'));
  19. $this->assertEquals('foo', $service->getRememberMeParameter());
  20. }
  21. public function testGetKey()
  22. {
  23. $service = $this->getService();
  24. $this->assertEquals('fookey', $service->getKey());
  25. }
  26. public function testAutoLoginReturnsNullWhenNoCookie()
  27. {
  28. $service = $this->getService(null, array('name' => 'foo'));
  29. $this->assertNull($service->autoLogin(new Request()));
  30. }
  31. /**
  32. * @expectedException \RuntimeException
  33. */
  34. public function testAutoLoginThrowsExceptionWhenImplementationDoesNotReturnUserInterface()
  35. {
  36. $service = $this->getService(null, array('name' => 'foo'));
  37. $request = new Request;
  38. $request->cookies->set('foo', 'foo');
  39. $service
  40. ->expects($this->once())
  41. ->method('processAutoLoginCookie')
  42. ->will($this->returnValue(null))
  43. ;
  44. $service->autoLogin($request);
  45. }
  46. public function testAutoLogin()
  47. {
  48. $service = $this->getService(null, array('name' => 'foo'));
  49. $request = new Request();
  50. $request->cookies->set('foo', 'foo');
  51. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  52. $user
  53. ->expects($this->once())
  54. ->method('getRoles')
  55. ->will($this->returnValue(array()))
  56. ;
  57. $service
  58. ->expects($this->once())
  59. ->method('processAutoLoginCookie')
  60. ->will($this->returnValue($user))
  61. ;
  62. $returnedToken = $service->autoLogin($request);
  63. $this->assertSame($user, $returnedToken->getUser());
  64. $this->assertSame('fookey', $returnedToken->getKey());
  65. $this->assertSame('fookey', $returnedToken->getProviderKey());
  66. }
  67. public function testLogout()
  68. {
  69. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  70. $request = new Request();
  71. $response = new Response();
  72. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  73. $service->logout($request, $response, $token);
  74. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  75. }
  76. public function testLoginFail()
  77. {
  78. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  79. $request = new Request();
  80. $service->loginFail($request);
  81. $this->assertTrue($request->attributes->get(RememberMeServicesInterface::COOKIE_ATTR_NAME)->isCleared());
  82. }
  83. public function testLoginSuccessIsNotProcessedWhenTokenDoesNotContainUserInterfaceImplementation()
  84. {
  85. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
  86. $request = new Request;
  87. $response = new Response;
  88. $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  89. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  90. $token
  91. ->expects($this->once())
  92. ->method('getUser')
  93. ->will($this->returnValue('foo'))
  94. ;
  95. $service
  96. ->expects($this->never())
  97. ->method('onLoginSuccess')
  98. ;
  99. $this->assertFalse($request->request->has('foo'));
  100. $service->loginSuccess($request, $response, $token);
  101. }
  102. public function testLoginSuccessIsNotProcessedWhenRememberMeIsNotRequested()
  103. {
  104. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  105. $request = new Request;
  106. $response = new Response;
  107. $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  108. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  109. $token
  110. ->expects($this->once())
  111. ->method('getUser')
  112. ->will($this->returnValue($account))
  113. ;
  114. $service
  115. ->expects($this->never())
  116. ->method('onLoginSuccess')
  117. ->will($this->returnValue(null))
  118. ;
  119. $this->assertFalse($request->request->has('foo'));
  120. $service->loginSuccess($request, $response, $token);
  121. }
  122. public function testLoginSuccessWhenRememberMeAlwaysIsTrue()
  123. {
  124. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
  125. $request = new Request;
  126. $response = new Response;
  127. $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  128. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  129. $token
  130. ->expects($this->once())
  131. ->method('getUser')
  132. ->will($this->returnValue($account))
  133. ;
  134. $service
  135. ->expects($this->once())
  136. ->method('onLoginSuccess')
  137. ->will($this->returnValue(null))
  138. ;
  139. $service->loginSuccess($request, $response, $token);
  140. }
  141. /**
  142. * @dataProvider getPositiveRememberMeParameterValues
  143. */
  144. public function testLoginSuccessWhenRememberMeParameterWithPathIsPositive($value)
  145. {
  146. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo[bar]'));
  147. $request = new Request;
  148. $request->request->set('foo', array('bar' => $value));
  149. $response = new Response;
  150. $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  151. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  152. $token
  153. ->expects($this->once())
  154. ->method('getUser')
  155. ->will($this->returnValue($account))
  156. ;
  157. $service
  158. ->expects($this->once())
  159. ->method('onLoginSuccess')
  160. ->will($this->returnValue(true))
  161. ;
  162. $service->loginSuccess($request, $response, $token);
  163. }
  164. /**
  165. * @dataProvider getPositiveRememberMeParameterValues
  166. */
  167. public function testLoginSuccessWhenRememberMeParameterIsPositive($value)
  168. {
  169. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  170. $request = new Request;
  171. $request->request->set('foo', $value);
  172. $response = new Response;
  173. $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  174. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  175. $token
  176. ->expects($this->once())
  177. ->method('getUser')
  178. ->will($this->returnValue($account))
  179. ;
  180. $service
  181. ->expects($this->once())
  182. ->method('onLoginSuccess')
  183. ->will($this->returnValue(true))
  184. ;
  185. $service->loginSuccess($request, $response, $token);
  186. }
  187. public function getPositiveRememberMeParameterValues()
  188. {
  189. return array(
  190. array('true'),
  191. array('1'),
  192. array('on'),
  193. array('yes'),
  194. );
  195. }
  196. protected function getService($userProvider = null, $options = array(), $logger = null)
  197. {
  198. if (null === $userProvider) {
  199. $userProvider = $this->getProvider();
  200. }
  201. return $this->getMockForAbstractClass('Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices', array(
  202. array($userProvider), 'fookey', 'fookey', $options, $logger
  203. ));
  204. }
  205. protected function getProvider()
  206. {
  207. $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
  208. $provider
  209. ->expects($this->any())
  210. ->method('supportsClass')
  211. ->will($this->returnValue(true))
  212. ;
  213. return $provider;
  214. }
  215. }