RememberMeServicesTest.php 7.6 KB

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