AbstractRememberMeServicesTest.php 7.3 KB

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