PageRenderTime 95ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/Security/Tests/Http/RememberMe/AbstractRememberMeServicesTest.php

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