RememberMeServicesTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Http\RememberMe;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. class RememberMeServicesTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testAutoLoginReturnsNullWhenNoCookie()
  8. {
  9. $service = $this->getService(null, array('name' => 'foo'));
  10. $this->assertNull($service->autoLogin(new Request()));
  11. }
  12. /**
  13. * @expectedException \RuntimeException
  14. * @expectedMessage processAutoLoginCookie() must return a TokenInterface implementation.
  15. */
  16. public function testAutoLoginThrowsExceptionWhenImplementationDoesNotReturnTokenInterface()
  17. {
  18. $service = $this->getService(null, array('name' => 'foo'));
  19. $request = new Request;
  20. $request->cookies->set('foo', 'foo');
  21. $service
  22. ->expects($this->once())
  23. ->method('processAutoLoginCookie')
  24. ->will($this->returnValue(null))
  25. ;
  26. $service->autoLogin($request);
  27. }
  28. public function testAutoLogin()
  29. {
  30. $service = $this->getService(null, array('name' => 'foo'));
  31. $request = new Request();
  32. $request->cookies->set('foo', 'foo');
  33. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  34. $service
  35. ->expects($this->once())
  36. ->method('processAutoLoginCookie')
  37. ->will($this->returnValue($token))
  38. ;
  39. $returnedToken = $service->autoLogin($request);
  40. $this->assertSame($token, $returnedToken);
  41. }
  42. public function testLogout()
  43. {
  44. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  45. $request = new Request();
  46. $response = new Response();
  47. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  48. $this->assertFalse($response->headers->hasCookie('foo'));
  49. $service->logout($request, $response, $token);
  50. $this->assertTrue($response->headers->getCookie('foo')->isCleared());
  51. }
  52. public function testLoginFail()
  53. {
  54. $service = $this->getService(null, array('name' => 'foo', 'path' => null, 'domain' => null));
  55. $request = new Request();
  56. $response = new Response();
  57. $this->assertFalse($response->headers->hasCookie('foo'));
  58. $service->loginFail($request, $response);
  59. $this->assertTrue($response->headers->getCookie('foo')->isCleared());
  60. }
  61. public function testLoginSuccessIsNotProcessedWhenTokenDoesNotContainAccountInterfaceImplementation()
  62. {
  63. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
  64. $request = new Request;
  65. $response = new Response;
  66. $account = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
  67. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  68. $token
  69. ->expects($this->once())
  70. ->method('getUser')
  71. ->will($this->returnValue('foo'))
  72. ;
  73. $service
  74. ->expects($this->never())
  75. ->method('onLoginSuccess')
  76. ;
  77. $this->assertFalse($request->request->has('foo'));
  78. $service->loginSuccess($request, $response, $token);
  79. }
  80. public function testLoginSuccessIsNotProcessedWhenRememberMeIsNotRequested()
  81. {
  82. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  83. $request = new Request;
  84. $response = new Response;
  85. $account = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
  86. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  87. $token
  88. ->expects($this->once())
  89. ->method('getUser')
  90. ->will($this->returnValue($account))
  91. ;
  92. $service
  93. ->expects($this->never())
  94. ->method('onLoginSuccess')
  95. ->will($this->returnValue(null))
  96. ;
  97. $this->assertFalse($request->request->has('foo'));
  98. $service->loginSuccess($request, $response, $token);
  99. }
  100. public function testLoginSuccessWhenRememberMeAlwaysIsTrue()
  101. {
  102. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => true));
  103. $request = new Request;
  104. $response = new Response;
  105. $account = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
  106. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  107. $token
  108. ->expects($this->once())
  109. ->method('getUser')
  110. ->will($this->returnValue($account))
  111. ;
  112. $service
  113. ->expects($this->once())
  114. ->method('onLoginSuccess')
  115. ->will($this->returnValue(null))
  116. ;
  117. $service->loginSuccess($request, $response, $token);
  118. }
  119. /**
  120. * @dataProvider getPositiveRememberMeParameterValues
  121. */
  122. public function testLoginSuccessWhenRememberMeParameterIsPositive($value)
  123. {
  124. $service = $this->getService(null, array('name' => 'foo', 'always_remember_me' => false, 'remember_me_parameter' => 'foo'));
  125. $request = new Request;
  126. $request->request->set('foo', $value);
  127. $response = new Response;
  128. $account = $this->getMock('Symfony\Component\Security\Core\User\AccountInterface');
  129. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  130. $token
  131. ->expects($this->once())
  132. ->method('getUser')
  133. ->will($this->returnValue($account))
  134. ;
  135. $service
  136. ->expects($this->once())
  137. ->method('onLoginSuccess')
  138. ->will($this->returnValue(true))
  139. ;
  140. $service->loginSuccess($request, $response, $token);
  141. }
  142. public function getPositiveRememberMeParameterValues()
  143. {
  144. return array(
  145. array('true'),
  146. array('1'),
  147. array('on'),
  148. array('yes'),
  149. );
  150. }
  151. public function testLoginSuccessRenewsRememberMeCookie()
  152. {
  153. $service = $this->getService();
  154. $token = $this->getMock(
  155. 'Symfony\Component\Security\Core\Authentication\Token\RememberMeToken',
  156. array(),
  157. array(),
  158. 'NonFunctionalRememberMeTokenMockClass',
  159. false
  160. );
  161. $service
  162. ->expects($this->once())
  163. ->method('onLoginSuccess')
  164. ->will($this->returnValue(null))
  165. ;
  166. $service->loginSuccess(new Request(), new Response(), $token);
  167. }
  168. protected function getService($userProvider = null, $options = array(), $logger = null)
  169. {
  170. if (null === $userProvider) {
  171. $userProvider = $this->getProvider();
  172. }
  173. return $this->getMockForAbstractClass('Symfony\Component\Security\Http\RememberMe\RememberMeServices', array(
  174. array($userProvider), 'fookey', 'fookey', $options, $logger
  175. ));
  176. }
  177. protected function getProvider()
  178. {
  179. $provider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
  180. $provider
  181. ->expects($this->any())
  182. ->method('supportsClass')
  183. ->will($this->returnValue(true))
  184. ;
  185. return $provider;
  186. }
  187. }