You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. use Luticate\Auth\Business\LuUsersBusiness;
  3. use Luticate\Auth\Controller\LuUsersController;
  4. use Luticate\Auth\Dbo\Users\LuUsersAddDbo;
  5. use Luticate\Auth\Dbo\Users\LuUsersDbo;
  6. use Luticate\Auth\Dbo\Users\LuUsersEditDbo;
  7. use Luticate\Auth\Dbo\Users\LuUsersLoginDbo;
  8. use Luticate\Utils\Business\LuBusinessException;
  9. use Luticate\Utils\Controller\LuticateApplication;
  10. use Luticate\Utils\DataAccess\LuDataAccess;
  11. /**
  12. * Created by PhpStorm.
  13. * User: robin
  14. * Date: 9/18/16
  15. * Time: 1:02 AM
  16. */
  17. class Db extends LuDataAccess
  18. {
  19. protected static $_connection = "luticatedb";
  20. }
  21. class LuUsersTest extends \PHPUnit_Framework_TestCase
  22. {
  23. protected function getCtrl()
  24. {
  25. return new LuUsersController();
  26. }
  27. public static function setUpBeforeClass()
  28. {
  29. Db::getPdo()->query("DELETE FROM lu_users WHERE username LIKE '_test_%'");
  30. }
  31. public static function tearDownAfterClass()
  32. {
  33. static::setUpBeforeClass();
  34. }
  35. public function testUserAdd1()
  36. {
  37. $ctrl = static::getCtrl();
  38. $newUser = new LuUsersAddDbo();
  39. $newUser->setUsername("_test_user1");
  40. $newUser->setPassword("test42");
  41. $newUser->setFirstname("Test");
  42. $newUser->setLastname("user1");
  43. $newUser->setEmail("test.user1@example.com");
  44. $newUser->setExternalAuth(null);
  45. $newUser->setProfileId(null);
  46. $user = $ctrl->add($newUser);
  47. $this->assertNotNull($user);
  48. $this->assertNotNull($user->getId());
  49. $this->assertSame("_test_user1", $user->getUsername());
  50. $this->assertSame("Test", $user->getFirstname());
  51. $this->assertSame("user1", $user->getLastname());
  52. $this->assertSame("test.user1@example.com", $user->getEmail());
  53. $this->assertNull($user->getExternalAuth());
  54. $this->assertNull($user->getProfileId());
  55. }
  56. public function testUserAdd2()
  57. {
  58. $ctrl = static::getCtrl();
  59. $newUser = new LuUsersAddDbo();
  60. $newUser->setUsername("_test_user1");
  61. $newUser->setPassword("test42");
  62. $newUser->setFirstname("Test");
  63. $newUser->setLastname("user1");
  64. $newUser->setEmail("test.user1@example.com");
  65. $newUser->setExternalAuth(null);
  66. $newUser->setProfileId(null);
  67. $this->expectException(LuBusinessException::class);
  68. $this->expectExceptionCode(400);
  69. $ctrl->add($newUser);
  70. }
  71. public function testUserAdd3()
  72. {
  73. $ctrl = static::getCtrl();
  74. $newUser = new LuUsersAddDbo();
  75. $newUser->setUsername("_test_user2");
  76. $newUser->setPassword("test42");
  77. $newUser->setFirstname("Test");
  78. $newUser->setLastname("user1");
  79. $newUser->setEmail("test.user1@example.com");
  80. $newUser->setExternalAuth(null);
  81. $newUser->setProfileId(null);
  82. $this->expectException(LuBusinessException::class);
  83. $this->expectExceptionCode(400);
  84. $ctrl->add($newUser);
  85. }
  86. public function testUserAdd4()
  87. {
  88. $ctrl = static::getCtrl();
  89. $newUser = new LuUsersAddDbo();
  90. $newUser->setUsername("_test_user2");
  91. $newUser->setPassword("test24");
  92. $newUser->setFirstname("Test");
  93. $newUser->setLastname("user2");
  94. $newUser->setEmail("test.user2@example.com");
  95. $newUser->setExternalAuth(null);
  96. $newUser->setProfileId(null);
  97. $user = $ctrl->add($newUser);
  98. $this->assertNotNull($user);
  99. $this->assertNotNull($user->getId());
  100. $this->assertSame("_test_user2", $user->getUsername());
  101. $this->assertSame("Test", $user->getFirstname());
  102. $this->assertSame("user2", $user->getLastname());
  103. $this->assertSame("test.user2@example.com", $user->getEmail());
  104. $this->assertNull($user->getExternalAuth());
  105. $this->assertNull($user->getProfileId());
  106. }
  107. public function testUserLogin1()
  108. {
  109. $ctrl = static::getCtrl();
  110. $loginDbo = new LuUsersLoginDbo();
  111. $loginDbo->setUsername("_test_user1");
  112. $loginDbo->setPassword("test42");
  113. $loginResult = $ctrl->login($loginDbo);
  114. $this->assertNotNull($loginResult);
  115. $this->assertSame("test.user1@example.com", $loginResult->getEmail());
  116. $loggedUser = LuUsersBusiness::getUserFromToken($loginResult->getToken());
  117. $this->assertSame("test.user1@example.com", $loggedUser->getEmail());
  118. }
  119. public function testUserLogin2()
  120. {
  121. $ctrl = static::getCtrl();
  122. $loginDbo = new LuUsersLoginDbo();
  123. $loginDbo->setUsername("_test_user2");
  124. $loginDbo->setPassword("test24");
  125. $loginResult = $ctrl->login($loginDbo);
  126. $this->assertNotNull($loginResult);
  127. $this->assertSame("test.user2@example.com", $loginResult->getEmail());
  128. $loggedUser = LuUsersBusiness::getUserFromToken($loginResult->getToken());
  129. $this->assertSame("test.user2@example.com", $loggedUser->getEmail());
  130. }
  131. public function testUserLogin3()
  132. {
  133. $ctrl = static::getCtrl();
  134. $loginDbo = new LuUsersLoginDbo();
  135. $loginDbo->setUsername("azertyuiop");
  136. $loginDbo->setPassword("azertyuiop");
  137. $this->expectException(LuBusinessException::class);
  138. $this->expectExceptionCode(401);
  139. $ctrl->login($loginDbo);
  140. }
  141. public function testUserGetMe1()
  142. {
  143. $ctrl = static::getCtrl();
  144. $loginDbo = new LuUsersLoginDbo();
  145. $loginDbo->setUsername("_test_user1");
  146. $loginDbo->setPassword("test42");
  147. $loginResult = $ctrl->login($loginDbo);
  148. $this->assertNotNull($loginResult);
  149. $this->assertSame("test.user1@example.com", $loginResult->getEmail());
  150. /**
  151. * @var $user LuUsersDbo
  152. */
  153. $user = LuUsersBusiness::getById($loginResult->getId());
  154. $me = $ctrl->getMe($user);
  155. $this->assertSame("test.user1@example.com", $me->getEmail());
  156. $this->assertSame("_test_user1", $user->getUsername());
  157. }
  158. public function testUserGetById1()
  159. {
  160. $ctrl = static::getCtrl();
  161. $user = LuUsersBusiness::getByUsernameOrEmail("test.user1@example.com");
  162. $user = $ctrl->getById($user->getId());
  163. $this->assertSame("test.user1@example.com", $user->getEmail());
  164. $this->assertSame("_test_user1", $user->getUsername());
  165. }
  166. public function testUserGetById2()
  167. {
  168. $ctrl = static::getCtrl();
  169. $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
  170. $user = $ctrl->getById($user->getId());
  171. $this->assertSame("test.user2@example.com", $user->getEmail());
  172. $this->assertSame("_test_user2", $user->getUsername());
  173. }
  174. public function testUserEditMe1()
  175. {
  176. $ctrl = static::getCtrl();
  177. $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
  178. $edited = new LuUsersEditDbo();
  179. $edited->setEmail("test.user2+edited@example.com");
  180. $edited->setFirstname("test-edited");
  181. $edited->setLastname("user2-edited");
  182. $ctrl->editMe($user, $edited);
  183. $editedUser = $ctrl->getById($user->getId());
  184. $this->assertSame("test.user2+edited@example.com", $editedUser->getEmail());
  185. $this->assertSame("test-edited", $editedUser->getFirstname());
  186. $this->assertSame("user2-edited", $editedUser->getLastname());
  187. }
  188. public function testUserEdit1()
  189. {
  190. $ctrl = static::getCtrl();
  191. $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
  192. $edited = new LuUsersEditDbo();
  193. $edited->setEmail("test.user2@example.com");
  194. $edited->setFirstname("test");
  195. $edited->setLastname("user2");
  196. $ctrl->edit($user->getId(), $edited);
  197. $editedUser = $ctrl->getById($user->getId());
  198. $this->assertSame("test.user2@example.com", $editedUser->getEmail());
  199. $this->assertSame("test", $editedUser->getFirstname());
  200. $this->assertSame("user2", $editedUser->getLastname());
  201. }
  202. public function testUserDelete1()
  203. {
  204. //TODO
  205. // $ctrl = static::getCtrl();
  206. //
  207. // $loginDbo = new LuUsersLoginDbo();
  208. // $loginDbo->setUsername("_test_user1");
  209. // $loginDbo->setPassword("test42");
  210. // $loginResult = $ctrl->login($loginDbo);
  211. //
  212. // $this->assertNotNull($loginResult);
  213. // $this->assertSame("test.user1@example.com", $loginResult->getEmail());
  214. //
  215. // $user = LuUsersBusiness::getByUsernameOrEmail("test.user2@example.com");
  216. //
  217. // $user = $ctrl->getById($user->getId());
  218. //
  219. // $this->assertSame("test.user2@example.com", $user->getEmail());
  220. // $this->assertSame("_test_user2", $user->getUsername());
  221. }
  222. }