diff options
Diffstat (limited to 'tests/units/Model')
51 files changed, 4322 insertions, 2350 deletions
diff --git a/tests/units/Model/AclTest.php b/tests/units/Model/AclTest.php deleted file mode 100644 index 28687a5c..00000000 --- a/tests/units/Model/AclTest.php +++ /dev/null @@ -1,308 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Core\Session; -use Kanboard\Model\Acl; -use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; -use Kanboard\Model\User; - -class AclTest extends Base -{ - public function testMatchAcl() - { - $acl_rules = array( - 'controller1' => array('action1', 'action3'), - 'controller3' => '*', - 'controller5' => '-', - 'controller6' => array(), - 'controllera' => '*', - ); - - $acl = new Acl($this->container); - $this->assertTrue($acl->matchAcl($acl_rules, 'controller1', 'aCtiOn1')); - $this->assertTrue($acl->matchAcl($acl_rules, 'controller1', 'action1')); - $this->assertTrue($acl->matchAcl($acl_rules, 'controller1', 'action3')); - $this->assertFalse($acl->matchAcl($acl_rules, 'controller1', 'action2')); - $this->assertFalse($acl->matchAcl($acl_rules, 'controller2', 'action2')); - $this->assertFalse($acl->matchAcl($acl_rules, 'controller2', 'action3')); - $this->assertTrue($acl->matchAcl($acl_rules, 'controller3', 'anything')); - $this->assertFalse($acl->matchAcl($acl_rules, 'controller4', 'anything')); - $this->assertFalse($acl->matchAcl($acl_rules, 'controller5', 'anything')); - $this->assertFalse($acl->matchAcl($acl_rules, 'controller6', 'anything')); - $this->assertTrue($acl->matchAcl($acl_rules, 'ControllerA', 'anything')); - $this->assertTrue($acl->matchAcl($acl_rules, 'controllera', 'anything')); - } - - public function testPublicActions() - { - $acl = new Acl($this->container); - $this->assertTrue($acl->isPublicAction('task', 'readonly')); - $this->assertTrue($acl->isPublicAction('board', 'readonly')); - $this->assertFalse($acl->isPublicAction('board', 'show')); - $this->assertTrue($acl->isPublicAction('feed', 'project')); - $this->assertTrue($acl->isPublicAction('feed', 'user')); - $this->assertTrue($acl->isPublicAction('ical', 'project')); - $this->assertTrue($acl->isPublicAction('ical', 'user')); - $this->assertTrue($acl->isPublicAction('oauth', 'github')); - $this->assertTrue($acl->isPublicAction('oauth', 'google')); - $this->assertTrue($acl->isPublicAction('auth', 'login')); - $this->assertTrue($acl->isPublicAction('auth', 'check')); - $this->assertTrue($acl->isPublicAction('auth', 'captcha')); - } - - public function testAdminActions() - { - $acl = new Acl($this->container); - $this->assertFalse($acl->isAdminAction('board', 'show')); - $this->assertFalse($acl->isAdminAction('task', 'show')); - $this->assertTrue($acl->isAdminAction('config', 'api')); - $this->assertTrue($acl->isAdminAction('config', 'anything')); - $this->assertTrue($acl->isAdminAction('config', 'anything')); - $this->assertTrue($acl->isAdminAction('user', 'save')); - } - - public function testProjectAdminActions() - { - $acl = new Acl($this->container); - $this->assertFalse($acl->isProjectAdminAction('config', 'save')); - $this->assertFalse($acl->isProjectAdminAction('user', 'index')); - $this->assertTrue($acl->isProjectAdminAction('project', 'remove')); - } - - public function testProjectManagerActions() - { - $acl = new Acl($this->container); - $this->assertFalse($acl->isProjectManagerAction('board', 'readonly')); - $this->assertFalse($acl->isProjectManagerAction('project', 'remove')); - $this->assertFalse($acl->isProjectManagerAction('project', 'show')); - $this->assertTrue($acl->isProjectManagerAction('project', 'disable')); - $this->assertTrue($acl->isProjectManagerAction('category', 'index')); - $this->assertTrue($acl->isProjectManagerAction('project', 'users')); - $this->assertFalse($acl->isProjectManagerAction('app', 'index')); - } - - public function testPageAccessNoSession() - { - $acl = new Acl($this->container); - $session = new Session; - $session = array(); - - $this->assertFalse($acl->isAllowed('board', 'readonly')); - $this->assertFalse($acl->isAllowed('task', 'show')); - $this->assertFalse($acl->isAllowed('config', 'application')); - $this->assertFalse($acl->isAllowed('project', 'users')); - $this->assertFalse($acl->isAllowed('task', 'remove')); - $this->assertTrue($acl->isAllowed('app', 'index')); - } - - public function testPageAccessEmptySession() - { - $acl = new Acl($this->container); - $session = new Session; - $session['user'] = array(); - - $this->assertFalse($acl->isAllowed('board', 'readonly')); - $this->assertFalse($acl->isAllowed('task', 'show')); - $this->assertFalse($acl->isAllowed('config', 'application')); - $this->assertFalse($acl->isAllowed('project', 'users')); - $this->assertFalse($acl->isAllowed('task', 'remove')); - $this->assertTrue($acl->isAllowed('app', 'index')); - } - - public function testPageAccessAdminUser() - { - $acl = new Acl($this->container); - $session = new Session; - - $session['user'] = array( - 'is_admin' => true, - ); - - $this->assertTrue($acl->isAllowed('board', 'readonly')); - $this->assertTrue($acl->isAllowed('task', 'readonly')); - $this->assertTrue($acl->isAllowed('webhook', 'github')); - $this->assertTrue($acl->isAllowed('task', 'show')); - $this->assertTrue($acl->isAllowed('task', 'update')); - $this->assertTrue($acl->isAllowed('config', 'application')); - $this->assertTrue($acl->isAllowed('project', 'show')); - $this->assertTrue($acl->isAllowed('project', 'users')); - $this->assertTrue($acl->isAllowed('project', 'remove')); - $this->assertTrue($acl->isAllowed('category', 'edit')); - $this->assertTrue($acl->isAllowed('task', 'remove')); - $this->assertTrue($acl->isAllowed('app', 'index')); - } - - public function testPageAccessProjectAdmin() - { - $acl = new Acl($this->container); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - $session = new Session; - - // We create our user - $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create a project and set our user as project manager - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->isMember(1, 2)); - $this->assertFalse($pp->isManager(1, 2)); - - // We fake a session for him - $session['user'] = array( - 'id' => 2, - 'is_admin' => false, - 'is_project_admin' => true, - ); - - $this->assertTrue($acl->isAllowed('board', 'readonly', 1)); - $this->assertTrue($acl->isAllowed('task', 'readonly', 1)); - $this->assertTrue($acl->isAllowed('webhook', 'github', 1)); - $this->assertTrue($acl->isAllowed('task', 'show', 1)); - $this->assertFalse($acl->isAllowed('task', 'show', 2)); - $this->assertTrue($acl->isAllowed('task', 'update', 1)); - $this->assertTrue($acl->isAllowed('project', 'show', 1)); - $this->assertFalse($acl->isAllowed('config', 'application', 1)); - - $this->assertTrue($acl->isAllowed('project', 'users', 1)); - $this->assertFalse($acl->isAllowed('project', 'users', 2)); - - $this->assertTrue($acl->isAllowed('project', 'remove', 1)); - $this->assertFalse($acl->isAllowed('project', 'remove', 2)); - - $this->assertTrue($acl->isAllowed('category', 'edit', 1)); - $this->assertTrue($acl->isAllowed('task', 'remove', 1)); - $this->assertTrue($acl->isAllowed('app', 'index', 1)); - } - - public function testPageAccessProjectManager() - { - $acl = new Acl($this->container); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - $session = new Session; - - // We create our user - $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create a project and set our user as project manager - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'), 2, true)); - $this->assertTrue($pp->isMember(1, 2)); - $this->assertTrue($pp->isManager(1, 2)); - - // We fake a session for him - $session['user'] = array( - 'id' => 2, - 'is_admin' => false, - ); - - $this->assertTrue($acl->isAllowed('board', 'readonly', 1)); - $this->assertTrue($acl->isAllowed('task', 'readonly', 1)); - $this->assertTrue($acl->isAllowed('webhook', 'github', 1)); - $this->assertTrue($acl->isAllowed('task', 'show', 1)); - $this->assertFalse($acl->isAllowed('task', 'show', 2)); - $this->assertTrue($acl->isAllowed('task', 'update', 1)); - $this->assertTrue($acl->isAllowed('project', 'show', 1)); - $this->assertFalse($acl->isAllowed('config', 'application', 1)); - - $this->assertTrue($acl->isAllowed('project', 'users', 1)); - $this->assertFalse($acl->isAllowed('project', 'users', 2)); - - $this->assertFalse($acl->isAllowed('project', 'remove', 1)); - $this->assertFalse($acl->isAllowed('project', 'remove', 2)); - - $this->assertTrue($acl->isAllowed('category', 'edit', 1)); - $this->assertTrue($acl->isAllowed('task', 'remove', 1)); - $this->assertTrue($acl->isAllowed('app', 'index', 1)); - } - - public function testPageAccessMember() - { - $acl = new Acl($this->container); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - - // We create our user - $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create a project and set our user as member - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->isMember(1, 2)); - $this->assertFalse($pp->isManager(1, 2)); - - $session = new Session; - - $session['user'] = array( - 'id' => 2, - 'is_admin' => false, - ); - - $this->assertTrue($acl->isAllowed('board', 'readonly', 1)); - $this->assertTrue($acl->isAllowed('task', 'readonly', 1)); - $this->assertTrue($acl->isAllowed('webhook', 'github', 1)); - $this->assertFalse($acl->isAllowed('board', 'show', 2)); - $this->assertTrue($acl->isAllowed('board', 'show', 1)); - $this->assertFalse($acl->isAllowed('task', 'show', 2)); - $this->assertTrue($acl->isAllowed('task', 'show', 1)); - $this->assertTrue($acl->isAllowed('task', 'update', 1)); - $this->assertTrue($acl->isAllowed('project', 'show', 1)); - $this->assertFalse($acl->isAllowed('config', 'application', 1)); - $this->assertFalse($acl->isAllowed('project', 'users', 1)); - $this->assertTrue($acl->isAllowed('task', 'remove', 1)); - $this->assertFalse($acl->isAllowed('task', 'remove', 2)); - $this->assertTrue($acl->isAllowed('app', 'index', 1)); - } - - public function testPageAccessNotMember() - { - $acl = new Acl($this->container); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - - // We create our user - $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create a project and set our user as member - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); - $this->assertFalse($pp->isMember(1, 2)); - $this->assertFalse($pp->isManager(1, 2)); - - $session = new Session; - - $session['user'] = array( - 'id' => 2, - 'is_admin' => false, - ); - - $this->assertFalse($acl->isAllowed('board', 'show', 2)); - $this->assertFalse($acl->isAllowed('board', 'show', 1)); - $this->assertFalse($acl->isAllowed('task', 'show', 1)); - $this->assertFalse($acl->isAllowed('task', 'update', 1)); - $this->assertFalse($acl->isAllowed('project', 'show', 1)); - $this->assertFalse($acl->isAllowed('config', 'application', 1)); - $this->assertFalse($acl->isAllowed('project', 'users', 1)); - $this->assertFalse($acl->isAllowed('task', 'remove', 1)); - $this->assertTrue($acl->isAllowed('app', 'index', 1)); - } - - public function testExtend() - { - $acl = new Acl($this->container); - - $this->assertFalse($acl->isProjectManagerAction('plop', 'show')); - - $acl->extend('project_manager_acl', array('plop' => '*')); - - $this->assertTrue($acl->isProjectManagerAction('plop', 'show')); - $this->assertTrue($acl->isProjectManagerAction('swimlane', 'index')); - } -} diff --git a/tests/units/Model/ActionTest.php b/tests/units/Model/ActionTest.php index 30f6b22c..ed687846 100644 --- a/tests/units/Model/ActionTest.php +++ b/tests/units/Model/ActionTest.php @@ -4,387 +4,506 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Action; use Kanboard\Model\Project; -use Kanboard\Model\Board; use Kanboard\Model\Task; -use Kanboard\Model\TaskPosition; -use Kanboard\Model\TaskCreation; -use Kanboard\Model\TaskFinder; -use Kanboard\Model\Category; use Kanboard\Model\User; -use Kanboard\Model\ProjectPermission; -use Kanboard\Integration\GithubWebhook; -use Kanboard\Integration\BitbucketWebhook; +use Kanboard\Model\Column; +use Kanboard\Model\Category; +use Kanboard\Model\ProjectUserRole; +use Kanboard\Core\Security\Role; class ActionTest extends Base { - public function testGetActions() + public function testCreate() { - $a = new Action($this->container); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); - $actions = $a->getAvailableActions(); - $this->assertNotEmpty($actions); - $this->assertEquals('Add a comment log when moving the task between columns', current($actions)); - $this->assertEquals('TaskLogMoveAnotherColumn', key($actions)); + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); } - public function testExtendActions() + public function testRemove() { - $a = new Action($this->container); - $a->extendActions('MyClass', 'Description'); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); - $actions = $a->getAvailableActions(); - $this->assertNotEmpty($actions); - $this->assertContains('Description', $actions); - $this->assertArrayHasKey('MyClass', $actions); + $this->assertNotEmpty($actionModel->getById(1)); + $this->assertTrue($actionModel->remove(1)); + $this->assertEmpty($actionModel->getById(1)); } - public function testGetEvents() + public function testGetById() { - $a = new Action($this->container); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); - $events = $a->getAvailableEvents(); - $this->assertNotEmpty($events); - $this->assertEquals('Bitbucket commit received', current($events)); - $this->assertEquals(BitbucketWebhook::EVENT_COMMIT, key($events)); + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); + + $action = $actionModel->getById(1); + $this->assertNotEmpty($action); + $this->assertEquals(1, $action['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $action['action_name']); + $this->assertEquals(Task::EVENT_CREATE, $action['event_name']); + $this->assertEquals(array('column_id' => 1, 'color_id' => 'red'), $action['params']); } - public function testGetCompatibleEvents() + public function testGetAll() { - $a = new Action($this->container); - $events = $a->getCompatibleEvents('TaskAssignSpecificUser'); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); - $this->assertNotEmpty($events); - $this->assertCount(2, $events); - $this->assertArrayHasKey(Task::EVENT_CREATE_UPDATE, $events); - $this->assertArrayHasKey(Task::EVENT_MOVE_COLUMN, $events); + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); + + $this->assertEquals(2, $actionModel->create(array( + 'project_id' => 2, + 'event_name' => Task::EVENT_MOVE_COLUMN, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 6, 'color_id' => 'blue'), + ))); + + $actions = $actionModel->getAll(); + $this->assertCount(2, $actions); + + $this->assertEquals(1, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CREATE, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 1, 'color_id' => 'red'), $actions[0]['params']); + + $this->assertEquals(2, $actions[1]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[1]['action_name']); + $this->assertEquals(Task::EVENT_MOVE_COLUMN, $actions[1]['event_name']); + $this->assertEquals(array('column_id' => 6, 'color_id' => 'blue'), $actions[1]['params']); } - public function testResolveDuplicatedParameters() + public function testGetAllByProject() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $a = new Action($this->container); - $c = new Category($this->container); - $u = new User($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - $this->assertEquals(2, $p->create(array('name' => 'P2'))); - - $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1))); - - $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 2))); - $this->assertEquals(3, $c->create(array('name' => 'C1', 'project_id' => 2))); - - $this->assertEquals(2, $u->create(array('username' => 'unittest1'))); - $this->assertEquals(3, $u->create(array('username' => 'unittest2'))); - - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->addMember(1, 3)); - $this->assertTrue($pp->addMember(2, 3)); - - // anything - $this->assertEquals('blah', $a->resolveParameters(array('name' => 'foobar', 'value' => 'blah'), 2)); - - // project_id - $this->assertEquals(2, $a->resolveParameters(array('name' => 'project_id', 'value' => 'blah'), 2)); - - // category_id - $this->assertEquals(3, $a->resolveParameters(array('name' => 'category_id', 'value' => 1), 2)); - $this->assertFalse($a->resolveParameters(array('name' => 'category_id', 'value' => 0), 2)); - $this->assertFalse($a->resolveParameters(array('name' => 'category_id', 'value' => 5), 2)); - - // column_id - $this->assertFalse($a->resolveParameters(array('name' => 'column_id', 'value' => 10), 2)); - $this->assertFalse($a->resolveParameters(array('name' => 'column_id', 'value' => 0), 2)); - $this->assertEquals(5, $a->resolveParameters(array('name' => 'column_id', 'value' => 1), 2)); - $this->assertEquals(6, $a->resolveParameters(array('name' => 'dest_column_id', 'value' => 2), 2)); - $this->assertEquals(7, $a->resolveParameters(array('name' => 'dst_column_id', 'value' => 3), 2)); - $this->assertEquals(8, $a->resolveParameters(array('name' => 'src_column_id', 'value' => 4), 2)); - - // user_id - $this->assertFalse($a->resolveParameters(array('name' => 'user_id', 'value' => 10), 2)); - $this->assertFalse($a->resolveParameters(array('name' => 'user_id', 'value' => 0), 2)); - $this->assertFalse($a->resolveParameters(array('name' => 'user_id', 'value' => 2), 2)); - $this->assertFalse($a->resolveParameters(array('name' => 'owner_id', 'value' => 2), 2)); - $this->assertEquals(3, $a->resolveParameters(array('name' => 'user_id', 'value' => 3), 2)); - $this->assertEquals(3, $a->resolveParameters(array('name' => 'owner_id', 'value' => 3), 2)); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); + + $this->assertEquals(2, $actionModel->create(array( + 'project_id' => 2, + 'event_name' => Task::EVENT_MOVE_COLUMN, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 6, 'color_id' => 'blue'), + ))); + + $actions = $actionModel->getAllByProject(1); + $this->assertCount(1, $actions); + + $this->assertEquals(1, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CREATE, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 1, 'color_id' => 'red'), $actions[0]['params']); + + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(1, $actions); + + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_MOVE_COLUMN, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 6, 'color_id' => 'blue'), $actions[0]['params']); } - public function testDuplicateSuccess() + public function testGetAllByUser() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $a = new Action($this->container); - $u = new User($this->container); + $projectModel = new Project($this->container); + $projectUserRoleModel = new ProjectUserRole($this->container); + $userModel = new User($this->container); + $actionModel = new Action($this->container); - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - $this->assertEquals(2, $p->create(array('name' => 'P2'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + $this->assertEquals(3, $projectModel->create(array('name' => 'test4', 'is_active' => 0))); - $this->assertEquals(2, $u->create(array('username' => 'unittest1'))); - $this->assertEquals(3, $u->create(array('username' => 'unittest2'))); + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2'))); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->addMember(1, 3)); - $this->assertTrue($pp->addMember(2, 3)); + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_VIEWER)); + $this->assertTrue($projectUserRoleModel->addUser(2, 3, Role::PROJECT_MANAGER)); + $this->assertTrue($projectUserRoleModel->addUser(3, 3, Role::PROJECT_MANAGER)); - $this->assertEquals(1, $a->create(array( + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, - 'event_name' => Task::EVENT_CREATE_UPDATE, - 'action_name' => 'TaskAssignSpecificUser', - 'params' => array( - 'column_id' => 1, - 'user_id' => 3, - ) + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), ))); - $action = $a->getById(1); - $this->assertNotEmpty($action); - $this->assertNotEmpty($action['params']); - $this->assertEquals(1, $action['project_id']); + $this->assertEquals(2, $actionModel->create(array( + 'project_id' => 2, + 'event_name' => Task::EVENT_MOVE_COLUMN, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 6, 'color_id' => 'blue'), + ))); - $this->assertTrue($a->duplicate(1, 2)); + $this->assertEquals(3, $actionModel->create(array( + 'project_id' => 3, + 'event_name' => Task::EVENT_MOVE_COLUMN, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 10, 'color_id' => 'green'), + ))); - $action = $a->getById(2); - $this->assertNotEmpty($action); - $this->assertNotEmpty($action['params']); - $this->assertEquals(2, $action['project_id']); - $this->assertEquals(Task::EVENT_CREATE_UPDATE, $action['event_name']); - $this->assertEquals('TaskAssignSpecificUser', $action['action_name']); - $this->assertEquals('column_id', $action['params'][0]['name']); - $this->assertEquals(5, $action['params'][0]['value']); - $this->assertEquals('user_id', $action['params'][1]['name']); - $this->assertEquals(3, $action['params'][1]['value']); + $actions = $actionModel->getAllByUser(1); + $this->assertCount(0, $actions); + + $actions = $actionModel->getAllByUser(2); + $this->assertCount(1, $actions); + + $this->assertEquals(1, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CREATE, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 1, 'color_id' => 'red'), $actions[0]['params']); + + $actions = $actionModel->getAllByUser(3); + $this->assertCount(1, $actions); + + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_MOVE_COLUMN, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 6, 'color_id' => 'blue'), $actions[0]['params']); } - public function testDuplicateUnableToResolveParams() + public function testDuplicateWithColumnAndColorParameter() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $a = new Action($this->container); - $u = new User($this->container); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - $this->assertEquals(2, $p->create(array('name' => 'P2'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); - $this->assertEquals(2, $u->create(array('username' => 'unittest1'))); + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); - $this->assertTrue($pp->addMember(1, 2)); + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(1, $actions); + + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CREATE, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 5, 'color_id' => 'red'), $actions[0]['params']); + } + + public function testDuplicateWithColumnsParameter() + { + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); - $this->assertEquals(1, $a->create(array( + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, - 'event_name' => Task::EVENT_CREATE_UPDATE, - 'action_name' => 'TaskAssignSpecificUser', - 'params' => array( - 'column_id' => 1, - 'user_id' => 2, - ) + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('src_column_id' => 1, 'dst_column_id' => 2, 'dest_column_id' => 3), ))); - $action = $a->getById(1); - $this->assertNotEmpty($action); - $this->assertNotEmpty($action['params']); - $this->assertEquals(1, $action['project_id']); - $this->assertEquals('user_id', $action['params'][1]['name']); - $this->assertEquals(2, $action['params'][1]['value']); + $this->assertTrue($actionModel->duplicate(1, 2)); - $this->assertTrue($a->duplicate(1, 2)); + $actions = $actionModel->getAllByProject(2); + $this->assertCount(1, $actions); - $action = $a->getById(2); - $this->assertEmpty($action); + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CREATE, $actions[0]['event_name']); + $this->assertEquals(array('src_column_id' => 5, 'dst_column_id' => 6, 'dest_column_id' => 7), $actions[0]['params']); } - public function testDuplicateMixedResults() + public function testDuplicateWithColumnParameterNotfound() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $a = new Action($this->container); - $u = new User($this->container); - $c = new Category($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - $this->assertEquals(2, $p->create(array('name' => 'P2'))); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + $columnModel = new Column($this->container); - $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1))); - $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 2))); - $this->assertEquals(3, $c->create(array('name' => 'C1', 'project_id' => 2))); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); - $this->assertEquals(2, $u->create(array('username' => 'unittest1'))); + $this->assertTrue($columnModel->update(2, 'My unique column')); - $this->assertTrue($pp->addMember(1, 2)); + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); - $this->assertEquals(1, $a->create(array( + $this->assertEquals(2, $actionModel->create(array( 'project_id' => 1, - 'event_name' => Task::EVENT_CREATE_UPDATE, - 'action_name' => 'TaskAssignSpecificUser', - 'params' => array( - 'column_id' => 1, - 'user_id' => 2, - ) + 'event_name' => Task::EVENT_MOVE_COLUMN, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 2, 'color_id' => 'green'), ))); - $action = $a->getById(1); - $this->assertNotEmpty($action); - $this->assertNotEmpty($action['params']); + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(1, $actions); + + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorColumn', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CREATE, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 5, 'color_id' => 'red'), $actions[0]['params']); + } + + public function testDuplicateWithProjectParameter() + { + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); - $this->assertEquals(2, $a->create(array( + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + $this->assertEquals(3, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, - 'event_name' => Task::EVENT_CREATE_UPDATE, - 'action_name' => 'TaskAssignCategoryColor', - 'params' => array( - 'color_id' => 'blue', - 'category_id' => 1, - ) + 'event_name' => Task::EVENT_CLOSE, + 'action_name' => '\Kanboard\Action\TaskDuplicateAnotherProject', + 'params' => array('column_id' => 1, 'project_id' => 3), ))); - $action = $a->getById(2); - $this->assertNotEmpty($action); - $this->assertNotEmpty($action['params']); - $this->assertEquals('category_id', $action['params'][1]['name']); - $this->assertEquals(1, $action['params'][1]['value']); + $this->assertTrue($actionModel->duplicate(1, 2)); - $actions = $a->getAllByProject(1); - $this->assertNotEmpty($actions); - $this->assertCount(2, $actions); + $actions = $actionModel->getAllByProject(2); + $this->assertCount(1, $actions); - $this->assertTrue($a->duplicate(1, 2)); + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskDuplicateAnotherProject', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CLOSE, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 5, 'project_id' => 3), $actions[0]['params']); + } - $actions = $a->getAllByProject(2); - $this->assertNotEmpty($actions); - $this->assertCount(1, $actions); + public function testDuplicateWithProjectParameterIdenticalToDestination() + { + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); - $actions = $a->getAll(); - $this->assertNotEmpty($actions); - $this->assertCount(3, $actions); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); - $action = $a->getById($actions[2]['id']); - $this->assertNotEmpty($action); - $this->assertNotEmpty($action['params']); - $this->assertEquals('color_id', $action['params'][0]['name']); - $this->assertEquals('blue', $action['params'][0]['value']); - $this->assertEquals('category_id', $action['params'][1]['name']); - $this->assertEquals(3, $action['params'][1]['value']); + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_CLOSE, + 'action_name' => '\Kanboard\Action\TaskDuplicateAnotherProject', + 'params' => array('column_id' => 1, 'project_id' => 2), + ))); + + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(0, $actions); } - public function testSingleAction() + public function testDuplicateWithUserParameter() { - $tp = new TaskPosition($this->container); - $tc = new TaskCreation($this->container); - $tf = new TaskFinder($this->container); - $board = new Board($this->container); - $project = new Project($this->container); - $action = new Action($this->container); - - // We create a project - $this->assertEquals(1, $project->create(array('name' => 'unit_test'))); - - // We create a new action - $this->assertEquals(1, $action->create(array( + $projectUserRoleModel = new ProjectUserRole($this->container); + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($projectUserRoleModel->addUser(2, 2, Role::PROJECT_MEMBER)); + + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, 'event_name' => Task::EVENT_MOVE_COLUMN, - 'action_name' => 'TaskClose', - 'params' => array( - 'column_id' => 4, - ) + 'action_name' => '\Kanboard\Action\TaskAssignSpecificUser', + 'params' => array('column_id' => 1, 'user_id' => 2), ))); - // We create a task - $this->assertEquals(1, $tc->create(array( - 'title' => 'unit_test', + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(1, $actions); + + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignSpecificUser', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_MOVE_COLUMN, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 5, 'user_id' => 2), $actions[0]['params']); + } + + public function testDuplicateWithUserParameterButNotAssignable() + { + $projectUserRoleModel = new ProjectUserRole($this->container); + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($projectUserRoleModel->addUser(2, 2, Role::PROJECT_VIEWER)); + + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, - 'owner_id' => 1, - 'color_id' => 'red', - 'column_id' => 1, + 'event_name' => Task::EVENT_MOVE_COLUMN, + 'action_name' => '\Kanboard\Action\TaskAssignSpecificUser', + 'params' => array('column_id' => 1, 'user_id' => 2), ))); - // We attach events - $action->attachEvents(); + $this->assertTrue($actionModel->duplicate(1, 2)); - // Our task should be open - $t1 = $tf->getById(1); - $this->assertEquals(1, $t1['is_active']); - $this->assertEquals(1, $t1['column_id']); + $actions = $actionModel->getAllByProject(2); + $this->assertCount(0, $actions); + } + + public function testDuplicateWithUserParameterButNotAvailable() + { + $projectUserRoleModel = new ProjectUserRole($this->container); + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); - // We move our task - $tp->movePosition(1, 1, 4, 1); + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); - // Our task should be closed - $t1 = $tf->getById(1); - $this->assertEquals(4, $t1['column_id']); - $this->assertEquals(0, $t1['is_active']); + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => Task::EVENT_MOVE_COLUMN, + 'action_name' => '\Kanboard\Action\TaskAssignSpecificUser', + 'params' => array('column_id' => 1, 'owner_id' => 2), + ))); + + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(0, $actions); } - public function testMultipleActions() + public function testDuplicateWithCategoryParameter() { - $tp = new TaskPosition($this->container); - $tc = new TaskCreation($this->container); - $tf = new TaskFinder($this->container); - $b = new Board($this->container); - $p = new Project($this->container); - $a = new Action($this->container); - $g = new GithubWebhook($this->container); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'unit_test'))); - - // We create a new action - $this->assertEquals(1, $a->create(array( + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + $categoryModel = new Category($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(1, $categoryModel->create(array('name' => 'c1', 'project_id' => 1))); + $this->assertEquals(2, $categoryModel->create(array('name' => 'c1', 'project_id' => 2))); + + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, - 'event_name' => GithubWebhook::EVENT_ISSUE_OPENED, - 'action_name' => 'TaskCreation', - 'params' => array() + 'event_name' => Task::EVENT_CREATE_UPDATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorCategory', + 'params' => array('column_id' => 1, 'category_id' => 1), ))); - $this->assertEquals(2, $a->create(array( + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(1, $actions); + + $this->assertEquals(2, $actions[0]['project_id']); + $this->assertEquals('\Kanboard\Action\TaskAssignColorCategory', $actions[0]['action_name']); + $this->assertEquals(Task::EVENT_CREATE_UPDATE, $actions[0]['event_name']); + $this->assertEquals(array('column_id' => 5, 'category_id' => 2), $actions[0]['params']); + } + + public function testDuplicateWithCategoryParameterButDifferentName() + { + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + $categoryModel = new Category($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(1, $categoryModel->create(array('name' => 'c1', 'project_id' => 1))); + $this->assertEquals(2, $categoryModel->create(array('name' => 'c2', 'project_id' => 2))); + + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, - 'event_name' => GithubWebhook::EVENT_ISSUE_LABEL_CHANGE, - 'action_name' => 'TaskAssignCategoryLabel', - 'params' => array( - 'label' => 'bug', - 'category_id' => 1, - ) + 'event_name' => Task::EVENT_CREATE_UPDATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorCategory', + 'params' => array('column_id' => 1, 'category_id' => 1), ))); - $this->assertEquals(3, $a->create(array( + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(0, $actions); + } + + public function testDuplicateWithCategoryParameterButNotFound() + { + $projectModel = new Project($this->container); + $actionModel = new Action($this->container); + $categoryModel = new Category($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertEquals(1, $categoryModel->create(array('name' => 'c1', 'project_id' => 1))); + + $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, 'event_name' => Task::EVENT_CREATE_UPDATE, - 'action_name' => 'TaskAssignColorCategory', - 'params' => array( - 'color_id' => 'red', - 'category_id' => 1, - ) + 'action_name' => '\Kanboard\Action\TaskAssignColorCategory', + 'params' => array('column_id' => 1, 'category_id' => 1), ))); - // We attach events - $a->attachEvents(); - $g->setProjectId(1); - - // We create a Github issue - $issue = array( - 'number' => 123, - 'title' => 'Bugs everywhere', - 'body' => 'There is a bug!', - 'html_url' => 'http://localhost/', - ); - - $this->assertTrue($g->handleIssueOpened($issue)); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['is_active']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals('yellow', $task['color_id']); - - // We assign a label to our issue - $label = array( - 'name' => 'bug', - ); - - $this->assertTrue($g->handleIssueLabeled($issue, $label)); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['is_active']); - $this->assertEquals(1, $task['category_id']); - $this->assertEquals('red', $task['color_id']); + $this->assertTrue($actionModel->duplicate(1, 2)); + + $actions = $actionModel->getAllByProject(2); + $this->assertCount(0, $actions); } } diff --git a/tests/units/Model/AuthenticationTest.php b/tests/units/Model/AuthenticationTest.php deleted file mode 100644 index 6b48affe..00000000 --- a/tests/units/Model/AuthenticationTest.php +++ /dev/null @@ -1,39 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\User; -use Kanboard\Model\Authentication; - -class AuthenticationTest extends Base -{ - public function testHasCaptcha() - { - $u = new User($this->container); - $a = new Authentication($this->container); - - $this->assertFalse($a->hasCaptcha('not_found')); - $this->assertFalse($a->hasCaptcha('admin')); - - $this->assertTrue($u->incrementFailedLogin('admin')); - $this->assertTrue($u->incrementFailedLogin('admin')); - $this->assertTrue($u->incrementFailedLogin('admin')); - - $this->assertFalse($a->hasCaptcha('not_found')); - $this->assertTrue($a->hasCaptcha('admin')); - } - - public function testHandleFailedLogin() - { - $u = new User($this->container); - $a = new Authentication($this->container); - - $this->assertFalse($u->isLocked('admin')); - - for ($i = 0; $i <= 6; $i++) { - $a->handleFailedLogin('admin'); - } - - $this->assertTrue($u->isLocked('admin')); - } -} diff --git a/tests/units/Model/BoardTest.php b/tests/units/Model/BoardTest.php index 125b9962..bb0778ce 100644 --- a/tests/units/Model/BoardTest.php +++ b/tests/units/Model/BoardTest.php @@ -4,6 +4,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Project; use Kanboard\Model\Board; +use Kanboard\Model\Column; use Kanboard\Model\Config; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; @@ -15,12 +16,13 @@ class BoardTest extends Base { $p = new Project($this->container); $b = new Board($this->container); + $columnModel = new Column($this->container); $c = new Config($this->container); // Default columns $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $columns = $b->getColumnsList(1); + $columns = $columnModel->getList(1); $this->assertTrue(is_array($columns)); $this->assertEquals(4, count($columns)); @@ -33,10 +35,11 @@ class BoardTest extends Base $input = ' column #1 , column #2, '; $this->assertTrue($c->save(array('board_columns' => $input))); + $this->container['memoryCache']->flush(); $this->assertEquals($input, $c->get('board_columns')); $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); - $columns = $b->getColumnsList(2); + $columns = $columnModel->getList(2); $this->assertTrue(is_array($columns)); $this->assertEquals(2, count($columns)); @@ -160,225 +163,4 @@ class BoardTest extends Base $this->assertEquals(1, $board[1]['columns'][3]['tasks'][0]['position']); $this->assertEquals(1, $board[1]['columns'][3]['tasks'][0]['swimlane_id']); } - - public function testGetColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - $column = $b->getColumn(3); - $this->assertNotEmpty($column); - $this->assertEquals('Work in progress', $column['title']); - - $column = $b->getColumn(33); - $this->assertEmpty($column); - } - - public function testRemoveColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertTrue($b->removeColumn(3)); - $this->assertFalse($b->removeColumn(322)); - - $columns = $b->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals(3, count($columns)); - } - - public function testUpdateColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - $this->assertTrue($b->updateColumn(3, 'blah', 5)); - $this->assertTrue($b->updateColumn(2, 'boo')); - - $column = $b->getColumn(3); - $this->assertNotEmpty($column); - $this->assertEquals('blah', $column['title']); - $this->assertEquals(5, $column['task_limit']); - - $column = $b->getColumn(2); - $this->assertNotEmpty($column); - $this->assertEquals('boo', $column['title']); - $this->assertEquals(0, $column['task_limit']); - } - - public function testAddColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertNotFalse($b->addColumn(1, 'another column')); - $this->assertNotFalse($b->addColumn(1, 'one more', 3, 'one more description')); - - $columns = $b->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals(6, count($columns)); - - $this->assertEquals('another column', $columns[4]['title']); - $this->assertEquals(0, $columns[4]['task_limit']); - $this->assertEquals(5, $columns[4]['position']); - - $this->assertEquals('one more', $columns[5]['title']); - $this->assertEquals(3, $columns[5]['task_limit']); - $this->assertEquals(6, $columns[5]['position']); - $this->assertEquals('one more description', $columns[5]['description']); - } - - public function testMoveColumns() - { - $p = new Project($this->container); - $b = new Board($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); - - // We get the columns of the project 2 - $columns = $b->getColumns(2); - $columns_id = array_keys($b->getColumnsList(2)); - $this->assertNotEmpty($columns); - - // Initial order: 5, 6, 7, 8 - - // Move the column 1 down - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[0], $columns[0]['id']); - - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals($columns_id[1], $columns[1]['id']); - - $this->assertTrue($b->moveDown(2, $columns[0]['id'])); - $columns = $b->getColumns(2); // Sorted by position - - // New order: 6, 5, 7, 8 - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[1], $columns[0]['id']); - - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals($columns_id[0], $columns[1]['id']); - - // Move the column 3 up - $this->assertTrue($b->moveUp(2, $columns[2]['id'])); - $columns = $b->getColumns(2); - - // New order: 6, 7, 5, 8 - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[1], $columns[0]['id']); - - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals($columns_id[2], $columns[1]['id']); - - $this->assertEquals(3, $columns[2]['position']); - $this->assertEquals($columns_id[0], $columns[2]['id']); - - // Move column 1 up (must do nothing because it's the first column) - $this->assertFalse($b->moveUp(2, $columns[0]['id'])); - $columns = $b->getColumns(2); - - // Order: 6, 7, 5, 8 - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[1], $columns[0]['id']); - - // Move column 4 down (must do nothing because it's the last column) - $this->assertFalse($b->moveDown(2, $columns[3]['id'])); - $columns = $b->getColumns(2); - - // Order: 6, 7, 5, 8 - - $this->assertEquals(4, $columns[3]['position']); - $this->assertEquals($columns_id[3], $columns[3]['id']); - } - - public function testMoveUpAndRemoveColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - // We remove the second column - $this->assertTrue($b->removeColumn(2)); - - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(3, $columns[1]['position']); - $this->assertEquals(4, $columns[2]['position']); - - $this->assertEquals(1, $columns[0]['id']); - $this->assertEquals(3, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - - // We move up the second column - $this->assertTrue($b->moveUp(1, $columns[1]['id'])); - - // Check the new positions - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals(3, $columns[2]['position']); - - $this->assertEquals(3, $columns[0]['id']); - $this->assertEquals(1, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - } - - public function testMoveDownAndRemoveColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - // We remove the second column - $this->assertTrue($b->removeColumn(2)); - - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(3, $columns[1]['position']); - $this->assertEquals(4, $columns[2]['position']); - - $this->assertEquals(1, $columns[0]['id']); - $this->assertEquals(3, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - - // We move up the second column - $this->assertTrue($b->moveDown(1, $columns[0]['id'])); - - // Check the new positions - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals(3, $columns[2]['position']); - - $this->assertEquals(3, $columns[0]['id']); - $this->assertEquals(1, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - } } diff --git a/tests/units/Model/ColumnTest.php b/tests/units/Model/ColumnTest.php new file mode 100644 index 00000000..e40f89c6 --- /dev/null +++ b/tests/units/Model/ColumnTest.php @@ -0,0 +1,236 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Project; +use Kanboard\Model\Column; + +class ColumnTest extends Base +{ + public function testGetColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $column = $columnModel->getById(3); + $this->assertNotEmpty($column); + $this->assertEquals('Work in progress', $column['title']); + + $column = $columnModel->getById(33); + $this->assertEmpty($column); + } + + public function testGetFirstColumnId() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $columnModel->getFirstColumnId(1)); + } + + public function testGetLastColumnId() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(4, $columnModel->getLastColumnId(1)); + } + + public function testGetLastColumnPosition() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(4, $columnModel->getLastColumnPosition(1)); + } + + public function testGetColumnIdByTitle() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(2, $columnModel->getColumnIdByTitle(1, 'Ready')); + } + + public function testGetTitleByColumnId() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals('Work in progress', $columnModel->getColumnTitleById(3)); + } + + public function testGetAll() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $columns = $columnModel->getAll(1); + $this->assertCount(4, $columns); + + $this->assertEquals(1, $columns[0]['id']); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals('Backlog', $columns[0]['title']); + + $this->assertEquals(2, $columns[1]['id']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals('Ready', $columns[1]['title']); + + $this->assertEquals(3, $columns[2]['id']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals('Work in progress', $columns[2]['title']); + + $this->assertEquals(4, $columns[3]['id']); + $this->assertEquals(4, $columns[3]['position']); + $this->assertEquals('Done', $columns[3]['title']); + } + + public function testGetList() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $columns = $columnModel->getList(1); + $this->assertCount(4, $columns); + $this->assertEquals('Backlog', $columns[1]); + $this->assertEquals('Ready', $columns[2]); + $this->assertEquals('Work in progress', $columns[3]); + $this->assertEquals('Done', $columns[4]); + + $columns = $columnModel->getList(1, true); + $this->assertCount(5, $columns); + $this->assertEquals('All columns', $columns[-1]); + $this->assertEquals('Backlog', $columns[1]); + $this->assertEquals('Ready', $columns[2]); + $this->assertEquals('Work in progress', $columns[3]); + $this->assertEquals('Done', $columns[4]); + } + + public function testAddColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertNotFalse($columnModel->create(1, 'another column')); + $this->assertNotFalse($columnModel->create(1, 'one more', 3, 'one more description')); + + $columns = $columnModel->getAll(1); + $this->assertTrue(is_array($columns)); + $this->assertEquals(6, count($columns)); + + $this->assertEquals('another column', $columns[4]['title']); + $this->assertEquals(0, $columns[4]['task_limit']); + $this->assertEquals(5, $columns[4]['position']); + + $this->assertEquals('one more', $columns[5]['title']); + $this->assertEquals(3, $columns[5]['task_limit']); + $this->assertEquals(6, $columns[5]['position']); + $this->assertEquals('one more description', $columns[5]['description']); + } + + public function testUpdateColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->assertTrue($columnModel->update(3, 'blah', 5)); + $this->assertTrue($columnModel->update(2, 'boo')); + + $column = $columnModel->getById(3); + $this->assertNotEmpty($column); + $this->assertEquals('blah', $column['title']); + $this->assertEquals(5, $column['task_limit']); + + $column = $columnModel->getById(2); + $this->assertNotEmpty($column); + $this->assertEquals('boo', $column['title']); + $this->assertEquals(0, $column['task_limit']); + } + + public function testRemoveColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($columnModel->remove(3)); + $this->assertFalse($columnModel->remove(322)); + + $columns = $columnModel->getAll(1); + $this->assertTrue(is_array($columns)); + $this->assertEquals(3, count($columns)); + } + + public function testChangePosition() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + $columns = $columnModel->getAll(1); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals(1, $columns[0]['id']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals(2, $columns[1]['id']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals(3, $columns[2]['id']); + + $this->assertTrue($columnModel->changePosition(1, 3, 2)); + + $columns = $columnModel->getAll(1); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals(1, $columns[0]['id']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals(3, $columns[1]['id']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals(2, $columns[2]['id']); + + $this->assertTrue($columnModel->changePosition(1, 2, 1)); + + $columns = $columnModel->getAll(1); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals(2, $columns[0]['id']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals(1, $columns[1]['id']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals(3, $columns[2]['id']); + + $this->assertTrue($columnModel->changePosition(1, 2, 2)); + + $columns = $columnModel->getAll(1); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals(1, $columns[0]['id']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals(2, $columns[1]['id']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals(3, $columns[2]['id']); + + $this->assertTrue($columnModel->changePosition(1, 4, 1)); + + $columns = $columnModel->getAll(1); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals(4, $columns[0]['id']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals(1, $columns[1]['id']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals(2, $columns[2]['id']); + + $this->assertFalse($columnModel->changePosition(1, 2, 0)); + $this->assertFalse($columnModel->changePosition(1, 2, 5)); + } +} diff --git a/tests/units/Model/CommentTest.php b/tests/units/Model/CommentTest.php index 07c39fe3..ec4e7a56 100644 --- a/tests/units/Model/CommentTest.php +++ b/tests/units/Model/CommentTest.php @@ -90,53 +90,4 @@ class CommentTest extends Base $this->assertFalse($c->remove(1)); $this->assertFalse($c->remove(1111)); } - - public function testValidateCreation() - { - $c = new Comment($this->container); - - $result = $c->validateCreation(array('user_id' => 1, 'task_id' => 1, 'comment' => 'bla')); - $this->assertTrue($result[0]); - - $result = $c->validateCreation(array('user_id' => 1, 'task_id' => 1, 'comment' => '')); - $this->assertFalse($result[0]); - - $result = $c->validateCreation(array('user_id' => 1, 'task_id' => 'a', 'comment' => 'bla')); - $this->assertFalse($result[0]); - - $result = $c->validateCreation(array('user_id' => 'b', 'task_id' => 1, 'comment' => 'bla')); - $this->assertFalse($result[0]); - - $result = $c->validateCreation(array('user_id' => 1, 'comment' => 'bla')); - $this->assertFalse($result[0]); - - $result = $c->validateCreation(array('task_id' => 1, 'comment' => 'bla')); - $this->assertTrue($result[0]); - - $result = $c->validateCreation(array('comment' => 'bla')); - $this->assertFalse($result[0]); - - $result = $c->validateCreation(array()); - $this->assertFalse($result[0]); - } - - public function testValidateModification() - { - $c = new Comment($this->container); - - $result = $c->validateModification(array('id' => 1, 'comment' => 'bla')); - $this->assertTrue($result[0]); - - $result = $c->validateModification(array('id' => 1, 'comment' => '')); - $this->assertFalse($result[0]); - - $result = $c->validateModification(array('comment' => 'bla')); - $this->assertFalse($result[0]); - - $result = $c->validateModification(array('id' => 'b', 'comment' => 'bla')); - $this->assertFalse($result[0]); - - $result = $c->validateModification(array()); - $this->assertFalse($result[0]); - } } diff --git a/tests/units/Model/ConfigTest.php b/tests/units/Model/ConfigTest.php index 17617ceb..447c9238 100644 --- a/tests/units/Model/ConfigTest.php +++ b/tests/units/Model/ConfigTest.php @@ -3,10 +3,78 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Config; -use Kanboard\Core\Session; +use Kanboard\Core\Session\SessionManager; class ConfigTest extends Base { + public function testGetTimezones() + { + $configModel = new Config($this->container); + $this->assertNotEmpty($configModel->getTimezones()); + $this->assertArrayHasKey('Europe/Paris', $configModel->getTimezones()); + $this->assertContains('Europe/Paris', $configModel->getTimezones()); + $this->assertArrayNotHasKey('', $configModel->getTimezones()); + + $this->assertArrayHasKey('', $configModel->getTimezones(true)); + $this->assertContains('Application default', $configModel->getTimezones(true)); + } + + public function testGetLanguages() + { + $configModel = new Config($this->container); + $this->assertNotEmpty($configModel->getLanguages()); + $this->assertArrayHasKey('fr_FR', $configModel->getLanguages()); + $this->assertContains('Français', $configModel->getLanguages()); + $this->assertArrayNotHasKey('', $configModel->getLanguages()); + + $this->assertArrayHasKey('', $configModel->getLanguages(true)); + $this->assertContains('Application default', $configModel->getLanguages(true)); + } + + public function testGetJsLanguage() + { + $configModel = new Config($this->container); + $this->assertEquals('en', $configModel->getJsLanguageCode()); + + $this->container['sessionStorage']->user = array('language' => 'fr_FR'); + $this->assertEquals('fr', $configModel->getJsLanguageCode()); + + $this->container['sessionStorage']->user = array('language' => 'xx_XX'); + $this->assertEquals('en', $configModel->getJsLanguageCode()); + } + + public function testGetCurrentLanguage() + { + $configModel = new Config($this->container); + $this->assertEquals('en_US', $configModel->getCurrentLanguage()); + + $this->container['sessionStorage']->user = array('language' => 'fr_FR'); + $this->assertEquals('fr_FR', $configModel->getCurrentLanguage()); + + $this->container['sessionStorage']->user = array('language' => 'xx_XX'); + $this->assertEquals('xx_XX', $configModel->getCurrentLanguage()); + } + + public function testGetCurrentTimezone() + { + $configModel = new Config($this->container); + $this->assertEquals('UTC', $configModel->getCurrentTimezone()); + + $this->container['sessionStorage']->user = array('timezone' => 'Europe/Paris'); + $this->assertEquals('Europe/Paris', $configModel->getCurrentTimezone()); + + $this->container['sessionStorage']->user = array('timezone' => 'Something'); + $this->assertEquals('Something', $configModel->getCurrentTimezone()); + } + + public function testRegenerateToken() + { + $configModel = new Config($this->container); + $token = $configModel->getOption('api_token'); + $this->assertTrue($configModel->regenerateToken('api_token')); + $this->assertNotEquals($token, $configModel->getOption('api_token')); + } + public function testCRUDOperations() { $c = new Config($this->container); @@ -37,62 +105,73 @@ class ConfigTest extends Base $c = new Config($this->container); $this->assertTrue($c->save(array('application_url' => 'http://localhost/'))); - $this->assertEquals('http://localhost/', $c->get('application_url')); + $this->assertEquals('http://localhost/', $c->getOption('application_url')); $this->assertTrue($c->save(array('application_url' => 'http://localhost'))); - $this->assertEquals('http://localhost/', $c->get('application_url')); + $this->assertEquals('http://localhost/', $c->getOption('application_url')); $this->assertTrue($c->save(array('application_url' => ''))); - $this->assertEquals('', $c->get('application_url')); + $this->assertEquals('', $c->getOption('application_url')); } public function testDefaultValues() { $c = new Config($this->container); - $this->assertEquals('en_US', $c->get('application_language')); - $this->assertEquals('UTC', $c->get('application_timezone')); - - $this->assertEmpty($c->get('webhook_url_task_modification')); - $this->assertEmpty($c->get('webhook_url_task_creation')); - $this->assertEmpty($c->get('board_columns')); - $this->assertEmpty($c->get('application_url')); - - $this->assertNotEmpty($c->get('webhook_token')); - $this->assertNotEmpty($c->get('api_token')); + $this->assertEquals(172800, $c->getOption('board_highlight_period')); + $this->assertEquals(60, $c->getOption('board_public_refresh_interval')); + $this->assertEquals(10, $c->getOption('board_private_refresh_interval')); + $this->assertEmpty($c->getOption('board_columns')); + + $this->assertEquals('yellow', $c->getOption('default_color')); + $this->assertEquals('en_US', $c->getOption('application_language')); + $this->assertEquals('UTC', $c->getOption('application_timezone')); + $this->assertEquals('m/d/Y', $c->getOption('application_date_format')); + $this->assertEmpty($c->getOption('application_url')); + $this->assertEmpty($c->getOption('application_stylesheet')); + $this->assertEquals('USD', $c->getOption('application_currency')); + + $this->assertEquals(0, $c->getOption('calendar_user_subtasks_time_tracking')); + $this->assertEquals('date_started', $c->getOption('calendar_user_tasks')); + $this->assertEquals('date_started', $c->getOption('calendar_user_tasks')); + + $this->assertEquals(0, $c->getOption('integration_gravatar')); + $this->assertEquals(1, $c->getOption('cfd_include_closed_tasks')); + $this->assertEquals(1, $c->getOption('password_reset')); + + $this->assertEquals(1, $c->getOption('subtask_time_tracking')); + $this->assertEquals(0, $c->getOption('subtask_restriction')); + $this->assertEmpty($c->getOption('project_categories')); + + $this->assertEmpty($c->getOption('webhook_url_task_modification')); + $this->assertEmpty($c->getOption('webhook_url_task_creation')); + $this->assertNotEmpty($c->getOption('webhook_token')); + $this->assertEmpty($c->getOption('webhook_url')); + + $this->assertNotEmpty($c->getOption('api_token')); } - public function testGet() + public function testGetOption() { $c = new Config($this->container); - $this->assertEquals('', $c->get('board_columns')); - $this->assertEquals('test', $c->get('board_columns', 'test')); - $this->assertEquals(0, $c->get('board_columns', 0)); + $this->assertEquals('', $c->getOption('board_columns')); + $this->assertEquals('test', $c->getOption('board_columns', 'test')); + $this->assertEquals(0, $c->getOption('board_columns', 0)); } - public function testGetWithSession() + public function testGetWithCaching() { - $this->container['session'] = new Session; $c = new Config($this->container); - session_id('test'); + $this->assertEquals('UTC', $c->get('application_timezone')); + $this->assertTrue($c->save(array('application_timezone' => 'Europe/Paris'))); - $this->assertTrue(Session::isOpen()); + $this->assertEquals('UTC', $c->get('application_timezone')); // cached value + $this->assertEquals('Europe/Paris', $c->getOption('application_timezone')); $this->assertEquals('', $c->get('board_columns')); $this->assertEquals('test', $c->get('board_columns', 'test')); - - $this->container['session']['config'] = array( - 'board_columns' => 'foo', - 'empty_value' => 0 - ); - - $this->assertEquals('foo', $c->get('board_columns')); - $this->assertEquals('foo', $c->get('board_columns', 'test')); $this->assertEquals('test', $c->get('empty_value', 'test')); - - session_id(''); - unset($this->container['session']); } } diff --git a/tests/units/Model/CurrencyTest.php b/tests/units/Model/CurrencyTest.php new file mode 100644 index 00000000..0bc71da6 --- /dev/null +++ b/tests/units/Model/CurrencyTest.php @@ -0,0 +1,53 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Currency; + +class CurrencyTest extends Base +{ + public function testGetCurrencies() + { + $currencyModel = new Currency($this->container); + $currencies = $currencyModel->getCurrencies(); + $this->assertArrayHasKey('EUR', $currencies); + } + + public function testGetAll() + { + $currencyModel = new Currency($this->container); + $currencies = $currencyModel->getAll(); + $this->assertCount(0, $currencies); + + $this->assertNotFalse($currencyModel->create('USD', 9.9)); + $currencies = $currencyModel->getAll(); + $this->assertCount(1, $currencies); + $this->assertEquals('USD', $currencies[0]['currency']); + $this->assertEquals(9.9, $currencies[0]['rate']); + } + + public function testCreate() + { + $currencyModel = new Currency($this->container); + $this->assertNotFalse($currencyModel->create('EUR', 1.2)); + $this->assertNotFalse($currencyModel->create('EUR', 1.5)); + } + + public function testUpdate() + { + $currencyModel = new Currency($this->container); + $this->assertNotFalse($currencyModel->create('EUR', 1.1)); + $this->assertNotFalse($currencyModel->update('EUR', 2.2)); + } + + public function testGetPrice() + { + $currencyModel = new Currency($this->container); + + $this->assertEquals(123, $currencyModel->getPrice('USD', 123)); + + $this->assertNotFalse($currencyModel->create('EUR', 0.5)); + $this->assertEquals(50.0, $currencyModel->getPrice('EUR', 100)); + $this->assertEquals(50.0, $currencyModel->getPrice('EUR', 100)); // test with cached result + } +} diff --git a/tests/units/Model/CustomFilterTest.php b/tests/units/Model/CustomFilterTest.php index 190da899..a73bc401 100644 --- a/tests/units/Model/CustomFilterTest.php +++ b/tests/units/Model/CustomFilterTest.php @@ -52,34 +52,6 @@ class CustomFilterTest extends Base $this->assertEquals(1, $filter['is_shared']); } - public function testValidation() - { - $cf = new CustomFilter($this->container); - - // Validate creation - $r = $cf->validateCreation(array('filter' => 'test', 'name' => 'test', 'user_id' => 1, 'project_id' => 1, 'is_shared' => 0)); - $this->assertTrue($r[0]); - - $r = $cf->validateCreation(array('filter' => str_repeat('a', 101), 'name' => 'test', 'user_id' => 1, 'project_id' => 1, 'is_shared' => 0)); - $this->assertFalse($r[0]); - - $r = $cf->validateCreation(array('name' => 'test', 'user_id' => 1, 'project_id' => 1, 'is_shared' => 0)); - $this->assertFalse($r[0]); - - // Validate modification - $r = $cf->validateModification(array('id' => 1, 'filter' => 'test', 'name' => 'test', 'user_id' => 1, 'project_id' => 1, 'is_shared' => 0)); - $this->assertTrue($r[0]); - - $r = $cf->validateModification(array('filter' => 'test', 'name' => 'test', 'user_id' => 1, 'project_id' => 1, 'is_shared' => 0)); - $this->assertFalse($r[0]); - - $r = $cf->validateModification(array('id' => 1, 'filter' => str_repeat('a', 101), 'name' => 'test', 'user_id' => 1, 'project_id' => 1, 'is_shared' => 0)); - $this->assertFalse($r[0]); - - $r = $cf->validateModification(array('id' => 1, 'name' => 'test', 'user_id' => 1, 'project_id' => 1, 'is_shared' => 0)); - $this->assertFalse($r[0]); - } - public function testGetAll() { $u = new User($this->container); diff --git a/tests/units/Model/FileTest.php b/tests/units/Model/FileTest.php deleted file mode 100644 index 29f6ee93..00000000 --- a/tests/units/Model/FileTest.php +++ /dev/null @@ -1,263 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\Task; -use Kanboard\Model\File; -use Kanboard\Model\TaskCreation; -use Kanboard\Model\Project; - -class FileTest extends Base -{ - public function setUp() - { - parent::setUp(); - - $this->container['objectStorage'] = $this - ->getMockBuilder('\Kanboard\Core\ObjectStorage\FileStorage') - ->setConstructorArgs(array($this->container)) - ->setMethods(array('put', 'moveFile', 'remove')) - ->getMock(); - } - - public function testCreation() - { - $p = new Project($this->container); - $f = new File($this->container); - $tc = new TaskCreation($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); - - $this->assertEquals(1, $f->create(1, 'test', '/tmp/foo', 10)); - - $file = $f->getById(1); - $this->assertNotEmpty($file); - $this->assertEquals('test', $file['name']); - $this->assertEquals('/tmp/foo', $file['path']); - $this->assertEquals(0, $file['is_image']); - $this->assertEquals(1, $file['task_id']); - $this->assertEquals(time(), $file['date'], '', 2); - $this->assertEquals(0, $file['user_id']); - $this->assertEquals(10, $file['size']); - - $this->assertEquals(2, $f->create(1, 'test2.png', '/tmp/foobar', 10)); - - $file = $f->getById(2); - $this->assertNotEmpty($file); - $this->assertEquals('test2.png', $file['name']); - $this->assertEquals('/tmp/foobar', $file['path']); - $this->assertEquals(1, $file['is_image']); - } - - public function testCreationFileNameTooLong() - { - $p = new Project($this->container); - $f = new File($this->container); - $tc = new TaskCreation($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); - - $this->assertNotFalse($f->create(1, 'test', '/tmp/foo', 10)); - $this->assertNotFalse($f->create(1, str_repeat('a', 1000), '/tmp/foo', 10)); - - $files = $f->getAll(1); - $this->assertNotEmpty($files); - $this->assertCount(2, $files); - - $this->assertEquals(str_repeat('a', 255), $files[0]['name']); - $this->assertEquals('test', $files[1]['name']); - } - - public function testIsImage() - { - $f = new File($this->container); - - $this->assertTrue($f->isImage('test.png')); - $this->assertTrue($f->isImage('test.jpeg')); - $this->assertTrue($f->isImage('test.gif')); - $this->assertTrue($f->isImage('test.jpg')); - $this->assertTrue($f->isImage('test.JPG')); - - $this->assertFalse($f->isImage('test.bmp')); - $this->assertFalse($f->isImage('test')); - $this->assertFalse($f->isImage('test.pdf')); - } - - public function testGeneratePath() - { - $f = new File($this->container); - - $this->assertStringStartsWith('12'.DIRECTORY_SEPARATOR.'34'.DIRECTORY_SEPARATOR, $f->generatePath(12, 34, 'test.png')); - $this->assertNotEquals($f->generatePath(12, 34, 'test1.png'), $f->generatePath(12, 34, 'test2.png')); - } - - public function testUploadScreenshot() - { - $p = new Project($this->container); - $tc = new TaskCreation($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); - - $data = base64_encode('image data'); - - $f = $this - ->getMockBuilder('\Kanboard\Model\File') - ->setConstructorArgs(array($this->container)) - ->setMethods(array('generateThumbnailFromData')) - ->getMock(); - - $this->container['objectStorage'] - ->expects($this->once()) - ->method('put') - ->with( - $this->stringContains('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR), - $this->equalTo(base64_decode($data)) - ) - ->will($this->returnValue(true)); - - $f->expects($this->once()) - ->method('generateThumbnailFromData'); - - $this->assertEquals(1, $f->uploadScreenshot(1, 1, $data)); - - $file = $f->getById(1); - $this->assertNotEmpty($file); - $this->assertStringStartsWith('Screenshot taken ', $file['name']); - $this->assertStringStartsWith('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR, $file['path']); - $this->assertEquals(1, $file['is_image']); - $this->assertEquals(1, $file['task_id']); - $this->assertEquals(time(), $file['date'], '', 2); - $this->assertEquals(0, $file['user_id']); - $this->assertEquals(10, $file['size']); - } - - public function testUploadFileContent() - { - $p = new Project($this->container); - $f = new File($this->container); - $tc = new TaskCreation($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); - - $data = base64_encode('file data'); - - $this->container['objectStorage'] - ->expects($this->once()) - ->method('put') - ->with( - $this->stringContains('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR), - $this->equalTo(base64_decode($data)) - ) - ->will($this->returnValue(true)); - - $this->assertEquals(1, $f->uploadContent(1, 1, 'my file.pdf', $data)); - - $file = $f->getById(1); - $this->assertNotEmpty($file); - $this->assertEquals('my file.pdf', $file['name']); - $this->assertStringStartsWith('1'.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR, $file['path']); - $this->assertEquals(0, $file['is_image']); - $this->assertEquals(1, $file['task_id']); - $this->assertEquals(time(), $file['date'], '', 2); - $this->assertEquals(0, $file['user_id']); - $this->assertEquals(9, $file['size']); - } - - public function testGetAll() - { - $p = new Project($this->container); - $f = new File($this->container); - $tc = new TaskCreation($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); - - $this->assertEquals(1, $f->create(1, 'B.pdf', '/tmp/foo', 10)); - $this->assertEquals(2, $f->create(1, 'A.png', '/tmp/foo', 10)); - $this->assertEquals(3, $f->create(1, 'D.doc', '/tmp/foo', 10)); - $this->assertEquals(4, $f->create(1, 'C.JPG', '/tmp/foo', 10)); - - $files = $f->getAll(1); - $this->assertNotEmpty($files); - $this->assertCount(4, $files); - $this->assertEquals('A.png', $files[0]['name']); - $this->assertEquals('B.pdf', $files[1]['name']); - $this->assertEquals('C.JPG', $files[2]['name']); - $this->assertEquals('D.doc', $files[3]['name']); - - $files = $f->getAllImages(1); - $this->assertNotEmpty($files); - $this->assertCount(2, $files); - $this->assertEquals('A.png', $files[0]['name']); - $this->assertEquals('C.JPG', $files[1]['name']); - - $files = $f->getAllDocuments(1); - $this->assertNotEmpty($files); - $this->assertCount(2, $files); - $this->assertEquals('B.pdf', $files[0]['name']); - $this->assertEquals('D.doc', $files[1]['name']); - } - - public function testRemove() - { - $p = new Project($this->container); - $f = new File($this->container); - $tc = new TaskCreation($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); - - $this->assertEquals(1, $f->create(1, 'B.pdf', DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo1', 10)); - $this->assertEquals(2, $f->create(1, 'A.png', DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo2', 10)); - $this->assertEquals(3, $f->create(1, 'D.doc', DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo3', 10)); - - $this->container['objectStorage'] - ->expects($this->at(0)) - ->method('remove') - ->with( - $this->equalTo(DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo2') - ) - ->will($this->returnValue(true)); - - $this->container['objectStorage'] - ->expects($this->at(1)) - ->method('remove') - ->with( - $this->equalTo('thumbnails'.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo2') - ) - ->will($this->returnValue(true)); - - $this->container['objectStorage'] - ->expects($this->at(2)) - ->method('remove') - ->with( - $this->equalTo(DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo1') - ) - ->will($this->returnValue(true)); - - $this->container['objectStorage'] - ->expects($this->at(3)) - ->method('remove') - ->with( - $this->equalTo(DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'foo3') - ) - ->will($this->returnValue(true)); - - $this->assertTrue($f->remove(2)); - - $files = $f->getAll(1); - $this->assertNotEmpty($files); - $this->assertCount(2, $files); - $this->assertEquals('B.pdf', $files[0]['name']); - $this->assertEquals('D.doc', $files[1]['name']); - - $this->assertTrue($f->removeAll(1)); - - $files = $f->getAll(1); - $this->assertEmpty($files); - } -} diff --git a/tests/units/Model/GroupMemberTest.php b/tests/units/Model/GroupMemberTest.php new file mode 100644 index 00000000..16f769e8 --- /dev/null +++ b/tests/units/Model/GroupMemberTest.php @@ -0,0 +1,76 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Group; +use Kanboard\Model\User; +use Kanboard\Model\GroupMember; + +class GroupMemberTest extends Base +{ + public function testAddRemove() + { + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + + $this->assertEquals(1, $groupModel->create('Test')); + + $this->assertTrue($groupMemberModel->addUser(1, 1)); + $this->assertFalse($groupMemberModel->addUser(1, 1)); + + $users = $groupMemberModel->getMembers(1); + $this->assertCount(1, $users); + $this->assertEquals('admin', $users[0]['username']); + + $this->assertEmpty($groupMemberModel->getNotMembers(1)); + + $this->assertTrue($groupMemberModel->removeUser(1, 1)); + $this->assertFalse($groupMemberModel->removeUser(1, 1)); + + $this->assertEmpty($groupMemberModel->getMembers(1)); + } + + public function testMembers() + { + $userModel = new User($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + + $this->assertEquals(1, $groupModel->create('Group A')); + $this->assertEquals(2, $groupModel->create('Group B')); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user4'))); + + $this->assertTrue($groupMemberModel->addUser(1, 1)); + $this->assertTrue($groupMemberModel->addUser(1, 2)); + $this->assertTrue($groupMemberModel->addUser(1, 5)); + $this->assertTrue($groupMemberModel->addUser(2, 3)); + $this->assertTrue($groupMemberModel->addUser(2, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + + $users = $groupMemberModel->getMembers(1); + $this->assertCount(3, $users); + $this->assertEquals('admin', $users[0]['username']); + $this->assertEquals('user1', $users[1]['username']); + $this->assertEquals('user4', $users[2]['username']); + + $users = $groupMemberModel->getNotMembers(1); + $this->assertCount(2, $users); + $this->assertEquals('user2', $users[0]['username']); + $this->assertEquals('user3', $users[1]['username']); + + $users = $groupMemberModel->getMembers(2); + $this->assertCount(3, $users); + $this->assertEquals('user2', $users[0]['username']); + $this->assertEquals('user3', $users[1]['username']); + $this->assertEquals('user4', $users[2]['username']); + + $users = $groupMemberModel->getNotMembers(2); + $this->assertCount(2, $users); + $this->assertEquals('admin', $users[0]['username']); + $this->assertEquals('user1', $users[1]['username']); + } +} diff --git a/tests/units/Model/GroupTest.php b/tests/units/Model/GroupTest.php new file mode 100644 index 00000000..36b47dc5 --- /dev/null +++ b/tests/units/Model/GroupTest.php @@ -0,0 +1,60 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Group; + +class GroupTest extends Base +{ + public function testCreation() + { + $groupModel = new Group($this->container); + $this->assertEquals(1, $groupModel->create('Test')); + $this->assertFalse($groupModel->create('Test')); + } + + public function testGetById() + { + $groupModel = new Group($this->container); + $this->assertEquals(1, $groupModel->create('Test')); + + $group = $groupModel->getById(1); + $this->assertEquals('Test', $group['name']); + $this->assertEquals('', $group['external_id']); + + $this->assertEmpty($groupModel->getById(2)); + } + + public function testGetAll() + { + $groupModel = new Group($this->container); + $this->assertEquals(1, $groupModel->create('B')); + $this->assertEquals(2, $groupModel->create('A', 'uuid')); + + $groups = $groupModel->getAll(); + $this->assertCount(2, $groups); + $this->assertEquals('A', $groups[0]['name']); + $this->assertEquals('uuid', $groups[0]['external_id']); + $this->assertEquals('B', $groups[1]['name']); + $this->assertEquals('', $groups[1]['external_id']); + } + + public function testUpdate() + { + $groupModel = new Group($this->container); + $this->assertEquals(1, $groupModel->create('Test')); + $this->assertTrue($groupModel->update(array('id' => 1, 'name' => 'My group', 'external_id' => 'test'))); + + $group = $groupModel->getById(1); + $this->assertEquals('My group', $group['name']); + $this->assertEquals('test', $group['external_id']); + } + + public function testRemove() + { + $groupModel = new Group($this->container); + $this->assertEquals(1, $groupModel->create('Test')); + $this->assertTrue($groupModel->remove(1)); + $this->assertEmpty($groupModel->getById(1)); + } +} diff --git a/tests/units/Model/LastLoginTest.php b/tests/units/Model/LastLoginTest.php new file mode 100644 index 00000000..fc5ea1e5 --- /dev/null +++ b/tests/units/Model/LastLoginTest.php @@ -0,0 +1,43 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\LastLogin; + +class LastLoginTest extends Base +{ + public function testCreate() + { + $lastLoginModel = new LastLogin($this->container); + + $this->assertTrue($lastLoginModel->create('Test1', 1, '127.0.0.1', 'My browser')); + $this->assertTrue($lastLoginModel->create('Test2', 1, '127.0.0.1', str_repeat('Too long', 50))); + $this->assertTrue($lastLoginModel->create('Test3', 1, '2001:0db8:0000:0000:0000:ff00:0042:8329', 'My Ipv6 browser')); + + $connections = $lastLoginModel->getAll(1); + $this->assertCount(3, $connections); + + $this->assertEquals('Test3', $connections[0]['auth_type']); + $this->assertEquals('2001:0db8:0000:0000:0000:ff00:0042:8329', $connections[0]['ip']); + + $this->assertEquals('Test2', $connections[1]['auth_type']); + $this->assertEquals('127.0.0.1', $connections[1]['ip']); + + $this->assertEquals('Test1', $connections[2]['auth_type']); + $this->assertEquals('127.0.0.1', $connections[2]['ip']); + } + + public function testCleanup() + { + $lastLoginModel = new LastLogin($this->container); + + for ($i = 0; $i < $lastLoginModel::NB_LOGINS + 5; $i++) { + $this->assertTrue($lastLoginModel->create('Test' . $i, 1, '127.0.0.1', 'My browser')); + } + + $connections = $lastLoginModel->getAll(1); + $this->assertCount(10, $connections); + $this->assertEquals('Test14', $connections[0]['auth_type']); + $this->assertEquals('Test5', $connections[9]['auth_type']); + } +} diff --git a/tests/units/Model/LinkTest.php b/tests/units/Model/LinkTest.php index de9d843a..b102646d 100644 --- a/tests/units/Model/LinkTest.php +++ b/tests/units/Model/LinkTest.php @@ -124,50 +124,4 @@ class LinkTest extends Base $this->assertArrayNotHasKey(0, $links); $this->assertEquals('relates to', $links[1]); } - - public function testValidateCreation() - { - $l = new Link($this->container); - - $r = $l->validateCreation(array('label' => 'a')); - $this->assertTrue($r[0]); - - $r = $l->validateCreation(array('label' => 'a', 'opposite_label' => 'b')); - $this->assertTrue($r[0]); - - $r = $l->validateCreation(array('label' => 'relates to')); - $this->assertFalse($r[0]); - - $r = $l->validateCreation(array('label' => 'a', 'opposite_label' => 'a')); - $this->assertFalse($r[0]); - - $r = $l->validateCreation(array('label' => '')); - $this->assertFalse($r[0]); - } - - public function testValidateModification() - { - $l = new Link($this->container); - - $r = $l->validateModification(array('id' => 20, 'label' => 'a', 'opposite_id' => 0)); - $this->assertTrue($r[0]); - - $r = $l->validateModification(array('id' => 20, 'label' => 'a', 'opposite_id' => '1')); - $this->assertTrue($r[0]); - - $r = $l->validateModification(array('id' => 20, 'label' => 'relates to', 'opposite_id' => '1')); - $this->assertFalse($r[0]); - - $r = $l->validateModification(array('id' => 20, 'label' => '', 'opposite_id' => '1')); - $this->assertFalse($r[0]); - - $r = $l->validateModification(array('label' => '', 'opposite_id' => '1')); - $this->assertFalse($r[0]); - - $r = $l->validateModification(array('id' => 20, 'opposite_id' => '1')); - $this->assertFalse($r[0]); - - $r = $l->validateModification(array('label' => 'test')); - $this->assertFalse($r[0]); - } } diff --git a/tests/units/Model/NotificationTest.php b/tests/units/Model/NotificationTest.php index 7f9977ce..03ee5867 100644 --- a/tests/units/Model/NotificationTest.php +++ b/tests/units/Model/NotificationTest.php @@ -7,7 +7,7 @@ use Kanboard\Model\TaskCreation; use Kanboard\Model\Subtask; use Kanboard\Model\Comment; use Kanboard\Model\User; -use Kanboard\Model\File; +use Kanboard\Model\TaskFile; use Kanboard\Model\Task; use Kanboard\Model\Project; use Kanboard\Model\Notification; @@ -23,7 +23,7 @@ class NotificationTest extends Base $tc = new TaskCreation($this->container); $s = new Subtask($this->container); $c = new Comment($this->container); - $f = new File($this->container); + $f = new TaskFile($this->container); $this->assertEquals(1, $p->create(array('name' => 'test'))); $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); diff --git a/tests/units/Model/PasswordResetTest.php b/tests/units/Model/PasswordResetTest.php new file mode 100644 index 00000000..f88d24fb --- /dev/null +++ b/tests/units/Model/PasswordResetTest.php @@ -0,0 +1,85 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\User; +use Kanboard\Model\PasswordReset; + +class PasswordResetTest extends Base +{ + public function testCreate() + { + $userModel = new User($this->container); + $passwordResetModel = new PasswordReset($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'email' => 'user1@localhost'))); + + $this->assertFalse($passwordResetModel->create('user0')); + $this->assertFalse($passwordResetModel->create('user1')); + $this->assertNotFalse($passwordResetModel->create('user2')); + } + + public function testGetUserIdByToken() + { + $userModel = new User($this->container); + $passwordResetModel = new PasswordReset($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user2', 'email' => 'user1@localhost'))); + + $token = $passwordResetModel->create('user2'); + $this->assertEquals(2, $passwordResetModel->getUserIdByToken($token)); + } + + public function testGetUserIdByTokenWhenExpired() + { + $userModel = new User($this->container); + $passwordResetModel = new PasswordReset($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user2', 'email' => 'user1@localhost'))); + + $token = $passwordResetModel->create('user2', strtotime('-1 year')); + $this->assertFalse($passwordResetModel->getUserIdByToken($token)); + } + + public function testDisableTokens() + { + $userModel = new User($this->container); + $passwordResetModel = new PasswordReset($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user2', 'email' => 'user1@localhost'))); + + $token1 = $passwordResetModel->create('user2'); + $token2 = $passwordResetModel->create('user2'); + + $this->assertEquals(2, $passwordResetModel->getUserIdByToken($token1)); + $this->assertEquals(2, $passwordResetModel->getUserIdByToken($token2)); + + $this->assertTrue($passwordResetModel->disable(2)); + + $this->assertFalse($passwordResetModel->getUserIdByToken($token1)); + $this->assertFalse($passwordResetModel->getUserIdByToken($token2)); + } + + public function testGetAll() + { + $userModel = new User($this->container); + $passwordResetModel = new PasswordReset($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user2', 'email' => 'user1@localhost'))); + $this->assertNotFalse($passwordResetModel->create('user2')); + $this->assertNotFalse($passwordResetModel->create('user2')); + + $tokens = $passwordResetModel->getAll(1); + $this->assertCount(0, $tokens); + + $tokens = $passwordResetModel->getAll(2); + $this->assertCount(2, $tokens); + $this->assertNotEmpty($tokens[0]['token']); + $this->assertNotEmpty($tokens[0]['date_creation']); + $this->assertNotEmpty($tokens[0]['date_expiration']); + $this->assertEquals(2, $tokens[0]['user_id']); + $this->assertArrayHasKey('user_agent', $tokens[0]); + $this->assertArrayHasKey('ip', $tokens[0]); + } +} diff --git a/tests/units/Model/ProjectActivityTest.php b/tests/units/Model/ProjectActivityTest.php index 5a242cb2..04d3004d 100644 --- a/tests/units/Model/ProjectActivityTest.php +++ b/tests/units/Model/ProjectActivityTest.php @@ -9,7 +9,6 @@ use Kanboard\Model\ProjectActivity; use Kanboard\Model\Project; use Kanboard\Model\Subtask; use Kanboard\Model\Comment; -use Kanboard\Model\File; class ProjectActivityTest extends Base { @@ -87,9 +86,10 @@ class ProjectActivityTest extends Base $max = 15; $nb_events = 100; + $task = $tf->getbyId(1); for ($i = 0; $i < $nb_events; $i++) { - $this->assertTrue($e->createEvent(1, 1, 1, Task::EVENT_CLOSE, array('task' => $tf->getbyId(1)))); + $this->assertTrue($e->createEvent(1, 1, 1, Task::EVENT_CLOSE, array('task' => $task))); } $this->assertEquals($nb_events, $this->container['db']->table('project_activities')->count()); @@ -98,20 +98,9 @@ class ProjectActivityTest extends Base $events = $e->getProject(1); $this->assertNotEmpty($events); - $this->assertTrue(is_array($events)); - $this->assertEquals($max, count($events)); + $this->assertCount($max, $events); $this->assertEquals(100, $events[0]['id']); $this->assertEquals(99, $events[1]['id']); $this->assertEquals(86, $events[14]['id']); - - // Cleanup during task creation - - $nb_events = ProjectActivity::MAX_EVENTS + 10; - - for ($i = 0; $i < $nb_events; $i++) { - $this->assertTrue($e->createEvent(1, 1, 1, Task::EVENT_CLOSE, array('task' => $tf->getbyId(1)))); - } - - $this->assertEquals(ProjectActivity::MAX_EVENTS, $this->container['db']->table('project_activities')->count()); } } diff --git a/tests/units/Model/ProjectDailyColumnStatsTest.php b/tests/units/Model/ProjectDailyColumnStatsTest.php index 4c801e02..5e8ec3e8 100644 --- a/tests/units/Model/ProjectDailyColumnStatsTest.php +++ b/tests/units/Model/ProjectDailyColumnStatsTest.php @@ -4,87 +4,262 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Project; use Kanboard\Model\ProjectDailyColumnStats; +use Kanboard\Model\Config; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskStatus; class ProjectDailyColumnStatsTest extends Base { + public function testUpdateTotalsWithScoreAtNull() + { + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $task = $this->container['db']->table(Task::TABLE)->findOne(); + $this->assertNull($task['score']); + + $stats = $this->container['db']->table(ProjectDailyColumnStats::TABLE) + ->asc('day') + ->asc('column_id') + ->columns('day', 'project_id', 'column_id', 'total', 'score') + ->findAll(); + + $expected = array( + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 1, + 'score' => 0, + ), + ); + + $this->assertEquals($expected, $stats); + } + public function testUpdateTotals() { - $p = new Project($this->container); - $pds = new ProjectDailyColumnStats($this->container); - $tc = new TaskCreation($this->container); - $ts = new TaskStatus($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertEquals(0, $pds->countDays(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d'))); - - for ($i = 0; $i < 10; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 1))); - } - - for ($i = 0; $i < 5; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 4))); - } - - $pds->updateTotals(1, date('Y-m-d', strtotime('-2days'))); - - for ($i = 0; $i < 15; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 3))); - } - - for ($i = 0; $i < 25; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 2))); - } - - $pds->updateTotals(1, date('Y-m-d', strtotime('-1 day'))); - - $this->assertNotFalse($ts->close(1)); - $this->assertNotFalse($ts->close(2)); - - for ($i = 0; $i < 3; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 3))); - } - - for ($i = 0; $i < 5; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 2))); - } - - for ($i = 0; $i < 4; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 4))); - } - - $pds->updateTotals(1, date('Y-m-d')); - - $this->assertEquals(3, $pds->countDays(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d'))); - $metrics = $pds->getAggregatedMetrics(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d')); - - $this->assertNotEmpty($metrics); - $this->assertEquals(4, count($metrics)); - $this->assertEquals(5, count($metrics[0])); - $this->assertEquals('Date', $metrics[0][0]); - $this->assertEquals('Backlog', $metrics[0][1]); - $this->assertEquals('Ready', $metrics[0][2]); - $this->assertEquals('Work in progress', $metrics[0][3]); - $this->assertEquals('Done', $metrics[0][4]); - - $this->assertEquals(date('Y-m-d', strtotime('-2days')), $metrics[1][0]); - $this->assertEquals(10, $metrics[1][1]); - $this->assertEquals(0, $metrics[1][2]); - $this->assertEquals(0, $metrics[1][3]); - $this->assertEquals(5, $metrics[1][4]); - - $this->assertEquals(date('Y-m-d', strtotime('-1day')), $metrics[2][0]); - $this->assertEquals(10, $metrics[2][1]); - $this->assertEquals(25, $metrics[2][2]); - $this->assertEquals(15, $metrics[2][3]); - $this->assertEquals(5, $metrics[2][4]); - - $this->assertEquals(date('Y-m-d'), $metrics[3][0]); - $this->assertEquals(10, $metrics[3][1]); - $this->assertEquals(30, $metrics[3][2]); - $this->assertEquals(18, $metrics[3][3]); - $this->assertEquals(9, $metrics[3][4]); + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->createTasks(1, 2, 1); + $this->createTasks(1, 3, 0); + + $this->createTasks(2, 5, 1); + $this->createTasks(2, 8, 1); + $this->createTasks(2, 0, 0); + $this->createTasks(2, 0, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(1, 9, 1); + $this->createTasks(1, 7, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + + $stats = $this->container['db']->table(ProjectDailyColumnStats::TABLE) + ->asc('day') + ->asc('column_id') + ->columns('day', 'project_id', 'column_id', 'total', 'score') + ->findAll(); + + $expected = array( + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 4, + 'score' => 11, + ), + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 4, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 4, + 'score' => 11, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 4, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 3, + 'total' => 1, + 'score' => 0, + ), + ); + + $this->assertEquals($expected, $stats); + } + + public function testUpdateTotalsWithOnlyOpenTasks() + { + $configModel = new Config($this->container); + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($configModel->save(array('cfd_include_closed_tasks' => 0))); + $this->container['memoryCache']->flush(); + + $this->createTasks(1, 2, 1); + $this->createTasks(1, 3, 0); + + $this->createTasks(2, 5, 1); + $this->createTasks(2, 8, 1); + $this->createTasks(2, 0, 0); + $this->createTasks(2, 0, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(1, 9, 1); + $this->createTasks(1, 7, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + + $stats = $this->container['db']->table(ProjectDailyColumnStats::TABLE) + ->asc('day') + ->asc('column_id') + ->columns('day', 'project_id', 'column_id', 'total', 'score') + ->findAll(); + + $expected = array( + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 2, + 'score' => 11, + ), + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 2, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 2, + 'score' => 11, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 2, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 3, + 'total' => 1, + 'score' => 0, + ), + ); + + $this->assertEquals($expected, $stats); + } + + public function testCountDays() + { + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->createTasks(1, 2, 1); + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + $this->assertEquals(1, $projectDailyColumnStats->countDays(1, '2016-01-16', '2016-01-17')); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + $this->assertEquals(2, $projectDailyColumnStats->countDays(1, '2016-01-16', '2016-01-17')); + } + + public function testGetAggregatedMetrics() + { + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->createTasks(1, 2, 1); + $this->createTasks(1, 3, 0); + + $this->createTasks(2, 5, 1); + $this->createTasks(2, 8, 1); + $this->createTasks(2, 0, 0); + $this->createTasks(2, 0, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(1, 9, 1); + $this->createTasks(1, 7, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + + $this->createTasks(2, 1, 1); + $this->createTasks(3, 1, 1); + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-18'); + + $expected = array( + array('Date', 'Backlog', 'Ready', 'Work in progress', 'Done'), + array('2016-01-16', 4, 4, 0, 0), + array('2016-01-17', 4, 4, 1, 0), + array('2016-01-18', 4, 5, 3, 0), + ); + + $this->assertEquals($expected, $projectDailyColumnStats->getAggregatedMetrics(1, '2016-01-16', '2016-01-18')); + + $expected = array( + array('Date', 'Backlog', 'Ready', 'Work in progress', 'Done'), + array('2016-01-16', 11, 13, 0, 0), + array('2016-01-17', 11, 13, 0, 0), + array('2016-01-18', 11, 14, 1, 0), + ); + + $this->assertEquals($expected, $projectDailyColumnStats->getAggregatedMetrics(1, '2016-01-16', '2016-01-18', 'score')); + } + + private function createTasks($column_id, $score, $is_active) + { + $taskCreationModel = new TaskCreation($this->container); + $this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => $column_id, 'score' => $score, 'is_active' => $is_active))); } } diff --git a/tests/units/Model/ProjectDailyStatsTest.php b/tests/units/Model/ProjectDailyStatsTest.php new file mode 100644 index 00000000..9efdb199 --- /dev/null +++ b/tests/units/Model/ProjectDailyStatsTest.php @@ -0,0 +1,48 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Project; +use Kanboard\Model\ProjectDailyStats; +use Kanboard\Model\Task; +use Kanboard\Model\TaskCreation; +use Kanboard\Model\TaskStatus; + +class ProjectDailyStatsTest extends Base +{ + public function testUpdateTotals() + { + $p = new Project($this->container); + $pds = new ProjectDailyStats($this->container); + $tc = new TaskCreation($this->container); + $ts = new TaskStatus($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertEquals(2, $p->create(array('name' => 'UnitTest'))); + + $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'date_started' => strtotime('-1 day')))); + $this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 1))); + $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 2))); + + $pds->updateTotals(1, date('Y-m-d', strtotime('-1 day'))); + + $this->assertTrue($ts->close(1)); + $pds->updateTotals(1, date('Y-m-d')); + + $metrics = $pds->getRawMetrics(1, date('Y-m-d', strtotime('-1days')), date('Y-m-d')); + $expected = array( + array( + 'day' => date('Y-m-d', strtotime('-1days')), + 'avg_lead_time' => 0, + 'avg_cycle_time' => 43200, + ), + array( + 'day' => date('Y-m-d'), + 'avg_lead_time' => 0, + 'avg_cycle_time' => 43200, + ) + ); + + $this->assertEquals($expected, $metrics); + } +} diff --git a/tests/units/Model/ProjectDuplicationTest.php b/tests/units/Model/ProjectDuplicationTest.php index e3234dfe..ee5b4ce4 100644 --- a/tests/units/Model/ProjectDuplicationTest.php +++ b/tests/units/Model/ProjectDuplicationTest.php @@ -5,17 +5,28 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Action; use Kanboard\Model\Project; use Kanboard\Model\Category; -use Kanboard\Model\ProjectPermission; +use Kanboard\Model\ProjectUserRole; +use Kanboard\Model\ProjectGroupRole; use Kanboard\Model\ProjectDuplication; use Kanboard\Model\User; +use Kanboard\Model\Group; +use Kanboard\Model\GroupMember; use Kanboard\Model\Swimlane; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; +use Kanboard\Core\Security\Role; class ProjectDuplicationTest extends Base { - public function testProjectName() + public function testGetSelections() + { + $projectDuplicationModel = new ProjectDuplication($this->container); + $this->assertCount(5, $projectDuplicationModel->getOptionalSelection()); + $this->assertCount(6, $projectDuplicationModel->getPossibleSelection()); + } + + public function testGetClonedProjectName() { $pd = new ProjectDuplication($this->container); @@ -28,54 +39,142 @@ class ProjectDuplicationTest extends Base $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 60))); } - public function testCopyProjectWithLongName() + public function testClonePublicProject() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); - $this->assertEquals(1, $p->create(array('name' => str_repeat('a', 50)))); + $this->assertEquals(1, $p->create(array('name' => 'Public'))); $this->assertEquals(2, $pd->duplicate(1)); $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals(str_repeat('a', 42).' (Clone)', $project['name']); + $this->assertEquals('Public (Clone)', $project['name']); + $this->assertEquals(1, $project['is_active']); + $this->assertEquals(0, $project['is_private']); + $this->assertEquals(0, $project['is_public']); + $this->assertEquals(0, $project['owner_id']); + $this->assertEmpty($project['token']); } - public function testClonePublicProject() + public function testClonePrivateProject() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); + $pp = new ProjectUserRole($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Public'))); + $this->assertEquals(1, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true)); $this->assertEquals(2, $pd->duplicate(1)); $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals('Public (Clone)', $project['name']); - $this->assertEquals(0, $project['is_private']); + $this->assertEquals('Private (Clone)', $project['name']); + $this->assertEquals(1, $project['is_active']); + $this->assertEquals(1, $project['is_private']); $this->assertEquals(0, $project['is_public']); + $this->assertEquals(0, $project['owner_id']); $this->assertEmpty($project['token']); + + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 1)); } - public function testClonePrivateProject() + public function testCloneSharedProject() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true)); + $this->assertEquals(1, $p->create(array('name' => 'Shared'))); + $this->assertTrue($p->update(array('id' => 1, 'is_public' => 1, 'token' => 'test'))); + + $project = $p->getById(1); + $this->assertEquals('test', $project['token']); + $this->assertEquals(1, $project['is_public']); + $this->assertEquals(2, $pd->duplicate(1)); $project = $p->getById(2); $this->assertNotEmpty($project); - $this->assertEquals('Private (Clone)', $project['name']); - $this->assertEquals(1, $project['is_private']); + $this->assertEquals('Shared (Clone)', $project['name']); + $this->assertEquals('', $project['token']); $this->assertEquals(0, $project['is_public']); - $this->assertEmpty($project['token']); + } - $pp = new ProjectPermission($this->container); + public function testCloneInactiveProject() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); - $this->assertEquals(array(1 => 'admin'), $pp->getMembers(1)); - $this->assertEquals(array(1 => 'admin'), $pp->getMembers(2)); + $this->assertEquals(1, $p->create(array('name' => 'Inactive'))); + $this->assertTrue($p->update(array('id' => 1, 'is_active' => 0))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['is_active']); + + $this->assertEquals(2, $pd->duplicate(1)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Inactive (Clone)', $project['name']); + $this->assertEquals(1, $project['is_active']); + } + + public function testCloneProjectWithOwner() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); + $projectUserRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Owner'))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['owner_id']); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Owner (Clone)', $project['name']); + $this->assertEquals(1, $project['owner_id']); + + $this->assertEquals(Role::PROJECT_MANAGER, $projectUserRoleModel->getUserRole(2, 1)); + } + + public function testCloneProjectWithDifferentName() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Owner'))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['owner_id']); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1, 'Foobar')); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Foobar', $project['name']); + $this->assertEquals(1, $project['owner_id']); + } + + public function testCloneProjectAndForceItToBePrivate() + { + $p = new Project($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Owner'))); + + $project = $p->getById(1); + $this->assertEquals(0, $project['owner_id']); + $this->assertEquals(0, $project['is_private']); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1, 'Foobar', true)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Foobar', $project['name']); + $this->assertEquals(1, $project['owner_id']); + $this->assertEquals(1, $project['is_private']); } public function testCloneProjectWithCategories() @@ -97,16 +196,9 @@ class ProjectDuplicationTest extends Base $this->assertEquals('P1 (Clone)', $project['name']); $categories = $c->getAll(2); - $this->assertNotempty($categories); - $this->assertEquals(3, count($categories)); - - $this->assertEquals(4, $categories[0]['id']); + $this->assertCount(3, $categories); $this->assertEquals('C1', $categories[0]['name']); - - $this->assertEquals(5, $categories[1]['id']); $this->assertEquals('C2', $categories[1]['name']); - - $this->assertEquals(6, $categories[2]['id']); $this->assertEquals('C3', $categories[2]['name']); } @@ -114,38 +206,119 @@ class ProjectDuplicationTest extends Base { $p = new Project($this->container); $c = new Category($this->container); - $pp = new ProjectPermission($this->container); + $pp = new ProjectUserRole($this->container); $u = new User($this->container); $pd = new ProjectDuplication($this->container); - $this->assertEquals(2, $u->create(array('username' => 'unittest1', 'password' => 'unittest'))); - $this->assertEquals(3, $u->create(array('username' => 'unittest2', 'password' => 'unittest'))); - $this->assertEquals(4, $u->create(array('username' => 'unittest3', 'password' => 'unittest'))); + $this->assertEquals(2, $u->create(array('username' => 'user1'))); + $this->assertEquals(3, $u->create(array('username' => 'user2'))); + $this->assertEquals(4, $u->create(array('username' => 'user3'))); $this->assertEquals(1, $p->create(array('name' => 'P1'))); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->addMember(1, 4)); - $this->assertTrue($pp->addManager(1, 3)); - $this->assertTrue($pp->isMember(1, 2)); - $this->assertTrue($pp->isMember(1, 3)); - $this->assertTrue($pp->isMember(1, 4)); - $this->assertFalse($pp->isManager(1, 2)); - $this->assertTrue($pp->isManager(1, 3)); - $this->assertFalse($pp->isManager(1, 4)); + + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($pp->addUser(1, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($pp->addUser(1, 4, Role::PROJECT_VIEWER)); $this->assertEquals(2, $pd->duplicate(1)); + $this->assertCount(3, $pp->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 2)); + $this->assertEquals(Role::PROJECT_MEMBER, $pp->getUserRole(2, 3)); + $this->assertEquals(Role::PROJECT_VIEWER, $pp->getUserRole(2, 4)); + } + + public function testCloneProjectWithUsersAndOverrideOwner() + { + $p = new Project($this->container); + $c = new Category($this->container); + $pp = new ProjectUserRole($this->container); + $u = new User($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'user1'))); + $this->assertEquals(1, $p->create(array('name' => 'P1'), 2)); + + $project = $p->getById(1); + $this->assertEquals(2, $project['owner_id']); + + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($pp->addUser(1, 1, Role::PROJECT_MEMBER)); + + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 1)); + + $this->assertCount(2, $pp->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 1)); + $project = $p->getById(2); - $this->assertNotEmpty($project); - $this->assertEquals('P1 (Clone)', $project['name']); + $this->assertEquals(1, $project['owner_id']); + } + + public function testCloneTeamProjectToPrivatProject() + { + $p = new Project($this->container); + $c = new Category($this->container); + $pp = new ProjectUserRole($this->container); + $u = new User($this->container); + $pd = new ProjectDuplication($this->container); + + $this->assertEquals(2, $u->create(array('username' => 'user1'))); + $this->assertEquals(3, $u->create(array('username' => 'user2'))); + $this->assertEquals(1, $p->create(array('name' => 'P1'), 2)); + + $project = $p->getById(1); + $this->assertEquals(2, $project['owner_id']); + $this->assertEquals(0, $project['is_private']); + + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($pp->addUser(1, 1, Role::PROJECT_MEMBER)); - $this->assertEquals(3, count($pp->getMembers(2))); - $this->assertTrue($pp->isMember(2, 2)); - $this->assertTrue($pp->isMember(2, 3)); - $this->assertTrue($pp->isMember(2, 4)); - $this->assertFalse($pp->isManager(2, 2)); - $this->assertTrue($pp->isManager(2, 3)); - $this->assertFalse($pp->isManager(2, 4)); + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission'), 3, 'My private project', true)); + + $this->assertCount(1, $pp->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $pp->getUserRole(2, 3)); + + $project = $p->getById(2); + $this->assertEquals(3, $project['owner_id']); + $this->assertEquals(1, $project['is_private']); + } + + public function testCloneProjectWithGroups() + { + $p = new Project($this->container); + $c = new Category($this->container); + $pd = new ProjectDuplication($this->container); + $userModel = new User($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $projectGroupRoleModel = new ProjectGroupRole($this->container); + $projectUserRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'P1'))); + + $this->assertEquals(1, $groupModel->create('G1')); + $this->assertEquals(2, $groupModel->create('G2')); + $this->assertEquals(3, $groupModel->create('G3')); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user3'))); + + $this->assertTrue($groupMemberModel->addUser(1, 2)); + $this->assertTrue($groupMemberModel->addUser(2, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 4)); + + $this->assertTrue($projectGroupRoleModel->addGroup(1, 1, Role::PROJECT_MANAGER)); + $this->assertTrue($projectGroupRoleModel->addGroup(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($projectGroupRoleModel->addGroup(1, 3, Role::PROJECT_VIEWER)); + + $this->assertEquals(2, $pd->duplicate(1)); + + $this->assertCount(3, $projectGroupRoleModel->getGroups(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $projectUserRoleModel->getUserRole(2, 2)); + $this->assertEquals(Role::PROJECT_MEMBER, $projectUserRoleModel->getUserRole(2, 3)); + $this->assertEquals(Role::PROJECT_VIEWER, $projectUserRoleModel->getUserRole(2, 4)); } public function testCloneProjectWithActionTaskAssignCurrentUser() @@ -170,7 +343,7 @@ class ProjectDuplicationTest extends Base $this->assertNotEmpty($actions); $this->assertEquals('TaskAssignCurrentUser', $actions[0]['action_name']); $this->assertNotEmpty($actions[0]['params']); - $this->assertEquals(6, $actions[0]['params'][0]['value']); + $this->assertEquals(6, $actions[0]['params']['column_id']); } public function testCloneProjectWithActionTaskAssignColorCategory() @@ -200,11 +373,11 @@ class ProjectDuplicationTest extends Base $this->assertNotEmpty($actions); $this->assertEquals('TaskAssignColorCategory', $actions[0]['action_name']); $this->assertNotEmpty($actions[0]['params']); - $this->assertEquals('blue', $actions[0]['params'][0]['value']); - $this->assertEquals(5, $actions[0]['params'][1]['value']); + $this->assertEquals('blue', $actions[0]['params']['color_id']); + $this->assertEquals(5, $actions[0]['params']['category_id']); } - public function testCloneProjectWithSwimlanesAndTasks() + public function testCloneProjectWithSwimlanes() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); @@ -212,31 +385,22 @@ class ProjectDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); - $this->assertEquals(1, $p->create(array('name' => 'P1'))); + $this->assertEquals(1, $p->create(array('name' => 'P1', 'default_swimlane' => 'New Default'))); // create initial swimlanes $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'S1'))); $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'S2'))); $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'S3'))); - $default_swimlane1 = $s->getDefault(1); - $default_swimlane1['default_swimlane'] = 'New Default'; + // create initial tasks + $this->assertEquals(1, $tc->create(array('title' => 'T0', 'project_id' => 1, 'swimlane_id' => 0))); + $this->assertEquals(2, $tc->create(array('title' => 'T1', 'project_id' => 1, 'swimlane_id' => 1))); + $this->assertEquals(3, $tc->create(array('title' => 'T2', 'project_id' => 1, 'swimlane_id' => 2))); + $this->assertEquals(4, $tc->create(array('title' => 'T3', 'project_id' => 1, 'swimlane_id' => 3))); - $this->assertTrue($s->updateDefault($default_swimlane1)); - - //create initial tasks - $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); - $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); - $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - - $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'swimlane', 'task'))); - $project = $p->getByName('P1 (Clone)'); - $this->assertNotFalse($project); - $project_id = $project['id']; - - // Check if Swimlanes have been duplicated - $swimlanes = $s->getAll($project_id); + $this->assertEquals(2, $pd->duplicate(1, array('category', 'swimlane'))); + $swimlanes = $s->getAll(2); $this->assertCount(3, $swimlanes); $this->assertEquals(4, $swimlanes[0]['id']); $this->assertEquals('S1', $swimlanes[0]['name']); @@ -244,29 +408,15 @@ class ProjectDuplicationTest extends Base $this->assertEquals('S2', $swimlanes[1]['name']); $this->assertEquals(6, $swimlanes[2]['id']); $this->assertEquals('S3', $swimlanes[2]['name']); - $new_default = $s->getDefault($project_id); - $this->assertEquals('New Default', $new_default['default_swimlane']); - - // Check if Tasks have been duplicated - $tasks = $tf->getAll($project_id); + $swimlane = $s->getDefault(2); + $this->assertEquals('New Default', $swimlane['default_swimlane']); - $this->assertCount(3, $tasks); - // $this->assertEquals(4, $tasks[0]['id']); - $this->assertEquals('T1', $tasks[0]['title']); - // $this->assertEquals(5, $tasks[1]['id']); - $this->assertEquals('T2', $tasks[1]['title']); - // $this->assertEquals(6, $tasks[2]['id']); - $this->assertEquals('T3', $tasks[2]['title']); - - $p->remove($project_id); - - $this->assertFalse($p->exists($project_id)); - $this->assertCount(0, $s->getAll($project_id)); - $this->assertCount(0, $tf->getAll($project_id)); + // Check if tasks are NOT been duplicated + $this->assertCount(0, $tf->getAll(2)); } - public function testCloneProjectWithSwimlanes() + public function testCloneProjectWithTasks() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); @@ -276,43 +426,22 @@ class ProjectDuplicationTest extends Base $this->assertEquals(1, $p->create(array('name' => 'P1'))); - // create initial swimlanes - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'S1'))); - $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'S2'))); - $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'S3'))); - - $default_swimlane1 = $s->getDefault(1); - $default_swimlane1['default_swimlane'] = 'New Default'; + // create initial tasks + $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2))); + $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3))); - $this->assertTrue($s->updateDefault($default_swimlane1)); - - //create initial tasks - $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); - $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); - $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - - $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'swimlane'))); - $project = $p->getByName('P1 (Clone)'); - $this->assertNotFalse($project); - $project_id = $project['id']; - - $swimlanes = $s->getAll($project_id); - - $this->assertCount(3, $swimlanes); - $this->assertEquals(4, $swimlanes[0]['id']); - $this->assertEquals('S1', $swimlanes[0]['name']); - $this->assertEquals(5, $swimlanes[1]['id']); - $this->assertEquals('S2', $swimlanes[1]['name']); - $this->assertEquals(6, $swimlanes[2]['id']); - $this->assertEquals('S3', $swimlanes[2]['name']); - $new_default = $s->getDefault($project_id); - $this->assertEquals('New Default', $new_default['default_swimlane']); + $this->assertEquals(2, $pd->duplicate(1, array('category', 'action', 'task'))); - // Check if Tasks have NOT been duplicated - $this->assertCount(0, $tf->getAll($project_id)); + // Check if Tasks have been duplicated + $tasks = $tf->getAll(2); + $this->assertCount(3, $tasks); + $this->assertEquals('T1', $tasks[0]['title']); + $this->assertEquals('T2', $tasks[1]['title']); + $this->assertEquals('T3', $tasks[2]['title']); } - public function testCloneProjectWithTasks() + public function testCloneProjectWithSwimlanesAndTasks() { $p = new Project($this->container); $pd = new ProjectDuplication($this->container); @@ -320,40 +449,39 @@ class ProjectDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); - $this->assertEquals(1, $p->create(array('name' => 'P1'))); + $this->assertEquals(1, $p->create(array('name' => 'P1', 'default_swimlane' => 'New Default'))); // create initial swimlanes $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'S1'))); $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'S2'))); $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'S3'))); - $default_swimlane1 = $s->getDefault(1); - $default_swimlane1['default_swimlane'] = 'New Default'; - - $this->assertTrue($s->updateDefault($default_swimlane1)); - - //create initial tasks + // create initial tasks $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'task'))); - $project = $p->getByName('P1 (Clone)'); - $this->assertNotFalse($project); - $project_id = $project['id']; + $this->assertEquals(2, $pd->duplicate(1, array('projectPermission', 'swimlane', 'task'))); + + // Check if Swimlanes have been duplicated + $swimlanes = $s->getAll(2); + $this->assertCount(3, $swimlanes); + $this->assertEquals(4, $swimlanes[0]['id']); + $this->assertEquals('S1', $swimlanes[0]['name']); + $this->assertEquals(5, $swimlanes[1]['id']); + $this->assertEquals('S2', $swimlanes[1]['name']); + $this->assertEquals(6, $swimlanes[2]['id']); + $this->assertEquals('S3', $swimlanes[2]['name']); - // Check if Swimlanes have NOT been duplicated - $this->assertCount(0, $s->getAll($project_id)); + $swimlane = $s->getDefault(2); + $this->assertEquals('New Default', $swimlane['default_swimlane']); // Check if Tasks have been duplicated - $tasks = $tf->getAll($project_id); + $tasks = $tf->getAll(2); $this->assertCount(3, $tasks); - //$this->assertEquals(4, $tasks[0]['id']); $this->assertEquals('T1', $tasks[0]['title']); - //$this->assertEquals(5, $tasks[1]['id']); $this->assertEquals('T2', $tasks[1]['title']); - //$this->assertEquals(6, $tasks[2]['id']); $this->assertEquals('T3', $tasks[2]['title']); } } diff --git a/tests/units/Model/ProjectFileTest.php b/tests/units/Model/ProjectFileTest.php new file mode 100644 index 00000000..d9b37fbe --- /dev/null +++ b/tests/units/Model/ProjectFileTest.php @@ -0,0 +1,311 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\ProjectFile; +use Kanboard\Model\Project; + +class ProjectFileTest extends Base +{ + public function testCreation() + { + $projectModel = new Project($this->container); + $fileModel = new ProjectFile($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10)); + + $file = $fileModel->getById(1); + $this->assertEquals('test', $file['name']); + $this->assertEquals('/tmp/foo', $file['path']); + $this->assertEquals(0, $file['is_image']); + $this->assertEquals(1, $file['project_id']); + $this->assertEquals(time(), $file['date'], '', 2); + $this->assertEquals(0, $file['user_id']); + $this->assertEquals(10, $file['size']); + + $this->assertEquals(2, $fileModel->create(1, 'test2.png', '/tmp/foobar', 10)); + + $file = $fileModel->getById(2); + $this->assertEquals('test2.png', $file['name']); + $this->assertEquals('/tmp/foobar', $file['path']); + $this->assertEquals(1, $file['is_image']); + } + + public function testCreationWithFileNameTooLong() + { + $projectModel = new Project($this->container); + $fileModel = new ProjectFile($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $this->assertNotFalse($fileModel->create(1, 'test', '/tmp/foo', 10)); + $this->assertNotFalse($fileModel->create(1, str_repeat('a', 1000), '/tmp/foo', 10)); + + $files = $fileModel->getAll(1); + $this->assertNotEmpty($files); + $this->assertCount(2, $files); + + $this->assertEquals(str_repeat('a', 255), $files[0]['name']); + $this->assertEquals('test', $files[1]['name']); + } + + public function testCreationWithSessionOpen() + { + $this->container['sessionStorage']->user = array('id' => 1); + + $projectModel = new Project($this->container); + $fileModel = new ProjectFile($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10)); + + $file = $fileModel->getById(1); + $this->assertEquals('test', $file['name']); + $this->assertEquals(1, $file['user_id']); + } + + public function testGetAll() + { + $projectModel = new Project($this->container); + $fileModel = new ProjectFile($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $this->assertEquals(1, $fileModel->create(1, 'B.pdf', '/tmp/foo', 10)); + $this->assertEquals(2, $fileModel->create(1, 'A.png', '/tmp/foo', 10)); + $this->assertEquals(3, $fileModel->create(1, 'D.doc', '/tmp/foo', 10)); + $this->assertEquals(4, $fileModel->create(1, 'C.JPG', '/tmp/foo', 10)); + + $fileModeliles = $fileModel->getAll(1); + $this->assertNotEmpty($fileModeliles); + $this->assertCount(4, $fileModeliles); + $this->assertEquals('A.png', $fileModeliles[0]['name']); + $this->assertEquals('B.pdf', $fileModeliles[1]['name']); + $this->assertEquals('C.JPG', $fileModeliles[2]['name']); + $this->assertEquals('D.doc', $fileModeliles[3]['name']); + + $fileModeliles = $fileModel->getAllImages(1); + $this->assertNotEmpty($fileModeliles); + $this->assertCount(2, $fileModeliles); + $this->assertEquals('A.png', $fileModeliles[0]['name']); + $this->assertEquals('C.JPG', $fileModeliles[1]['name']); + + $fileModeliles = $fileModel->getAllDocuments(1); + $this->assertNotEmpty($fileModeliles); + $this->assertCount(2, $fileModeliles); + $this->assertEquals('B.pdf', $fileModeliles[0]['name']); + $this->assertEquals('D.doc', $fileModeliles[1]['name']); + } + + public function testGetThumbnailPath() + { + $fileModel = new ProjectFile($this->container); + $this->assertEquals('thumbnails'.DIRECTORY_SEPARATOR.'test', $fileModel->getThumbnailPath('test')); + } + + public function testGeneratePath() + { + $fileModel = new ProjectFile($this->container); + + $this->assertStringStartsWith('projects'.DIRECTORY_SEPARATOR.'34'.DIRECTORY_SEPARATOR, $fileModel->generatePath(34, 'test.png')); + $this->assertNotEquals($fileModel->generatePath(34, 'test1.png'), $fileModel->generatePath(34, 'test2.png')); + } + + public function testUploadFiles() + { + $fileModel = $this + ->getMockBuilder('\Kanboard\Model\ProjectFile') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('generateThumbnailFromFile')) + ->getMock(); + + $projectModel = new Project($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $files = array( + 'name' => array( + 'file1.png', + 'file2.doc', + ), + 'tmp_name' => array( + '/tmp/phpYzdqkD', + '/tmp/phpeEwEWG', + ), + 'error' => array( + UPLOAD_ERR_OK, + UPLOAD_ERR_OK, + ), + 'size' => array( + 123, + 456, + ), + ); + + $fileModel + ->expects($this->once()) + ->method('generateThumbnailFromFile'); + + $this->container['objectStorage'] + ->expects($this->at(0)) + ->method('moveUploadedFile') + ->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything()); + + $this->container['objectStorage'] + ->expects($this->at(1)) + ->method('moveUploadedFile') + ->with($this->equalTo('/tmp/phpeEwEWG'), $this->anything()); + + $this->assertTrue($fileModel->uploadFiles(1, $files)); + + $files = $fileModel->getAll(1); + $this->assertCount(2, $files); + + $this->assertEquals(1, $files[0]['id']); + $this->assertEquals('file1.png', $files[0]['name']); + $this->assertEquals(1, $files[0]['is_image']); + $this->assertEquals(1, $files[0]['project_id']); + $this->assertEquals(0, $files[0]['user_id']); + $this->assertEquals(123, $files[0]['size']); + $this->assertEquals(time(), $files[0]['date'], '', 2); + + $this->assertEquals(2, $files[1]['id']); + $this->assertEquals('file2.doc', $files[1]['name']); + $this->assertEquals(0, $files[1]['is_image']); + $this->assertEquals(1, $files[1]['project_id']); + $this->assertEquals(0, $files[1]['user_id']); + $this->assertEquals(456, $files[1]['size']); + $this->assertEquals(time(), $files[1]['date'], '', 2); + } + + public function testUploadFilesWithEmptyFiles() + { + $fileModel = new ProjectFile($this->container); + $this->assertFalse($fileModel->uploadFiles(1, array())); + } + + public function testUploadFilesWithUploadError() + { + $files = array( + 'name' => array( + 'file1.png', + 'file2.doc', + ), + 'tmp_name' => array( + '', + '/tmp/phpeEwEWG', + ), + 'error' => array( + UPLOAD_ERR_CANT_WRITE, + UPLOAD_ERR_OK, + ), + 'size' => array( + 123, + 456, + ), + ); + + $fileModel = new ProjectFile($this->container); + $this->assertFalse($fileModel->uploadFiles(1, $files)); + } + + public function testUploadFilesWithObjectStorageError() + { + $files = array( + 'name' => array( + 'file1.csv', + 'file2.doc', + ), + 'tmp_name' => array( + '/tmp/phpYzdqkD', + '/tmp/phpeEwEWG', + ), + 'error' => array( + UPLOAD_ERR_OK, + UPLOAD_ERR_OK, + ), + 'size' => array( + 123, + 456, + ), + ); + + $this->container['objectStorage'] + ->expects($this->at(0)) + ->method('moveUploadedFile') + ->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything()) + ->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test'))); + + $fileModel = new ProjectFile($this->container); + $this->assertFalse($fileModel->uploadFiles(1, $files)); + } + + public function testUploadFileContent() + { + $fileModel = $this + ->getMockBuilder('\Kanboard\Model\ProjectFile') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('generateThumbnailFromFile')) + ->getMock(); + + $projectModel = new Project($this->container); + $data = 'test'; + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $this->container['objectStorage'] + ->expects($this->once()) + ->method('put') + ->with($this->anything(), $this->equalTo($data)); + + $this->assertEquals(1, $fileModel->uploadContent(1, 'test.doc', base64_encode($data))); + + $files = $fileModel->getAll(1); + $this->assertCount(1, $files); + + $this->assertEquals(1, $files[0]['id']); + $this->assertEquals('test.doc', $files[0]['name']); + $this->assertEquals(0, $files[0]['is_image']); + $this->assertEquals(1, $files[0]['project_id']); + $this->assertEquals(0, $files[0]['user_id']); + $this->assertEquals(4, $files[0]['size']); + $this->assertEquals(time(), $files[0]['date'], '', 2); + } + + public function testUploadImageContent() + { + $fileModel = $this + ->getMockBuilder('\Kanboard\Model\ProjectFile') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('generateThumbnailFromFile')) + ->getMock(); + + $projectModel = new Project($this->container); + $data = 'test'; + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $fileModel + ->expects($this->once()) + ->method('generateThumbnailFromFile'); + + $this->container['objectStorage'] + ->expects($this->once()) + ->method('put') + ->with($this->anything(), $this->equalTo($data)); + + $this->assertEquals(1, $fileModel->uploadContent(1, 'test.png', base64_encode($data))); + + $files = $fileModel->getAll(1); + $this->assertCount(1, $files); + + $this->assertEquals(1, $files[0]['id']); + $this->assertEquals('test.png', $files[0]['name']); + $this->assertEquals(1, $files[0]['is_image']); + $this->assertEquals(1, $files[0]['project_id']); + $this->assertEquals(0, $files[0]['user_id']); + $this->assertEquals(4, $files[0]['size']); + $this->assertEquals(time(), $files[0]['date'], '', 2); + } +} diff --git a/tests/units/Model/ProjectGroupRoleTest.php b/tests/units/Model/ProjectGroupRoleTest.php new file mode 100644 index 00000000..e38e812a --- /dev/null +++ b/tests/units/Model/ProjectGroupRoleTest.php @@ -0,0 +1,401 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Project; +use Kanboard\Model\User; +use Kanboard\Model\Group; +use Kanboard\Model\GroupMember; +use Kanboard\Model\ProjectGroupRole; +use Kanboard\Core\Security\Role; + +class ProjectGroupRoleTest extends Base +{ + public function testGetUserRole() + { + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 1)); + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + + $this->assertEquals(Role::PROJECT_VIEWER, $groupRoleModel->getUserRole(1, 1)); + $this->assertEquals('', $groupRoleModel->getUserRole(1, 2)); + } + + public function testAddGroup() + { + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $groupModel->create('Test')); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertFalse($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + + $groups = $groupRoleModel->getGroups(1); + $this->assertCount(1, $groups); + $this->assertEquals(1, $groups[0]['id']); + $this->assertEquals('Test', $groups[0]['name']); + $this->assertEquals(Role::PROJECT_VIEWER, $groups[0]['role']); + } + + public function testRemoveGroup() + { + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $groupModel->create('Test')); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->removeGroup(1, 1)); + $this->assertFalse($groupRoleModel->removeGroup(1, 1)); + + $this->assertEmpty($groupRoleModel->getGroups(1)); + } + + public function testChangeRole() + { + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $groupModel->create('Test')); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->changeGroupRole(1, 1, Role::PROJECT_MANAGER)); + + $groups = $groupRoleModel->getGroups(1); + $this->assertCount(1, $groups); + $this->assertEquals(1, $groups[0]['id']); + $this->assertEquals('Test', $groups[0]['name']); + $this->assertEquals(Role::PROJECT_MANAGER, $groups[0]['role']); + } + + public function testGetGroups() + { + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $groups = $groupRoleModel->getGroups(1); + $this->assertCount(3, $groups); + + $this->assertEquals(3, $groups[0]['id']); + $this->assertEquals('Group A', $groups[0]['name']); + $this->assertEquals(Role::PROJECT_MANAGER, $groups[0]['role']); + + $this->assertEquals(2, $groups[1]['id']); + $this->assertEquals('Group B', $groups[1]['name']); + $this->assertEquals(Role::PROJECT_MEMBER, $groups[1]['role']); + + $this->assertEquals(1, $groups[2]['id']); + $this->assertEquals('Group C', $groups[2]['name']); + $this->assertEquals(Role::PROJECT_VIEWER, $groups[2]['role']); + } + + public function testGetUsers() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + + $this->assertEquals(1, $groupModel->create('Group A')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group C')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $users = $groupRoleModel->getUsers(2); + $this->assertCount(0, $users); + + $users = $groupRoleModel->getUsers(1); + $this->assertCount(3, $users); + + $this->assertEquals(2, $users[0]['id']); + $this->assertEquals('user 1', $users[0]['username']); + $this->assertEquals('User #1', $users[0]['name']); + $this->assertEquals(Role::PROJECT_MANAGER, $users[0]['role']); + + $this->assertEquals(3, $users[1]['id']); + $this->assertEquals('user 2', $users[1]['username']); + $this->assertEquals('', $users[1]['name']); + $this->assertEquals(Role::PROJECT_MEMBER, $users[1]['role']); + + $this->assertEquals(4, $users[2]['id']); + $this->assertEquals('user 3', $users[2]['username']); + $this->assertEquals('', $users[2]['name']); + $this->assertEquals(Role::PROJECT_VIEWER, $users[2]['role']); + } + + public function testGetAssignableUsers() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + + $this->assertEquals(1, $groupModel->create('Group A')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group C')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $users = $groupRoleModel->getAssignableUsers(2); + $this->assertCount(0, $users); + + $users = $groupRoleModel->getAssignableUsers(1); + $this->assertCount(2, $users); + + $this->assertEquals(2, $users[0]['id']); + $this->assertEquals('user 1', $users[0]['username']); + $this->assertEquals('User #1', $users[0]['name']); + + $this->assertEquals(3, $users[1]['id']); + $this->assertEquals('user 2', $users[1]['username']); + $this->assertEquals('', $users[1]['name']); + } + + public function testGetAssignableUsersWithDisabledUsers() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2', 'is_active' => 0))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + + $this->assertEquals(1, $groupModel->create('Group A')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group C')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $users = $groupRoleModel->getAssignableUsers(2); + $this->assertCount(0, $users); + + $users = $groupRoleModel->getAssignableUsers(1); + $this->assertCount(1, $users); + + $this->assertEquals(2, $users[0]['id']); + $this->assertEquals('user 1', $users[0]['username']); + $this->assertEquals('User #1', $users[0]['name']); + } + + public function testGetProjectsByUser() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(2, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $projects = $groupRoleModel->getProjectsByUser(2); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $groupRoleModel->getProjectsByUser(3); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $groupRoleModel->getProjectsByUser(4); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $groupRoleModel->getProjectsByUser(5); + $this->assertCount(1, $projects); + $this->assertEquals('Project 2', $projects[2]); + } + + public function testGetActiveProjectsByUser() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1', 'is_active' => 0))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(2, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $projects = $groupRoleModel->getProjectsByUser(2, array(Project::ACTIVE)); + $this->assertCount(0, $projects); + + $projects = $groupRoleModel->getProjectsByUser(3, array(Project::ACTIVE)); + $this->assertCount(0, $projects); + + $projects = $groupRoleModel->getProjectsByUser(4, array(Project::ACTIVE)); + $this->assertCount(0, $projects); + + $projects = $groupRoleModel->getProjectsByUser(5, array(Project::ACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 2', $projects[2]); + } + + public function testGetInactiveProjectsByUser() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1', 'is_active' => 0))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(2, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $projects = $groupRoleModel->getProjectsByUser(2, array(Project::INACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $groupRoleModel->getProjectsByUser(3, array(Project::INACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $groupRoleModel->getProjectsByUser(4, array(Project::INACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $groupRoleModel->getProjectsByUser(5, array(Project::INACTIVE)); + $this->assertCount(0, $projects); + } + + public function testUserInMultipleGroupsShouldReturnHighestRole() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(2, $userModel->create(array('username' => 'My user'))); + + $this->assertEquals(1, $groupModel->create('Group A')); + $this->assertEquals(2, $groupModel->create('Group B')); + + $this->assertTrue($groupMemberModel->addUser(1, 1)); + $this->assertTrue($groupMemberModel->addUser(2, 1)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 2, Role::PROJECT_MANAGER)); + + $this->assertEquals(Role::PROJECT_MANAGER, $groupRoleModel->getUserRole(1, 1)); + } +} diff --git a/tests/units/Model/ProjectMetadataTest.php b/tests/units/Model/ProjectMetadataTest.php index 43d66b7b..5814987d 100644 --- a/tests/units/Model/ProjectMetadataTest.php +++ b/tests/units/Model/ProjectMetadataTest.php @@ -30,6 +30,11 @@ class ProjectMetadataTest extends Base $this->assertEquals(array('key1' => 'value2'), $pm->getAll(1)); $this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $pm->getAll(2)); + + $this->assertTrue($pm->remove(2, 'key1')); + $this->assertFalse($pm->remove(2, 'key1')); + + $this->assertEquals(array('key2' => 'value2'), $pm->getAll(2)); } public function testAutomaticRemove() diff --git a/tests/units/Model/ProjectPermissionTest.php b/tests/units/Model/ProjectPermissionTest.php index 1ee63a76..10fcdcc2 100644 --- a/tests/units/Model/ProjectPermissionTest.php +++ b/tests/units/Model/ProjectPermissionTest.php @@ -2,286 +2,317 @@ require_once __DIR__.'/../Base.php'; -use Kanboard\Model\Project; use Kanboard\Model\ProjectPermission; +use Kanboard\Model\Project; use Kanboard\Model\User; +use Kanboard\Model\Group; +use Kanboard\Model\GroupMember; +use Kanboard\Model\ProjectGroupRole; +use Kanboard\Model\ProjectUserRole; +use Kanboard\Core\Security\Role; class ProjectPermissionTest extends Base { - public function testAllowEverybody() + public function testFindByUsernames() { - $user = new User($this->container); - $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $this->assertNotFalse($user->create(array('username' => 'unittest#2', 'password' => 'unittest'))); - - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertFalse($pp->isEverybodyAllowed(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); - $this->assertFalse($pp->isUserAllowed(1, 3)); - $this->assertEquals(array(), $pp->getMembers(1)); - $this->assertEquals(array('Unassigned'), $pp->getMemberList(1)); - - $this->assertEmpty($pp->getMemberProjects(1)); - $this->assertEmpty($pp->getMemberProjects(2)); - $this->assertEmpty($pp->getMemberProjects(3)); - - $this->assertEmpty($pp->getMemberProjectIds(1)); - $this->assertEmpty($pp->getMemberProjectIds(2)); - $this->assertEmpty($pp->getMemberProjectIds(3)); - - $this->assertEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertEmpty($pp->getActiveMemberProjectIds(2)); - $this->assertEmpty($pp->getActiveMemberProjectIds(3)); - - $this->assertEmpty($pp->getActiveMemberProjects(1)); - $this->assertEmpty($pp->getActiveMemberProjects(2)); - $this->assertEmpty($pp->getActiveMemberProjects(3)); - - $this->assertTrue($p->update(array('id' => 1, 'is_everybody_allowed' => 1))); - $this->assertTrue($pp->isEverybodyAllowed(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - $this->assertTrue($pp->isUserAllowed(1, 3)); - $this->assertEquals(array('1' => 'admin', '2' => 'unittest#1', '3' => 'unittest#2'), $pp->getMembers(1)); - $this->assertEquals(array('Unassigned', '1' => 'admin', '2' => 'unittest#1', '3' => 'unittest#2'), $pp->getMemberList(1)); - - $this->assertNotEmpty($pp->getMemberProjects(1)); - $this->assertNotEmpty($pp->getMemberProjects(2)); - $this->assertNotEmpty($pp->getMemberProjects(3)); - - $this->assertNotEmpty($pp->getMemberProjectIds(1)); - $this->assertNotEmpty($pp->getMemberProjectIds(2)); - $this->assertNotEmpty($pp->getMemberProjectIds(3)); - - $this->assertNotEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertNotEmpty($pp->getActiveMemberProjectIds(2)); - $this->assertNotEmpty($pp->getActiveMemberProjectIds(3)); - - $this->assertNotEmpty($pp->getActiveMemberProjects(1)); - $this->assertNotEmpty($pp->getActiveMemberProjects(2)); - $this->assertNotEmpty($pp->getActiveMemberProjects(3)); - - $this->assertTrue($p->disable(1)); - - $this->assertEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertEmpty($pp->getActiveMemberProjectIds(2)); - $this->assertEmpty($pp->getActiveMemberProjectIds(3)); - - $this->assertEmpty($pp->getActiveMemberProjects(1)); - $this->assertEmpty($pp->getActiveMemberProjects(2)); - $this->assertEmpty($pp->getActiveMemberProjects(3)); - } + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermissionModel = new ProjectPermission($this->container); - public function testDisallowEverybody() - { - // We create a regular user - $user = new User($this->container); - $user->create(array('username' => 'unittest', 'password' => 'unittest')); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user3'))); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); + $this->assertEquals(1, $groupModel->create('Group A')); + $this->assertTrue($groupMemberModel->addUser(1, 2)); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MANAGER)); - $this->assertEmpty($pp->getMembers(1)); // Nobody is specified for the given project - $this->assertTrue($pp->isUserAllowed(1, 1)); // Admin should be allowed - $this->assertFalse($pp->isUserAllowed(1, 2)); // Regular user should be denied + $this->assertEquals(array('user1', 'user2'), $projectPermissionModel->findUsernames(1, 'us')); + $this->assertEmpty($projectPermissionModel->findUsernames(1, 'a')); + $this->assertEmpty($projectPermissionModel->findUsernames(2, 'user')); } - public function testAllowUser() + public function testGetQueryByRole() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $user = new User($this->container); - - $this->assertNotFalse($user->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - - $this->assertEmpty($pp->getMemberProjects(1)); - $this->assertEmpty($pp->getMemberProjects(2)); - - $this->assertEmpty($pp->getMemberProjectIds(1)); - $this->assertEmpty($pp->getMemberProjectIds(2)); - - $this->assertEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertEmpty($pp->getActiveMemberProjectIds(2)); - - $this->assertEmpty($pp->getActiveMemberProjects(1)); - $this->assertEmpty($pp->getActiveMemberProjects(2)); - - // We allow the admin user - $this->assertTrue($pp->addMember(1, 1)); - $this->assertTrue($pp->addMember(1, 2)); - - // Non-existant project - $this->assertFalse($pp->addMember(50, 1)); - - // Non-existant user - $this->assertFalse($pp->addMember(1, 50)); - - // Both users should be allowed - $this->assertEquals(array('1' => 'admin', '2' => 'unittest'), $pp->getMembers(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - - $this->assertNotEmpty($pp->getMemberProjects(1)); - $this->assertNotEmpty($pp->getMemberProjects(2)); - - $this->assertNotEmpty($pp->getMemberProjectIds(1)); - $this->assertNotEmpty($pp->getMemberProjectIds(2)); - - $this->assertNotEmpty($pp->getActiveMemberProjectIds(1)); - $this->assertNotEmpty($pp->getActiveMemberProjectIds(2)); - - $this->assertNotEmpty($pp->getActiveMemberProjects(1)); - $this->assertNotEmpty($pp->getActiveMemberProjects(2)); + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + $this->assertEquals(3, $projectModel->create(array('name' => 'Project 3'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + + $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(1, 4, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 5, Role::PROJECT_MEMBER)); + + $this->assertTrue($userRoleModel->addUser(2, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(2, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(2, 5, Role::PROJECT_MANAGER)); + + $this->assertTrue($userRoleModel->addUser(3, 4, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(3, 5, Role::PROJECT_VIEWER)); + + $this->assertEmpty($projectPermission->getQueryByRole(array(), Role::PROJECT_MANAGER)->findAll()); + + $users = $projectPermission->getQueryByRole(array(1, 2), Role::PROJECT_MANAGER)->findAll(); + $this->assertCount(3, $users); + $this->assertEquals('user 1', $users[0]['username']); + $this->assertEquals('Project 1', $users[0]['project_name']); + $this->assertEquals('user 2', $users[1]['username']); + $this->assertEquals('Project 1', $users[1]['project_name']); + $this->assertEquals('user 4', $users[2]['username']); + $this->assertEquals('Project 2', $users[2]['project_name']); + + $users = $projectPermission->getQueryByRole(array(1), Role::PROJECT_MANAGER)->findAll(); + $this->assertCount(2, $users); + $this->assertEquals('user 1', $users[0]['username']); + $this->assertEquals('Project 1', $users[0]['project_name']); + $this->assertEquals('user 2', $users[1]['username']); + $this->assertEquals('Project 1', $users[1]['project_name']); + + $users = $projectPermission->getQueryByRole(array(1, 2, 3), Role::PROJECT_MEMBER)->findAll(); + $this->assertCount(4, $users); + $this->assertEquals('user 3', $users[0]['username']); + $this->assertEquals('Project 1', $users[0]['project_name']); + $this->assertEquals('user 4', $users[1]['username']); + $this->assertEquals('Project 1', $users[1]['project_name']); + $this->assertEquals('user 1', $users[2]['username']); + $this->assertEquals('Project 2', $users[2]['project_name']); + $this->assertEquals('user 2', $users[3]['username']); + $this->assertEquals('Project 2', $users[3]['project_name']); + + $users = $projectPermission->getQueryByRole(array(1, 2, 3), Role::PROJECT_VIEWER)->findAll(); + $this->assertCount(1, $users); + $this->assertEquals('user 4', $users[0]['username']); + $this->assertEquals('Project 3', $users[0]['project_name']); } - public function testRevokeUser() + public function testEverybodyAllowed() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $user = new User($this->container); - - $user->create(array('username' => 'unittest', 'password' => 'unittest')); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $projectModel = new Project($this->container); + $projectPermission = new ProjectPermission($this->container); - // We revoke our admin user (not existing row) - $this->assertFalse($pp->revokeMember(1, 1)); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2', 'is_everybody_allowed' => 1))); - // We should have nobody in the users list - $this->assertEmpty($pp->getMembers(1)); - - // Only admin is allowed - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); - - // We allow only the regular user - $this->assertTrue($pp->addMember(1, 2)); - - // All users should be allowed (admin and regular) - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - - // However, we should have only our regular user in the list - $this->assertEquals(array('2' => 'unittest'), $pp->getMembers(1)); - - // We allow our admin, we should have both in the list - $this->assertTrue($pp->addMember(1, 1)); - $this->assertEquals(array('1' => 'admin', '2' => 'unittest'), $pp->getMembers(1)); - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - - // We revoke the regular user - $this->assertTrue($pp->revokeMember(1, 2)); - - // Only admin should be allowed - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); - - // We should have only admin in the list - $this->assertEquals(array('1' => 'admin'), $pp->getMembers(1)); - - // We revoke the admin user - $this->assertTrue($pp->revokeMember(1, 1)); - $this->assertEmpty($pp->getMembers(1)); - - // Only admin should be allowed again - $this->assertTrue($pp->isUserAllowed(1, 1)); - $this->assertFalse($pp->isUserAllowed(1, 2)); + $this->assertFalse($projectPermission->isEverybodyAllowed(1)); + $this->assertTrue($projectPermission->isEverybodyAllowed(2)); } - public function testManager() + public function testIsUserAllowed() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - - $this->assertEquals(2, $u->create(array('username' => 'unittest', 'password' => 'unittest'))); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertFalse($pp->isMember(1, 2)); - $this->assertFalse($pp->isManager(1, 2)); - - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'), 1, true)); - $this->assertFalse($pp->isMember(2, 2)); - $this->assertFalse($pp->isManager(2, 2)); - - $this->assertEquals(3, $p->create(array('name' => 'UnitTest3'), 2, true)); - $this->assertTrue($pp->isMember(3, 2)); - $this->assertTrue($pp->isManager(3, 2)); - - $this->assertEquals(4, $p->create(array('name' => 'UnitTest4'))); - - $this->assertTrue($pp->addManager(4, 2)); - $this->assertTrue($pp->isMember(4, 2)); - $this->assertTrue($pp->isManager(4, 2)); - - $this->assertEquals(5, $p->create(array('name' => 'UnitTest5'))); - $this->assertTrue($pp->addMember(5, 2)); - $this->assertTrue($pp->changeRole(5, 2, 1)); - $this->assertTrue($pp->isMember(5, 2)); - $this->assertTrue($pp->isManager(5, 2)); - $this->assertTrue($pp->changeRole(5, 2, 0)); - $this->assertTrue($pp->isMember(5, 2)); - $this->assertFalse($pp->isManager(5, 2)); + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(1, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 2)); + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 4, Role::PROJECT_MANAGER)); + + $this->assertTrue($projectPermission->isUserAllowed(1, 2)); + $this->assertTrue($projectPermission->isUserAllowed(1, 3)); + $this->assertTrue($projectPermission->isUserAllowed(1, 4)); + $this->assertFalse($projectPermission->isUserAllowed(1, 5)); + + $this->assertFalse($projectPermission->isUserAllowed(2, 2)); + $this->assertFalse($projectPermission->isUserAllowed(2, 3)); + $this->assertFalse($projectPermission->isUserAllowed(2, 4)); + $this->assertFalse($projectPermission->isUserAllowed(2, 5)); } - public function testUsersList() + public function testIsAssignable() { - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - - $user = new User($this->container); - $this->assertNotFalse($user->create(array('username' => 'unittest', 'password' => 'unittest'))); - - // We create project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - - // No restriction, we should have no body - $this->assertEquals( - array('Unassigned'), - $pp->getMemberList(1) - ); + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(1, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 2)); + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 4, Role::PROJECT_MANAGER)); + + $this->assertFalse($projectPermission->isAssignable(1, 2)); + $this->assertTrue($projectPermission->isAssignable(1, 3)); + $this->assertTrue($projectPermission->isAssignable(1, 4)); + $this->assertFalse($projectPermission->isAssignable(1, 5)); + + $this->assertFalse($projectPermission->isAssignable(2, 2)); + $this->assertFalse($projectPermission->isAssignable(2, 3)); + $this->assertFalse($projectPermission->isAssignable(2, 4)); + $this->assertFalse($projectPermission->isAssignable(2, 5)); + } - // We allow only the regular user - $this->assertTrue($pp->addMember(1, 2)); + public function testIsAssignableWhenUserIsDisabled() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); - $this->assertEquals( - array(0 => 'Unassigned', 2 => 'unittest'), - $pp->getMemberList(1) - ); + $this->assertEquals(2, $userModel->create(array('username' => 'user 1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2', 'is_active' => 0))); - // We allow the admin user - $this->assertTrue($pp->addMember(1, 1)); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); - $this->assertEquals( - array(0 => 'Unassigned', 1 => 'admin', 2 => 'unittest'), - $pp->getMemberList(1) - ); + $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); - // We revoke only the regular user - $this->assertTrue($pp->revokeMember(1, 2)); + $this->assertTrue($projectPermission->isAssignable(1, 2)); + $this->assertFalse($projectPermission->isAssignable(1, 3)); + } - $this->assertEquals( - array(0 => 'Unassigned', 1 => 'admin'), - $pp->getMemberList(1) - ); + public function testIsMember() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(1, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 2)); + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 4, Role::PROJECT_MANAGER)); + + $this->assertTrue($projectPermission->isMember(1, 2)); + $this->assertTrue($projectPermission->isMember(1, 3)); + $this->assertTrue($projectPermission->isMember(1, 4)); + $this->assertFalse($projectPermission->isMember(1, 5)); + + $this->assertFalse($projectPermission->isMember(2, 2)); + $this->assertFalse($projectPermission->isMember(2, 3)); + $this->assertFalse($projectPermission->isMember(2, 4)); + $this->assertFalse($projectPermission->isMember(2, 5)); + } - // We revoke only the admin user, we should have everybody - $this->assertTrue($pp->revokeMember(1, 1)); + public function testGetActiveProjectIds() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2', 'is_active' => 0))); + + $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(2, 2, Role::PROJECT_VIEWER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_VIEWER)); + + $this->assertEmpty($projectPermission->getActiveProjectIds(1)); + $this->assertEquals(array(1), $projectPermission->getActiveProjectIds(2)); + $this->assertEquals(array(1), $projectPermission->getActiveProjectIds(3)); + } - $this->assertEquals( - array(0 => 'Unassigned'), - $pp->getMemberList(1) - ); + public function testDuplicate() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $projectPermission = new ProjectPermission($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + $this->assertEquals(6, $userModel->create(array('username' => 'user 5', 'name' => 'User #5'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $this->assertTrue($userRoleModel->addUser(1, 5, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(1, 6, Role::PROJECT_MEMBER)); + + $this->assertTrue($projectPermission->duplicate(1, 2)); + + $this->assertCount(2, $userRoleModel->getUsers(2)); + $this->assertCount(3, $groupRoleModel->getUsers(2)); } } diff --git a/tests/units/Model/ProjectTest.php b/tests/units/Model/ProjectTest.php index f90c0dc1..5478fa40 100644 --- a/tests/units/Model/ProjectTest.php +++ b/tests/units/Model/ProjectTest.php @@ -5,12 +5,9 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Core\Translator; use Kanboard\Subscriber\ProjectModificationDateSubscriber; use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; use Kanboard\Model\User; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; -use Kanboard\Model\Acl; -use Kanboard\Model\Board; use Kanboard\Model\Config; use Kanboard\Model\Category; @@ -44,6 +41,14 @@ class ProjectTest extends Base $this->assertEmpty($project['token']); } + public function testCreationWithDuplicateName() + { + $p = new Project($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertEquals(2, $p->create(array('name' => 'UnitTest'))); + } + public function testCreationWithStartAndDate() { $p = new Project($this->container); @@ -79,6 +84,7 @@ class ProjectTest extends Base // Single category $this->assertTrue($c->save(array('project_categories' => 'Test1'))); + $this->container['memoryCache']->flush(); $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); $project = $p->getById(2); @@ -92,6 +98,7 @@ class ProjectTest extends Base // Multiple categories badly formatted $this->assertTrue($c->save(array('project_categories' => 'ABC, , DEF 3, '))); + $this->container['memoryCache']->flush(); $this->assertEquals(3, $p->create(array('name' => 'UnitTest3'))); $project = $p->getById(3); @@ -105,6 +112,7 @@ class ProjectTest extends Base // No default categories $this->assertTrue($c->save(array('project_categories' => ' '))); + $this->container['memoryCache']->flush(); $this->assertEquals(4, $p->create(array('name' => 'UnitTest4'))); $project = $p->getById(4); @@ -270,30 +278,49 @@ class ProjectTest extends Base $project = $p->getByIdentifier(''); $this->assertFalse($project); + } - // Validation rules - $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'TEST1')); - $this->assertFalse($r[0]); + public function testThatProjectCreatorAreAlsoOwner() + { + $projectModel = new Project($this->container); + $userModel = new User($this->container); - $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test1')); - $this->assertFalse($r[0]); + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'Me'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'My project 1'), 2)); + $this->assertEquals(2, $projectModel->create(array('name' => 'My project 2'))); - $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST1')); - $this->assertTrue($r[0]); + $project = $projectModel->getByIdWithOwner(1); + $this->assertNotEmpty($project); + $this->assertSame('My project 1', $project['name']); + $this->assertSame('Me', $project['owner_name']); + $this->assertSame('user1', $project['owner_username']); + $this->assertEquals(2, $project['owner_id']); - $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'test3')); - $this->assertTrue($r[0]); + $project = $projectModel->getByIdWithOwner(2); + $this->assertNotEmpty($project); + $this->assertSame('My project 2', $project['name']); + $this->assertEquals('', $project['owner_name']); + $this->assertEquals('', $project['owner_username']); + $this->assertEquals(0, $project['owner_id']); + } - $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => '')); - $this->assertTrue($r[0]); + public function testPriority() + { + $projectModel = new Project($this->container); + $this->assertEquals(1, $projectModel->create(array('name' => 'My project 2'))); - $r = $p->validateModification(array('id' => 1, 'name' => 'test', 'identifier' => 'TEST2')); - $this->assertFalse($r[0]); + $project = $projectModel->getById(1); + $this->assertNotEmpty($project); + $this->assertEquals(0, $project['priority_default']); + $this->assertEquals(0, $project['priority_start']); + $this->assertEquals(3, $project['priority_end']); - $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'a-b-c')); - $this->assertFalse($r[0]); + $this->assertTrue($projectModel->update(array('id' => 1, 'priority_start' => 2, 'priority_end' => 5, 'priority_default' => 4))); - $r = $p->validateCreation(array('name' => 'test', 'identifier' => 'test 123')); - $this->assertFalse($r[0]); + $project = $projectModel->getById(1); + $this->assertNotEmpty($project); + $this->assertEquals(4, $project['priority_default']); + $this->assertEquals(2, $project['priority_start']); + $this->assertEquals(5, $project['priority_end']); } } diff --git a/tests/units/Model/ProjectUserRoleTest.php b/tests/units/Model/ProjectUserRoleTest.php new file mode 100644 index 00000000..06cd1b70 --- /dev/null +++ b/tests/units/Model/ProjectUserRoleTest.php @@ -0,0 +1,461 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Project; +use Kanboard\Model\User; +use Kanboard\Model\Group; +use Kanboard\Model\GroupMember; +use Kanboard\Model\ProjectGroupRole; +use Kanboard\Model\ProjectUserRole; +use Kanboard\Model\ProjectPermission; +use Kanboard\Core\Security\Role; + +class ProjectUserRoleTest extends Base +{ + public function testAddUser() + { + $projectModel = new Project($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_VIEWER)); + $this->assertFalse($userRoleModel->addUser(1, 1, Role::PROJECT_VIEWER)); + + $users = $userRoleModel->getUsers(1); + $this->assertCount(1, $users); + $this->assertEquals(1, $users[0]['id']); + $this->assertEquals('admin', $users[0]['username']); + $this->assertEquals('', $users[0]['name']); + $this->assertEquals(Role::PROJECT_VIEWER, $users[0]['role']); + } + + public function testRemoveUser() + { + $projectModel = new Project($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->removeUser(1, 1)); + $this->assertFalse($userRoleModel->removeUser(1, 1)); + + $this->assertEmpty($userRoleModel->getUsers(1)); + } + + public function testChangeRole() + { + $projectModel = new Project($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($userRoleModel->changeUserRole(1, 1, Role::PROJECT_MANAGER)); + + $users = $userRoleModel->getUsers(1); + $this->assertCount(1, $users); + $this->assertEquals(1, $users[0]['id']); + $this->assertEquals('admin', $users[0]['username']); + $this->assertEquals('', $users[0]['name']); + $this->assertEquals(Role::PROJECT_MANAGER, $users[0]['role']); + } + + public function testGetRole() + { + $projectModel = new Project($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEmpty($userRoleModel->getUserRole(1, 1)); + + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_VIEWER)); + $this->assertEquals(Role::PROJECT_VIEWER, $userRoleModel->getUserRole(1, 1)); + + $this->assertTrue($userRoleModel->changeUserRole(1, 1, Role::PROJECT_MEMBER)); + $this->assertEquals(Role::PROJECT_MEMBER, $userRoleModel->getUserRole(1, 1)); + + $this->assertTrue($userRoleModel->changeUserRole(1, 1, Role::PROJECT_MANAGER)); + $this->assertEquals(Role::PROJECT_MANAGER, $userRoleModel->getUserRole(1, 1)); + + $this->assertEquals('', $userRoleModel->getUserRole(1, 2)); + } + + public function testGetRoleWithGroups() + { + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 1)); + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + + $this->assertEquals(Role::PROJECT_VIEWER, $userRoleModel->getUserRole(1, 1)); + $this->assertEquals('', $userRoleModel->getUserRole(1, 2)); + } + + public function testGetAssignableUsersWithDisabledUsers() + { + $projectModel = new Project($this->container); + $userModel = new User($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User2'))); + + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + + $users = $userRoleModel->getAssignableUsers(1); + $this->assertCount(3, $users); + + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User1', $users[2]); + $this->assertEquals('User2', $users[3]); + + $this->assertTrue($userModel->disable(2)); + + $users = $userRoleModel->getAssignableUsers(1); + $this->assertCount(2, $users); + + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User2', $users[3]); + } + + public function testGetAssignableUsersWithoutGroups() + { + $projectModel = new Project($this->container); + $userModel = new User($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User2'))); + + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_VIEWER)); + + $users = $userRoleModel->getAssignableUsers(1); + $this->assertCount(2, $users); + + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User1', $users[2]); + } + + public function testGetAssignableUsersWithGroups() + { + $projectModel = new Project($this->container); + $userModel = new User($this->container); + $groupModel = new Group($this->container); + $userRoleModel = new ProjectUserRole($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $groupMemberModel = new GroupMember($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user3', 'name' => 'User3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user4', 'name' => 'User4'))); + + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_VIEWER)); + + $this->assertEquals(1, $groupModel->create('Group A')); + $this->assertEquals(2, $groupModel->create('Group B')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(1, 2, Role::PROJECT_MEMBER)); + + $users = $userRoleModel->getAssignableUsers(1); + $this->assertCount(3, $users); + + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User1', $users[2]); + $this->assertEquals('User4', $users[5]); + } + + public function testGetAssignableUsersList() + { + $projectModel = new Project($this->container); + $userModel = new User($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Test2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User2'))); + + $this->assertTrue($userRoleModel->addUser(2, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_VIEWER)); + + $users = $userRoleModel->getAssignableUsersList(1); + $this->assertCount(3, $users); + + $this->assertEquals('Unassigned', $users[0]); + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User1', $users[2]); + + $users = $userRoleModel->getAssignableUsersList(1, true, true, true); + $this->assertCount(4, $users); + + $this->assertEquals('Unassigned', $users[0]); + $this->assertEquals('Everybody', $users[-1]); + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User1', $users[2]); + + $users = $userRoleModel->getAssignableUsersList(2, true, true, true); + $this->assertCount(1, $users); + + $this->assertEquals('admin', $users[1]); + } + + public function testGetAssignableUsersWithEverybodyAllowed() + { + $projectModel = new Project($this->container); + $userModel = new User($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test', 'is_everybody_allowed' => 1))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user3', 'name' => 'User3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user4', 'name' => 'User4'))); + + $users = $userRoleModel->getAssignableUsers(1); + $this->assertCount(5, $users); + + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User1', $users[2]); + $this->assertEquals('User2', $users[3]); + $this->assertEquals('User3', $users[4]); + $this->assertEquals('User4', $users[5]); + } + + public function testGetAssignableUsersWithDisabledUsersAndEverybodyAllowed() + { + $projectModel = new Project($this->container); + $projectPermission = new ProjectPermission($this->container); + $userModel = new User($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User2'))); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1', 'is_everybody_allowed' => 1))); + + $this->assertTrue($projectPermission->isEverybodyAllowed(1)); + + $users = $userRoleModel->getAssignableUsers(1); + $this->assertCount(3, $users); + + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User1', $users[2]); + $this->assertEquals('User2', $users[3]); + + $this->assertTrue($userModel->disable(2)); + + $users = $userRoleModel->getAssignableUsers(1); + $this->assertCount(2, $users); + + $this->assertEquals('admin', $users[1]); + $this->assertEquals('User2', $users[3]); + } + + public function testGetProjectsByUser() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + $this->assertEquals(6, $userModel->create(array('username' => 'user 5', 'name' => 'User #5'))); + $this->assertEquals(7, $userModel->create(array('username' => 'user 6'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(2, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $this->assertTrue($userRoleModel->addUser(1, 6, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(2, 6, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(2, 7, Role::PROJECT_MEMBER)); + + $projects = $userRoleModel->getProjectsByUser(2); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $userRoleModel->getProjectsByUser(3); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $userRoleModel->getProjectsByUser(4); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $userRoleModel->getProjectsByUser(5); + $this->assertCount(1, $projects); + $this->assertEquals('Project 2', $projects[2]); + + $projects = $userRoleModel->getProjectsByUser(6); + $this->assertCount(2, $projects); + $this->assertEquals('Project 1', $projects[1]); + $this->assertEquals('Project 2', $projects[2]); + + $projects = $userRoleModel->getProjectsByUser(7); + $this->assertCount(1, $projects); + $this->assertEquals('Project 2', $projects[2]); + } + + public function testGetActiveProjectsByUser() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1', 'is_active' => 0))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + $this->assertEquals(6, $userModel->create(array('username' => 'user 5', 'name' => 'User #5'))); + $this->assertEquals(7, $userModel->create(array('username' => 'user 6'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(2, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $this->assertTrue($userRoleModel->addUser(1, 6, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(2, 6, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(2, 7, Role::PROJECT_MEMBER)); + + $projects = $userRoleModel->getProjectsByUser(2, array(Project::ACTIVE)); + $this->assertCount(0, $projects); + + $projects = $userRoleModel->getProjectsByUser(3, array(Project::ACTIVE)); + $this->assertCount(0, $projects); + + $projects = $userRoleModel->getProjectsByUser(4, array(Project::ACTIVE)); + $this->assertCount(0, $projects); + + $projects = $userRoleModel->getProjectsByUser(5, array(Project::ACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 2', $projects[2]); + + $projects = $userRoleModel->getProjectsByUser(6, array(Project::ACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 2', $projects[2]); + + $projects = $userRoleModel->getProjectsByUser(7, array(Project::ACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 2', $projects[2]); + } + + public function testGetInactiveProjectsByUser() + { + $userModel = new User($this->container); + $projectModel = new Project($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $groupRoleModel = new ProjectGroupRole($this->container); + $userRoleModel = new ProjectUserRole($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1', 'is_active' => 0))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2'))); + + $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user 2'))); + $this->assertEquals(4, $userModel->create(array('username' => 'user 3'))); + $this->assertEquals(5, $userModel->create(array('username' => 'user 4'))); + $this->assertEquals(6, $userModel->create(array('username' => 'user 5', 'name' => 'User #5'))); + $this->assertEquals(7, $userModel->create(array('username' => 'user 6'))); + + $this->assertEquals(1, $groupModel->create('Group C')); + $this->assertEquals(2, $groupModel->create('Group B')); + $this->assertEquals(3, $groupModel->create('Group A')); + + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 5)); + $this->assertTrue($groupMemberModel->addUser(3, 3)); + $this->assertTrue($groupMemberModel->addUser(3, 2)); + + $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER)); + $this->assertTrue($groupRoleModel->addGroup(2, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER)); + + $this->assertTrue($userRoleModel->addUser(1, 6, Role::PROJECT_MANAGER)); + $this->assertTrue($userRoleModel->addUser(2, 6, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(2, 7, Role::PROJECT_MEMBER)); + + $projects = $userRoleModel->getProjectsByUser(2, array(Project::INACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $userRoleModel->getProjectsByUser(3, array(Project::INACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $userRoleModel->getProjectsByUser(4, array(Project::INACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $userRoleModel->getProjectsByUser(5, array(Project::INACTIVE)); + $this->assertCount(0, $projects); + + $projects = $userRoleModel->getProjectsByUser(6, array(Project::INACTIVE)); + $this->assertCount(1, $projects); + $this->assertEquals('Project 1', $projects[1]); + + $projects = $userRoleModel->getProjectsByUser(7, array(Project::INACTIVE)); + $this->assertCount(0, $projects); + } +} diff --git a/tests/units/Model/SubtaskTest.php b/tests/units/Model/SubtaskTest.php index 04b274cc..78945fd1 100644 --- a/tests/units/Model/SubtaskTest.php +++ b/tests/units/Model/SubtaskTest.php @@ -8,8 +8,7 @@ use Kanboard\Model\Subtask; use Kanboard\Model\Project; use Kanboard\Model\Category; use Kanboard\Model\User; -use Kanboard\Core\Session; -use Kanboard\Model\UserSession; +use Kanboard\Core\User\UserSession; class SubtaskTest extends Base { @@ -160,7 +159,7 @@ class SubtaskTest extends Base $this->assertEquals(0, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_INPROGRESS, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -168,7 +167,7 @@ class SubtaskTest extends Base $this->assertEquals(0, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_DONE, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -176,7 +175,7 @@ class SubtaskTest extends Base $this->assertEquals(0, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_TODO, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -190,7 +189,6 @@ class SubtaskTest extends Base $tc = new TaskCreation($this->container); $s = new Subtask($this->container); $p = new Project($this->container); - $ss = new Session; $us = new UserSession($this->container); $this->assertEquals(1, $p->create(array('name' => 'test1'))); @@ -205,9 +203,9 @@ class SubtaskTest extends Base $this->assertEquals(1, $subtask['task_id']); // Set the current logged user - $ss['user'] = array('id' => 1); + $this->container['sessionStorage']->user = array('id' => 1); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_INPROGRESS, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -215,7 +213,7 @@ class SubtaskTest extends Base $this->assertEquals(1, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_DONE, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -223,7 +221,7 @@ class SubtaskTest extends Base $this->assertEquals(1, $subtask['user_id']); $this->assertEquals(1, $subtask['task_id']); - $this->assertTrue($s->toggleStatus(1)); + $this->assertEquals(Subtask::STATUS_TODO, $s->toggleStatus(1)); $subtask = $s->getById(1); $this->assertNotEmpty($subtask); @@ -254,117 +252,6 @@ class SubtaskTest extends Base } } - public function testMoveUp() - { - $tc = new TaskCreation($this->container); - $s = new Subtask($this->container); - $p = new Project($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); - - $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1))); - $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1))); - $this->assertEquals(3, $s->create(array('title' => 'subtask #3', 'task_id' => 1))); - - // Check positions - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(1, $subtask['position']); - - $subtask = $s->getById(2); - $this->assertNotEmpty($subtask); - $this->assertEquals(2, $subtask['position']); - - $subtask = $s->getById(3); - $this->assertNotEmpty($subtask); - $this->assertEquals(3, $subtask['position']); - - // Move up - $this->assertTrue($s->moveUp(1, 2)); - - // Check positions - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(2, $subtask['position']); - - $subtask = $s->getById(2); - $this->assertNotEmpty($subtask); - $this->assertEquals(1, $subtask['position']); - - $subtask = $s->getById(3); - $this->assertNotEmpty($subtask); - $this->assertEquals(3, $subtask['position']); - - // We can't move up #2 - $this->assertFalse($s->moveUp(1, 2)); - - // Test remove - $this->assertTrue($s->remove(1)); - $this->assertTrue($s->moveUp(1, 3)); - - // Check positions - $subtask = $s->getById(1); - $this->assertEmpty($subtask); - - $subtask = $s->getById(2); - $this->assertNotEmpty($subtask); - $this->assertEquals(2, $subtask['position']); - - $subtask = $s->getById(3); - $this->assertNotEmpty($subtask); - $this->assertEquals(1, $subtask['position']); - } - - public function testMoveDown() - { - $tc = new TaskCreation($this->container); - $s = new Subtask($this->container); - $p = new Project($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); - - $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1))); - $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1))); - $this->assertEquals(3, $s->create(array('title' => 'subtask #3', 'task_id' => 1))); - - // Move down #1 - $this->assertTrue($s->moveDown(1, 1)); - - // Check positions - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(2, $subtask['position']); - - $subtask = $s->getById(2); - $this->assertNotEmpty($subtask); - $this->assertEquals(1, $subtask['position']); - - $subtask = $s->getById(3); - $this->assertNotEmpty($subtask); - $this->assertEquals(3, $subtask['position']); - - // We can't move down #3 - $this->assertFalse($s->moveDown(1, 3)); - - // Test remove - $this->assertTrue($s->remove(1)); - $this->assertTrue($s->moveDown(1, 2)); - - // Check positions - $subtask = $s->getById(1); - $this->assertEmpty($subtask); - - $subtask = $s->getById(2); - $this->assertNotEmpty($subtask); - $this->assertEquals(2, $subtask['position']); - - $subtask = $s->getById(3); - $this->assertNotEmpty($subtask); - $this->assertEquals(1, $subtask['position']); - } - public function testDuplicate() { $tc = new TaskCreation($this->container); @@ -411,4 +298,69 @@ class SubtaskTest extends Base $this->assertEquals(1, $subtasks[0]['position']); $this->assertEquals(2, $subtasks[1]['position']); } + + public function testChangePosition() + { + $taskCreationModel = new TaskCreation($this->container); + $subtaskModel = new Subtask($this->container); + $projectModel = new Project($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1))); + + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1))); + $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1))); + $this->assertEquals(3, $subtaskModel->create(array('title' => 'subtask #3', 'task_id' => 1))); + + $subtasks = $subtaskModel->getAll(1); + $this->assertEquals(1, $subtasks[0]['position']); + $this->assertEquals(1, $subtasks[0]['id']); + $this->assertEquals(2, $subtasks[1]['position']); + $this->assertEquals(2, $subtasks[1]['id']); + $this->assertEquals(3, $subtasks[2]['position']); + $this->assertEquals(3, $subtasks[2]['id']); + + $this->assertTrue($subtaskModel->changePosition(1, 3, 2)); + + $subtasks = $subtaskModel->getAll(1); + $this->assertEquals(1, $subtasks[0]['position']); + $this->assertEquals(1, $subtasks[0]['id']); + $this->assertEquals(2, $subtasks[1]['position']); + $this->assertEquals(3, $subtasks[1]['id']); + $this->assertEquals(3, $subtasks[2]['position']); + $this->assertEquals(2, $subtasks[2]['id']); + + $this->assertTrue($subtaskModel->changePosition(1, 2, 1)); + + $subtasks = $subtaskModel->getAll(1); + $this->assertEquals(1, $subtasks[0]['position']); + $this->assertEquals(2, $subtasks[0]['id']); + $this->assertEquals(2, $subtasks[1]['position']); + $this->assertEquals(1, $subtasks[1]['id']); + $this->assertEquals(3, $subtasks[2]['position']); + $this->assertEquals(3, $subtasks[2]['id']); + + $this->assertTrue($subtaskModel->changePosition(1, 2, 2)); + + $subtasks = $subtaskModel->getAll(1); + $this->assertEquals(1, $subtasks[0]['position']); + $this->assertEquals(1, $subtasks[0]['id']); + $this->assertEquals(2, $subtasks[1]['position']); + $this->assertEquals(2, $subtasks[1]['id']); + $this->assertEquals(3, $subtasks[2]['position']); + $this->assertEquals(3, $subtasks[2]['id']); + + $this->assertTrue($subtaskModel->changePosition(1, 1, 3)); + + $subtasks = $subtaskModel->getAll(1); + $this->assertEquals(1, $subtasks[0]['position']); + $this->assertEquals(2, $subtasks[0]['id']); + $this->assertEquals(2, $subtasks[1]['position']); + $this->assertEquals(3, $subtasks[1]['id']); + $this->assertEquals(3, $subtasks[2]['position']); + $this->assertEquals(1, $subtasks[2]['id']); + + $this->assertFalse($subtaskModel->changePosition(1, 2, 0)); + $this->assertFalse($subtaskModel->changePosition(1, 2, 4)); + } } diff --git a/tests/units/Model/SubtaskTimeTrackingTest.php b/tests/units/Model/SubtaskTimeTrackingTest.php index 309be64a..40461eea 100644 --- a/tests/units/Model/SubtaskTimeTrackingTest.php +++ b/tests/units/Model/SubtaskTimeTrackingTest.php @@ -9,7 +9,6 @@ use Kanboard\Model\SubtaskTimeTracking; use Kanboard\Model\Project; use Kanboard\Model\Category; use Kanboard\Model\User; -use Kanboard\Core\Session; class SubtaskTimeTrackingTest extends Base { @@ -38,9 +37,8 @@ class SubtaskTimeTrackingTest extends Base $s = new Subtask($this->container); $st = new SubtaskTimeTracking($this->container); $p = new Project($this->container); - $ss = new Session; - $ss['user'] = array('id' => 1); + $this->container['sessionStorage']->user = array('id' => 1); $this->assertEquals(1, $p->create(array('name' => 'test1'))); $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); diff --git a/tests/units/Model/SwimlaneTest.php b/tests/units/Model/SwimlaneTest.php index 3d048abd..f8b496cf 100644 --- a/tests/units/Model/SwimlaneTest.php +++ b/tests/units/Model/SwimlaneTest.php @@ -86,7 +86,23 @@ class SwimlaneTest extends Base $this->assertEquals(0, $default['show_default_swimlane']); } - public function testDisable() + public function testDisableEnableDefaultSwimlane() + { + $projectModel = new Project($this->container); + $swimlaneModel = new Swimlane($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->assertTrue($swimlaneModel->disableDefault(1)); + $default = $swimlaneModel->getDefault(1); + $this->assertEquals(0, $default['show_default_swimlane']); + + $this->assertTrue($swimlaneModel->enableDefault(1)); + $default = $swimlaneModel->getDefault(1); + $this->assertEquals(1, $default['show_default_swimlane']); + } + + public function testDisableEnable() { $p = new Project($this->container); $s = new Swimlane($this->container); @@ -210,172 +226,6 @@ class SwimlaneTest extends Base $this->assertEquals(1, $swimlane['position']); } - public function testMoveUp() - { - $p = new Project($this->container); - $s = new Swimlane($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); - $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'Swimlane #2'))); - $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'Swimlane #3'))); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(3, $swimlane['position']); - - // Move the swimlane 3 up - $this->assertTrue($s->moveUp(1, 3)); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(3, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - - // First swimlane can be moved up - $this->assertFalse($s->moveUp(1, 1)); - - // Move with a disabled swimlane - $this->assertTrue($s->disable(1, 1)); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(0, $swimlane['is_active']); - $this->assertEquals(0, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - // Move the 2nd swimlane up - $this->assertTrue($s->moveUp(1, 2)); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(0, $swimlane['is_active']); - $this->assertEquals(0, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - } - - public function testMoveDown() - { - $p = new Project($this->container); - $s = new Swimlane($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); - $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'Swimlane #2'))); - $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'Swimlane #3'))); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(3, $swimlane['position']); - - // Move the swimlane 1 down - $this->assertTrue($s->moveDown(1, 1)); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(3, $swimlane['position']); - - // Last swimlane can be moved down - $this->assertFalse($s->moveDown(1, 3)); - - // Move with a disabled swimlane - $this->assertTrue($s->disable(1, 3)); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(0, $swimlane['is_active']); - $this->assertEquals(0, $swimlane['position']); - - // Move the 2st swimlane down - $this->assertTrue($s->moveDown(1, 2)); - - $swimlane = $s->getById(1); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(1, $swimlane['position']); - - $swimlane = $s->getById(2); - $this->assertNotEmpty($swimlane); - $this->assertEquals(1, $swimlane['is_active']); - $this->assertEquals(2, $swimlane['position']); - - $swimlane = $s->getById(3); - $this->assertNotEmpty($swimlane); - $this->assertEquals(0, $swimlane['is_active']); - $this->assertEquals(0, $swimlane['position']); - } - public function testDuplicateSwimlane() { $p = new Project($this->container); @@ -406,4 +256,93 @@ class SwimlaneTest extends Base $new_default = $s->getDefault(2); $this->assertEquals('New Default', $new_default['default_swimlane']); } + + public function testChangePosition() + { + $projectModel = new Project($this->container); + $swimlaneModel = new Swimlane($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); + $this->assertEquals(2, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #2'))); + $this->assertEquals(3, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #3'))); + $this->assertEquals(4, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #4'))); + + $swimlanes = $swimlaneModel->getAllByStatus(1); + $this->assertEquals(1, $swimlanes[0]['position']); + $this->assertEquals(1, $swimlanes[0]['id']); + $this->assertEquals(2, $swimlanes[1]['position']); + $this->assertEquals(2, $swimlanes[1]['id']); + $this->assertEquals(3, $swimlanes[2]['position']); + $this->assertEquals(3, $swimlanes[2]['id']); + + $this->assertTrue($swimlaneModel->changePosition(1, 3, 2)); + + $swimlanes = $swimlaneModel->getAllByStatus(1); + $this->assertEquals(1, $swimlanes[0]['position']); + $this->assertEquals(1, $swimlanes[0]['id']); + $this->assertEquals(2, $swimlanes[1]['position']); + $this->assertEquals(3, $swimlanes[1]['id']); + $this->assertEquals(3, $swimlanes[2]['position']); + $this->assertEquals(2, $swimlanes[2]['id']); + + $this->assertTrue($swimlaneModel->changePosition(1, 2, 1)); + + $swimlanes = $swimlaneModel->getAllByStatus(1); + $this->assertEquals(1, $swimlanes[0]['position']); + $this->assertEquals(2, $swimlanes[0]['id']); + $this->assertEquals(2, $swimlanes[1]['position']); + $this->assertEquals(1, $swimlanes[1]['id']); + $this->assertEquals(3, $swimlanes[2]['position']); + $this->assertEquals(3, $swimlanes[2]['id']); + + $this->assertTrue($swimlaneModel->changePosition(1, 2, 2)); + + $swimlanes = $swimlaneModel->getAllByStatus(1); + $this->assertEquals(1, $swimlanes[0]['position']); + $this->assertEquals(1, $swimlanes[0]['id']); + $this->assertEquals(2, $swimlanes[1]['position']); + $this->assertEquals(2, $swimlanes[1]['id']); + $this->assertEquals(3, $swimlanes[2]['position']); + $this->assertEquals(3, $swimlanes[2]['id']); + + $this->assertTrue($swimlaneModel->changePosition(1, 4, 1)); + + $swimlanes = $swimlaneModel->getAllByStatus(1); + $this->assertEquals(1, $swimlanes[0]['position']); + $this->assertEquals(4, $swimlanes[0]['id']); + $this->assertEquals(2, $swimlanes[1]['position']); + $this->assertEquals(1, $swimlanes[1]['id']); + $this->assertEquals(3, $swimlanes[2]['position']); + $this->assertEquals(2, $swimlanes[2]['id']); + + $this->assertFalse($swimlaneModel->changePosition(1, 2, 0)); + $this->assertFalse($swimlaneModel->changePosition(1, 2, 5)); + } + + public function testChangePositionWithInactiveSwimlane() + { + $projectModel = new Project($this->container); + $swimlaneModel = new Swimlane($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); + $this->assertEquals(2, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #2', 'is_active' => 0))); + $this->assertEquals(3, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #3', 'is_active' => 0))); + $this->assertEquals(4, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #4'))); + + $swimlanes = $swimlaneModel->getAllByStatus(1); + $this->assertEquals(1, $swimlanes[0]['position']); + $this->assertEquals(1, $swimlanes[0]['id']); + $this->assertEquals(2, $swimlanes[1]['position']); + $this->assertEquals(4, $swimlanes[1]['id']); + + $this->assertTrue($swimlaneModel->changePosition(1, 4, 1)); + + $swimlanes = $swimlaneModel->getAllByStatus(1); + $this->assertEquals(1, $swimlanes[0]['position']); + $this->assertEquals(4, $swimlanes[0]['id']); + $this->assertEquals(2, $swimlanes[1]['position']); + $this->assertEquals(1, $swimlanes[1]['id']); + } } diff --git a/tests/units/Model/TaskCreationTest.php b/tests/units/Model/TaskCreationTest.php index d76937b2..781a7147 100644 --- a/tests/units/Model/TaskCreationTest.php +++ b/tests/units/Model/TaskCreationTest.php @@ -8,7 +8,6 @@ use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; use Kanboard\Model\TaskStatus; use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; class TaskCreationTest extends Base { @@ -182,8 +181,7 @@ class TaskCreationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); - $_SESSION = array(); - $_SESSION['user']['id'] = 1; + $this->container['sessionStorage']->user = array('id' => 1); $this->assertEquals(1, $p->create(array('name' => 'test'))); $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); @@ -194,8 +192,6 @@ class TaskCreationTest extends Base $this->assertEquals(1, $task['id']); $this->assertEquals(1, $task['creator_id']); - - $_SESSION = array(); } public function testColumnId() @@ -299,7 +295,7 @@ class TaskCreationTest extends Base $task = $tf->getById(2); $this->assertNotEmpty($task); $this->assertEquals(2, $task['id']); - $this->assertEquals($timestamp, $task['date_due']); + $this->assertEquals(date('Y-m-d 00:00', $timestamp), date('Y-m-d 00:00', $task['date_due'])); $task = $tf->getById(3); $this->assertEquals(3, $task['id']); @@ -406,6 +402,7 @@ class TaskCreationTest extends Base $this->assertEquals('yellow', $task['color_id']); $this->assertTrue($c->save(array('default_color' => 'orange'))); + $this->container['memoryCache']->flush(); $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test2'))); @@ -425,6 +422,6 @@ class TaskCreationTest extends Base $task = $tf->getById(1); $this->assertNotEmpty($task); - $this->assertEquals('2050-01-10 12:30', date('Y-m-d H:i', $task['date_due'])); + $this->assertEquals('2050-01-10 00:00', date('Y-m-d H:i', $task['date_due'])); } } diff --git a/tests/units/Model/TaskDuplicationTest.php b/tests/units/Model/TaskDuplicationTest.php index 5273928c..8649c6b0 100644 --- a/tests/units/Model/TaskDuplicationTest.php +++ b/tests/units/Model/TaskDuplicationTest.php @@ -2,16 +2,18 @@ require_once __DIR__.'/../Base.php'; +use Kanboard\Core\DateParser; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskDuplication; use Kanboard\Model\TaskFinder; use Kanboard\Model\TaskStatus; use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; +use Kanboard\Model\ProjectUserRole; use Kanboard\Model\Category; use Kanboard\Model\User; use Kanboard\Model\Swimlane; +use Kanboard\Core\Security\Role; class TaskDuplicationTest extends Base { @@ -31,8 +33,7 @@ class TaskDuplicationTest extends Base $this->assertEquals(1, $task['project_id']); $this->assertEquals(0, $task['creator_id']); - $_SESSION = array(); - $_SESSION['user']['id'] = 1; + $this->container['sessionStorage']->user = array('id' => 1); // We duplicate our task $this->assertEquals(2, $td->duplicate(1)); @@ -41,8 +42,6 @@ class TaskDuplicationTest extends Base $task = $tf->getById(2); $this->assertNotEmpty($task); $this->assertEquals(1, $task['creator_id']); - - $_SESSION = array(); } public function testDuplicateSameProject() @@ -130,7 +129,7 @@ class TaskDuplicationTest extends Base // Check the values of the duplicated task $task = $tf->getById(2); $this->assertNotEmpty($task); - $this->assertEquals(1, $task['owner_id']); + $this->assertEquals(0, $task['owner_id']); $this->assertEquals(0, $task['category_id']); $this->assertEquals(0, $task['swimlane_id']); $this->assertEquals(6, $task['column_id']); @@ -336,7 +335,7 @@ class TaskDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); $p = new Project($this->container); - $pp = new ProjectPermission($this->container); + $pp = new ProjectUserRole($this->container); // We create 2 projects $this->assertEquals(1, $p->create(array('name' => 'test1'))); @@ -360,10 +359,8 @@ class TaskDuplicationTest extends Base // We create a new user for our project $user = new User($this->container); $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->addMember(2, 2)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - $this->assertTrue($pp->isUserAllowed(2, 2)); + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($pp->addUser(2, 2, Role::PROJECT_MEMBER)); // We duplicate our task to the 2nd project $this->assertEquals(3, $td->duplicateToProject(1, 2)); @@ -394,7 +391,7 @@ class TaskDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); $p = new Project($this->container); - $pp = new ProjectPermission($this->container); + $pr = new ProjectUserRole($this->container); // We create 2 projects $this->assertEquals(1, $p->create(array('name' => 'test1'))); @@ -402,6 +399,7 @@ class TaskDuplicationTest extends Base // We create a task $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2))); + $this->assertTrue($pr->addUser(2, 1, Role::PROJECT_MEMBER)); // We duplicate our task to the 2nd project $this->assertEquals(2, $td->duplicateToProject(1, 2, null, null, null, 1)); @@ -428,7 +426,6 @@ class TaskDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); $p = new Project($this->container); - $pp = new ProjectPermission($this->container); $user = new User($this->container); // We create 2 projects @@ -449,7 +446,7 @@ class TaskDuplicationTest extends Base // Check the values of the moved task $task = $tf->getById(1); $this->assertNotEmpty($task); - $this->assertEquals(1, $task['owner_id']); + $this->assertEquals(0, $task['owner_id']); $this->assertEquals(0, $task['category_id']); $this->assertEquals(0, $task['swimlane_id']); $this->assertEquals(2, $task['project_id']); @@ -499,7 +496,7 @@ class TaskDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); $p = new Project($this->container); - $pp = new ProjectPermission($this->container); + $pp = new ProjectUserRole($this->container); $user = new User($this->container); // We create 2 projects @@ -508,10 +505,8 @@ class TaskDuplicationTest extends Base // We create a new user for our project $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->addMember(2, 2)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - $this->assertTrue($pp->isUserAllowed(2, 2)); + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($pp->addUser(2, 2, Role::PROJECT_MEMBER)); // We create a task $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2))); @@ -534,7 +529,7 @@ class TaskDuplicationTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); $p = new Project($this->container); - $pp = new ProjectPermission($this->container); + $pp = new ProjectUserRole($this->container); $user = new User($this->container); // We create 2 projects @@ -543,10 +538,8 @@ class TaskDuplicationTest extends Base // We create a new user for our project $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->addMember(2, 2)); - $this->assertTrue($pp->isUserAllowed(1, 2)); - $this->assertTrue($pp->isUserAllowed(2, 2)); + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($pp->addUser(2, 2, Role::PROJECT_MEMBER)); // We create a task $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 3))); @@ -673,6 +666,7 @@ class TaskDuplicationTest extends Base $tf = new TaskFinder($this->container); $p = new Project($this->container); $c = new Category($this->container); + $dp = new DateParser($this->container); $this->assertEquals(1, $p->create(array('name' => 'test1'))); @@ -693,7 +687,7 @@ class TaskDuplicationTest extends Base $this->assertNotEmpty($task); $this->assertEquals(Task::RECURRING_STATUS_PROCESSED, $task['recurrence_status']); $this->assertEquals(2, $task['recurrence_child']); - $this->assertEquals(1436561776, $task['date_due'], '', 2); + $this->assertEquals(1436486400, $task['date_due'], '', 2); $task = $tf->getById(2); $this->assertNotEmpty($task); @@ -703,6 +697,6 @@ class TaskDuplicationTest extends Base $this->assertEquals(Task::RECURRING_BASEDATE_TRIGGERDATE, $task['recurrence_basedate']); $this->assertEquals(1, $task['recurrence_parent']); $this->assertEquals(2, $task['recurrence_factor']); - $this->assertEquals(strtotime('+2 days'), $task['date_due'], '', 2); + $this->assertEquals($dp->removeTimeFromTimestamp(strtotime('+2 days')), $task['date_due'], '', 2); } } diff --git a/tests/units/Model/TaskExportTest.php b/tests/units/Model/TaskExportTest.php index 40b3a5a2..b40b0771 100644 --- a/tests/units/Model/TaskExportTest.php +++ b/tests/units/Model/TaskExportTest.php @@ -51,7 +51,7 @@ class TaskExportTest extends Base $this->assertEquals($i, count($rows)); $this->assertEquals('Task Id', $rows[0][0]); $this->assertEquals(1, $rows[1][0]); - $this->assertEquals('Task #'.($i - 1), $rows[$i - 1][12]); + $this->assertEquals('Task #'.($i - 1), $rows[$i - 1][13]); $this->assertTrue(in_array($rows[$i - 1][4], array('Default swimlane', 'S1', 'S2'))); } } diff --git a/tests/units/Model/TaskExternalLinkTest.php b/tests/units/Model/TaskExternalLinkTest.php new file mode 100644 index 00000000..28ccab83 --- /dev/null +++ b/tests/units/Model/TaskExternalLinkTest.php @@ -0,0 +1,123 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskCreation; +use Kanboard\Model\Project; +use Kanboard\Model\TaskExternalLink; +use Kanboard\Core\ExternalLink\ExternalLinkManager; +use Kanboard\ExternalLink\WebLinkProvider; + +class TaskExternalLinkTest extends Base +{ + public function testCreate() + { + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $link = $taskExternalLinkModel->getById(1); + $this->assertNotEmpty($link); + $this->assertEquals('My website', $link['title']); + $this->assertEquals('http://kanboard.net/', $link['url']); + $this->assertEquals('related', $link['dependency']); + $this->assertEquals('weblink', $link['link_type']); + $this->assertEquals(0, $link['creator_id']); + $this->assertEquals(time(), $link['date_modification'], '', 2); + $this->assertEquals(time(), $link['date_creation'], '', 2); + } + + public function testCreateWithUserSession() + { + $this->container['sessionStorage']->user = array('id' => 1); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $link = $taskExternalLinkModel->getById(1); + $this->assertNotEmpty($link); + $this->assertEquals('My website', $link['title']); + $this->assertEquals('http://kanboard.net/', $link['url']); + $this->assertEquals('related', $link['dependency']); + $this->assertEquals('weblink', $link['link_type']); + $this->assertEquals(1, $link['creator_id']); + $this->assertEquals(time(), $link['date_modification'], '', 2); + $this->assertEquals(time(), $link['date_creation'], '', 2); + } + + public function testModification() + { + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + sleep(1); + + $this->assertTrue($taskExternalLinkModel->update(array('id' => 1, 'url' => 'https://kanboard.net/'))); + + $link = $taskExternalLinkModel->getById(1); + $this->assertNotEmpty($link); + $this->assertEquals('https://kanboard.net/', $link['url']); + $this->assertEquals(time(), $link['date_modification'], '', 2); + } + + public function testRemove() + { + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'id' => '', 'url' => 'http://kanboard.net/', 'title' => 'My website', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $this->assertTrue($taskExternalLinkModel->remove(1)); + $this->assertFalse($taskExternalLinkModel->remove(1)); + + $this->assertEmpty($taskExternalLinkModel->getById(1)); + } + + public function testGetAll() + { + $this->container['sessionStorage']->user = array('id' => 1); + $this->container['externalLinkManager'] = new ExternalLinkManager($this->container); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $taskExternalLinkModel = new TaskExternalLink($this->container); + $webLinkProvider = new WebLinkProvider($this->container); + + $this->container['externalLinkManager']->register($webLinkProvider); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Test', 'project_id' => 1))); + $this->assertEquals(1, $taskExternalLinkModel->create(array('task_id' => 1, 'url' => 'https://miniflux.net/', 'title' => 'MX', 'link_type' => 'weblink', 'dependency' => 'related'))); + $this->assertEquals(2, $taskExternalLinkModel->create(array('task_id' => 1, 'url' => 'http://kanboard.net/', 'title' => 'KB', 'link_type' => 'weblink', 'dependency' => 'related'))); + + $links = $taskExternalLinkModel->getAll(1); + $this->assertCount(2, $links); + $this->assertEquals('KB', $links[0]['title']); + $this->assertEquals('MX', $links[1]['title']); + $this->assertEquals('Web Link', $links[0]['type']); + $this->assertEquals('Web Link', $links[1]['type']); + $this->assertEquals('Related', $links[0]['dependency_label']); + $this->assertEquals('Related', $links[1]['dependency_label']); + $this->assertEquals('admin', $links[0]['creator_username']); + $this->assertEquals('admin', $links[1]['creator_username']); + $this->assertEquals('', $links[0]['creator_name']); + $this->assertEquals('', $links[1]['creator_name']); + } +} diff --git a/tests/units/Model/TaskFileTest.php b/tests/units/Model/TaskFileTest.php new file mode 100644 index 00000000..b7db96a9 --- /dev/null +++ b/tests/units/Model/TaskFileTest.php @@ -0,0 +1,446 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\Task; +use Kanboard\Model\TaskFile; +use Kanboard\Model\TaskCreation; +use Kanboard\Model\Project; + +class TaskFileTest extends Base +{ + public function testCreation() + { + $projectModel = new Project($this->container); + $fileModel = new TaskFile($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10)); + + $file = $fileModel->getById(1); + $this->assertEquals('test', $file['name']); + $this->assertEquals('/tmp/foo', $file['path']); + $this->assertEquals(0, $file['is_image']); + $this->assertEquals(1, $file['task_id']); + $this->assertEquals(time(), $file['date'], '', 2); + $this->assertEquals(0, $file['user_id']); + $this->assertEquals(10, $file['size']); + + $this->assertEquals(2, $fileModel->create(1, 'test2.png', '/tmp/foobar', 10)); + + $file = $fileModel->getById(2); + $this->assertEquals('test2.png', $file['name']); + $this->assertEquals('/tmp/foobar', $file['path']); + $this->assertEquals(1, $file['is_image']); + } + + public function testCreationWithFileNameTooLong() + { + $projectModel = new Project($this->container); + $fileModel = new TaskFile($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->assertNotFalse($fileModel->create(1, 'test', '/tmp/foo', 10)); + $this->assertNotFalse($fileModel->create(1, str_repeat('a', 1000), '/tmp/foo', 10)); + + $files = $fileModel->getAll(1); + $this->assertNotEmpty($files); + $this->assertCount(2, $files); + + $this->assertEquals(str_repeat('a', 255), $files[0]['name']); + $this->assertEquals('test', $files[1]['name']); + } + + public function testCreationWithSessionOpen() + { + $this->container['sessionStorage']->user = array('id' => 1); + + $projectModel = new Project($this->container); + $fileModel = new TaskFile($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(1, $fileModel->create(1, 'test', '/tmp/foo', 10)); + + $file = $fileModel->getById(1); + $this->assertEquals('test', $file['name']); + $this->assertEquals(1, $file['user_id']); + } + + public function testGetAll() + { + $projectModel = new Project($this->container); + $fileModel = new TaskFile($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->assertEquals(1, $fileModel->create(1, 'B.pdf', '/tmp/foo', 10)); + $this->assertEquals(2, $fileModel->create(1, 'A.png', '/tmp/foo', 10)); + $this->assertEquals(3, $fileModel->create(1, 'D.doc', '/tmp/foo', 10)); + $this->assertEquals(4, $fileModel->create(1, 'C.JPG', '/tmp/foo', 10)); + + $fileModeliles = $fileModel->getAll(1); + $this->assertNotEmpty($fileModeliles); + $this->assertCount(4, $fileModeliles); + $this->assertEquals('A.png', $fileModeliles[0]['name']); + $this->assertEquals('B.pdf', $fileModeliles[1]['name']); + $this->assertEquals('C.JPG', $fileModeliles[2]['name']); + $this->assertEquals('D.doc', $fileModeliles[3]['name']); + + $fileModeliles = $fileModel->getAllImages(1); + $this->assertNotEmpty($fileModeliles); + $this->assertCount(2, $fileModeliles); + $this->assertEquals('A.png', $fileModeliles[0]['name']); + $this->assertEquals('C.JPG', $fileModeliles[1]['name']); + + $fileModeliles = $fileModel->getAllDocuments(1); + $this->assertNotEmpty($fileModeliles); + $this->assertCount(2, $fileModeliles); + $this->assertEquals('B.pdf', $fileModeliles[0]['name']); + $this->assertEquals('D.doc', $fileModeliles[1]['name']); + } + + public function testIsImage() + { + $fileModel = new TaskFile($this->container); + + $this->assertTrue($fileModel->isImage('test.png')); + $this->assertTrue($fileModel->isImage('test.jpeg')); + $this->assertTrue($fileModel->isImage('test.gif')); + $this->assertTrue($fileModel->isImage('test.jpg')); + $this->assertTrue($fileModel->isImage('test.JPG')); + + $this->assertFalse($fileModel->isImage('test.bmp')); + $this->assertFalse($fileModel->isImage('test')); + $this->assertFalse($fileModel->isImage('test.pdf')); + } + + public function testGetThumbnailPath() + { + $fileModel = new TaskFile($this->container); + $this->assertEquals('thumbnails'.DIRECTORY_SEPARATOR.'test', $fileModel->getThumbnailPath('test')); + } + + public function testGeneratePath() + { + $fileModel = new TaskFile($this->container); + + $this->assertStringStartsWith('tasks'.DIRECTORY_SEPARATOR.'34'.DIRECTORY_SEPARATOR, $fileModel->generatePath(34, 'test.png')); + $this->assertNotEquals($fileModel->generatePath(34, 'test1.png'), $fileModel->generatePath(34, 'test2.png')); + } + + public function testUploadFiles() + { + $fileModel = $this + ->getMockBuilder('\Kanboard\Model\TaskFile') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('generateThumbnailFromFile')) + ->getMock(); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $files = array( + 'name' => array( + 'file1.png', + 'file2.doc', + ), + 'tmp_name' => array( + '/tmp/phpYzdqkD', + '/tmp/phpeEwEWG', + ), + 'error' => array( + UPLOAD_ERR_OK, + UPLOAD_ERR_OK, + ), + 'size' => array( + 123, + 456, + ), + ); + + $fileModel + ->expects($this->once()) + ->method('generateThumbnailFromFile'); + + $this->container['objectStorage'] + ->expects($this->at(0)) + ->method('moveUploadedFile') + ->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything()); + + $this->container['objectStorage'] + ->expects($this->at(1)) + ->method('moveUploadedFile') + ->with($this->equalTo('/tmp/phpeEwEWG'), $this->anything()); + + $this->assertTrue($fileModel->uploadFiles(1, $files)); + + $files = $fileModel->getAll(1); + $this->assertCount(2, $files); + + $this->assertEquals(1, $files[0]['id']); + $this->assertEquals('file1.png', $files[0]['name']); + $this->assertEquals(1, $files[0]['is_image']); + $this->assertEquals(1, $files[0]['task_id']); + $this->assertEquals(0, $files[0]['user_id']); + $this->assertEquals(123, $files[0]['size']); + $this->assertEquals(time(), $files[0]['date'], '', 2); + + $this->assertEquals(2, $files[1]['id']); + $this->assertEquals('file2.doc', $files[1]['name']); + $this->assertEquals(0, $files[1]['is_image']); + $this->assertEquals(1, $files[1]['task_id']); + $this->assertEquals(0, $files[1]['user_id']); + $this->assertEquals(456, $files[1]['size']); + $this->assertEquals(time(), $files[1]['date'], '', 2); + } + + public function testUploadFilesWithEmptyFiles() + { + $fileModel = new TaskFile($this->container); + $this->assertFalse($fileModel->uploadFiles(1, array())); + } + + public function testUploadFilesWithUploadError() + { + $files = array( + 'name' => array( + 'file1.png', + 'file2.doc', + ), + 'tmp_name' => array( + '', + '/tmp/phpeEwEWG', + ), + 'error' => array( + UPLOAD_ERR_CANT_WRITE, + UPLOAD_ERR_OK, + ), + 'size' => array( + 123, + 456, + ), + ); + + $fileModel = new TaskFile($this->container); + $this->assertFalse($fileModel->uploadFiles(1, $files)); + } + + public function testUploadFilesWithObjectStorageError() + { + $files = array( + 'name' => array( + 'file1.csv', + 'file2.doc', + ), + 'tmp_name' => array( + '/tmp/phpYzdqkD', + '/tmp/phpeEwEWG', + ), + 'error' => array( + UPLOAD_ERR_OK, + UPLOAD_ERR_OK, + ), + 'size' => array( + 123, + 456, + ), + ); + + $this->container['objectStorage'] + ->expects($this->at(0)) + ->method('moveUploadedFile') + ->with($this->equalTo('/tmp/phpYzdqkD'), $this->anything()) + ->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test'))); + + $fileModel = new TaskFile($this->container); + $this->assertFalse($fileModel->uploadFiles(1, $files)); + } + + public function testUploadFileContent() + { + $fileModel = $this + ->getMockBuilder('\Kanboard\Model\TaskFile') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('generateThumbnailFromFile')) + ->getMock(); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $data = 'test'; + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['objectStorage'] + ->expects($this->once()) + ->method('put') + ->with($this->anything(), $this->equalTo($data)); + + $this->assertEquals(1, $fileModel->uploadContent(1, 'test.doc', base64_encode($data))); + + $files = $fileModel->getAll(1); + $this->assertCount(1, $files); + + $this->assertEquals(1, $files[0]['id']); + $this->assertEquals('test.doc', $files[0]['name']); + $this->assertEquals(0, $files[0]['is_image']); + $this->assertEquals(1, $files[0]['task_id']); + $this->assertEquals(0, $files[0]['user_id']); + $this->assertEquals(4, $files[0]['size']); + $this->assertEquals(time(), $files[0]['date'], '', 2); + } + + public function testUploadFileContentWithObjectStorageError() + { + $fileModel = $this + ->getMockBuilder('\Kanboard\Model\TaskFile') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('generateThumbnailFromFile')) + ->getMock(); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $data = 'test'; + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['objectStorage'] + ->expects($this->once()) + ->method('put') + ->with($this->anything(), $this->equalTo($data)) + ->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test'))); + + $this->assertFalse($fileModel->uploadContent(1, 'test.doc', base64_encode($data))); + } + + public function testUploadScreenshot() + { + $fileModel = $this + ->getMockBuilder('\Kanboard\Model\TaskFile') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('generateThumbnailFromFile')) + ->getMock(); + + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $data = 'test'; + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $fileModel + ->expects($this->once()) + ->method('generateThumbnailFromFile'); + + $this->container['objectStorage'] + ->expects($this->once()) + ->method('put') + ->with($this->anything(), $this->equalTo($data)); + + $this->assertEquals(1, $fileModel->uploadScreenshot(1, base64_encode($data))); + + $files = $fileModel->getAll(1); + $this->assertCount(1, $files); + + $this->assertEquals(1, $files[0]['id']); + $this->assertStringStartsWith('Screenshot taken ', $files[0]['name']); + $this->assertEquals(1, $files[0]['is_image']); + $this->assertEquals(1, $files[0]['task_id']); + $this->assertEquals(0, $files[0]['user_id']); + $this->assertEquals(4, $files[0]['size']); + $this->assertEquals(time(), $files[0]['date'], '', 2); + } + + public function testRemove() + { + $fileModel = new TaskFile($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(1, $fileModel->create(1, 'test', 'tmp/foo', 10)); + + $this->container['objectStorage'] + ->expects($this->once()) + ->method('remove') + ->with('tmp/foo'); + + $this->assertTrue($fileModel->remove(1)); + } + + public function testRemoveWithObjectStorageError() + { + $fileModel = new TaskFile($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(1, $fileModel->create(1, 'test', 'tmp/foo', 10)); + + $this->container['objectStorage'] + ->expects($this->once()) + ->method('remove') + ->with('tmp/foo') + ->will($this->throwException(new \Kanboard\Core\ObjectStorage\ObjectStorageException('test'))); + + $this->assertFalse($fileModel->remove(1)); + } + + public function testRemoveImage() + { + $fileModel = new TaskFile($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(1, $fileModel->create(1, 'image.gif', 'tmp/image.gif', 10)); + + $this->container['objectStorage'] + ->expects($this->at(0)) + ->method('remove') + ->with('tmp/image.gif'); + + $this->container['objectStorage'] + ->expects($this->at(1)) + ->method('remove') + ->with('thumbnails'.DIRECTORY_SEPARATOR.'tmp/image.gif'); + + $this->assertTrue($fileModel->remove(1)); + } + + public function testRemoveAll() + { + $fileModel = new TaskFile($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(1, $fileModel->create(1, 'test', 'tmp/foo', 10)); + $this->assertEquals(2, $fileModel->create(1, 'test', 'tmp/foo', 10)); + + $this->container['objectStorage'] + ->expects($this->exactly(2)) + ->method('remove') + ->with('tmp/foo'); + + $this->assertTrue($fileModel->removeAll(1)); + } +} diff --git a/tests/units/Model/TaskFilterTest.php b/tests/units/Model/TaskFilterTest.php index b668b7cc..daa193b2 100644 --- a/tests/units/Model/TaskFilterTest.php +++ b/tests/units/Model/TaskFilterTest.php @@ -6,6 +6,7 @@ use Kanboard\Model\Project; use Kanboard\Model\User; use Kanboard\Model\TaskFilter; use Kanboard\Model\TaskCreation; +use Kanboard\Model\TaskLink; use Kanboard\Core\DateParser; use Kanboard\Model\Category; use Kanboard\Model\Subtask; @@ -552,6 +553,65 @@ class TaskFilterTest extends Base $this->assertEquals('task3', $tasks[0]['title']); } + public function testSearchWithLink() + { + $p = new Project($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tl = new TaskLink($this->container); + $tf = new TaskFilter($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(2, $u->create(array('username' => 'bob', 'name' => 'Bob Ryan'))); + $this->assertNotFalse($tc->create(array('project_id' => 1, 'title' => 'my task title is awesome', 'color_id' => 'light_green'))); + $this->assertNotFalse($tc->create(array('project_id' => 1, 'title' => 'my task title is amazing', 'color_id' => 'blue'))); + $this->assertNotFalse($tc->create(array('project_id' => 1, 'title' => 'Bob at work'))); + $this->assertNotFalse($tc->create(array('project_id' => 1, 'title' => 'I have a bad feeling about that'))); + $this->assertEquals(1, $tl->create(1, 2, 9)); // #1 is a milestone of #2 + $this->assertEquals(3, $tl->create(2, 1, 2)); // #2 blocks #1 + $this->assertEquals(5, $tl->create(3, 2, 2)); // #3 blocks #2 + + $tf->search('link:"is a milestone of"'); + $tasks = $tf->findAll(); + $this->assertNotEmpty($tasks); + $this->assertCount(1, $tasks); + $this->assertEquals('my task title is awesome', $tasks[0]['title']); + + $tf->search('link:"is a milestone of" amazing'); + $tasks = $tf->findAll(); + $this->assertEmpty($tasks); + + $tf->search('link:"unknown"'); + $tasks = $tf->findAll(); + $this->assertEmpty($tasks); + + $tf->search('link:unknown'); + $tasks = $tf->findAll(); + $this->assertEmpty($tasks); + + $tf->search('link:blocks amazing'); + $tasks = $tf->findAll(); + $this->assertNotEmpty($tasks); + $this->assertCount(1, $tasks); + $this->assertEquals('my task title is amazing', $tasks[0]['title']); + + $tf->search('link:"is a milestone of" link:blocks'); + $tasks = $tf->findAll(); + $this->assertNotEmpty($tasks); + $this->assertCount(3, $tasks); + $this->assertEquals('my task title is awesome', $tasks[0]['title']); + $this->assertEquals('my task title is amazing', $tasks[1]['title']); + $this->assertEquals('Bob at work', $tasks[2]['title']); + + $tf->search('link:"is a milestone of" link:blocks link:unknown'); + $tasks = $tf->findAll(); + $this->assertNotEmpty($tasks); + $this->assertCount(3, $tasks); + $this->assertEquals('my task title is awesome', $tasks[0]['title']); + $this->assertEquals('my task title is amazing', $tasks[1]['title']); + $this->assertEquals('Bob at work', $tasks[2]['title']); + } + public function testCopy() { $tf = new TaskFilter($this->container); diff --git a/tests/units/Model/TaskFinderTest.php b/tests/units/Model/TaskFinderTest.php index e22f14e1..b21a4ea3 100644 --- a/tests/units/Model/TaskFinderTest.php +++ b/tests/units/Model/TaskFinderTest.php @@ -6,7 +6,6 @@ use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; use Kanboard\Model\Category; use Kanboard\Model\User; diff --git a/tests/units/Model/TaskLinkTest.php b/tests/units/Model/TaskLinkTest.php index 4db42d99..192a4298 100644 --- a/tests/units/Model/TaskLinkTest.php +++ b/tests/units/Model/TaskLinkTest.php @@ -194,53 +194,4 @@ class TaskLinkTest extends Base $links = $tl->getAll(2); $this->assertEmpty($links); } - - public function testValidation() - { - $tl = new TaskLink($this->container); - $p = new Project($this->container); - $tc = new TaskCreation($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'A'))); - $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'B'))); - - $links = $tl->getAll(1); - $this->assertEmpty($links); - - $links = $tl->getAll(2); - $this->assertEmpty($links); - - // Check creation - $r = $tl->validateCreation(array('task_id' => 1, 'link_id' => 1, 'opposite_task_id' => 2)); - $this->assertTrue($r[0]); - - $r = $tl->validateCreation(array('task_id' => 1, 'link_id' => 1)); - $this->assertFalse($r[0]); - - $r = $tl->validateCreation(array('task_id' => 1, 'opposite_task_id' => 2)); - $this->assertFalse($r[0]); - - $r = $tl->validateCreation(array('task_id' => 1, 'opposite_task_id' => 2)); - $this->assertFalse($r[0]); - - $r = $tl->validateCreation(array('task_id' => 1, 'link_id' => 1, 'opposite_task_id' => 1)); - $this->assertFalse($r[0]); - - // Check modification - $r = $tl->validateModification(array('id' => 1, 'task_id' => 1, 'link_id' => 1, 'opposite_task_id' => 2)); - $this->assertTrue($r[0]); - - $r = $tl->validateModification(array('id' => 1, 'task_id' => 1, 'link_id' => 1)); - $this->assertFalse($r[0]); - - $r = $tl->validateModification(array('id' => 1, 'task_id' => 1, 'opposite_task_id' => 2)); - $this->assertFalse($r[0]); - - $r = $tl->validateModification(array('id' => 1, 'task_id' => 1, 'opposite_task_id' => 2)); - $this->assertFalse($r[0]); - - $r = $tl->validateModification(array('id' => 1, 'task_id' => 1, 'link_id' => 1, 'opposite_task_id' => 1)); - $this->assertFalse($r[0]); - } } diff --git a/tests/units/Model/TaskMetadataTest.php b/tests/units/Model/TaskMetadataTest.php index 9ce7d6be..2683c297 100644 --- a/tests/units/Model/TaskMetadataTest.php +++ b/tests/units/Model/TaskMetadataTest.php @@ -33,5 +33,10 @@ class TaskMetadataTest extends Base $this->assertEquals(array('key1' => 'value2'), $tm->getAll(1)); $this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $tm->getAll(2)); + + $this->assertTrue($tm->remove(2, 'key1')); + $this->assertFalse($tm->remove(2, 'key1')); + + $this->assertEquals(array('key2' => 'value2'), $tm->getAll(2)); } } diff --git a/tests/units/Model/TaskModificationTest.php b/tests/units/Model/TaskModificationTest.php index 49b51f9b..119201f0 100644 --- a/tests/units/Model/TaskModificationTest.php +++ b/tests/units/Model/TaskModificationTest.php @@ -8,7 +8,6 @@ use Kanboard\Model\TaskModification; use Kanboard\Model\TaskFinder; use Kanboard\Model\TaskStatus; use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; class TaskModificationTest extends Base { @@ -42,6 +41,24 @@ class TaskModificationTest extends Base $this->assertEquals(1, $event_data['owner_id']); } + public function testThatNoEventAreFiredWhenNoChanges() + { + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $tm = new TaskModification($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); + + $this->container['dispatcher']->addListener(Task::EVENT_CREATE_UPDATE, array($this, 'onCreateUpdate')); + $this->container['dispatcher']->addListener(Task::EVENT_UPDATE, array($this, 'onUpdate')); + + $this->assertTrue($tm->update(array('id' => 1, 'title' => 'test'))); + + $this->assertEmpty($this->container['dispatcher']->getCalledListeners()); + } + public function testChangeTitle() { $p = new Project($this->container); diff --git a/tests/units/Model/TaskMovedDateSubscriberTest.php b/tests/units/Model/TaskMovedDateSubscriberTest.php deleted file mode 100644 index 0dd6e995..00000000 --- a/tests/units/Model/TaskMovedDateSubscriberTest.php +++ /dev/null @@ -1,77 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskPosition; -use Kanboard\Model\TaskCreation; -use Kanboard\Model\TaskFinder; -use Kanboard\Model\Project; -use Kanboard\Model\Swimlane; -use Kanboard\Subscriber\TaskMovedDateSubscriber; -use Symfony\Component\EventDispatcher\EventDispatcher; - -class TaskMovedDateSubscriberTest extends Base -{ - public function testMoveTaskAnotherColumn() - { - $tp = new TaskPosition($this->container); - $tc = new TaskCreation($this->container); - $p = new Project($this->container); - $tf = new TaskFinder($this->container); - - $this->container['dispatcher'] = new EventDispatcher; - $this->container['dispatcher']->addSubscriber(new TaskMovedDateSubscriber($this->container)); - - $now = time(); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals($now, $task['date_moved'], '', 1); - - sleep(1); - - $this->assertTrue($tp->movePosition(1, 1, 2, 1)); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertNotEquals($now, $task['date_moved']); - } - - public function testMoveTaskAnotherSwimlane() - { - $tp = new TaskPosition($this->container); - $tc = new TaskCreation($this->container); - $p = new Project($this->container); - $tf = new TaskFinder($this->container); - $s = new Swimlane($this->container); - - $this->container['dispatcher'] = new EventDispatcher; - $this->container['dispatcher']->addSubscriber(new TaskMovedDateSubscriber($this->container)); - - $now = time(); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'S1'))); - $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'S2'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals($now, $task['date_moved'], '', 1); - $this->assertEquals(1, $task['column_id']); - $this->assertEquals(0, $task['swimlane_id']); - - sleep(1); - - $this->assertTrue($tp->movePosition(1, 1, 2, 1, 2)); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertNotEquals($now, $task['date_moved']); - $this->assertEquals(2, $task['column_id']); - $this->assertEquals(2, $task['swimlane_id']); - } -} diff --git a/tests/units/Model/TaskPermissionTest.php b/tests/units/Model/TaskPermissionTest.php index 52a36549..0b093bbb 100644 --- a/tests/units/Model/TaskPermissionTest.php +++ b/tests/units/Model/TaskPermissionTest.php @@ -9,7 +9,7 @@ use Kanboard\Model\TaskPermission; use Kanboard\Model\Project; use Kanboard\Model\Category; use Kanboard\Model\User; -use Kanboard\Model\UserSession; +use Kanboard\Core\User\UserSession; class TaskPermissionTest extends Base { @@ -33,7 +33,7 @@ class TaskPermissionTest extends Base // User #1 can remove everything $user = $u->getbyId(1); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(1); $this->assertNotEmpty($task); @@ -42,7 +42,7 @@ class TaskPermissionTest extends Base // User #2 can't remove the task #1 $user = $u->getbyId(2); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(1); $this->assertNotEmpty($task); @@ -51,7 +51,7 @@ class TaskPermissionTest extends Base // User #1 can remove everything $user = $u->getbyId(1); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(2); $this->assertNotEmpty($task); @@ -60,7 +60,7 @@ class TaskPermissionTest extends Base // User #2 can remove his own task $user = $u->getbyId(2); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(2); $this->assertNotEmpty($task); @@ -69,7 +69,7 @@ class TaskPermissionTest extends Base // User #1 can remove everything $user = $u->getbyId(1); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(3); $this->assertNotEmpty($task); @@ -78,7 +78,7 @@ class TaskPermissionTest extends Base // User #2 can't remove the task #3 $user = $u->getbyId(2); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(3); $this->assertNotEmpty($task); @@ -87,7 +87,7 @@ class TaskPermissionTest extends Base // User #1 can remove everything $user = $u->getbyId(1); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(4); $this->assertNotEmpty($task); @@ -96,7 +96,7 @@ class TaskPermissionTest extends Base // User #2 can't remove the task #4 $user = $u->getbyId(2); $this->assertNotEmpty($user); - $us->refresh($user); + $us->initialize($user); $task = $tf->getbyId(4); $this->assertNotEmpty($task); diff --git a/tests/units/Model/TaskPositionTest.php b/tests/units/Model/TaskPositionTest.php index 42612f44..28145a66 100644 --- a/tests/units/Model/TaskPositionTest.php +++ b/tests/units/Model/TaskPositionTest.php @@ -3,7 +3,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Task; -use Kanboard\Model\Board; +use Kanboard\Model\Column; use Kanboard\Model\TaskStatus; use Kanboard\Model\TaskPosition; use Kanboard\Model\TaskCreation; @@ -21,23 +21,23 @@ class TaskPositionTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); $p = new Project($this->container); - $b = new Board($this->container); + $columnModel = new Column($this->container); $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); - $this->assertEquals(0, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(0, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($tp->movePosition(1, 1, 2, 1)); - $this->assertEquals(25, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(25, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($tp->movePosition(1, 1, 3, 1)); - $this->assertEquals(50, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(50, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($tp->movePosition(1, 1, 4, 1)); - $this->assertEquals(75, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(75, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($ts->close(1)); - $this->assertEquals(100, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(100, $t->getProgress($tf->getById(1), $columnModel->getList(1))); } public function testMoveTaskToWrongPosition() @@ -106,7 +106,7 @@ class TaskPositionTest extends Base $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); $this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); - // We move the task 2 to the column 3 + // We move the task 1 to the column 3 $this->assertTrue($tp->movePosition(1, 1, 3, 1)); // Check tasks position @@ -235,7 +235,7 @@ class TaskPositionTest extends Base $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 1, 'column_id' => 1))); $this->assertEquals(4, $tc->create(array('title' => 'Task #4', 'project_id' => 1, 'column_id' => 1))); - // Move the last task to the bottom + // Move the first task to the bottom $this->assertTrue($tp->movePosition(1, 1, 1, 4)); // Check tasks position diff --git a/tests/units/Model/TaskStatusTest.php b/tests/units/Model/TaskStatusTest.php index de08ffb3..86f31d70 100644 --- a/tests/units/Model/TaskStatusTest.php +++ b/tests/units/Model/TaskStatusTest.php @@ -2,16 +2,51 @@ require_once __DIR__.'/../Base.php'; +use Kanboard\Model\Swimlane; use Kanboard\Model\Subtask; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; use Kanboard\Model\TaskStatus; use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; class TaskStatusTest extends Base { + public function testCloseBySwimlaneAndColumn() + { + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + $ts = new TaskStatus($this->container); + $p = new Project($this->container); + $s = new Swimlane($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $s->create(array('name' => 'test', 'project_id' => 1))); + $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); + $this->assertEquals(2, $tc->create(array('title' => 'test', 'project_id' => 1))); + $this->assertEquals(3, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2))); + $this->assertEquals(4, $tc->create(array('title' => 'test', 'project_id' => 1, 'swimlane_id' => 1))); + + $this->assertEquals(2, $tf->countByColumnAndSwimlaneId(1, 1, 0)); + $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 1, 1)); + $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 2, 0)); + + $ts->closeTasksBySwimlaneAndColumn(0, 1); + $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 0)); + $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 1, 1)); + $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 2, 0)); + + $ts->closeTasksBySwimlaneAndColumn(1, 1); + $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 0)); + $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 1)); + $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 2, 0)); + + $ts->closeTasksBySwimlaneAndColumn(0, 2); + $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 0)); + $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 1)); + $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 2, 0)); + } + public function testStatus() { $tc = new TaskCreation($this->container); diff --git a/tests/units/Model/TaskTest.php b/tests/units/Model/TaskTest.php index 192dc098..60e752e5 100644 --- a/tests/units/Model/TaskTest.php +++ b/tests/units/Model/TaskTest.php @@ -7,7 +7,6 @@ use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; use Kanboard\Model\TaskStatus; use Kanboard\Model\Project; -use Kanboard\Model\ProjectPermission; use Kanboard\Model\Category; use Kanboard\Model\User; diff --git a/tests/units/Model/UserLockingTest.php b/tests/units/Model/UserLockingTest.php new file mode 100644 index 00000000..c743f8eb --- /dev/null +++ b/tests/units/Model/UserLockingTest.php @@ -0,0 +1,43 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\UserLocking; + +class UserLockingTest extends Base +{ + public function testFailedLogin() + { + $u = new UserLocking($this->container); + + $this->assertEquals(0, $u->getFailedLogin('admin')); + $this->assertEquals(0, $u->getFailedLogin('not_found')); + + $this->assertTrue($u->incrementFailedLogin('admin')); + $this->assertTrue($u->incrementFailedLogin('admin')); + + $this->assertEquals(2, $u->getFailedLogin('admin')); + $this->assertTrue($u->resetFailedLogin('admin')); + $this->assertEquals(0, $u->getFailedLogin('admin')); + } + + public function testLocking() + { + $u = new UserLocking($this->container); + + $this->assertFalse($u->isLocked('admin')); + $this->assertFalse($u->isLocked('not_found')); + $this->assertTrue($u->lock('admin', 1)); + $this->assertTrue($u->isLocked('admin')); + } + + public function testCaptcha() + { + $u = new UserLocking($this->container); + $this->assertTrue($u->incrementFailedLogin('admin')); + $this->assertFalse($u->hasCaptcha('admin', 2)); + + $this->assertTrue($u->incrementFailedLogin('admin')); + $this->assertTrue($u->hasCaptcha('admin', 2)); + } +} diff --git a/tests/units/Model/UserMentionTest.php b/tests/units/Model/UserMentionTest.php new file mode 100644 index 00000000..d50c9285 --- /dev/null +++ b/tests/units/Model/UserMentionTest.php @@ -0,0 +1,114 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Core\Security\Role; +use Kanboard\Event\GenericEvent; +use Kanboard\Model\User; +use Kanboard\Model\Task; +use Kanboard\Model\TaskCreation; +use Kanboard\Model\Project; +use Kanboard\Model\ProjectUserRole; +use Kanboard\Model\UserMention; + +class UserMentionTest extends Base +{ + public function testGetMentionedUsersWithNoMentions() + { + $userModel = new User($this->container); + $userMentionModel = new UserMention($this->container); + + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + $this->assertEmpty($userMentionModel->getMentionedUsers('test')); + } + + public function testGetMentionedUsersWithNotficationDisabled() + { + $userModel = new User($this->container); + $userMentionModel = new UserMention($this->container); + + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + $this->assertEmpty($userMentionModel->getMentionedUsers('test @user1')); + } + + public function testGetMentionedUsersWithNotficationEnabled() + { + $userModel = new User($this->container); + $userMentionModel = new UserMention($this->container); + + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'name' => 'Foobar', 'notifications_enabled' => 1))); + + $users = $userMentionModel->getMentionedUsers('test @user2'); + $this->assertCount(1, $users); + $this->assertEquals('user2', $users[0]['username']); + $this->assertEquals('Foobar', $users[0]['name']); + $this->assertEquals('', $users[0]['email']); + $this->assertEquals('', $users[0]['language']); + } + + public function testGetMentionedUsersWithNotficationEnabledAndUserLoggedIn() + { + $this->container['sessionStorage']->user = array('id' => 3); + $userModel = new User($this->container); + $userMentionModel = new UserMention($this->container); + + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'name' => 'Foobar', 'notifications_enabled' => 1))); + + $this->assertEmpty($userMentionModel->getMentionedUsers('test @user2')); + } + + public function testFireEventsWithMultipleMentions() + { + $projectUserRoleModel = new ProjectUserRole($this->container); + $projectModel = new Project($this->container); + $userModel = new User($this->container); + $userMentionModel = new UserMention($this->container); + $event = new GenericEvent(array('project_id' => 1)); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User 1', 'notifications_enabled' => 1))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User 2', 'notifications_enabled' => 1))); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + $this->assertTrue($projectUserRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + + $this->container['dispatcher']->addListener(Task::EVENT_USER_MENTION, array($this, 'onUserMention')); + + $userMentionModel->fireEvents('test @user1 @user2', Task::EVENT_USER_MENTION, $event); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(Task::EVENT_USER_MENTION.'.UserMentionTest::onUserMention', $called); + } + + public function testFireEventsWithNoProjectId() + { + $projectUserRoleModel = new ProjectUserRole($this->container); + $projectModel = new Project($this->container); + $taskCreationModel = new TaskCreation($this->container); + $userModel = new User($this->container); + $userMentionModel = new UserMention($this->container); + $event = new GenericEvent(array('task_id' => 1)); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'User 1', 'notifications_enabled' => 1))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'name' => 'User 2', 'notifications_enabled' => 1))); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + $this->assertTrue($projectUserRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'Task 1'))); + + $this->container['dispatcher']->addListener(Task::EVENT_USER_MENTION, array($this, 'onUserMention')); + + $userMentionModel->fireEvents('test @user1 @user2', Task::EVENT_USER_MENTION, $event); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(Task::EVENT_USER_MENTION.'.UserMentionTest::onUserMention', $called); + } + + public function onUserMention($event) + { + $this->assertInstanceOf('Kanboard\Event\GenericEvent', $event); + $this->assertEquals(array('id' => '3', 'username' => 'user2', 'name' => 'User 2', 'email' => null, 'language' => null), $event['mention']); + } +} diff --git a/tests/units/Model/UserMetadataTest.php b/tests/units/Model/UserMetadataTest.php index cc1fff12..457f1fb2 100644 --- a/tests/units/Model/UserMetadataTest.php +++ b/tests/units/Model/UserMetadataTest.php @@ -29,5 +29,10 @@ class UserMetadataTest extends Base $this->assertEquals(array('key1' => 'value2'), $m->getAll(1)); $this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $m->getAll(2)); + + $this->assertTrue($m->remove(2, 'key1')); + $this->assertFalse($m->remove(2, 'key1')); + + $this->assertEquals(array('key2' => 'value2'), $m->getAll(2)); } } diff --git a/tests/units/Model/UserNotificationTest.php b/tests/units/Model/UserNotificationTest.php index 729667de..e1928661 100644 --- a/tests/units/Model/UserNotificationTest.php +++ b/tests/units/Model/UserNotificationTest.php @@ -7,14 +7,18 @@ use Kanboard\Model\TaskCreation; use Kanboard\Model\Subtask; use Kanboard\Model\Comment; use Kanboard\Model\User; -use Kanboard\Model\File; +use Kanboard\Model\Group; +use Kanboard\Model\GroupMember; use Kanboard\Model\Project; -use Kanboard\Model\Task; use Kanboard\Model\ProjectPermission; +use Kanboard\Model\Task; +use Kanboard\Model\ProjectUserRole; +use Kanboard\Model\ProjectGroupRole; use Kanboard\Model\UserNotification; use Kanboard\Model\UserNotificationFilter; use Kanboard\Model\UserNotificationType; use Kanboard\Subscriber\UserNotificationSubscriber; +use Kanboard\Core\Security\Role; class UserNotificationTest extends Base { @@ -23,11 +27,11 @@ class UserNotificationTest extends Base $u = new User($this->container); $p = new Project($this->container); $n = new UserNotification($this->container); - $pp = new ProjectPermission($this->container); + $pp = new ProjectUserRole($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); $this->assertEquals(2, $u->create(array('username' => 'user1'))); - $this->assertTrue($pp->addMember(1, 2)); + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MEMBER)); $this->assertEmpty($n->getUsersWithNotificationEnabled(1)); $n->enableNotification(2); @@ -96,12 +100,48 @@ class UserNotificationTest extends Base $this->assertEquals(array(1), $settings['notification_projects']); } + public function testGetGroupMembersWithNotificationEnabled() + { + $userModel = new User($this->container); + $groupModel = new Group($this->container); + $groupMemberModel = new GroupMember($this->container); + $projectModel = new Project($this->container); + $userNotificationModel = new UserNotification($this->container); + $projectGroupRole = new ProjectGroupRole($this->container); + $projectUserRole = new ProjectUserRole($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2', 'email' => '', 'notifications_enabled' => 1))); + $this->assertEquals(4, $userModel->create(array('username' => 'user3'))); + + $this->assertEquals(1, $groupModel->create('G1')); + $this->assertEquals(2, $groupModel->create('G2')); + + $this->assertTrue($groupMemberModel->addUser(1, 2)); + $this->assertTrue($groupMemberModel->addUser(1, 3)); + $this->assertTrue($groupMemberModel->addUser(1, 4)); + $this->assertTrue($groupMemberModel->addUser(2, 2)); + $this->assertTrue($groupMemberModel->addUser(2, 3)); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + + $this->assertTrue($projectGroupRole->addGroup(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($projectGroupRole->addGroup(1, 2, Role::PROJECT_VIEWER)); + + $this->assertTrue($projectUserRole->addUser(1, 2, Role::PROJECT_MEMBER)); + + $users = $userNotificationModel->getUsersWithNotificationEnabled(1); + $this->assertCount(2, $users); + $this->assertEquals('user1', $users[0]['username']); + $this->assertEquals('user2', $users[1]['username']); + } + public function testGetProjectMembersWithNotifications() { $u = new User($this->container); $p = new Project($this->container); $n = new UserNotification($this->container); - $pp = new ProjectPermission($this->container); + $pp = new ProjectUserRole($this->container); $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); @@ -118,16 +158,16 @@ class UserNotificationTest extends Base $this->assertNotFalse($u->create(array('username' => 'user4'))); // Nobody is member of any projects - $this->assertEmpty($pp->getMembers(1)); + $this->assertEmpty($pp->getUsers(1)); $this->assertEmpty($n->getUsersWithNotificationEnabled(1)); // We allow all users to be member of our projects - $this->assertTrue($pp->addMember(1, 1)); - $this->assertTrue($pp->addMember(1, 2)); - $this->assertTrue($pp->addMember(1, 3)); - $this->assertTrue($pp->addMember(1, 4)); + $this->assertTrue($pp->addUser(1, 1, Role::PROJECT_MEMBER)); + $this->assertTrue($pp->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($pp->addUser(1, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($pp->addUser(1, 4, Role::PROJECT_MEMBER)); - $this->assertNotEmpty($pp->getMembers(1)); + $this->assertNotEmpty($pp->getUsers(1)); $users = $n->getUsersWithNotificationEnabled(1); $this->assertNotEmpty($users); diff --git a/tests/units/Model/UserSessionTest.php b/tests/units/Model/UserSessionTest.php deleted file mode 100644 index 66f6faa7..00000000 --- a/tests/units/Model/UserSessionTest.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Core\Session; -use Kanboard\Model\UserSession; - -class UserSessionTest extends Base -{ - public function testIsAdmin() - { - $s = new Session; - $us = new UserSession($this->container); - - $this->assertFalse($us->isAdmin()); - - $s['user'] = array(); - $this->assertFalse($us->isAdmin()); - - $s['user'] = array('is_admin' => '1'); - $this->assertFalse($us->isAdmin()); - - $s['user'] = array('is_admin' => false); - $this->assertFalse($us->isAdmin()); - - $s['user'] = array('is_admin' => '2'); - $this->assertFalse($us->isAdmin()); - - $s['user'] = array('is_admin' => true); - $this->assertTrue($us->isAdmin()); - } -} diff --git a/tests/units/Model/UserTest.php b/tests/units/Model/UserTest.php index 90a80954..e411da0c 100644 --- a/tests/units/Model/UserTest.php +++ b/tests/units/Model/UserTest.php @@ -9,34 +9,10 @@ use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; use Kanboard\Model\Project; +use Kanboard\Core\Security\Role; class UserTest extends Base { - public function testFailedLogin() - { - $u = new User($this->container); - - $this->assertEquals(0, $u->getFailedLogin('admin')); - $this->assertEquals(0, $u->getFailedLogin('not_found')); - - $this->assertTrue($u->incrementFailedLogin('admin')); - $this->assertTrue($u->incrementFailedLogin('admin')); - - $this->assertEquals(2, $u->getFailedLogin('admin')); - $this->assertTrue($u->resetFailedLogin('admin')); - $this->assertEquals(0, $u->getFailedLogin('admin')); - } - - public function testLocking() - { - $u = new User($this->container); - - $this->assertFalse($u->isLocked('admin')); - $this->assertFalse($u->isLocked('not_found')); - $this->assertTrue($u->lock('admin', 1)); - $this->assertTrue($u->isLocked('admin')); - } - public function testGetByEmail() { $u = new User($this->container); @@ -47,33 +23,27 @@ class UserTest extends Base $this->assertEmpty($u->getByEmail('')); } - public function testGetByGitlabId() + public function testGetByExternalId() { $u = new User($this->container); $this->assertNotFalse($u->create(array('username' => 'user1', 'password' => '123456', 'gitlab_id' => '1234'))); - $this->assertNotEmpty($u->getByGitlabId('1234')); - $this->assertEmpty($u->getByGitlabId('')); - } + $this->assertNotEmpty($u->getByExternalId('gitlab_id', '1234')); + $this->assertEmpty($u->getByExternalId('gitlab_id', '')); - public function testGetByGithubId() - { $u = new User($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'password' => '123456', 'github_id' => 'plop'))); - $this->assertNotFalse($u->create(array('username' => 'user2', 'password' => '123456', 'github_id' => ''))); + $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->getByGithubId('plop')); - $this->assertEmpty($u->getByGithubId('')); - } + $this->assertNotEmpty($u->getByExternalId('github_id', 'plop')); + $this->assertEmpty($u->getByExternalId('github_id', '')); - public function testGetByGoogleId() - { $u = new User($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'password' => '123456', 'google_id' => '1234'))); - $this->assertNotFalse($u->create(array('username' => 'user2', 'password' => '123456', 'google_id' => ''))); + $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->getByGoogleId('1234')); - $this->assertEmpty($u->getByGoogleId('')); + $this->assertNotEmpty($u->getByExternalId('google_id', '1234')); + $this->assertEmpty($u->getByExternalId('google_id', '')); } public function testGetByToken() @@ -126,13 +96,14 @@ class UserTest extends Base $this->assertEquals('you', $users[2]['username']); } - public function testGetList() + public function testGetActiveUsersList() { $u = new User($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->getList(); + $users = $u->getActiveUsersList(); $expected = array( 1 => 'admin', @@ -142,7 +113,7 @@ class UserTest extends Base $this->assertEquals($expected, $users); - $users = $u->getList(true); + $users = $u->getActiveUsersList(true); $expected = array( User::EVERYBODY_ID => 'Everybody', @@ -197,7 +168,7 @@ class UserTest extends Base 'password' => '1234', 'confirmation' => '1234', 'name' => 'me', - 'is_admin' => '', + 'role' => Role::APP_ADMIN, ); $u->prepare($input); @@ -207,9 +178,6 @@ class UserTest extends Base $this->assertNotEquals('1234', $input['password']); $this->assertNotEmpty($input['password']); - $this->assertArrayHasKey('is_admin', $input); - $this->assertInternalType('integer', $input['is_admin']); - $input = array( 'username' => 'user1', 'password' => '1234', @@ -273,8 +241,8 @@ class UserTest extends Base $u = new User($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', 'is_project_admin' => 1))); - $this->assertEquals(5, $u->create(array('username' => 'user #4', 'gitlab_id' => ''))); + $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'))); @@ -283,7 +251,7 @@ class UserTest extends Base $this->assertTrue(is_array($user)); $this->assertEquals('admin', $user['username']); $this->assertEquals('', $user['name']); - $this->assertEquals(1, $user['is_admin']); + $this->assertEquals(Role::APP_ADMIN, $user['role']); $this->assertEquals(0, $user['is_ldap_user']); $user = $u->getById(2); @@ -291,7 +259,7 @@ class UserTest extends Base $this->assertTrue(is_array($user)); $this->assertEquals('user #1', $user['username']); $this->assertEquals('User', $user['name']); - $this->assertEquals(0, $user['is_admin']); + $this->assertEquals(Role::APP_USER, $user['role']); $this->assertEquals(0, $user['is_ldap_user']); $user = $u->getById(3); @@ -299,27 +267,28 @@ class UserTest extends Base $this->assertTrue(is_array($user)); $this->assertEquals('user #2', $user['username']); $this->assertEquals('', $user['name']); - $this->assertEquals(0, $user['is_admin']); + $this->assertEquals(Role::APP_USER, $user['role']); $this->assertEquals(1, $user['is_ldap_user']); $user = $u->getById(4); $this->assertNotFalse($user); $this->assertTrue(is_array($user)); $this->assertEquals('user #3', $user['username']); - $this->assertEquals(0, $user['is_admin']); - $this->assertEquals(1, $user['is_project_admin']); + $this->assertEquals(Role::APP_MANAGER, $user['role']); $user = $u->getById(5); $this->assertNotFalse($user); $this->assertTrue(is_array($user)); $this->assertEquals('user #4', $user['username']); $this->assertEquals('', $user['gitlab_id']); + $this->assertEquals(Role::APP_ADMIN, $user['role']); $user = $u->getById(6); $this->assertNotFalse($user); $this->assertTrue(is_array($user)); $this->assertEquals('user #5', $user['username']); $this->assertEquals('1234', $user['gitlab_id']); + $this->assertEquals(Role::APP_USER, $user['role']); } public function testUpdate() @@ -336,7 +305,7 @@ class UserTest extends Base $this->assertTrue(is_array($user)); $this->assertEquals('biloute', $user['username']); $this->assertEquals('Toto', $user['name']); - $this->assertEquals(0, $user['is_admin']); + $this->assertEquals(Role::APP_USER, $user['role']); $this->assertEquals(0, $user['is_ldap_user']); $user = $u->getById(3); @@ -423,4 +392,24 @@ class UserTest extends Base $this->assertEquals('toto', $user['username']); $this->assertEmpty($user['token']); } + + public function testEnableDisable() + { + $userModel = new User($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/UserUnreadNotificationTest.php b/tests/units/Model/UserUnreadNotificationTest.php index bf274d95..62889bf0 100644 --- a/tests/units/Model/UserUnreadNotificationTest.php +++ b/tests/units/Model/UserUnreadNotificationTest.php @@ -7,7 +7,6 @@ use Kanboard\Model\TaskCreation; use Kanboard\Model\Subtask; use Kanboard\Model\Comment; use Kanboard\Model\User; -use Kanboard\Model\File; use Kanboard\Model\Task; use Kanboard\Model\Project; use Kanboard\Model\UserUnreadNotification; |