123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- use Luticate\Auth\Business\LuUsersBusiness;
- use Luticate\Auth\Controller\LuUsersController;
- use Luticate\Auth\Dbo\Users\LuUsersAddDbo;
- use Luticate\Auth\Dbo\Users\LuUsersDbo;
- use Luticate\Auth\Dbo\Users\LuUsersEditDbo;
- use Luticate\Auth\Dbo\Users\LuUsersLoginDbo;
- use Luticate\Utils\Business\LuBusinessException;
- use Luticate\Utils\Controller\LuticateApplication;
- use Luticate\Utils\DataAccess\LuDataAccess;
-
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 9/18/16
- * Time: 1:02 AM
- */
-
- class Db extends LuDataAccess
- {
- protected static $_connection = "luticatedb";
- }
-
- class LuUsersTest extends \PHPUnit_Framework_TestCase
- {
- protected function getCtrl()
- {
- return new LuUsersController();
- }
-
- public static function setUpBeforeClass()
- {
- Db::getPdo()->query("DELETE FROM lu_users WHERE username LIKE '_test_%'");
- }
-
- public static function tearDownAfterClass()
- {
- static::setUpBeforeClass();
- }
-
- public function testUserAdd1()
- {
- $ctrl = static::getCtrl();
-
- $newUser = new LuUsersAddDbo();
- $newUser->setUsername("_test_user1");
- $newUser->setPassword("test42");
- $newUser->setFirstname("Test");
- $newUser->setLastname("user1");
- $newUser->setEmail("test.user1@example.com");
- $newUser->setExternalAuth(null);
- $newUser->setProfileId(null);
-
- $user = $ctrl->add($newUser);
-
- $this->assertNotNull($user);
- $this->assertNotNull($user->getId());
- $this->assertSame("_test_user1", $user->getUsername());
- $this->assertSame("Test", $user->getFirstname());
- $this->assertSame("user1", $user->getLastname());
- $this->assertSame("test.user1@example.com", $user->getEmail());
- $this->assertNull($user->getExternalAuth());
- $this->assertNull($user->getProfileId());
- }
-
- public function testUserAdd2()
- {
- $ctrl = static::getCtrl();
-
- $newUser = new LuUsersAddDbo();
- $newUser->setUsername("_test_user1");
- $newUser->setPassword("test42");
- $newUser->setFirstname("Test");
- $newUser->setLastname("user1");
- $newUser->setEmail("test.user1@example.com");
- $newUser->setExternalAuth(null);
- $newUser->setProfileId(null);
-
- $this->expectException(LuBusinessException::class);
- $this->expectExceptionCode(400);
- $ctrl->add($newUser);
- }
-
- public function testUserAdd3()
- {
- $ctrl = static::getCtrl();
-
- $newUser = new LuUsersAddDbo();
- $newUser->setUsername("_test_user2");
- $newUser->setPassword("test42");
- $newUser->setFirstname("Test");
- $newUser->setLastname("user1");
- $newUser->setEmail("test.user1@example.com");
- $newUser->setExternalAuth(null);
- $newUser->setProfileId(null);
-
- $this->expectException(LuBusinessException::class);
- $this->expectExceptionCode(400);
- $ctrl->add($newUser);
- }
-
- public function testUserAdd4()
- {
- $ctrl = static::getCtrl();
-
- $newUser = new LuUsersAddDbo();
- $newUser->setUsername("_test_user2");
- $newUser->setPassword("test24");
- $newUser->setFirstname("Test");
- $newUser->setLastname("user2");
- $newUser->setEmail("test.user2@example.com");
- $newUser->setExternalAuth(null);
- $newUser->setProfileId(null);
-
- $user = $ctrl->add($newUser);
-
- $this->assertNotNull($user);
- $this->assertNotNull($user->getId());
- $this->assertSame("_test_user2", $user->getUsername());
- $this->assertSame("Test", $user->getFirstname());
- $this->assertSame("user2", $user->getLastname());
- $this->assertSame("test.user2@example.com", $user->getEmail());
- $this->assertNull($user->getExternalAuth());
- $this->assertNull($user->getProfileId());
- }
-
- public function testUserLogin1()
- {
- $ctrl = static::getCtrl();
-
- $loginDbo = new LuUsersLoginDbo();
- $loginDbo->setUsername("_test_user1");
- $loginDbo->setPassword("test42");
- $loginResult = $ctrl->login($loginDbo);
-
- $this->assertNotNull($loginResult);
- $this->assertSame("test.user1@example.com", $loginResult->getEmail());
-
- $loggedUser = LuUsersBusiness::getUserFromToken($loginResult->getToken());
-
- $this->assertSame("test.user1@example.com", $loggedUser->getEmail());
- }
-
- public function testUserLogin2()
- {
- $ctrl = static::getCtrl();
-
- $loginDbo = new LuUsersLoginDbo();
- $loginDbo->setUsername("_test_user2");
- $loginDbo->setPassword("test24");
- $loginResult = $ctrl->login($loginDbo);
-
- $this->assertNotNull($loginResult);
- $this->assertSame("test.user2@example.com", $loginResult->getEmail());
-
- $loggedUser = LuUsersBusiness::getUserFromToken($loginResult->getToken());
-
- $this->assertSame("test.user2@example.com", $loggedUser->getEmail());
- }
-
- public function testUserLogin3()
- {
- $ctrl = static::getCtrl();
-
- $loginDbo = new LuUsersLoginDbo();
- $loginDbo->setUsername("azertyuiop");
- $loginDbo->setPassword("azertyuiop");
-
- $this->expectException(LuBusinessException::class);
- $this->expectExceptionCode(401);
- $ctrl->login($loginDbo);
- }
-
- public function testUserGetMe1()
- {
- $ctrl = static::getCtrl();
-
- $loginDbo = new LuUsersLoginDbo();
- $loginDbo->setUsername("_test_user1");
- $loginDbo->setPassword("test42");
- $loginResult = $ctrl->login($loginDbo);
-
- $this->assertNotNull($loginResult);
- $this->assertSame("test.user1@example.com", $loginResult->getEmail());
-
- /**
- * @var $user LuUsersDbo
- */
- $user = LuUsersBusiness::getById($loginResult->getId());
-
- $me = $ctrl->getMe($user);
-
- $this->assertSame("test.user1@example.com", $me->getEmail());
- $this->assertSame("_test_user1", $user->getUsername());
- }
-
- public function testUserGetById1()
- {
- $ctrl = static::getCtrl();
-
- $user = LuUsersBusiness::getByUsernameOrEmail("test.user1@example.com");
-
- $user = $ctrl->getById($user->getId());
-
- $this->assertSame("test.user1@example.com", $user->getEmail());
- $this->assertSame("_test_user1", $user->getUsername());
- }
-
- public function testUserGetById2()
- {
- $ctrl = static::getCtrl();
-
- $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
-
- $user = $ctrl->getById($user->getId());
-
- $this->assertSame("test.user2@example.com", $user->getEmail());
- $this->assertSame("_test_user2", $user->getUsername());
- }
-
- public function testUserEditMe1()
- {
- $ctrl = static::getCtrl();
-
- $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
-
- $edited = new LuUsersEditDbo();
- $edited->setEmail("test.user2+edited@example.com");
- $edited->setFirstname("test-edited");
- $edited->setLastname("user2-edited");
-
- $ctrl->editMe($user, $edited);
-
- $editedUser = $ctrl->getById($user->getId());
-
- $this->assertSame("test.user2+edited@example.com", $editedUser->getEmail());
- $this->assertSame("test-edited", $editedUser->getFirstname());
- $this->assertSame("user2-edited", $editedUser->getLastname());
- }
-
- public function testUserEdit1()
- {
- $ctrl = static::getCtrl();
-
- $user = LuUsersBusiness::getByUsernameOrEmail("_test_user2");
-
- $edited = new LuUsersEditDbo();
- $edited->setEmail("test.user2@example.com");
- $edited->setFirstname("test");
- $edited->setLastname("user2");
-
- $ctrl->edit($user->getId(), $edited);
-
- $editedUser = $ctrl->getById($user->getId());
-
- $this->assertSame("test.user2@example.com", $editedUser->getEmail());
- $this->assertSame("test", $editedUser->getFirstname());
- $this->assertSame("user2", $editedUser->getLastname());
- }
-
- public function testUserDelete1()
- {
- //TODO
- // $ctrl = static::getCtrl();
- //
- // $loginDbo = new LuUsersLoginDbo();
- // $loginDbo->setUsername("_test_user1");
- // $loginDbo->setPassword("test42");
- // $loginResult = $ctrl->login($loginDbo);
- //
- // $this->assertNotNull($loginResult);
- // $this->assertSame("test.user1@example.com", $loginResult->getEmail());
- //
- // $user = LuUsersBusiness::getByUsernameOrEmail("test.user2@example.com");
- //
- // $user = $ctrl->getById($user->getId());
- //
- // $this->assertSame("test.user2@example.com", $user->getEmail());
- // $this->assertSame("_test_user2", $user->getUsername());
- }
- }
|