PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/myCore/lib/laravel/symfony/security-core/Symfony/Component/Security/Core/Tests/Authentication/Token/AbstractTokenTest.php

https://gitlab.com/fabian.morales/marlon_becerra
PHP | 285 lines | 205 code | 45 blank | 35 comment | 0 complexity | 63d1895301e8c4db998257793246ddd4 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\Core\Tests\Authentication\Token;
  11. use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
  12. use Symfony\Component\Security\Core\Role\Role;
  13. use Symfony\Component\Security\Core\Role\SwitchUserRole;
  14. class TestUser
  15. {
  16. protected $name;
  17. public function __construct($name)
  18. {
  19. $this->name = $name;
  20. }
  21. public function __toString()
  22. {
  23. return $this->name;
  24. }
  25. }
  26. class ConcreteToken extends AbstractToken
  27. {
  28. private $credentials = 'credentials_value';
  29. public function __construct($user, array $roles = array())
  30. {
  31. parent::__construct($roles);
  32. $this->setUser($user);
  33. }
  34. public function serialize()
  35. {
  36. return serialize(array($this->credentials, parent::serialize()));
  37. }
  38. public function unserialize($serialized)
  39. {
  40. list($this->credentials, $parentStr) = unserialize($serialized);
  41. parent::unserialize($parentStr);
  42. }
  43. public function getCredentials() {}
  44. }
  45. class AbstractTokenTest extends \PHPUnit_Framework_TestCase
  46. {
  47. public function testGetUsername()
  48. {
  49. $token = $this->getToken(array('ROLE_FOO'));
  50. $token->setUser('fabien');
  51. $this->assertEquals('fabien', $token->getUsername());
  52. $token->setUser(new TestUser('fabien'));
  53. $this->assertEquals('fabien', $token->getUsername());
  54. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  55. $user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));
  56. $token->setUser($user);
  57. $this->assertEquals('fabien', $token->getUsername());
  58. }
  59. public function testEraseCredentials()
  60. {
  61. $token = $this->getToken(array('ROLE_FOO'));
  62. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  63. $user->expects($this->once())->method('eraseCredentials');
  64. $token->setUser($user);
  65. $token->eraseCredentials();
  66. }
  67. /**
  68. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::serialize
  69. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::unserialize
  70. */
  71. public function testSerialize()
  72. {
  73. $token = $this->getToken(array('ROLE_FOO'));
  74. $token->setAttributes(array('foo' => 'bar'));
  75. $uToken = unserialize(serialize($token));
  76. $this->assertEquals($token->getRoles(), $uToken->getRoles());
  77. $this->assertEquals($token->getAttributes(), $uToken->getAttributes());
  78. }
  79. public function testSerializeParent()
  80. {
  81. $user = new TestUser('fabien');
  82. $token = new ConcreteToken($user, array('ROLE_FOO'));
  83. $parentToken = new ConcreteToken($user, array(new SwitchUserRole('ROLE_PREVIOUS', $token)));
  84. $uToken = unserialize(serialize($parentToken));
  85. $this->assertEquals(
  86. current($parentToken->getRoles())->getSource()->getUser(),
  87. current($uToken->getRoles())->getSource()->getUser()
  88. );
  89. }
  90. /**
  91. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::__construct
  92. */
  93. public function testConstructor()
  94. {
  95. $token = $this->getToken(array('ROLE_FOO'));
  96. $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
  97. $token = $this->getToken(array(new Role('ROLE_FOO')));
  98. $this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
  99. $token = $this->getToken(array(new Role('ROLE_FOO'), 'ROLE_BAR'));
  100. $this->assertEquals(array(new Role('ROLE_FOO'), new Role('ROLE_BAR')), $token->getRoles());
  101. }
  102. /**
  103. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::isAuthenticated
  104. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAuthenticated
  105. */
  106. public function testAuthenticatedFlag()
  107. {
  108. $token = $this->getToken();
  109. $this->assertFalse($token->isAuthenticated());
  110. $token->setAuthenticated(true);
  111. $this->assertTrue($token->isAuthenticated());
  112. $token->setAuthenticated(false);
  113. $this->assertFalse($token->isAuthenticated());
  114. }
  115. /**
  116. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::getAttributes
  117. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAttributes
  118. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::hasAttribute
  119. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::getAttribute
  120. * @covers Symfony\Component\Security\Core\Authentication\Token\AbstractToken::setAttribute
  121. */
  122. public function testAttributes()
  123. {
  124. $attributes = array('foo' => 'bar');
  125. $token = $this->getToken();
  126. $token->setAttributes($attributes);
  127. $this->assertEquals($attributes, $token->getAttributes(), '->getAttributes() returns the token attributes');
  128. $this->assertEquals('bar', $token->getAttribute('foo'), '->getAttribute() returns the value of an attribute');
  129. $token->setAttribute('foo', 'foo');
  130. $this->assertEquals('foo', $token->getAttribute('foo'), '->setAttribute() changes the value of an attribute');
  131. $this->assertTrue($token->hasAttribute('foo'), '->hasAttribute() returns true if the attribute is defined');
  132. $this->assertFalse($token->hasAttribute('oof'), '->hasAttribute() returns false if the attribute is not defined');
  133. try {
  134. $token->getAttribute('foobar');
  135. $this->fail('->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
  136. } catch (\Exception $e) {
  137. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
  138. $this->assertEquals('This token has no "foobar" attribute.', $e->getMessage(), '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
  139. }
  140. }
  141. /**
  142. * @dataProvider getUsers
  143. */
  144. public function testSetUser($user)
  145. {
  146. $token = $this->getToken();
  147. $token->setUser($user);
  148. $this->assertSame($user, $token->getUser());
  149. }
  150. public function getUsers()
  151. {
  152. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  153. $advancedUser = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
  154. return array(
  155. array($advancedUser),
  156. array($user),
  157. array(new TestUser('foo')),
  158. array('foo'),
  159. );
  160. }
  161. /**
  162. * @dataProvider getUserChanges
  163. */
  164. public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
  165. {
  166. $token = $this->getToken();
  167. $token->setAuthenticated(true);
  168. $this->assertTrue($token->isAuthenticated());
  169. $token->setUser($firstUser);
  170. $this->assertTrue($token->isAuthenticated());
  171. $token->setUser($secondUser);
  172. $this->assertFalse($token->isAuthenticated());
  173. }
  174. public function getUserChanges()
  175. {
  176. $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
  177. $advancedUser = $this->getMock('Symfony\Component\Security\Core\User\AdvancedUserInterface');
  178. return array(
  179. array(
  180. 'foo', 'bar',
  181. ),
  182. array(
  183. 'foo', new TestUser('bar'),
  184. ),
  185. array(
  186. 'foo', $user,
  187. ),
  188. array(
  189. 'foo', $advancedUser
  190. ),
  191. array(
  192. $user, 'foo'
  193. ),
  194. array(
  195. $advancedUser, 'foo'
  196. ),
  197. array(
  198. $user, new TestUser('foo'),
  199. ),
  200. array(
  201. $advancedUser, new TestUser('foo'),
  202. ),
  203. array(
  204. new TestUser('foo'), new TestUser('bar'),
  205. ),
  206. array(
  207. new TestUser('foo'), 'bar',
  208. ),
  209. array(
  210. new TestUser('foo'), $user,
  211. ),
  212. array(
  213. new TestUser('foo'), $advancedUser,
  214. ),
  215. array(
  216. $user, $advancedUser
  217. ),
  218. array(
  219. $advancedUser, $user
  220. ),
  221. );
  222. }
  223. /**
  224. * @dataProvider getUsers
  225. */
  226. public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($user)
  227. {
  228. $token = $this->getToken();
  229. $token->setAuthenticated(true);
  230. $this->assertTrue($token->isAuthenticated());
  231. $token->setUser($user);
  232. $this->assertTrue($token->isAuthenticated());
  233. $token->setUser($user);
  234. $this->assertTrue($token->isAuthenticated());
  235. }
  236. protected function getToken(array $roles = array())
  237. {
  238. return $this->getMockForAbstractClass('Symfony\Component\Security\Core\Authentication\Token\AbstractToken', array($roles));
  239. }
  240. }