UserManipulatorTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Tests\Util;
  11. use FOS\UserBundle\Util\UserManipulator;
  12. use FOS\UserBundle\Tests\TestUser;
  13. class UserManipulatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testCreate()
  16. {
  17. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  18. $user = new TestUser();
  19. $username = 'test_username';
  20. $password = 'test_password';
  21. $email = 'test@email.org';
  22. $active = true; // it is enabled
  23. $superadmin = false;
  24. $userManagerMock->expects($this->once())
  25. ->method('createUser')
  26. ->will($this->returnValue($user));
  27. $userManagerMock->expects($this->once())
  28. ->method('updateUser')
  29. ->will($this->returnValue($user))
  30. ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
  31. $manipulator = new UserManipulator($userManagerMock);
  32. $manipulator->create($username, $password, $email, $active, $superadmin);
  33. $this->assertEquals($username, $user->getUsername());
  34. $this->assertEquals($password, $user->getPlainPassword());
  35. $this->assertEquals($email, $user->getEmail());
  36. $this->assertEquals($active, $user->isEnabled());
  37. $this->assertEquals($superadmin, $user->isSuperAdmin());
  38. }
  39. public function testActivateWithValidUsername()
  40. {
  41. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  42. $username = 'test_username';
  43. $user = new TestUser();
  44. $user->setUsername($username);
  45. $user->setEnabled(false);
  46. $userManagerMock->expects($this->once())
  47. ->method('findUserByUsername')
  48. ->will($this->returnValue($user))
  49. ->with($this->equalTo($username));
  50. $userManagerMock->expects($this->once())
  51. ->method('updateUser')
  52. ->will($this->returnValue($user))
  53. ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
  54. $manipulator = new UserManipulator($userManagerMock);
  55. $manipulator->activate($username);
  56. $this->assertEquals($username, $user->getUsername());
  57. $this->assertEquals(true, $user->isEnabled());
  58. }
  59. /**
  60. * @expectedException \InvalidArgumentException
  61. */
  62. public function testActivateWithInvalidUsername()
  63. {
  64. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  65. $invalidusername = 'invalid_username';
  66. $userManagerMock->expects($this->once())
  67. ->method('findUserByUsername')
  68. ->will($this->returnValue(null))
  69. ->with($this->equalTo($invalidusername));
  70. $userManagerMock->expects($this->never())
  71. ->method('updateUser');
  72. $manipulator = new UserManipulator($userManagerMock);
  73. $manipulator->activate($invalidusername);
  74. }
  75. public function testDeactivateWithValidUsername()
  76. {
  77. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  78. $username = 'test_username';
  79. $user = new TestUser();
  80. $user->setUsername($username);
  81. $user->setEnabled(true);
  82. $userManagerMock->expects($this->once())
  83. ->method('findUserByUsername')
  84. ->will($this->returnValue($user))
  85. ->with($this->equalTo($username));
  86. $userManagerMock->expects($this->once())
  87. ->method('updateUser')
  88. ->will($this->returnValue($user))
  89. ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
  90. $manipulator = new UserManipulator($userManagerMock);
  91. $manipulator->deactivate($username);
  92. $this->assertEquals($username, $user->getUsername());
  93. $this->assertEquals(false, $user->isEnabled());
  94. }
  95. /**
  96. * @expectedException \InvalidArgumentException
  97. */
  98. public function testDeactivateWithInvalidUsername()
  99. {
  100. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  101. $invalidusername = 'invalid_username';
  102. $userManagerMock->expects($this->once())
  103. ->method('findUserByUsername')
  104. ->will($this->returnValue(null))
  105. ->with($this->equalTo($invalidusername));
  106. $userManagerMock->expects($this->never())
  107. ->method('updateUser');
  108. $manipulator = new UserManipulator($userManagerMock);
  109. $manipulator->deactivate($invalidusername);
  110. }
  111. public function testPromoteWithValidUsername()
  112. {
  113. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  114. $username = 'test_username';
  115. $user = new TestUser();
  116. $user->setUsername($username);
  117. $user->setSuperAdmin(false);
  118. $userManagerMock->expects($this->once())
  119. ->method('findUserByUsername')
  120. ->will($this->returnValue($user))
  121. ->with($this->equalTo($username));
  122. $userManagerMock->expects($this->once())
  123. ->method('updateUser')
  124. ->will($this->returnValue($user))
  125. ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
  126. $manipulator = new UserManipulator($userManagerMock);
  127. $manipulator->promote($username);
  128. $this->assertEquals($username, $user->getUsername());
  129. $this->assertEquals(true, $user->isSuperAdmin());
  130. }
  131. /**
  132. * @expectedException \InvalidArgumentException
  133. */
  134. public function testPromoteWithInvalidUsername()
  135. {
  136. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  137. $invalidusername = 'invalid_username';
  138. $userManagerMock->expects($this->once())
  139. ->method('findUserByUsername')
  140. ->will($this->returnValue(null))
  141. ->with($this->equalTo($invalidusername));
  142. $userManagerMock->expects($this->never())
  143. ->method('updateUser');
  144. $manipulator = new UserManipulator($userManagerMock);
  145. $manipulator->promote($invalidusername);
  146. }
  147. public function testDemoteWithValidUsername()
  148. {
  149. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  150. $username = 'test_username';
  151. $user = new TestUser();
  152. $user->setUsername($username);
  153. $user->setSuperAdmin(true);
  154. $userManagerMock->expects($this->once())
  155. ->method('findUserByUsername')
  156. ->will($this->returnValue($user))
  157. ->with($this->equalTo($username));
  158. $userManagerMock->expects($this->once())
  159. ->method('updateUser')
  160. ->will($this->returnValue($user))
  161. ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
  162. $manipulator = new UserManipulator($userManagerMock);
  163. $manipulator->demote($username);
  164. $this->assertEquals($username, $user->getUsername());
  165. $this->assertEquals(false, $user->isSuperAdmin());
  166. }
  167. /**
  168. * @expectedException \InvalidArgumentException
  169. */
  170. public function testDemoteWithInvalidUsername()
  171. {
  172. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  173. $invalidusername = 'invalid_username';
  174. $userManagerMock->expects($this->once())
  175. ->method('findUserByUsername')
  176. ->will($this->returnValue(null))
  177. ->with($this->equalTo($invalidusername));
  178. $userManagerMock->expects($this->never())
  179. ->method('updateUser');
  180. $manipulator = new UserManipulator($userManagerMock);
  181. $manipulator->demote($invalidusername);
  182. }
  183. public function testChangePasswordWithValidUsername()
  184. {
  185. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  186. $user = new TestUser();
  187. $username = 'test_username';
  188. $password = 'test_password';
  189. $oldpassword = 'old_password';
  190. $user->setUsername($username);
  191. $user->setPlainPassword($oldpassword);
  192. $userManagerMock->expects($this->once())
  193. ->method('findUserByUsername')
  194. ->will($this->returnValue($user))
  195. ->with($this->equalTo($username));
  196. $userManagerMock->expects($this->once())
  197. ->method('updateUser')
  198. ->will($this->returnValue($user))
  199. ->with($this->isInstanceOf('FOS\UserBundle\Tests\TestUser'));
  200. $manipulator = new UserManipulator($userManagerMock);
  201. $manipulator->changePassword($username, $password);
  202. $this->assertEquals($username, $user->getUsername());
  203. $this->assertEquals($password, $user->getPlainPassword());
  204. }
  205. /**
  206. * @expectedException \InvalidArgumentException
  207. */
  208. public function testChangePasswordWithInvalidUsername()
  209. {
  210. $userManagerMock = $this->getMock('FOS\UserBundle\Model\UserManagerInterface');
  211. $invalidusername = 'invalid_username';
  212. $password = 'test_password';
  213. $userManagerMock->expects($this->once())
  214. ->method('findUserByUsername')
  215. ->will($this->returnValue(null))
  216. ->with($this->equalTo($invalidusername));
  217. $userManagerMock->expects($this->never())
  218. ->method('updateUser');
  219. $manipulator = new UserManipulator($userManagerMock);
  220. $manipulator->changePassword($invalidusername, $password);
  221. }
  222. }