diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-07-24 13:29:24 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-07-24 13:29:24 -0400 |
commit | be22f0619e25b15c8c3d002cf1f190aa9976bc71 (patch) | |
tree | d2007a08747154830bf2df202f4321fe7d0e8ec9 | |
parent | a6d22bf2715347d4f340376efee75dc57176c8b6 (diff) |
Remove method UserModel::getFullname()
-rw-r--r-- | app/Helper/UserHelper.php | 3 | ||||
-rw-r--r-- | app/Model/UserModel.php | 13 | ||||
-rw-r--r-- | tests/units/Helper/UserHelperTest.php | 15 | ||||
-rw-r--r-- | tests/units/Model/UserModelTest.php | 384 | ||||
-rw-r--r-- | tests/units/Model/UserTest.php | 400 |
5 files changed, 402 insertions, 413 deletions
diff --git a/app/Helper/UserHelper.php b/app/Helper/UserHelper.php index ab259a62..e42bafe4 100644 --- a/app/Helper/UserHelper.php +++ b/app/Helper/UserHelper.php @@ -50,7 +50,8 @@ class UserHelper extends Base */ public function getFullname(array $user = array()) { - return $this->userModel->getFullname(empty($user) ? $this->userSession->getAll() : $user); + $user = empty($user) ? $this->userSession->getAll() : $user; + return $user['name'] ?: $user['username']; } /** diff --git a/app/Model/UserModel.php b/app/Model/UserModel.php index f7a051c5..56b1a960 100644 --- a/app/Model/UserModel.php +++ b/app/Model/UserModel.php @@ -65,17 +65,6 @@ class UserModel extends Base } /** - * Return the full name - * - * @param array $user User properties - * @return string - */ - public function getFullname(array $user) - { - return $user['name'] ?: $user['username']; - } - - /** * Return true is the given user id is administrator * * @access public @@ -230,7 +219,7 @@ class UserModel extends Base $result = array(); foreach ($users as $user) { - $result[$user['id']] = $this->getFullname($user); + $result[$user['id']] = $this->helper->user->getFullname($user); } asort($result); diff --git a/tests/units/Helper/UserHelperTest.php b/tests/units/Helper/UserHelperTest.php index d5bd1789..4d1947d8 100644 --- a/tests/units/Helper/UserHelperTest.php +++ b/tests/units/Helper/UserHelperTest.php @@ -13,6 +13,21 @@ use Kanboard\Model\UserModel; class UserHelperTest extends Base { + public function testGetFullname() + { + $userModel = new UserModel($this->container); + $userHelper = new UserHelper($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User #2'))); + + $user1 = $userModel->getById(2); + $user2 = $userModel->getById(3); + + $this->assertEquals('user1', $userHelper->getFullname($user1)); + $this->assertEquals('User #2', $userHelper->getFullname($user2)); + } + public function testInitials() { $helper = new UserHelper($this->container); diff --git a/tests/units/Model/UserModelTest.php b/tests/units/Model/UserModelTest.php new file mode 100644 index 00000000..a0c9c575 --- /dev/null +++ b/tests/units/Model/UserModelTest.php @@ -0,0 +1,384 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\UserModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\CommentModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Core\Security\Role; + +class UserModelTest extends Base +{ + public function testGetByEmail() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1', 'password' => '123456', 'email' => 'user1@localhost'))); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'password' => '123456', 'email' => ''))); + + $this->assertNotEmpty($userModel->getByEmail('user1@localhost')); + $this->assertEmpty($userModel->getByEmail('')); + } + + public function testGetByExternalId() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1', 'password' => '123456', 'gitlab_id' => '1234'))); + + $this->assertNotEmpty($userModel->getByExternalId('gitlab_id', '1234')); + $this->assertEmpty($userModel->getByExternalId('gitlab_id', '')); + + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'password' => '123456', 'github_id' => 'plop'))); + $this->assertNotFalse($userModel->create(array('username' => 'user3', 'password' => '123456', 'github_id' => ''))); + + $this->assertNotEmpty($userModel->getByExternalId('github_id', 'plop')); + $this->assertEmpty($userModel->getByExternalId('github_id', '')); + + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user4', 'password' => '123456', 'google_id' => '1234'))); + $this->assertNotFalse($userModel->create(array('username' => 'user5', 'password' => '123456', 'google_id' => ''))); + + $this->assertNotEmpty($userModel->getByExternalId('google_id', '1234')); + $this->assertEmpty($userModel->getByExternalId('google_id', '')); + } + + public function testGetByToken() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1', 'token' => 'random'))); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'token' => ''))); + + $this->assertNotEmpty($userModel->getByToken('random')); + $this->assertEmpty($userModel->getByToken('')); + } + + public function testGetByUsername() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + + $this->assertNotEmpty($userModel->getByUsername('user1')); + $this->assertEmpty($userModel->getByUsername('user2')); + $this->assertEmpty($userModel->getByUsername('')); + } + + public function testExists() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + + $this->assertTrue($userModel->exists(1)); + $this->assertTrue($userModel->exists(2)); + $this->assertFalse($userModel->exists(3)); + } + + public function testCount() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + $this->assertEquals(2, $userModel->count()); + } + + public function testGetAll() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'you'))); + $this->assertEquals(3, $userModel->create(array('username' => 'me', 'name' => 'Me'))); + + $users = $userModel->getAll(); + $this->assertCount(3, $users); + $this->assertEquals('admin', $users[0]['username']); + $this->assertEquals('me', $users[1]['username']); + $this->assertEquals('you', $users[2]['username']); + } + + public function testGetActiveUsersList() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'you'))); + $this->assertEquals(3, $userModel->create(array('username' => 'me', 'name' => 'Me too'))); + $this->assertEquals(4, $userModel->create(array('username' => 'foobar', 'is_active' => 0))); + + $users = $userModel->getActiveUsersList(); + + $expected = array( + 1 => 'admin', + 3 => 'Me too', + 2 => 'you', + ); + + $this->assertEquals($expected, $users); + + $users = $userModel->getActiveUsersList(true); + + $expected = array( + UserModel::EVERYBODY_ID => 'Everybody', + 1 => 'admin', + 3 => 'Me too', + 2 => 'you', + ); + + $this->assertEquals($expected, $users); + } + + public function testIsAdmin() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + + $this->assertTrue($userModel->isAdmin(1)); + $this->assertFalse($userModel->isAdmin(2)); + } + + public function testPassword() + { + $password = 'test123'; + $hash = password_hash($password, PASSWORD_BCRYPT); + + $this->assertNotEmpty($hash); + $this->assertTrue(password_verify($password, $hash)); + } + + public function testPrepare() + { + $userModel = new UserModel($this->container); + + $input = array( + 'username' => 'user1', + 'password' => '1234', + 'confirmation' => '1234', + 'name' => 'me', + 'role' => Role::APP_ADMIN, + ); + + $userModel->prepare($input); + $this->assertArrayNotHasKey('confirmation', $input); + + $this->assertArrayHasKey('password', $input); + $this->assertNotEquals('1234', $input['password']); + $this->assertNotEmpty($input['password']); + + $input = array( + 'username' => 'user1', + 'password' => '1234', + 'current_password' => 'bla', + 'confirmation' => '1234', + 'name' => 'me', + 'is_ldap_user' => '1', + ); + + $userModel->prepare($input); + $this->assertArrayNotHasKey('confirmation', $input); + $this->assertArrayNotHasKey('current_password', $input); + + $this->assertArrayHasKey('password', $input); + $this->assertNotEquals('1234', $input['password']); + $this->assertNotEmpty($input['password']); + + $this->assertArrayHasKey('is_ldap_user', $input); + $this->assertEquals(1, $input['is_ldap_user']); + + $input = array( + 'id' => 2, + 'name' => 'me', + ); + + $userModel->prepare($input); + $this->assertEquals(array('id' => 2, 'name' => 'me'), $input); + + $input = array( + 'gitlab_id' => '1234', + ); + + $userModel->prepare($input); + $this->assertEquals(array('gitlab_id' => 1234), $input); + + $input = array( + 'gitlab_id' => '', + ); + + $userModel->prepare($input); + $this->assertEquals(array('gitlab_id' => null), $input); + + $input = array( + 'gitlab_id' => 'something', + ); + + $userModel->prepare($input); + $this->assertEquals(array('gitlab_id' => 0), $input); + + $input = array( + 'username' => 'something', + 'password' => '' + ); + + $userModel->prepare($input); + $this->assertEquals(array('username' => 'something'), $input); + } + + public function testCreate() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'user #1', 'password' => '123456', 'name' => 'User'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user #2', 'is_ldap_user' => 1))); + $this->assertEquals(4, $userModel->create(array('username' => 'user #3', 'role' => Role::APP_MANAGER))); + $this->assertEquals(5, $userModel->create(array('username' => 'user #4', 'gitlab_id' => '', 'role' => Role::APP_ADMIN))); + $this->assertEquals(6, $userModel->create(array('username' => 'user #5', 'gitlab_id' => '1234'))); + $this->assertFalse($userModel->create(array('username' => 'user #1'))); + + $user = $userModel->getById(1); + $this->assertEquals('admin', $user['username']); + $this->assertEquals('', $user['name']); + $this->assertEquals(Role::APP_ADMIN, $user['role']); + $this->assertEquals(0, $user['is_ldap_user']); + + $user = $userModel->getById(2); + $this->assertEquals('user #1', $user['username']); + $this->assertEquals('User', $user['name']); + $this->assertEquals(Role::APP_USER, $user['role']); + $this->assertEquals(0, $user['is_ldap_user']); + + $user = $userModel->getById(3); + $this->assertEquals('user #2', $user['username']); + $this->assertEquals('', $user['name']); + $this->assertEquals(Role::APP_USER, $user['role']); + $this->assertEquals(1, $user['is_ldap_user']); + + $user = $userModel->getById(4); + $this->assertEquals('user #3', $user['username']); + $this->assertEquals(Role::APP_MANAGER, $user['role']); + + $user = $userModel->getById(5); + $this->assertEquals('user #4', $user['username']); + $this->assertEquals('', $user['gitlab_id']); + $this->assertEquals(Role::APP_ADMIN, $user['role']); + + $user = $userModel->getById(6); + $this->assertEquals('user #5', $user['username']); + $this->assertEquals('1234', $user['gitlab_id']); + $this->assertEquals(Role::APP_USER, $user['role']); + } + + public function testUpdate() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); + $this->assertEquals(3, $userModel->create(array('username' => 'plop', 'gitlab_id' => '123'))); + + $this->assertTrue($userModel->update(array('id' => 2, 'username' => 'biloute'))); + $this->assertTrue($userModel->update(array('id' => 3, 'gitlab_id' => ''))); + + $user = $userModel->getById(2); + $this->assertEquals('biloute', $user['username']); + $this->assertEquals('Toto', $user['name']); + $this->assertEquals(Role::APP_USER, $user['role']); + $this->assertEquals(0, $user['is_ldap_user']); + + $user = $userModel->getById(3); + $this->assertNotEmpty($user); + $this->assertEquals(null, $user['gitlab_id']); + } + + public function testRemove() + { + $userModel = new UserModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $commentModel = new CommentModel($this->container); + + $this->assertNotFalse($userModel->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'owner_id' => 2))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'Subtask #1', 'user_id' => 2, 'task_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('comment' => 'foobar', 'user_id' => 2, 'task_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(1, $task['id']); + $this->assertEquals(2, $task['owner_id']); + + $this->assertTrue($userModel->remove(1)); + $this->assertTrue($userModel->remove(2)); + $this->assertFalse($userModel->remove(2)); + $this->assertFalse($userModel->remove(55)); + + // Make sure that assigned tasks are unassigned after removing the user + $task = $taskFinderModel->getById(1); + $this->assertEquals(1, $task['id']); + $this->assertEquals(0, $task['owner_id']); + + // Make sure that assigned subtasks are unassigned after removing the user + $subtask = $subtaskModel->getById(1); + $this->assertEquals(1, $subtask['id']); + $this->assertEquals(0, $subtask['user_id']); + + // Make sure that comments are not related to the user anymore + $comment = $commentModel->getById(1); + $this->assertEquals(1, $comment['id']); + $this->assertEquals(0, $comment['user_id']); + + // Make sure that private projects are also removed + $user_id1 = $userModel->create(array('username' => 'toto1', 'password' => '123456', 'name' => 'Toto')); + $user_id2 = $userModel->create(array('username' => 'toto2', 'password' => '123456', 'name' => 'Toto')); + $this->assertNotFalse($user_id1); + $this->assertNotFalse($user_id2); + $this->assertEquals(2, $projectModel->create(array('name' => 'Private project #1', 'is_private' => 1), $user_id1, true)); + $this->assertEquals(3, $projectModel->create(array('name' => 'Private project #2', 'is_private' => 1), $user_id2, true)); + + $this->assertTrue($userModel->remove($user_id1)); + + $this->assertNotEmpty($projectModel->getById(1)); + $this->assertNotEmpty($projectModel->getById(3)); + + $this->assertEmpty($projectModel->getById(2)); + } + + public function testEnableDisablePublicAccess() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'toto', 'password' => '123456'))); + + $user = $userModel->getById(2); + $this->assertNotEmpty($user); + $this->assertEquals('toto', $user['username']); + $this->assertEmpty($user['token']); + + $this->assertTrue($userModel->enablePublicAccess(2)); + + $user = $userModel->getById(2); + $this->assertNotEmpty($user); + $this->assertEquals('toto', $user['username']); + $this->assertNotEmpty($user['token']); + + $this->assertTrue($userModel->disablePublicAccess(2)); + + $user = $userModel->getById(2); + $this->assertNotEmpty($user); + $this->assertEquals('toto', $user['username']); + $this->assertEmpty($user['token']); + } + + public function testEnableDisable() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'toto'))); + + $this->assertTrue($userModel->isActive(2)); + $user = $userModel->getById(2); + $this->assertEquals(1, $user['is_active']); + + $this->assertTrue($userModel->disable(2)); + $user = $userModel->getById(2); + $this->assertEquals(0, $user['is_active']); + $this->assertFalse($userModel->isActive(2)); + + $this->assertTrue($userModel->enable(2)); + $user = $userModel->getById(2); + $this->assertEquals(1, $user['is_active']); + $this->assertTrue($userModel->isActive(2)); + } +} diff --git a/tests/units/Model/UserTest.php b/tests/units/Model/UserTest.php deleted file mode 100644 index 0be6172e..00000000 --- a/tests/units/Model/UserTest.php +++ /dev/null @@ -1,400 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\UserModel; -use Kanboard\Model\SubtaskModel; -use Kanboard\Model\CommentModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\ProjectModel; -use Kanboard\Core\Security\Role; - -class UserTest extends Base -{ - public function testGetByEmail() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'password' => '123456', 'email' => 'user1@localhost'))); - $this->assertNotFalse($u->create(array('username' => 'user2', 'password' => '123456', 'email' => ''))); - - $this->assertNotEmpty($u->getByEmail('user1@localhost')); - $this->assertEmpty($u->getByEmail('')); - } - - public function testGetByExternalId() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'password' => '123456', 'gitlab_id' => '1234'))); - - $this->assertNotEmpty($u->getByExternalId('gitlab_id', '1234')); - $this->assertEmpty($u->getByExternalId('gitlab_id', '')); - - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user2', 'password' => '123456', 'github_id' => 'plop'))); - $this->assertNotFalse($u->create(array('username' => 'user3', 'password' => '123456', 'github_id' => ''))); - - $this->assertNotEmpty($u->getByExternalId('github_id', 'plop')); - $this->assertEmpty($u->getByExternalId('github_id', '')); - - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user4', 'password' => '123456', 'google_id' => '1234'))); - $this->assertNotFalse($u->create(array('username' => 'user5', 'password' => '123456', 'google_id' => ''))); - - $this->assertNotEmpty($u->getByExternalId('google_id', '1234')); - $this->assertEmpty($u->getByExternalId('google_id', '')); - } - - public function testGetByToken() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'token' => 'random'))); - $this->assertNotFalse($u->create(array('username' => 'user2', 'token' => ''))); - - $this->assertNotEmpty($u->getByToken('random')); - $this->assertEmpty($u->getByToken('')); - } - - public function testGetByUsername() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1'))); - - $this->assertNotEmpty($u->getByUsername('user1')); - $this->assertEmpty($u->getByUsername('user2')); - $this->assertEmpty($u->getByUsername('')); - } - - public function testExists() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1'))); - - $this->assertTrue($u->exists(1)); - $this->assertTrue($u->exists(2)); - $this->assertFalse($u->exists(3)); - } - - public function testCount() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1'))); - $this->assertEquals(2, $u->count()); - } - - public function testGetAll() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'you'))); - $this->assertEquals(3, $u->create(array('username' => 'me', 'name' => 'Me'))); - - $users = $u->getAll(); - $this->assertCount(3, $users); - $this->assertEquals('admin', $users[0]['username']); - $this->assertEquals('me', $users[1]['username']); - $this->assertEquals('you', $users[2]['username']); - } - - public function testGetActiveUsersList() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'you'))); - $this->assertEquals(3, $u->create(array('username' => 'me', 'name' => 'Me too'))); - $this->assertEquals(4, $u->create(array('username' => 'foobar', 'is_active' => 0))); - - $users = $u->getActiveUsersList(); - - $expected = array( - 1 => 'admin', - 3 => 'Me too', - 2 => 'you', - ); - - $this->assertEquals($expected, $users); - - $users = $u->getActiveUsersList(true); - - $expected = array( - UserModel::EVERYBODY_ID => 'Everybody', - 1 => 'admin', - 3 => 'Me too', - 2 => 'you', - ); - - $this->assertEquals($expected, $users); - } - - public function testGetFullname() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'user1'))); - $this->assertEquals(3, $u->create(array('username' => 'user2', 'name' => 'User #2'))); - - $user1 = $u->getById(2); - $user2 = $u->getById(3); - - $this->assertNotEmpty($user1); - $this->assertNotEmpty($user2); - - $this->assertEquals('user1', $u->getFullname($user1)); - $this->assertEquals('User #2', $u->getFullname($user2)); - } - - public function testIsAdmin() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'user1'))); - - $this->assertTrue($u->isAdmin(1)); - $this->assertFalse($u->isAdmin(2)); - } - - public function testPassword() - { - $password = 'test123'; - $hash = password_hash($password, PASSWORD_BCRYPT); - - $this->assertNotEmpty($hash); - $this->assertTrue(password_verify($password, $hash)); - } - - public function testPrepare() - { - $u = new UserModel($this->container); - - $input = array( - 'username' => 'user1', - 'password' => '1234', - 'confirmation' => '1234', - 'name' => 'me', - 'role' => Role::APP_ADMIN, - ); - - $u->prepare($input); - $this->assertArrayNotHasKey('confirmation', $input); - - $this->assertArrayHasKey('password', $input); - $this->assertNotEquals('1234', $input['password']); - $this->assertNotEmpty($input['password']); - - $input = array( - 'username' => 'user1', - 'password' => '1234', - 'current_password' => 'bla', - 'confirmation' => '1234', - 'name' => 'me', - 'is_ldap_user' => '1', - ); - - $u->prepare($input); - $this->assertArrayNotHasKey('confirmation', $input); - $this->assertArrayNotHasKey('current_password', $input); - - $this->assertArrayHasKey('password', $input); - $this->assertNotEquals('1234', $input['password']); - $this->assertNotEmpty($input['password']); - - $this->assertArrayHasKey('is_ldap_user', $input); - $this->assertEquals(1, $input['is_ldap_user']); - - $input = array( - 'id' => 2, - 'name' => 'me', - ); - - $u->prepare($input); - $this->assertEquals(array('id' => 2, 'name' => 'me'), $input); - - $input = array( - 'gitlab_id' => '1234', - ); - - $u->prepare($input); - $this->assertEquals(array('gitlab_id' => 1234), $input); - - $input = array( - 'gitlab_id' => '', - ); - - $u->prepare($input); - $this->assertEquals(array('gitlab_id' => null), $input); - - $input = array( - 'gitlab_id' => 'something', - ); - - $u->prepare($input); - $this->assertEquals(array('gitlab_id' => 0), $input); - - $input = array( - 'username' => 'something', - 'password' => '' - ); - - $u->prepare($input); - $this->assertEquals(array('username' => 'something'), $input); - } - - public function testCreate() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'user #1', 'password' => '123456', 'name' => 'User'))); - $this->assertEquals(3, $u->create(array('username' => 'user #2', 'is_ldap_user' => 1))); - $this->assertEquals(4, $u->create(array('username' => 'user #3', 'role' => Role::APP_MANAGER))); - $this->assertEquals(5, $u->create(array('username' => 'user #4', 'gitlab_id' => '', 'role' => Role::APP_ADMIN))); - $this->assertEquals(6, $u->create(array('username' => 'user #5', 'gitlab_id' => '1234'))); - $this->assertFalse($u->create(array('username' => 'user #1'))); - - $user = $u->getById(1); - $this->assertEquals('admin', $user['username']); - $this->assertEquals('', $user['name']); - $this->assertEquals(Role::APP_ADMIN, $user['role']); - $this->assertEquals(0, $user['is_ldap_user']); - - $user = $u->getById(2); - $this->assertEquals('user #1', $user['username']); - $this->assertEquals('User', $user['name']); - $this->assertEquals(Role::APP_USER, $user['role']); - $this->assertEquals(0, $user['is_ldap_user']); - - $user = $u->getById(3); - $this->assertEquals('user #2', $user['username']); - $this->assertEquals('', $user['name']); - $this->assertEquals(Role::APP_USER, $user['role']); - $this->assertEquals(1, $user['is_ldap_user']); - - $user = $u->getById(4); - $this->assertEquals('user #3', $user['username']); - $this->assertEquals(Role::APP_MANAGER, $user['role']); - - $user = $u->getById(5); - $this->assertEquals('user #4', $user['username']); - $this->assertEquals('', $user['gitlab_id']); - $this->assertEquals(Role::APP_ADMIN, $user['role']); - - $user = $u->getById(6); - $this->assertEquals('user #5', $user['username']); - $this->assertEquals('1234', $user['gitlab_id']); - $this->assertEquals(Role::APP_USER, $user['role']); - } - - public function testUpdate() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); - $this->assertEquals(3, $u->create(array('username' => 'plop', 'gitlab_id' => '123'))); - - $this->assertTrue($u->update(array('id' => 2, 'username' => 'biloute'))); - $this->assertTrue($u->update(array('id' => 3, 'gitlab_id' => ''))); - - $user = $u->getById(2); - $this->assertEquals('biloute', $user['username']); - $this->assertEquals('Toto', $user['name']); - $this->assertEquals(Role::APP_USER, $user['role']); - $this->assertEquals(0, $user['is_ldap_user']); - - $user = $u->getById(3); - $this->assertNotEmpty($user); - $this->assertEquals(null, $user['gitlab_id']); - } - - public function testRemove() - { - $u = new UserModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SubtaskModel($this->container); - $c = new CommentModel($this->container); - - $this->assertNotFalse($u->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'owner_id' => 2))); - $this->assertEquals(1, $s->create(array('title' => 'Subtask #1', 'user_id' => 2, 'task_id' => 1))); - $this->assertEquals(1, $c->create(array('comment' => 'foobar', 'user_id' => 2, 'task_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(1, $task['id']); - $this->assertEquals(2, $task['owner_id']); - - $this->assertTrue($u->remove(1)); - $this->assertTrue($u->remove(2)); - $this->assertFalse($u->remove(2)); - $this->assertFalse($u->remove(55)); - - // Make sure that assigned tasks are unassigned after removing the user - $task = $tf->getById(1); - $this->assertEquals(1, $task['id']); - $this->assertEquals(0, $task['owner_id']); - - // Make sure that assigned subtasks are unassigned after removing the user - $subtask = $s->getById(1); - $this->assertEquals(1, $subtask['id']); - $this->assertEquals(0, $subtask['user_id']); - - // Make sure that comments are not related to the user anymore - $comment = $c->getById(1); - $this->assertEquals(1, $comment['id']); - $this->assertEquals(0, $comment['user_id']); - - // Make sure that private projects are also removed - $user_id1 = $u->create(array('username' => 'toto1', 'password' => '123456', 'name' => 'Toto')); - $user_id2 = $u->create(array('username' => 'toto2', 'password' => '123456', 'name' => 'Toto')); - $this->assertNotFalse($user_id1); - $this->assertNotFalse($user_id2); - $this->assertEquals(2, $p->create(array('name' => 'Private project #1', 'is_private' => 1), $user_id1, true)); - $this->assertEquals(3, $p->create(array('name' => 'Private project #2', 'is_private' => 1), $user_id2, true)); - - $this->assertTrue($u->remove($user_id1)); - - $this->assertNotEmpty($p->getById(1)); - $this->assertNotEmpty($p->getById(3)); - - $this->assertEmpty($p->getById(2)); - } - - public function testEnableDisablePublicAccess() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'toto', 'password' => '123456'))); - - $user = $u->getById(2); - $this->assertNotEmpty($user); - $this->assertEquals('toto', $user['username']); - $this->assertEmpty($user['token']); - - $this->assertTrue($u->enablePublicAccess(2)); - - $user = $u->getById(2); - $this->assertNotEmpty($user); - $this->assertEquals('toto', $user['username']); - $this->assertNotEmpty($user['token']); - - $this->assertTrue($u->disablePublicAccess(2)); - - $user = $u->getById(2); - $this->assertNotEmpty($user); - $this->assertEquals('toto', $user['username']); - $this->assertEmpty($user['token']); - } - - public function testEnableDisable() - { - $userModel = new UserModel($this->container); - $this->assertEquals(2, $userModel->create(array('username' => 'toto'))); - - $this->assertTrue($userModel->isActive(2)); - $user = $userModel->getById(2); - $this->assertEquals(1, $user['is_active']); - - $this->assertTrue($userModel->disable(2)); - $user = $userModel->getById(2); - $this->assertEquals(0, $user['is_active']); - $this->assertFalse($userModel->isActive(2)); - - $this->assertTrue($userModel->enable(2)); - $user = $userModel->getById(2); - $this->assertEquals(1, $user['is_active']); - $this->assertTrue($userModel->isActive(2)); - } -} |