AbstractRememberMeServicesTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 testLoginSuccessWhenRememberMeParameterIsPositive($value)
  145. {
  146. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  147. $request = new Request;
  148. $request->request->set('foo', $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. public function getPositiveRememberMeParameterValues()
  165. {
  166. return array(
  167. array('true'),
  168. array('1'),
  169. array('on'),
  170. array('yes'),
  171. );
  172. }
  173. protected function getService($userProvider = null, $options = array(), $logger = null)
  174. {
  175. if (null === $userProvider) {
  176. $userProvider = $this->getProvider();
  177. }
  178. return $this->getMockForAbstractClass('Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices', array(
  179. array($userProvider), 'fookey', 'fookey', $options, $logger
  180. ));
  181. }
  182. protected function getProvider()
  183. {
  184. $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
  185. $provider
  186. ->expects($this->any())
  187. ->method('supportsClass')
  188. ->will($this->returnValue(true))
  189. ;
  190. return $provider;
  191. }
  192. }