diff options
Diffstat (limited to 'tests/units/Model')
43 files changed, 4703 insertions, 3871 deletions
diff --git a/tests/units/Model/ActionTest.php b/tests/units/Model/ActionModelTest.php index 5db18983..4e21a999 100644 --- a/tests/units/Model/ActionTest.php +++ b/tests/units/Model/ActionModelTest.php @@ -11,7 +11,7 @@ use Kanboard\Model\CategoryModel; use Kanboard\Model\ProjectUserRoleModel; use Kanboard\Core\Security\Role; -class ActionTest extends Base +class ActionModelTest extends Base { public function testCreate() { @@ -69,6 +69,24 @@ class ActionTest extends Base $this->assertEquals(array('column_id' => 1, 'color_id' => 'red'), $action['params']); } + public function testGetProjectId() + { + $projectModel = new ProjectModel($this->container); + $actionModel = new ActionModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => TaskModel::EVENT_CREATE, + 'action_name' => '\Kanboard\Action\TaskAssignColorColumn', + 'params' => array('column_id' => 1, 'color_id' => 'red'), + ))); + + $this->assertEquals(1, $actionModel->getProjectId(1)); + $this->assertSame(0, $actionModel->getProjectId(42)); + } + public function testGetAll() { $projectModel = new ProjectModel($this->container); diff --git a/tests/units/Model/BoardTest.php b/tests/units/Model/BoardTest.php index 80587d89..9f540c63 100644 --- a/tests/units/Model/BoardTest.php +++ b/tests/units/Model/BoardTest.php @@ -3,12 +3,8 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\ProjectModel; -use Kanboard\Model\BoardModel; use Kanboard\Model\ColumnModel; use Kanboard\Model\ConfigModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\SwimlaneModel; class BoardTest extends Base { @@ -45,121 +41,4 @@ class BoardTest extends Base $this->assertEquals('column #1', $columns[5]); $this->assertEquals('column #2', $columns[6]); } - - public function testGetBoard() - { - $p = new ProjectModel($this->container); - $b = new BoardModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - $board = $b->getBoard(1); - $this->assertNotEmpty($board); - $this->assertEquals(1, count($board)); - $this->assertEquals(6, count($board[0])); - $this->assertArrayHasKey('name', $board[0]); - $this->assertArrayHasKey('nb_tasks', $board[0]); - $this->assertArrayHasKey('columns', $board[0]); - $this->assertArrayHasKey('tasks', $board[0]['columns'][2]); - $this->assertArrayHasKey('nb_tasks', $board[0]['columns'][2]); - $this->assertArrayHasKey('title', $board[0]['columns'][2]); - $this->assertArrayHasKey('nb_column_tasks', $board[0]['columns'][0]); - $this->assertArrayHasKey('total_score', $board[0]['columns'][0]); - } - - public function testGetBoardWithSwimlane() - { - $b = new BoardModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'test 1'))); - $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' => 3))); - $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); - $this->assertEquals(4, $tc->create(array('title' => 'Task #4', 'project_id' => 1, 'column_id' => 3))); - $this->assertEquals(5, $tc->create(array('title' => 'Task #5', 'project_id' => 1, 'column_id' => 4, 'score' => 2))); - $this->assertEquals(6, $tc->create(array('title' => 'Task #6', 'project_id' => 1, 'column_id' => 4, 'score' => 3, 'swimlane_id' => 1))); - - $board = $b->getBoard(1); - $this->assertNotEmpty($board); - $this->assertEquals(2, count($board)); - $this->assertEquals(6, count($board[0])); - $this->assertArrayHasKey('name', $board[0]); - $this->assertArrayHasKey('nb_tasks', $board[0]); - $this->assertArrayHasKey('columns', $board[0]); - $this->assertArrayHasKey('tasks', $board[0]['columns'][2]); - $this->assertArrayHasKey('nb_tasks', $board[0]['columns'][2]); - $this->assertArrayHasKey('title', $board[0]['columns'][2]); - $this->assertArrayHasKey('nb_column_tasks', $board[0]['columns'][0]); - $this->assertArrayNotHasKey('nb_column_tasks', $board[1]['columns'][0]); - $this->assertArrayNotHasKey('total_score', $board[1]['columns'][0]); - $this->assertArrayHasKey('score', $board[0]['columns'][3]); - $this->assertArrayHasKey('total_score', $board[0]['columns'][3]); - $this->assertEquals(2, $board[0]['columns'][3]['score']); - $this->assertEquals(5, $board[0]['columns'][3]['total_score']); - - $task = $tf->getById(1); - $this->assertEquals(1, $task['id']); - $this->assertEquals(1, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(1, $board[0]['columns'][0]['tasks'][0]['id']); - $this->assertEquals(1, $board[0]['columns'][0]['tasks'][0]['column_id']); - $this->assertEquals(1, $board[0]['columns'][0]['tasks'][0]['position']); - $this->assertEquals(0, $board[0]['columns'][0]['tasks'][0]['swimlane_id']); - - $task = $tf->getById(2); - $this->assertEquals(2, $task['id']); - $this->assertEquals(3, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(2, $board[0]['columns'][2]['tasks'][0]['id']); - $this->assertEquals(3, $board[0]['columns'][2]['tasks'][0]['column_id']); - $this->assertEquals(1, $board[0]['columns'][2]['tasks'][0]['position']); - $this->assertEquals(0, $board[0]['columns'][2]['tasks'][0]['swimlane_id']); - - $task = $tf->getById(3); - $this->assertEquals(3, $task['id']); - $this->assertEquals(2, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(1, $task['swimlane_id']); - $this->assertEquals(3, $board[1]['columns'][1]['tasks'][0]['id']); - $this->assertEquals(2, $board[1]['columns'][1]['tasks'][0]['column_id']); - $this->assertEquals(1, $board[1]['columns'][1]['tasks'][0]['position']); - $this->assertEquals(1, $board[1]['columns'][1]['tasks'][0]['swimlane_id']); - - $task = $tf->getById(4); - $this->assertEquals(4, $task['id']); - $this->assertEquals(3, $task['column_id']); - $this->assertEquals(2, $task['position']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(4, $board[0]['columns'][2]['tasks'][1]['id']); - $this->assertEquals(3, $board[0]['columns'][2]['tasks'][1]['column_id']); - $this->assertEquals(2, $board[0]['columns'][2]['tasks'][1]['position']); - $this->assertEquals(0, $board[0]['columns'][2]['tasks'][1]['swimlane_id']); - - $task = $tf->getById(5); - $this->assertEquals(5, $task['id']); - $this->assertEquals(4, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(5, $board[0]['columns'][3]['tasks'][0]['id']); - $this->assertEquals(4, $board[0]['columns'][3]['tasks'][0]['column_id']); - $this->assertEquals(1, $board[0]['columns'][3]['tasks'][0]['position']); - $this->assertEquals(0, $board[0]['columns'][3]['tasks'][0]['swimlane_id']); - - $task = $tf->getById(6); - $this->assertEquals(6, $task['id']); - $this->assertEquals(4, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(1, $task['swimlane_id']); - $this->assertEquals(6, $board[1]['columns'][3]['tasks'][0]['id']); - $this->assertEquals(4, $board[1]['columns'][3]['tasks'][0]['column_id']); - $this->assertEquals(1, $board[1]['columns'][3]['tasks'][0]['position']); - $this->assertEquals(1, $board[1]['columns'][3]['tasks'][0]['swimlane_id']); - } } diff --git a/tests/units/Model/CategoryTest.php b/tests/units/Model/CategoryModelTest.php index 1fdc51f6..80a20af6 100644 --- a/tests/units/Model/CategoryTest.php +++ b/tests/units/Model/CategoryModelTest.php @@ -8,7 +8,7 @@ use Kanboard\Model\TaskFinderModel; use Kanboard\Model\ProjectModel; use Kanboard\Model\CategoryModel; -class CategoryTest extends Base +class CategoryModelTest extends Base { public function testCreation() { @@ -81,6 +81,18 @@ class CategoryTest extends Base $this->assertSame(0, $categoryModel->getIdByName(1, 'Category #2')); } + public function testGetProjectId() + { + $projectModel = new ProjectModel($this->container); + $categoryModel = new CategoryModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $categoryModel->create(array('name' => 'Category #1', 'project_id' => 1, 'description' => 'test'))); + + $this->assertEquals(1, $categoryModel->getProjectId(1)); + $this->assertSame(0, $categoryModel->getProjectId(2)); + } + public function testGetList() { $projectModel = new ProjectModel($this->container); diff --git a/tests/units/Model/CommentModelTest.php b/tests/units/Model/CommentModelTest.php new file mode 100644 index 00000000..4178a839 --- /dev/null +++ b/tests/units/Model/CommentModelTest.php @@ -0,0 +1,106 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\CommentModel; + +class CommentModelTest extends Base +{ + public function testCreate() + { + $commentModel = new CommentModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'comment' => 'bla bla', 'user_id' => 1))); + $this->assertEquals(2, $commentModel->create(array('task_id' => 1, 'comment' => 'bla bla'))); + + $comment = $commentModel->getById(1); + $this->assertNotEmpty($comment); + $this->assertEquals('bla bla', $comment['comment']); + $this->assertEquals(1, $comment['task_id']); + $this->assertEquals(1, $comment['user_id']); + $this->assertEquals('admin', $comment['username']); + $this->assertEquals(time(), $comment['date_creation'], '', 3); + + $comment = $commentModel->getById(2); + $this->assertNotEmpty($comment); + $this->assertEquals('bla bla', $comment['comment']); + $this->assertEquals(1, $comment['task_id']); + $this->assertEquals(0, $comment['user_id']); + $this->assertEquals('', $comment['username']); + $this->assertEquals(time(), $comment['date_creation'], '', 3); + } + + public function testGetAll() + { + $commentModel = new CommentModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1))); + $this->assertEquals(2, $commentModel->create(array('task_id' => 1, 'comment' => 'c2', 'user_id' => 1))); + $this->assertEquals(3, $commentModel->create(array('task_id' => 1, 'comment' => 'c3', 'user_id' => 1))); + + $comments = $commentModel->getAll(1); + + $this->assertNotEmpty($comments); + $this->assertEquals(3, count($comments)); + $this->assertEquals(1, $comments[0]['id']); + $this->assertEquals(2, $comments[1]['id']); + $this->assertEquals(3, $comments[2]['id']); + + $this->assertEquals(3, $commentModel->count(1)); + } + + public function testUpdate() + { + $commentModel = new CommentModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1))); + $this->assertTrue($commentModel->update(array('id' => 1, 'comment' => 'bla'))); + + $comment = $commentModel->getById(1); + $this->assertNotEmpty($comment); + $this->assertEquals('bla', $comment['comment']); + } + + public function testRemove() + { + $commentModel = new CommentModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1))); + + $this->assertTrue($commentModel->remove(1)); + $this->assertFalse($commentModel->remove(1)); + $this->assertFalse($commentModel->remove(1111)); + } + + public function testGetProjectId() + { + $commentModel = new CommentModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1))); + + $this->assertEquals(1, $commentModel->getProjectId(1)); + $this->assertSame(0, $commentModel->getProjectId(2)); + } +} diff --git a/tests/units/Model/CommentTest.php b/tests/units/Model/CommentTest.php deleted file mode 100644 index 7250ae0b..00000000 --- a/tests/units/Model/CommentTest.php +++ /dev/null @@ -1,92 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\ProjectModel; -use Kanboard\Model\CommentModel; - -class CommentTest extends Base -{ - public function testCreate() - { - $c = new CommentModel($this->container); - $tc = new TaskCreationModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - $this->assertEquals(1, $c->create(array('task_id' => 1, 'comment' => 'bla bla', 'user_id' => 1))); - $this->assertEquals(2, $c->create(array('task_id' => 1, 'comment' => 'bla bla'))); - - $comment = $c->getById(1); - $this->assertNotEmpty($comment); - $this->assertEquals('bla bla', $comment['comment']); - $this->assertEquals(1, $comment['task_id']); - $this->assertEquals(1, $comment['user_id']); - $this->assertEquals('admin', $comment['username']); - $this->assertEquals(time(), $comment['date_creation'], '', 3); - - $comment = $c->getById(2); - $this->assertNotEmpty($comment); - $this->assertEquals('bla bla', $comment['comment']); - $this->assertEquals(1, $comment['task_id']); - $this->assertEquals(0, $comment['user_id']); - $this->assertEquals('', $comment['username']); - $this->assertEquals(time(), $comment['date_creation'], '', 3); - } - - public function testGetAll() - { - $c = new CommentModel($this->container); - $tc = new TaskCreationModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - $this->assertNotFalse($c->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1))); - $this->assertNotFalse($c->create(array('task_id' => 1, 'comment' => 'c2', 'user_id' => 1))); - $this->assertNotFalse($c->create(array('task_id' => 1, 'comment' => 'c3', 'user_id' => 1))); - - $comments = $c->getAll(1); - - $this->assertNotEmpty($comments); - $this->assertEquals(3, count($comments)); - $this->assertEquals(1, $comments[0]['id']); - $this->assertEquals(2, $comments[1]['id']); - $this->assertEquals(3, $comments[2]['id']); - - $this->assertEquals(3, $c->count(1)); - } - - public function testUpdate() - { - $c = new CommentModel($this->container); - $tc = new TaskCreationModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - $this->assertNotFalse($c->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1))); - $this->assertTrue($c->update(array('id' => 1, 'comment' => 'bla'))); - - $comment = $c->getById(1); - $this->assertNotEmpty($comment); - $this->assertEquals('bla', $comment['comment']); - } - - public function validateRemove() - { - $c = new CommentModel($this->container); - $tc = new TaskCreationModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); - $this->assertTrue($c->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1))); - - $this->assertTrue($c->remove(1)); - $this->assertFalse($c->remove(1)); - $this->assertFalse($c->remove(1111)); - } -} diff --git a/tests/units/Model/GroupTest.php b/tests/units/Model/GroupModelTest.php index 85c2c5d9..4ad0a167 100644 --- a/tests/units/Model/GroupTest.php +++ b/tests/units/Model/GroupModelTest.php @@ -4,7 +4,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\GroupModel; -class GroupTest extends Base +class GroupModelTest extends Base { public function testCreation() { @@ -57,4 +57,13 @@ class GroupTest extends Base $this->assertTrue($groupModel->remove(1)); $this->assertEmpty($groupModel->getById(1)); } + + public function testGetOrCreateExternalGroupId() + { + $groupModel = new GroupModel($this->container); + $this->assertEquals(1, $groupModel->create('Group 1', 'ExternalId1')); + $this->assertEquals(1, $groupModel->getOrCreateExternalGroupId('Group 1', 'ExternalId1')); + $this->assertEquals(1, $groupModel->getOrCreateExternalGroupId('Group 2', 'ExternalId1')); + $this->assertEquals(2, $groupModel->getOrCreateExternalGroupId('Group 2', 'ExternalId2')); + } } diff --git a/tests/units/Model/NotificationModelTest.php b/tests/units/Model/NotificationModelTest.php new file mode 100644 index 00000000..0bd9db6e --- /dev/null +++ b/tests/units/Model/NotificationModelTest.php @@ -0,0 +1,104 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\CommentModel; +use Kanboard\Model\TaskFileModel; +use Kanboard\Model\TaskLinkModel; +use Kanboard\Model\TaskModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\NotificationModel; +use Kanboard\Subscriber\NotificationSubscriber; + +class NotificationModelTest extends Base +{ + public function testGetTitle() + { + $notificationModel = new NotificationModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $commentModel = new CommentModel($this->container); + $taskFileModel = new TaskFileModel($this->container); + $taskLinkModel = new TaskLinkModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'test', 'task_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('comment' => 'test', 'task_id' => 1, 'user_id' => 1))); + $this->assertEquals(1, $taskFileModel->create(1, 'test', 'blah', 123)); + $this->assertEquals(1, $taskLinkModel->create(1, 2, 1)); + + $task = $taskFinderModel->getDetails(1); + $subtask = $subtaskModel->getById(1, true); + $comment = $commentModel->getById(1); + $file = $commentModel->getById(1); + $tasklink = $taskLinkModel->getById(1); + + foreach (NotificationSubscriber::getSubscribedEvents() as $eventName => $values) { + $eventData = array( + 'task' => $task, + 'comment' => $comment, + 'subtask' => $subtask, + 'file' => $file, + 'task_link' => $tasklink, + 'changes' => array() + ); + + $this->assertNotEmpty($notificationModel->getTitleWithoutAuthor($eventName, $eventData)); + $this->assertNotEmpty($notificationModel->getTitleWithAuthor('Foobar', $eventName, $eventData)); + } + + $this->assertNotEmpty($notificationModel->getTitleWithoutAuthor(TaskModel::EVENT_OVERDUE, array('tasks' => array(array('id' => 1))))); + $this->assertNotEmpty($notificationModel->getTitleWithoutAuthor('unknown', array())); + } + + public function testGetTaskIdFromEvent() + { + $notificationModel = new NotificationModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $commentModel = new CommentModel($this->container); + $taskFileModel = new TaskFileModel($this->container); + $taskLinkModel = new TaskLinkModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'test', 'task_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('comment' => 'test', 'task_id' => 1, 'user_id' => 1))); + $this->assertEquals(1, $taskFileModel->create(1, 'test', 'blah', 123)); + + $task = $taskFinderModel->getDetails(1); + $subtask = $subtaskModel->getById(1, true); + $comment = $commentModel->getById(1); + $file = $commentModel->getById(1); + $tasklink = $taskLinkModel->getById(1); + + $this->assertNotEmpty($task); + $this->assertNotEmpty($subtask); + $this->assertNotEmpty($comment); + $this->assertNotEmpty($file); + + foreach (NotificationSubscriber::getSubscribedEvents() as $eventName => $values) { + $task_id = $notificationModel->getTaskIdFromEvent($eventName, array( + 'task' => $task, + 'comment' => $comment, + 'subtask' => $subtask, + 'file' => $file, + 'task_link' => $tasklink, + 'changes' => array() + )); + + $this->assertEquals($task_id, $task['id']); + } + + $this->assertEquals(1, $notificationModel->getTaskIdFromEvent(TaskModel::EVENT_OVERDUE, array('tasks' => array(array('id' => 1))))); + } +} diff --git a/tests/units/Model/NotificationTest.php b/tests/units/Model/NotificationTest.php deleted file mode 100644 index 96ee5f4e..00000000 --- a/tests/units/Model/NotificationTest.php +++ /dev/null @@ -1,68 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\SubtaskModel; -use Kanboard\Model\CommentModel; -use Kanboard\Model\TaskFileModel; -use Kanboard\Model\TaskModel; -use Kanboard\Model\ProjectModel; -use Kanboard\Model\NotificationModel; -use Kanboard\Subscriber\NotificationSubscriber; - -class NotificationTest extends Base -{ - public function testGetTitle() - { - $wn = new NotificationModel($this->container); - $p = new ProjectModel($this->container); - $tf = new TaskFinderModel($this->container); - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $c = new CommentModel($this->container); - $f = new TaskFileModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - $this->assertEquals(1, $s->create(array('title' => 'test', 'task_id' => 1))); - $this->assertEquals(1, $c->create(array('comment' => 'test', 'task_id' => 1, 'user_id' => 1))); - $this->assertEquals(1, $f->create(1, 'test', 'blah', 123)); - - $task = $tf->getDetails(1); - $subtask = $s->getById(1, true); - $comment = $c->getById(1); - $file = $c->getById(1); - - $this->assertNotEmpty($task); - $this->assertNotEmpty($subtask); - $this->assertNotEmpty($comment); - $this->assertNotEmpty($file); - - foreach (NotificationSubscriber::getSubscribedEvents() as $event_name => $values) { - $title = $wn->getTitleWithoutAuthor($event_name, array( - 'task' => $task, - 'comment' => $comment, - 'subtask' => $subtask, - 'file' => $file, - 'changes' => array() - )); - - $this->assertNotEmpty($title); - - $title = $wn->getTitleWithAuthor('foobar', $event_name, array( - 'task' => $task, - 'comment' => $comment, - 'subtask' => $subtask, - 'file' => $file, - 'changes' => array() - )); - - $this->assertNotEmpty($title); - } - - $this->assertNotEmpty($wn->getTitleWithoutAuthor(TaskModel::EVENT_OVERDUE, array('tasks' => array(array('id' => 1))))); - $this->assertNotEmpty($wn->getTitleWithoutAuthor('unkown', array())); - } -} diff --git a/tests/units/Model/ProjectDuplicationModelTest.php b/tests/units/Model/ProjectDuplicationModelTest.php new file mode 100644 index 00000000..54261728 --- /dev/null +++ b/tests/units/Model/ProjectDuplicationModelTest.php @@ -0,0 +1,548 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\ActionModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\CategoryModel; +use Kanboard\Model\ProjectUserRoleModel; +use Kanboard\Model\ProjectGroupRoleModel; +use Kanboard\Model\ProjectDuplicationModel; +use Kanboard\Model\TagModel; +use Kanboard\Model\TaskTagModel; +use Kanboard\Model\UserModel; +use Kanboard\Model\GroupModel; +use Kanboard\Model\GroupMemberModel; +use Kanboard\Model\SwimlaneModel; +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Core\Security\Role; + +class ProjectDuplicationModelTest extends Base +{ + public function testGetSelections() + { + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $this->assertCount(7, $projectDuplicationModel->getOptionalSelection()); + $this->assertCount(8, $projectDuplicationModel->getPossibleSelection()); + } + + public function testGetClonedProjectName() + { + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals('test (Clone)', $projectDuplicationModel->getClonedProjectName('test')); + + $this->assertEquals(50, strlen($projectDuplicationModel->getClonedProjectName(str_repeat('a', 50)))); + $this->assertEquals(str_repeat('a', 42).' (Clone)', $projectDuplicationModel->getClonedProjectName(str_repeat('a', 50))); + + $this->assertEquals(50, strlen($projectDuplicationModel->getClonedProjectName(str_repeat('a', 60)))); + $this->assertEquals(str_repeat('a', 42).' (Clone)', $projectDuplicationModel->getClonedProjectName(str_repeat('a', 60))); + } + + public function testClonePublicProject() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Public'))); + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $project = $projectModel->getById(2); + $this->assertNotEmpty($project); + $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 testClonePrivateProject() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Private', 'is_private' => 1), 1, true)); + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $project = $projectModel->getById(2); + $this->assertNotEmpty($project); + $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, $projectUserRoleModel->getUserRole(2, 1)); + } + + public function testCloneSharedProject() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Shared'))); + $this->assertTrue($projectModel->update(array('id' => 1, 'is_public' => 1, 'token' => 'test'))); + + $project = $projectModel->getById(1); + $this->assertEquals('test', $project['token']); + $this->assertEquals(1, $project['is_public']); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $project = $projectModel->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Shared (Clone)', $project['name']); + $this->assertEquals('', $project['token']); + $this->assertEquals(0, $project['is_public']); + } + + public function testCloneInactiveProject() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Inactive'))); + $this->assertTrue($projectModel->update(array('id' => 1, 'is_active' => 0))); + + $project = $projectModel->getById(1); + $this->assertEquals(0, $project['is_active']); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $project = $projectModel->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Inactive (Clone)', $project['name']); + $this->assertEquals(1, $project['is_active']); + } + + public function testCloneProjectWithOwner() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Owner'))); + + $project = $projectModel->getById(1); + $this->assertEquals(0, $project['owner_id']); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('projectPermissionModel'), 1)); + + $project = $projectModel->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 testCloneProjectWithDifferentPriorities() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array( + 'name' => 'My project', + 'priority_default' => 2, + 'priority_start' => -2, + 'priority_end' => 8, + ))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $project = $projectModel->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('My project (Clone)', $project['name']); + $this->assertEquals(2, $project['priority_default']); + $this->assertEquals(-2, $project['priority_start']); + $this->assertEquals(8, $project['priority_end']); + } + + public function testCloneProjectWithDifferentName() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Owner'))); + + $project = $projectModel->getById(1); + $this->assertEquals(0, $project['owner_id']); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('projectPermissionModel'), 1, 'Foobar')); + + $project = $projectModel->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Foobar', $project['name']); + $this->assertEquals(1, $project['owner_id']); + } + + public function testCloneProjectAndForceItToBePrivate() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Owner'))); + + $project = $projectModel->getById(1); + $this->assertEquals(0, $project['owner_id']); + $this->assertEquals(0, $project['is_private']); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('projectPermissionModel'), 1, 'Foobar', true)); + + $project = $projectModel->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() + { + $projectModel = new ProjectModel($this->container); + $categoryModel = new CategoryModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + + $this->assertEquals(1, $categoryModel->create(array('name' => 'C1', 'project_id' => 1))); + $this->assertEquals(2, $categoryModel->create(array('name' => 'C2', 'project_id' => 1))); + $this->assertEquals(3, $categoryModel->create(array('name' => 'C3', 'project_id' => 1))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $project = $projectModel->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('P1 (Clone)', $project['name']); + + $categories = $categoryModel->getAll(2); + $this->assertCount(3, $categories); + $this->assertEquals('C1', $categories[0]['name']); + $this->assertEquals('C2', $categories[1]['name']); + $this->assertEquals('C3', $categories[2]['name']); + } + + public function testCloneProjectWithUsers() + { + $projectModel = new ProjectModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + $userModel = new UserModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $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(1, $projectModel->create(array('name' => 'P1'))); + + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($projectUserRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($projectUserRoleModel->addUser(1, 4, Role::PROJECT_VIEWER)); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $this->assertCount(3, $projectUserRoleModel->getUsers(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 testCloneProjectWithUsersAndOverrideOwner() + { + $projectModel = new ProjectModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + $userModel = new UserModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'), 2)); + + $project = $projectModel->getById(1); + $this->assertEquals(2, $project['owner_id']); + + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($projectUserRoleModel->addUser(1, 1, Role::PROJECT_MEMBER)); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('projectPermissionModel'), 1)); + + $this->assertCount(2, $projectUserRoleModel->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $projectUserRoleModel->getUserRole(2, 2)); + $this->assertEquals(Role::PROJECT_MANAGER, $projectUserRoleModel->getUserRole(2, 1)); + + $project = $projectModel->getById(2); + $this->assertEquals(1, $project['owner_id']); + } + + public function testCloneTeamProjectToPrivatProject() + { + $projectModel = new ProjectModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + $userModel = new UserModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user2'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'), 2)); + + $project = $projectModel->getById(1); + $this->assertEquals(2, $project['owner_id']); + $this->assertEquals(0, $project['is_private']); + + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MANAGER)); + $this->assertTrue($projectUserRoleModel->addUser(1, 1, Role::PROJECT_MEMBER)); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('projectPermissionModel'), 3, 'My private project', true)); + + $this->assertCount(1, $projectUserRoleModel->getUsers(2)); + $this->assertEquals(Role::PROJECT_MANAGER, $projectUserRoleModel->getUserRole(2, 3)); + + $project = $projectModel->getById(2); + $this->assertEquals(3, $project['owner_id']); + $this->assertEquals(1, $project['is_private']); + } + + public function testCloneProjectWithGroups() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $userModel = new UserModel($this->container); + $groupModel = new GroupModel($this->container); + $groupMemberModel = new GroupMemberModel($this->container); + $projectGroupRoleModel = new ProjectGroupRoleModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + + $this->assertEquals(1, $projectModel->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, $projectDuplicationModel->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() + { + $projectModel = new ProjectModel($this->container); + $actionModel = new ActionModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => TaskModel::EVENT_MOVE_COLUMN, + 'action_name' => 'TaskAssignCurrentUser', + 'params' => array('column_id' => 2), + ))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $actions = $actionModel->getAllByProject(2); + + $this->assertNotEmpty($actions); + $this->assertEquals('TaskAssignCurrentUser', $actions[0]['action_name']); + $this->assertNotEmpty($actions[0]['params']); + $this->assertEquals(6, $actions[0]['params']['column_id']); + } + + public function testCloneProjectWithActionTaskAssignColorCategory() + { + $projectModel = new ProjectModel($this->container); + $actionModel = new ActionModel($this->container); + $categoryModel = new CategoryModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + + $this->assertEquals(1, $categoryModel->create(array('name' => 'C1', 'project_id' => 1))); + $this->assertEquals(2, $categoryModel->create(array('name' => 'C2', 'project_id' => 1))); + $this->assertEquals(3, $categoryModel->create(array('name' => 'C3', 'project_id' => 1))); + + $this->assertEquals(1, $actionModel->create(array( + 'project_id' => 1, + 'event_name' => TaskModel::EVENT_CREATE_UPDATE, + 'action_name' => 'TaskAssignColorCategory', + 'params' => array('color_id' => 'blue', 'category_id' => 2), + ))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1)); + + $actions = $actionModel->getAllByProject(2); + + $this->assertNotEmpty($actions); + $this->assertEquals('TaskAssignColorCategory', $actions[0]['action_name']); + $this->assertNotEmpty($actions[0]['params']); + $this->assertEquals('blue', $actions[0]['params']['color_id']); + $this->assertEquals(5, $actions[0]['params']['category_id']); + } + + public function testCloneProjectWithSwimlanes() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1', 'default_swimlane' => 'New Default'))); + + // create initial swimlanes + $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'S1'))); + $this->assertEquals(2, $swimlaneModel->create(array('project_id' => 1, 'name' => 'S2'))); + $this->assertEquals(3, $swimlaneModel->create(array('project_id' => 1, 'name' => 'S3'))); + + // create initial tasks + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'T0', 'project_id' => 1, 'swimlane_id' => 0))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'T1', 'project_id' => 1, 'swimlane_id' => 1))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'T2', 'project_id' => 1, 'swimlane_id' => 2))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'T3', 'project_id' => 1, 'swimlane_id' => 3))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('categoryModel', 'swimlaneModel'))); + + $swimlanes = $swimlaneModel->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']); + + $swimlane = $swimlaneModel->getDefault(2); + $this->assertEquals('New Default', $swimlane['default_swimlane']); + + // Check if tasks are NOT been duplicated + $this->assertCount(0, $taskFinderModel->getAll(2)); + } + + public function testCloneProjectWithTasks() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + + // create initial tasks + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('categoryModel', 'actionModel', 'projectTaskDuplicationModel'))); + + // Check if Tasks have been duplicated + $tasks = $taskFinderModel->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 testCloneProjectWithSwimlanesAndTasks() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1', 'default_swimlane' => 'New Default'))); + + // create initial swimlanes + $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'S1'))); + $this->assertEquals(2, $swimlaneModel->create(array('project_id' => 1, 'name' => 'S2'))); + $this->assertEquals(3, $swimlaneModel->create(array('project_id' => 1, 'name' => 'S3'))); + + // create initial tasks + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('projectPermissionModel', 'swimlaneModel', 'projectTaskDuplicationModel'))); + + // Check if Swimlanes have been duplicated + $swimlanes = $swimlaneModel->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']); + + $swimlane = $swimlaneModel->getDefault(2); + $this->assertEquals('New Default', $swimlane['default_swimlane']); + + // Check if Tasks have been duplicated + $tasks = $taskFinderModel->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 testCloneProjectWithTags() + { + $projectModel = new ProjectModel($this->container); + $projectDuplicationModel = new ProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $tagModel = new TagModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'tags' => array('A')))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'tags' => array('A', 'B')))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'tags' => array('C')))); + + $this->assertEquals(2, $projectDuplicationModel->duplicate(1, array('categoryModel', 'actionModel', 'tagDuplicationModel', 'projectTaskDuplicationModel'))); + + $tasks = $taskFinderModel->getAll(2); + $this->assertCount(3, $tasks); + $this->assertEquals('T1', $tasks[0]['title']); + $this->assertEquals('T2', $tasks[1]['title']); + $this->assertEquals('T3', $tasks[2]['title']); + + $tags = $tagModel->getAllByProject(2); + $this->assertCount(3, $tags); + $this->assertEquals(4, $tags[0]['id']); + $this->assertEquals('A', $tags[0]['name']); + $this->assertEquals(5, $tags[1]['id']); + $this->assertEquals('B', $tags[1]['name']); + $this->assertEquals(6, $tags[2]['id']); + $this->assertEquals('C', $tags[2]['name']); + + $tags = $taskTagModel->getList(4); + $this->assertEquals('A', $tags[4]); + + $tags = $taskTagModel->getList(5); + $this->assertEquals('A', $tags[4]); + $this->assertEquals('B', $tags[5]); + + $tags = $taskTagModel->getList(6); + $this->assertEquals('C', $tags[6]); + } +} diff --git a/tests/units/Model/ProjectDuplicationTest.php b/tests/units/Model/ProjectDuplicationTest.php deleted file mode 100644 index 312c7168..00000000 --- a/tests/units/Model/ProjectDuplicationTest.php +++ /dev/null @@ -1,482 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\ActionModel; -use Kanboard\Model\ProjectModel; -use Kanboard\Model\CategoryModel; -use Kanboard\Model\ProjectUserRoleModel; -use Kanboard\Model\ProjectGroupRoleModel; -use Kanboard\Model\ProjectDuplicationModel; -use Kanboard\Model\UserModel; -use Kanboard\Model\GroupModel; -use Kanboard\Model\GroupMemberModel; -use Kanboard\Model\SwimlaneModel; -use Kanboard\Model\TaskModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Core\Security\Role; - -class ProjectDuplicationTest extends Base -{ - public function testGetSelections() - { - $projectDuplicationModel = new ProjectDuplicationModel($this->container); - $this->assertCount(6, $projectDuplicationModel->getOptionalSelection()); - $this->assertCount(7, $projectDuplicationModel->getPossibleSelection()); - } - - public function testGetClonedProjectName() - { - $pd = new ProjectDuplicationModel($this->container); - - $this->assertEquals('test (Clone)', $pd->getClonedProjectName('test')); - - $this->assertEquals(50, strlen($pd->getClonedProjectName(str_repeat('a', 50)))); - $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 50))); - - $this->assertEquals(50, strlen($pd->getClonedProjectName(str_repeat('a', 60)))); - $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 60))); - } - - public function testClonePublicProject() - { - $p = new ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'Public'))); - $this->assertEquals(2, $pd->duplicate(1)); - - $project = $p->getById(2); - $this->assertNotEmpty($project); - $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 testClonePrivateProject() - { - $p = new ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - $pp = new ProjectUserRoleModel($this->container); - - $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('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 testCloneSharedProject() - { - $p = new ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - - $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('Shared (Clone)', $project['name']); - $this->assertEquals('', $project['token']); - $this->assertEquals(0, $project['is_public']); - } - - public function testCloneInactiveProject() - { - $p = new ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - - $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 ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - $projectUserRoleModel = new ProjectUserRoleModel($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('projectPermissionModel'), 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 ProjectModel($this->container); - $pd = new ProjectDuplicationModel($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('projectPermissionModel'), 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 ProjectModel($this->container); - $pd = new ProjectDuplicationModel($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('projectPermissionModel'), 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() - { - $p = new ProjectModel($this->container); - $c = new CategoryModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - - $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1))); - $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 1))); - $this->assertEquals(3, $c->create(array('name' => 'C3', 'project_id' => 1))); - - $this->assertEquals(2, $pd->duplicate(1)); - - $project = $p->getById(2); - $this->assertNotEmpty($project); - $this->assertEquals('P1 (Clone)', $project['name']); - - $categories = $c->getAll(2); - $this->assertCount(3, $categories); - $this->assertEquals('C1', $categories[0]['name']); - $this->assertEquals('C2', $categories[1]['name']); - $this->assertEquals('C3', $categories[2]['name']); - } - - public function testCloneProjectWithUsers() - { - $p = new ProjectModel($this->container); - $pp = new ProjectUserRoleModel($this->container); - $u = new UserModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - - $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->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 ProjectModel($this->container); - $pp = new ProjectUserRoleModel($this->container); - $u = new UserModel($this->container); - $pd = new ProjectDuplicationModel($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('projectPermissionModel'), 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->assertEquals(1, $project['owner_id']); - } - - public function testCloneTeamProjectToPrivatProject() - { - $p = new ProjectModel($this->container); - $pp = new ProjectUserRoleModel($this->container); - $u = new UserModel($this->container); - $pd = new ProjectDuplicationModel($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(2, $pd->duplicate(1, array('projectPermissionModel'), 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 ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - $userModel = new UserModel($this->container); - $groupModel = new GroupModel($this->container); - $groupMemberModel = new GroupMemberModel($this->container); - $projectGroupRoleModel = new ProjectGroupRoleModel($this->container); - $projectUserRoleModel = new ProjectUserRoleModel($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() - { - $p = new ProjectModel($this->container); - $a = new ActionModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - - $this->assertEquals(1, $a->create(array( - 'project_id' => 1, - 'event_name' => TaskModel::EVENT_MOVE_COLUMN, - 'action_name' => 'TaskAssignCurrentUser', - 'params' => array('column_id' => 2), - ))); - - $this->assertEquals(2, $pd->duplicate(1)); - - $actions = $a->getAllByProject(2); - - $this->assertNotEmpty($actions); - $this->assertEquals('TaskAssignCurrentUser', $actions[0]['action_name']); - $this->assertNotEmpty($actions[0]['params']); - $this->assertEquals(6, $actions[0]['params']['column_id']); - } - - public function testCloneProjectWithActionTaskAssignColorCategory() - { - $p = new ProjectModel($this->container); - $a = new ActionModel($this->container); - $c = new CategoryModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - - $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1))); - $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 1))); - $this->assertEquals(3, $c->create(array('name' => 'C3', 'project_id' => 1))); - - $this->assertEquals(1, $a->create(array( - 'project_id' => 1, - 'event_name' => TaskModel::EVENT_CREATE_UPDATE, - 'action_name' => 'TaskAssignColorCategory', - 'params' => array('color_id' => 'blue', 'category_id' => 2), - ))); - - $this->assertEquals(2, $pd->duplicate(1)); - - $actions = $a->getAllByProject(2); - - $this->assertNotEmpty($actions); - $this->assertEquals('TaskAssignColorCategory', $actions[0]['action_name']); - $this->assertNotEmpty($actions[0]['params']); - $this->assertEquals('blue', $actions[0]['params']['color_id']); - $this->assertEquals(5, $actions[0]['params']['category_id']); - } - - public function testCloneProjectWithSwimlanes() - { - $p = new ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - $s = new SwimlaneModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $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'))); - - // 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->assertEquals(2, $pd->duplicate(1, array('categoryModel', 'swimlaneModel'))); - - $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']); - - $swimlane = $s->getDefault(2); - $this->assertEquals('New Default', $swimlane['default_swimlane']); - - // Check if tasks are NOT been duplicated - $this->assertCount(0, $tf->getAll(2)); - } - - public function testCloneProjectWithTasks() - { - $p = new ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'P1'))); - - // 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->assertEquals(2, $pd->duplicate(1, array('categoryModel', 'actionModel', 'taskModel'))); - - // 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 testCloneProjectWithSwimlanesAndTasks() - { - $p = new ProjectModel($this->container); - $pd = new ProjectDuplicationModel($this->container); - $s = new SwimlaneModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $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'))); - - // 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->assertEquals(2, $pd->duplicate(1, array('projectPermissionModel', 'swimlaneModel', 'taskModel'))); - - // 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']); - - $swimlane = $s->getDefault(2); - $this->assertEquals('New Default', $swimlane['default_swimlane']); - - // 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']); - } -} diff --git a/tests/units/Model/ProjectTest.php b/tests/units/Model/ProjectModelTest.php index 472d7351..81e0dd57 100644 --- a/tests/units/Model/ProjectTest.php +++ b/tests/units/Model/ProjectModelTest.php @@ -11,16 +11,16 @@ use Kanboard\Model\TaskCreationModel; use Kanboard\Model\ConfigModel; use Kanboard\Model\CategoryModel; -class ProjectTest extends Base +class ProjectModelTest extends Base { public function testCreationForAllLanguages() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); foreach ($this->container['languageModel']->getLanguages() as $locale => $language) { Translator::unload(); Translator::load($locale); - $this->assertNotFalse($p->create(array('name' => 'UnitTest '.$locale)), 'Unable to create project with '.$locale.':'.$language); + $this->assertNotFalse($projectModel->create(array('name' => 'UnitTest '.$locale)), 'Unable to create project with '.$locale.':'.$language); } Translator::unload(); @@ -28,11 +28,11 @@ class ProjectTest extends Base public function testCreation() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals(1, $project['is_active']); $this->assertEquals(0, $project['is_public']); @@ -43,19 +43,19 @@ class ProjectTest extends Base public function testCreationWithDuplicateName() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertEquals(2, $p->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'UnitTest'))); } public function testCreationWithStartAndDate() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest', 'start_date' => '2015-01-01', 'end_date' => '2015-12-31'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest', 'start_date' => '2015-01-01', 'end_date' => '2015-12-31'))); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals('2015-01-01', $project['start_date']); $this->assertEquals('2015-12-31', $project['end_date']); @@ -63,19 +63,19 @@ class ProjectTest extends Base public function testCreationWithDefaultCategories() { - $p = new ProjectModel($this->container); - $c = new ConfigModel($this->container); - $cat = new CategoryModel($this->container); + $projectModel = new ProjectModel($this->container); + $configModel = new ConfigModel($this->container); + $categoryModel = new CategoryModel($this->container); // Multiple categories correctly formatted - $this->assertTrue($c->save(array('project_categories' => 'Test1, Test2'))); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); + $this->assertTrue($configModel->save(array('project_categories' => 'Test1, Test2'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest1'))); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); - $categories = $cat->getAll(1); + $categories = $categoryModel->getAll(1); $this->assertNotEmpty($categories); $this->assertEquals(2, count($categories)); $this->assertEquals('Test1', $categories[0]['name']); @@ -83,85 +83,85 @@ class ProjectTest extends Base // Single category - $this->assertTrue($c->save(array('project_categories' => 'Test1'))); + $this->assertTrue($configModel->save(array('project_categories' => 'Test1'))); $this->container['memoryCache']->flush(); - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'UnitTest2'))); - $project = $p->getById(2); + $project = $projectModel->getById(2); $this->assertNotEmpty($project); - $categories = $cat->getAll(2); + $categories = $categoryModel->getAll(2); $this->assertNotEmpty($categories); $this->assertEquals(1, count($categories)); $this->assertEquals('Test1', $categories[0]['name']); // Multiple categories badly formatted - $this->assertTrue($c->save(array('project_categories' => 'ABC, , DEF 3, '))); + $this->assertTrue($configModel->save(array('project_categories' => 'ABC, , DEF 3, '))); $this->container['memoryCache']->flush(); - $this->assertEquals(3, $p->create(array('name' => 'UnitTest3'))); + $this->assertEquals(3, $projectModel->create(array('name' => 'UnitTest3'))); - $project = $p->getById(3); + $project = $projectModel->getById(3); $this->assertNotEmpty($project); - $categories = $cat->getAll(3); + $categories = $categoryModel->getAll(3); $this->assertNotEmpty($categories); $this->assertEquals(2, count($categories)); $this->assertEquals('ABC', $categories[0]['name']); $this->assertEquals('DEF 3', $categories[1]['name']); // No default categories - $this->assertTrue($c->save(array('project_categories' => ' '))); + $this->assertTrue($configModel->save(array('project_categories' => ' '))); $this->container['memoryCache']->flush(); - $this->assertEquals(4, $p->create(array('name' => 'UnitTest4'))); + $this->assertEquals(4, $projectModel->create(array('name' => 'UnitTest4'))); - $project = $p->getById(4); + $project = $projectModel->getById(4); $this->assertNotEmpty($project); - $categories = $cat->getAll(4); + $categories = $categoryModel->getAll(4); $this->assertEmpty($categories); } public function testUpdateLastModifiedDate() { - $p = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $projectModel = new ProjectModel($this->container); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); $now = time(); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals($now, $project['last_modified'], 'Wrong Timestamp', 1); sleep(1); - $this->assertTrue($p->updateModificationDate(1)); + $this->assertTrue($projectModel->updateModificationDate(1)); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertGreaterThan($now, $project['last_modified']); } public function testGetAllIds() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); - $this->assertEmpty($p->getAllByIds(array())); - $this->assertNotEmpty($p->getAllByIds(array(1, 2))); - $this->assertCount(1, $p->getAllByIds(array(1))); + $this->assertEmpty($projectModel->getAllByIds(array())); + $this->assertNotEmpty($projectModel->getAllByIds(array(1, 2))); + $this->assertCount(1, $projectModel->getAllByIds(array(1))); } public function testIsLastModified() { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); $now = time(); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals($now, $project['last_modified']); @@ -170,113 +170,113 @@ class ProjectTest extends Base $listener = new ProjectModificationDateSubscriber($this->container); $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, array($listener, 'execute')); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1))); $called = $this->container['dispatcher']->getCalledListeners(); $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.Kanboard\Subscriber\ProjectModificationDateSubscriber::execute', $called); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); - $this->assertTrue($p->isModifiedSince(1, $now)); + $this->assertTrue($projectModel->isModifiedSince(1, $now)); } public function testRemove() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertTrue($p->remove(1)); - $this->assertFalse($p->remove(1234)); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($projectModel->remove(1)); + $this->assertFalse($projectModel->remove(1234)); } public function testEnable() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertTrue($p->disable(1)); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($projectModel->disable(1)); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals(0, $project['is_active']); - $this->assertFalse($p->disable(1111)); + $this->assertFalse($projectModel->disable(1111)); } public function testDisable() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertTrue($p->disable(1)); - $this->assertTrue($p->enable(1)); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($projectModel->disable(1)); + $this->assertTrue($projectModel->enable(1)); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals(1, $project['is_active']); - $this->assertFalse($p->enable(1234567)); + $this->assertFalse($projectModel->enable(1234567)); } public function testEnablePublicAccess() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertTrue($p->enablePublicAccess(1)); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($projectModel->enablePublicAccess(1)); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals(1, $project['is_public']); $this->assertNotEmpty($project['token']); - $this->assertFalse($p->enablePublicAccess(123)); + $this->assertFalse($projectModel->enablePublicAccess(123)); } public function testDisablePublicAccess() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertTrue($p->enablePublicAccess(1)); - $this->assertTrue($p->disablePublicAccess(1)); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($projectModel->enablePublicAccess(1)); + $this->assertTrue($projectModel->disablePublicAccess(1)); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals(0, $project['is_public']); $this->assertEmpty($project['token']); - $this->assertFalse($p->disablePublicAccess(123)); + $this->assertFalse($projectModel->disablePublicAccess(123)); } public function testIdentifier() { - $p = new ProjectModel($this->container); + $projectModel = new ProjectModel($this->container); // Creation - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'identifier' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest1', 'identifier' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'UnitTest2'))); - $project = $p->getById(1); + $project = $projectModel->getById(1); $this->assertNotEmpty($project); $this->assertEquals('TEST1', $project['identifier']); - $project = $p->getById(2); + $project = $projectModel->getById(2); $this->assertNotEmpty($project); $this->assertEquals('', $project['identifier']); // Update - $this->assertTrue($p->update(array('id' => '2', 'identifier' => 'test2'))); + $this->assertTrue($projectModel->update(array('id' => '2', 'identifier' => 'test2'))); - $project = $p->getById(2); + $project = $projectModel->getById(2); $this->assertNotEmpty($project); $this->assertEquals('TEST2', $project['identifier']); - $project = $p->getByIdentifier('test1'); + $project = $projectModel->getByIdentifier('test1'); $this->assertNotEmpty($project); $this->assertEquals('TEST1', $project['identifier']); - $project = $p->getByIdentifier(''); + $project = $projectModel->getByIdentifier(''); $this->assertFalse($project); } @@ -303,34 +303,4 @@ class ProjectTest extends Base $this->assertEquals('', $project['owner_username']); $this->assertEquals(0, $project['owner_id']); } - - public function testPriority() - { - $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $projectModel->create(array('name' => 'My project 2'))); - - $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']); - - $this->assertEquals( - array(0 => 0, 1 => 1, 2 => 2, 3 => 3), - $projectModel->getPriorities($project) - ); - - $this->assertTrue($projectModel->update(array('id' => 1, 'priority_start' => 2, 'priority_end' => 5, 'priority_default' => 4))); - - $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']); - - $this->assertEquals( - array(2 => 2, 3 => 3, 4 => 4, 5 => 5), - $projectModel->getPriorities($project) - ); - } } diff --git a/tests/units/Model/ProjectPermissionTest.php b/tests/units/Model/ProjectPermissionTest.php index c9e0a481..a9f8ab04 100644 --- a/tests/units/Model/ProjectPermissionTest.php +++ b/tests/units/Model/ProjectPermissionTest.php @@ -267,6 +267,29 @@ class ProjectPermissionTest extends Base $this->assertEquals(array(1), $projectPermission->getActiveProjectIds(3)); } + public function testGetProjectIds() + { + $userModel = new UserModel($this->container); + $projectModel = new ProjectModel($this->container); + $userRoleModel = new ProjectUserRoleModel($this->container); + $projectPermission = new ProjectPermissionModel($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_MEMBER)); + $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MEMBER)); + $this->assertTrue($userRoleModel->addUser(2, 3, Role::PROJECT_MEMBER)); + + $this->assertEmpty($projectPermission->getProjectIds(1)); + $this->assertEquals(array(1, 2), $projectPermission->getProjectIds(2)); + $this->assertEquals(array(1, 2), $projectPermission->getProjectIds(3)); + } + public function testDuplicate() { $userModel = new UserModel($this->container); diff --git a/tests/units/Model/ProjectTaskPriorityModelTest.php b/tests/units/Model/ProjectTaskPriorityModelTest.php new file mode 100644 index 00000000..61661e5e --- /dev/null +++ b/tests/units/Model/ProjectTaskPriorityModelTest.php @@ -0,0 +1,84 @@ +<?php + +use Kanboard\Model\ProjectModel; +use Kanboard\Model\ProjectTaskPriorityModel; + +require_once __DIR__.'/../Base.php'; + +class ProjectTaskPriorityModelTest extends Base +{ + public function testPriority() + { + $projectModel = new ProjectModel($this->container); + $projectTaskPriorityModel = new ProjectTaskPriorityModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'My project 2'))); + $this->assertEquals(0, $projectTaskPriorityModel->getDefaultPriority(1)); + + $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']); + + $this->assertEquals( + array(0 => 0, 1 => 1, 2 => 2, 3 => 3), + $projectTaskPriorityModel->getPriorities($project) + ); + + $this->assertTrue($projectModel->update(array('id' => 1, 'priority_start' => 2, 'priority_end' => 5, 'priority_default' => 4))); + + $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']); + + $this->assertEquals( + array(2 => 2, 3 => 3, 4 => 4, 5 => 5), + $projectTaskPriorityModel->getPriorities($project) + ); + + $this->assertEquals(4, $projectTaskPriorityModel->getDefaultPriority(1)); + } + + public function testGetPrioritySettings() + { + $projectModel = new ProjectModel($this->container); + $projectTaskPriorityModel = new ProjectTaskPriorityModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'My project 2'))); + + $expected = array( + 'priority_default' => 0, + 'priority_start' => 0, + 'priority_end' => 3, + ); + + $this->assertEquals($expected, $projectTaskPriorityModel->getPrioritySettings(1)); + $this->assertNull($projectTaskPriorityModel->getPrioritySettings(2)); + } + + public function testGetPriorityForProject() + { + $projectModel = new ProjectModel($this->container); + $projectTaskPriorityModel = new ProjectTaskPriorityModel($this->container); + + $this->assertEquals(1, $projectModel->create(array( + 'name' => 'My project 1', + 'priority_default' => 2, + 'priority_start' => -2, + 'priority_end' => 8, + ))); + + $this->assertEquals(2, $projectTaskPriorityModel->getPriorityForProject(1, 42)); + $this->assertEquals(0, $projectTaskPriorityModel->getPriorityForProject(1, 0)); + $this->assertEquals(1, $projectTaskPriorityModel->getPriorityForProject(1, 1)); + $this->assertEquals(-2, $projectTaskPriorityModel->getPriorityForProject(1, -2)); + $this->assertEquals(-1, $projectTaskPriorityModel->getPriorityForProject(1, -1)); + $this->assertEquals(8, $projectTaskPriorityModel->getPriorityForProject(1, 8)); + $this->assertEquals(5, $projectTaskPriorityModel->getPriorityForProject(1, 5)); + $this->assertEquals(2, $projectTaskPriorityModel->getPriorityForProject(1, 9)); + $this->assertEquals(2, $projectTaskPriorityModel->getPriorityForProject(1, -3)); + } +} diff --git a/tests/units/Model/SubtaskModelTest.php b/tests/units/Model/SubtaskModelTest.php new file mode 100644 index 00000000..eed37cf3 --- /dev/null +++ b/tests/units/Model/SubtaskModelTest.php @@ -0,0 +1,175 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\TaskFinderModel; + +class SubtaskModelTest extends Base +{ + public function testCreation() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1))); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(1, $subtask['id']); + $this->assertEquals(1, $subtask['task_id']); + $this->assertEquals('subtask #1', $subtask['title']); + $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); + $this->assertEquals(0, $subtask['time_estimated']); + $this->assertEquals(0, $subtask['time_spent']); + $this->assertEquals(0, $subtask['user_id']); + $this->assertEquals(1, $subtask['position']); + } + + public function testCreationUpdateTaskTimeTracking() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1))); + + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_estimated' => 2, 'time_spent' => 1))); + $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => 5, 'time_spent' => 5))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(7, $task['time_estimated']); + $this->assertEquals(6, $task['time_spent']); + } + + public function testModification() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $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->assertTrue($subtaskModel->update(array('id' => 1, 'task_id' => 1, 'user_id' => 1, 'status' => SubtaskModel::STATUS_INPROGRESS))); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(1, $subtask['id']); + $this->assertEquals(1, $subtask['task_id']); + $this->assertEquals('subtask #1', $subtask['title']); + $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtask['status']); + $this->assertEquals(0, $subtask['time_estimated']); + $this->assertEquals(0, $subtask['time_spent']); + $this->assertEquals(1, $subtask['user_id']); + $this->assertEquals(1, $subtask['position']); + } + + public function testModificationUpdateTaskTimeTracking() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $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->assertTrue($subtaskModel->update(array('id' => 1, 'task_id' => 1, 'time_estimated' => 2, 'time_spent' => 1))); + $this->assertTrue($subtaskModel->update(array('id' => 2, 'task_id' => 1, 'time_estimated' => 2, 'time_spent' => 1))); + $this->assertTrue($subtaskModel->update(array('id' => 1, 'task_id' => 1, 'time_estimated' => 5, 'time_spent' => 5))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(7, $task['time_estimated']); + $this->assertEquals(6, $task['time_spent']); + } + + public function testRemove() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1))); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + + $this->assertTrue($subtaskModel->remove(1)); + + $subtask = $subtaskModel->getById(1); + $this->assertEmpty($subtask); + } + + public function testDuplicate() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + + // We create a project + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + // We create 2 tasks + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'test 2', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 0))); + + // We create many subtasks for the first task + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_estimated' => 5, 'time_spent' => 3, 'status' => 1, 'another_subtask' => 'on'))); + $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => '', 'time_spent' => '', 'status' => 2, 'user_id' => 1))); + + // We duplicate our subtasks + $this->assertTrue($subtaskModel->duplicate(1, 2)); + $subtasks = $subtaskModel->getAll(2); + + $this->assertNotEmpty($subtasks); + $this->assertEquals(2, count($subtasks)); + + $this->assertEquals('subtask #1', $subtasks[0]['title']); + $this->assertEquals('subtask #2', $subtasks[1]['title']); + + $this->assertEquals(2, $subtasks[0]['task_id']); + $this->assertEquals(2, $subtasks[1]['task_id']); + + $this->assertEquals(5, $subtasks[0]['time_estimated']); + $this->assertEquals(0, $subtasks[1]['time_estimated']); + + $this->assertEquals(0, $subtasks[0]['time_spent']); + $this->assertEquals(0, $subtasks[1]['time_spent']); + + $this->assertEquals(0, $subtasks[0]['status']); + $this->assertEquals(0, $subtasks[1]['status']); + + $this->assertEquals(0, $subtasks[0]['user_id']); + $this->assertEquals(0, $subtasks[1]['user_id']); + + $this->assertEquals(1, $subtasks[0]['position']); + $this->assertEquals(2, $subtasks[1]['position']); + } + + public function testGetProjectId() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($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(1, $subtaskModel->getProjectId(1)); + $this->assertEquals(0, $subtaskModel->getProjectId(2)); + } +} diff --git a/tests/units/Model/SubtaskPositionModelTest.php b/tests/units/Model/SubtaskPositionModelTest.php new file mode 100644 index 00000000..92412392 --- /dev/null +++ b/tests/units/Model/SubtaskPositionModelTest.php @@ -0,0 +1,77 @@ +<?php + +use Kanboard\Model\SubtaskPositionModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\ProjectModel; + +require_once __DIR__.'/../Base.php'; + +class SubtaskPositionModelTest extends Base +{ + public function testChangePosition() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskPositionModel = new SubtaskPositionModel($this->container); + $projectModel = new ProjectModel($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($subtaskPositionModel->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($subtaskPositionModel->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($subtaskPositionModel->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($subtaskPositionModel->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($subtaskPositionModel->changePosition(1, 2, 0)); + $this->assertFalse($subtaskPositionModel->changePosition(1, 2, 4)); + } +} diff --git a/tests/units/Model/SubtaskStatusModelTest.php b/tests/units/Model/SubtaskStatusModelTest.php new file mode 100644 index 00000000..af4c3955 --- /dev/null +++ b/tests/units/Model/SubtaskStatusModelTest.php @@ -0,0 +1,123 @@ +<?php + +use Kanboard\Model\ProjectModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\SubtaskStatusModel; +use Kanboard\Model\TaskCreationModel; + +require_once __DIR__.'/../Base.php'; + +class SubtaskStatusModelTest extends Base +{ + public function testToggleStatusWithoutSession() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskStatusModel = new SubtaskStatusModel($this->container); + $projectModel = new ProjectModel($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))); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); + $this->assertEquals(0, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + + $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtaskStatusModel->toggleStatus(1)); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtask['status']); + $this->assertEquals(0, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + + $this->assertEquals(SubtaskModel::STATUS_DONE, $subtaskStatusModel->toggleStatus(1)); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_DONE, $subtask['status']); + $this->assertEquals(0, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + + $this->assertEquals(SubtaskModel::STATUS_TODO, $subtaskStatusModel->toggleStatus(1)); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); + $this->assertEquals(0, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + } + + public function testToggleStatusWithSession() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + $subtaskStatusModel = new SubtaskStatusModel($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))); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); + $this->assertEquals(0, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + + // Set the current logged user + $this->container['sessionStorage']->user = array('id' => 1); + + $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtaskStatusModel->toggleStatus(1)); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtask['status']); + $this->assertEquals(1, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + + $this->assertEquals(SubtaskModel::STATUS_DONE, $subtaskStatusModel->toggleStatus(1)); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_DONE, $subtask['status']); + $this->assertEquals(1, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + + $this->assertEquals(SubtaskModel::STATUS_TODO, $subtaskStatusModel->toggleStatus(1)); + + $subtask = $subtaskModel->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); + $this->assertEquals(1, $subtask['user_id']); + $this->assertEquals(1, $subtask['task_id']); + } + + public function testCloseAll() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + $subtaskStatusModel = new SubtaskStatusModel($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->assertTrue($subtaskStatusModel->closeAll(1)); + + $subtasks = $subtaskModel->getAll(1); + $this->assertNotEmpty($subtasks); + + foreach ($subtasks as $subtask) { + $this->assertEquals(SubtaskModel::STATUS_DONE, $subtask['status']); + } + } +} diff --git a/tests/units/Model/SubtaskTaskConversionModelTest.php b/tests/units/Model/SubtaskTaskConversionModelTest.php new file mode 100644 index 00000000..51a623b2 --- /dev/null +++ b/tests/units/Model/SubtaskTaskConversionModelTest.php @@ -0,0 +1,37 @@ +<?php + +use Kanboard\Model\ProjectModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\SubtaskTaskConversionModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; + +require_once __DIR__.'/../Base.php'; + +class SubtaskTaskConversionModelTest extends Base +{ + public function testConvertToTask() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $projectModel = new ProjectModel($this->container); + $subtaskConversion = new SubtaskTaskConversionModel($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, 'user_id' => 1, 'time_spent' => 2, 'time_estimated' => 3))); + $task_id = $subtaskConversion->convertToTask(1, 1); + + $this->assertNotFalse($task_id); + $this->assertEmpty($subtaskModel->getById(1)); + + $task = $taskFinderModel->getById($task_id); + $this->assertEquals('subtask #1', $task['title']); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals(1, $task['owner_id']); + $this->assertEquals(2, $task['time_spent']); + $this->assertEquals(3, $task['time_estimated']); + } +} diff --git a/tests/units/Model/SubtaskTest.php b/tests/units/Model/SubtaskTest.php deleted file mode 100644 index b65ee609..00000000 --- a/tests/units/Model/SubtaskTest.php +++ /dev/null @@ -1,388 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\SubtaskModel; -use Kanboard\Model\ProjectModel; -use Kanboard\Core\User\UserSession; -use Kanboard\Model\TaskFinderModel; - -class SubtaskTest extends Base -{ - public function onSubtaskCreated($event) - { - $this->assertInstanceOf('Kanboard\Event\SubtaskEvent', $event); - $data = $event->getAll(); - - $this->assertArrayHasKey('id', $data); - $this->assertArrayHasKey('title', $data); - $this->assertArrayHasKey('status', $data); - $this->assertArrayHasKey('time_estimated', $data); - $this->assertArrayHasKey('time_spent', $data); - $this->assertArrayHasKey('status', $data); - $this->assertArrayHasKey('task_id', $data); - $this->assertArrayHasKey('user_id', $data); - $this->assertArrayHasKey('position', $data); - $this->assertNotEmpty($data['task_id']); - $this->assertNotEmpty($data['id']); - } - - public function onSubtaskUpdated($event) - { - $this->assertInstanceOf('Kanboard\Event\SubtaskEvent', $event); - $data = $event->getAll(); - - $this->assertArrayHasKey('id', $data); - $this->assertArrayHasKey('title', $data); - $this->assertArrayHasKey('status', $data); - $this->assertArrayHasKey('time_estimated', $data); - $this->assertArrayHasKey('time_spent', $data); - $this->assertArrayHasKey('status', $data); - $this->assertArrayHasKey('task_id', $data); - $this->assertArrayHasKey('user_id', $data); - $this->assertArrayHasKey('position', $data); - $this->assertArrayHasKey('changes', $data); - $this->assertArrayHasKey('user_id', $data['changes']); - $this->assertArrayHasKey('status', $data['changes']); - - $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $data['changes']['status']); - $this->assertEquals(1, $data['changes']['user_id']); - } - - public function onSubtaskDeleted($event) - { - $this->assertInstanceOf('Kanboard\Event\SubtaskEvent', $event); - $data = $event->getAll(); - - $this->assertArrayHasKey('id', $data); - $this->assertArrayHasKey('title', $data); - $this->assertArrayHasKey('status', $data); - $this->assertArrayHasKey('time_estimated', $data); - $this->assertArrayHasKey('time_spent', $data); - $this->assertArrayHasKey('status', $data); - $this->assertArrayHasKey('task_id', $data); - $this->assertArrayHasKey('user_id', $data); - $this->assertArrayHasKey('position', $data); - $this->assertNotEmpty($data['task_id']); - $this->assertNotEmpty($data['id']); - } - - public function testCreation() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); - - $this->container['dispatcher']->addListener(SubtaskModel::EVENT_CREATE, array($this, 'onSubtaskCreated')); - - $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1))); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(1, $subtask['id']); - $this->assertEquals(1, $subtask['task_id']); - $this->assertEquals('subtask #1', $subtask['title']); - $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); - $this->assertEquals(0, $subtask['time_estimated']); - $this->assertEquals(0, $subtask['time_spent']); - $this->assertEquals(0, $subtask['user_id']); - $this->assertEquals(1, $subtask['position']); - } - - public function testModification() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); - - $this->container['dispatcher']->addListener(SubtaskModel::EVENT_UPDATE, array($this, 'onSubtaskUpdated')); - - $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1))); - $this->assertTrue($s->update(array('id' => 1, 'user_id' => 1, 'status' => SubtaskModel::STATUS_INPROGRESS))); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(1, $subtask['id']); - $this->assertEquals(1, $subtask['task_id']); - $this->assertEquals('subtask #1', $subtask['title']); - $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtask['status']); - $this->assertEquals(0, $subtask['time_estimated']); - $this->assertEquals(0, $subtask['time_spent']); - $this->assertEquals(1, $subtask['user_id']); - $this->assertEquals(1, $subtask['position']); - } - - public function testRemove() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $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->container['dispatcher']->addListener(SubtaskModel::EVENT_DELETE, array($this, 'onSubtaskDeleted')); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - - $this->assertTrue($s->remove(1)); - - $subtask = $s->getById(1); - $this->assertEmpty($subtask); - } - - public function testToggleStatusWithoutSession() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $p = new ProjectModel($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))); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); - $this->assertEquals(0, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - - $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $s->toggleStatus(1)); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtask['status']); - $this->assertEquals(0, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - - $this->assertEquals(SubtaskModel::STATUS_DONE, $s->toggleStatus(1)); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_DONE, $subtask['status']); - $this->assertEquals(0, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - - $this->assertEquals(SubtaskModel::STATUS_TODO, $s->toggleStatus(1)); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); - $this->assertEquals(0, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - } - - public function testToggleStatusWithSession() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $p = new ProjectModel($this->container); - $us = new UserSession($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))); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); - $this->assertEquals(0, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - - // Set the current logged user - $this->container['sessionStorage']->user = array('id' => 1); - - $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $s->toggleStatus(1)); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_INPROGRESS, $subtask['status']); - $this->assertEquals(1, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - - $this->assertEquals(SubtaskModel::STATUS_DONE, $s->toggleStatus(1)); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_DONE, $subtask['status']); - $this->assertEquals(1, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - - $this->assertEquals(SubtaskModel::STATUS_TODO, $s->toggleStatus(1)); - - $subtask = $s->getById(1); - $this->assertNotEmpty($subtask); - $this->assertEquals(SubtaskModel::STATUS_TODO, $subtask['status']); - $this->assertEquals(1, $subtask['user_id']); - $this->assertEquals(1, $subtask['task_id']); - } - - public function testCloseAll() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $p = new ProjectModel($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->assertTrue($s->closeAll(1)); - - $subtasks = $s->getAll(1); - $this->assertNotEmpty($subtasks); - - foreach ($subtasks as $subtask) { - $this->assertEquals(SubtaskModel::STATUS_DONE, $subtask['status']); - } - } - - public function testDuplicate() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $p = new ProjectModel($this->container); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - - // We create 2 tasks - $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); - $this->assertEquals(2, $tc->create(array('title' => 'test 2', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 0))); - - // We create many subtasks for the first task - $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_estimated' => 5, 'time_spent' => 3, 'status' => 1, 'another_subtask' => 'on'))); - $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => '', 'time_spent' => '', 'status' => 2, 'user_id' => 1))); - - // We duplicate our subtasks - $this->assertTrue($s->duplicate(1, 2)); - $subtasks = $s->getAll(2); - - $this->assertNotFalse($subtasks); - $this->assertNotEmpty($subtasks); - $this->assertEquals(2, count($subtasks)); - - $this->assertEquals('subtask #1', $subtasks[0]['title']); - $this->assertEquals('subtask #2', $subtasks[1]['title']); - - $this->assertEquals(2, $subtasks[0]['task_id']); - $this->assertEquals(2, $subtasks[1]['task_id']); - - $this->assertEquals(5, $subtasks[0]['time_estimated']); - $this->assertEquals(0, $subtasks[1]['time_estimated']); - - $this->assertEquals(0, $subtasks[0]['time_spent']); - $this->assertEquals(0, $subtasks[1]['time_spent']); - - $this->assertEquals(0, $subtasks[0]['status']); - $this->assertEquals(0, $subtasks[1]['status']); - - $this->assertEquals(0, $subtasks[0]['user_id']); - $this->assertEquals(0, $subtasks[1]['user_id']); - - $this->assertEquals(1, $subtasks[0]['position']); - $this->assertEquals(2, $subtasks[1]['position']); - } - - public function testChangePosition() - { - $taskCreationModel = new TaskCreationModel($this->container); - $subtaskModel = new SubtaskModel($this->container); - $projectModel = new ProjectModel($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)); - } - - public function testConvertToTask() - { - $taskCreationModel = new TaskCreationModel($this->container); - $taskFinderModel = new TaskFinderModel($this->container); - $subtaskModel = new SubtaskModel($this->container); - $projectModel = new ProjectModel($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, 'user_id' => 1, 'time_spent' => 2, 'time_estimated' => 3))); - $task_id = $subtaskModel->convertToTask(1, 1); - - $this->assertNotFalse($task_id); - $this->assertEmpty($subtaskModel->getById(1)); - - $task = $taskFinderModel->getById($task_id); - $this->assertEquals('subtask #1', $task['title']); - $this->assertEquals(1, $task['project_id']); - $this->assertEquals(1, $task['owner_id']); - $this->assertEquals(2, $task['time_spent']); - $this->assertEquals(3, $task['time_estimated']); - } -} diff --git a/tests/units/Model/SubtaskTimeTrackingModelTest.php b/tests/units/Model/SubtaskTimeTrackingModelTest.php new file mode 100644 index 00000000..8b0fe698 --- /dev/null +++ b/tests/units/Model/SubtaskTimeTrackingModelTest.php @@ -0,0 +1,277 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\ConfigModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\SubtaskTimeTrackingModel; +use Kanboard\Model\ProjectModel; + +class SubtaskTimeTrackingModelTest extends Base +{ + public function testToggleTimer() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); + + $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_TODO)); + $this->assertTrue($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_INPROGRESS)); + $this->assertTrue($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_DONE)); + } + + public function testToggleTimerWhenFeatureDisabled() + { + $configModel = new ConfigModel($this->container); + $configModel->save(array('subtask_time_tracking' => '0')); + $this->container['memoryCache']->flush(); + + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); + + $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_TODO)); + $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_INPROGRESS)); + $this->assertFalse($subtaskTimeTrackingModel->toggleTimer(1, 1, SubtaskModel::STATUS_DONE)); + } + + public function testHasTimer() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); + + $this->assertFalse($subtaskTimeTrackingModel->hasTimer(1, 1)); + $this->assertTrue($subtaskTimeTrackingModel->logStartTime(1, 1)); + $this->assertTrue($subtaskTimeTrackingModel->hasTimer(1, 1)); + $this->assertFalse($subtaskTimeTrackingModel->logStartTime(1, 1)); + $this->assertTrue($subtaskTimeTrackingModel->logEndTime(1, 1)); + $this->assertFalse($subtaskTimeTrackingModel->hasTimer(1, 1)); + } + + public function testGetTimerStatus() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->container['sessionStorage']->user = array('id' => 1); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1, 'user_id' => 1))); + + // Nothing started + $subtasks = $subtaskModel->getAll(1); + $this->assertNotEmpty($subtasks); + $this->assertEquals(0, $subtasks[0]['timer_start_date']); + $this->assertFalse($subtasks[0]['is_timer_started']); + + $subtask = $subtaskModel->getById(1, true); + $this->assertNotEmpty($subtask); + $this->assertEquals(0, $subtask['timer_start_date']); + $this->assertFalse($subtask['is_timer_started']); + + // Start the clock + $this->assertTrue($subtaskTimeTrackingModel->logStartTime(1, 1)); + + $subtasks = $subtaskModel->getAll(1); + $this->assertNotEmpty($subtasks); + $this->assertEquals(time(), $subtasks[0]['timer_start_date'], '', 3); + $this->assertTrue($subtasks[0]['is_timer_started']); + + $subtask = $subtaskModel->getById(1, true); + $this->assertNotEmpty($subtask); + $this->assertEquals(time(), $subtask['timer_start_date'], '', 3); + $this->assertTrue($subtask['is_timer_started']); + + // Stop the clock + $this->assertTrue($subtaskTimeTrackingModel->logEndTime(1, 1)); + $subtasks = $subtaskModel->getAll(1); + $this->assertNotEmpty($subtasks); + $this->assertEquals(0, $subtasks[0]['timer_start_date']); + $this->assertFalse($subtasks[0]['is_timer_started']); + + $subtask = $subtaskModel->getById(1, true); + $this->assertNotEmpty($subtask); + $this->assertEquals(0, $subtask['timer_start_date']); + $this->assertFalse($subtask['is_timer_started']); + } + + public function testLogStartTime() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); + + $this->assertTrue($subtaskTimeTrackingModel->logStartTime(1, 1)); + + $timesheet = $subtaskTimeTrackingModel->getUserTimesheet(1); + $this->assertNotEmpty($timesheet); + $this->assertCount(1, $timesheet); + $this->assertNotEmpty($timesheet[0]['start']); + $this->assertEmpty($timesheet[0]['end']); + $this->assertEquals(1, $timesheet[0]['user_id']); + $this->assertEquals(1, $timesheet[0]['subtask_id']); + } + + public function testLogStartEnd() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); + + // No start time + $this->assertTrue($subtaskTimeTrackingModel->logEndTime(1, 1)); + $timesheet = $subtaskTimeTrackingModel->getUserTimesheet(1); + $this->assertEmpty($timesheet); + + // Log start and end time + $this->assertTrue($subtaskTimeTrackingModel->logStartTime(1, 1)); + sleep(1); + $this->assertTrue($subtaskTimeTrackingModel->logEndTime(1, 1)); + + $timesheet = $subtaskTimeTrackingModel->getUserTimesheet(1); + $this->assertNotEmpty($timesheet); + $this->assertCount(1, $timesheet); + $this->assertNotEmpty($timesheet[0]['start']); + $this->assertNotEmpty($timesheet[0]['end']); + $this->assertEquals(1, $timesheet[0]['user_id']); + $this->assertEquals(1, $timesheet[0]['subtask_id']); + $this->assertNotEquals($timesheet[0]['start'], $timesheet[0]['end']); + } + + public function testCalculateSubtaskTime() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2, 'time_estimated' => 3.3))); + $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 1.1, 'time_estimated' => 4.4))); + + $time = $subtaskTimeTrackingModel->calculateSubtaskTime(1); + $this->assertCount(2, $time); + $this->assertEquals(3.3, $time['time_spent'], 'Total spent', 0.01); + $this->assertEquals(7.7, $time['time_estimated'], 'Total estimated', 0.01); + } + + public function testUpdateSubtaskTimeSpent() + { + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2))); + $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1))); + + $this->assertTrue($subtaskTimeTrackingModel->logStartTime(1, 1)); + $this->assertTrue($subtaskTimeTrackingModel->logStartTime(2, 1)); + + // Fake start time + $this->container['db']->table(SubtaskTimeTrackingModel::TABLE)->update(array('start' => time() - 3600)); + + $this->assertTrue($subtaskTimeTrackingModel->logEndTime(1, 1)); + $this->assertTrue($subtaskTimeTrackingModel->logEndTime(2, 1)); + + $timesheet = $subtaskTimeTrackingModel->getUserTimesheet(1); + $this->assertNotEmpty($timesheet); + $this->assertCount(2, $timesheet); + $this->assertEquals(3600, $timesheet[0]['end'] - $timesheet[0]['start'], 'Wrong timestamps', 1); + $this->assertEquals(3600, $timesheet[1]['end'] - $timesheet[1]['start'], 'Wrong timestamps', 1); + + $time = $subtaskTimeTrackingModel->calculateSubtaskTime(1); + $this->assertEquals(4.2, $time['time_spent'], 'Total spent', 0.01); + $this->assertEquals(0, $time['time_estimated'], 'Total estimated', 0.01); + + $time = $subtaskTimeTrackingModel->calculateSubtaskTime(2); + $this->assertEquals(0, $time['time_spent'], 'Total spent', 0.01); + $this->assertEquals(0, $time['time_estimated'], 'Total estimated', 0.01); + } + + public function testUpdateTaskTimeTracking() + { + $taskFinderModel = new TaskFinderModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $subtaskTimeTrackingModel = new SubtaskTimeTrackingModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test 1', 'project_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'test 2', 'project_id' => 1, 'time_estimated' => 1.5, 'time_spent' => 0.5))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'test 3', 'project_id' => 1, 'time_estimated' => 4, 'time_spent' => 2))); + + $this->assertEquals(1, $subtaskModel->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_spent' => 2.2))); + $this->assertEquals(2, $subtaskModel->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => 1))); + + $this->assertEquals(3, $subtaskModel->create(array('title' => 'subtask #3', 'task_id' => 2, 'time_spent' => 3.4))); + $this->assertEquals(4, $subtaskModel->create(array('title' => 'subtask #4', 'task_id' => 2, 'time_estimated' => 1.25))); + + $this->assertEquals(5, $subtaskModel->create(array('title' => 'subtask #5', 'task_id' => 3, 'time_spent' => 8))); + + $subtaskTimeTrackingModel->updateTaskTimeTracking(1); + $subtaskTimeTrackingModel->updateTaskTimeTracking(2); + $subtaskTimeTrackingModel->updateTaskTimeTracking(3); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2.2, $task['time_spent'], 'Total spent', 0.01); + $this->assertEquals(1, $task['time_estimated'], 'Total estimated', 0.01); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(3.4, $task['time_spent'], 'Total spent', 0.01); + $this->assertEquals(1.25, $task['time_estimated'], 'Total estimated', 0.01); + + $task = $taskFinderModel->getById(3); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['time_estimated']); + $this->assertEquals(8, $task['time_spent']); + + $this->assertTrue($subtaskModel->remove(3)); + $this->assertTrue($subtaskModel->remove(4)); + + $subtaskTimeTrackingModel->updateTaskTimeTracking(2); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['time_estimated']); + $this->assertEquals(0, $task['time_spent']); + } +} diff --git a/tests/units/Model/SubtaskTimeTrackingTest.php b/tests/units/Model/SubtaskTimeTrackingTest.php deleted file mode 100644 index d5ae62ae..00000000 --- a/tests/units/Model/SubtaskTimeTrackingTest.php +++ /dev/null @@ -1,240 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\SubtaskModel; -use Kanboard\Model\SubtaskTimeTrackingModel; -use Kanboard\Model\ProjectModel; - -class SubtaskTimeTrackingTest extends Base -{ - public function testHasTimer() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $st = new SubtaskTimeTrackingModel($this->container); - $p = new ProjectModel($this->container); - - $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))); - $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); - - $this->assertFalse($st->hasTimer(1, 1)); - $this->assertTrue($st->logStartTime(1, 1)); - $this->assertTrue($st->hasTimer(1, 1)); - $this->assertFalse($st->logStartTime(1, 1)); - $this->assertTrue($st->logEndTime(1, 1)); - $this->assertFalse($st->hasTimer(1, 1)); - } - - public function testGetTimerStatus() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $st = new SubtaskTimeTrackingModel($this->container); - $p = new ProjectModel($this->container); - - $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))); - $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1, 'user_id' => 1))); - - // Nothing started - $subtasks = $s->getAll(1); - $this->assertNotEmpty($subtasks); - $this->assertEquals(0, $subtasks[0]['timer_start_date']); - $this->assertFalse($subtasks[0]['is_timer_started']); - - $subtask = $s->getById(1, true); - $this->assertNotEmpty($subtask); - $this->assertEquals(0, $subtask['timer_start_date']); - $this->assertFalse($subtask['is_timer_started']); - - // Start the clock - $this->assertTrue($st->logStartTime(1, 1)); - - $subtasks = $s->getAll(1); - $this->assertNotEmpty($subtasks); - $this->assertEquals(time(), $subtasks[0]['timer_start_date'], '', 3); - $this->assertTrue($subtasks[0]['is_timer_started']); - - $subtask = $s->getById(1, true); - $this->assertNotEmpty($subtask); - $this->assertEquals(time(), $subtask['timer_start_date'], '', 3); - $this->assertTrue($subtask['is_timer_started']); - - // Stop the clock - $this->assertTrue($st->logEndTime(1, 1)); - $subtasks = $s->getAll(1); - $this->assertNotEmpty($subtasks); - $this->assertEquals(0, $subtasks[0]['timer_start_date']); - $this->assertFalse($subtasks[0]['is_timer_started']); - - $subtask = $s->getById(1, true); - $this->assertNotEmpty($subtask); - $this->assertEquals(0, $subtask['timer_start_date']); - $this->assertFalse($subtask['is_timer_started']); - } - - public function testLogStartTime() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $st = new SubtaskTimeTrackingModel($this->container); - $p = new ProjectModel($this->container); - - $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))); - $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); - - $this->assertTrue($st->logStartTime(1, 1)); - - $timesheet = $st->getUserTimesheet(1); - $this->assertNotEmpty($timesheet); - $this->assertCount(1, $timesheet); - $this->assertNotEmpty($timesheet[0]['start']); - $this->assertEmpty($timesheet[0]['end']); - $this->assertEquals(1, $timesheet[0]['user_id']); - $this->assertEquals(1, $timesheet[0]['subtask_id']); - } - - public function testLogStartEnd() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $st = new SubtaskTimeTrackingModel($this->container); - $p = new ProjectModel($this->container); - - $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))); - $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1))); - - // No start time - $this->assertTrue($st->logEndTime(1, 1)); - $timesheet = $st->getUserTimesheet(1); - $this->assertEmpty($timesheet); - - // Log start and end time - $this->assertTrue($st->logStartTime(1, 1)); - sleep(1); - $this->assertTrue($st->logEndTime(1, 1)); - - $timesheet = $st->getUserTimesheet(1); - $this->assertNotEmpty($timesheet); - $this->assertCount(1, $timesheet); - $this->assertNotEmpty($timesheet[0]['start']); - $this->assertNotEmpty($timesheet[0]['end']); - $this->assertEquals(1, $timesheet[0]['user_id']); - $this->assertEquals(1, $timesheet[0]['subtask_id']); - $this->assertNotEquals($timesheet[0]['start'], $timesheet[0]['end']); - } - - public function testCalculateSubtaskTime() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $st = new SubtaskTimeTrackingModel($this->container); - $p = new ProjectModel($this->container); - - $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))); - $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2, 'time_estimated' => 3.3))); - $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 1.1, 'time_estimated' => 4.4))); - - $time = $st->calculateSubtaskTime(1); - $this->assertCount(2, $time); - $this->assertEquals(3.3, $time['time_spent'], 'Total spent', 0.01); - $this->assertEquals(7.7, $time['time_estimated'], 'Total estimated', 0.01); - } - - public function testUpdateSubtaskTimeSpent() - { - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $st = new SubtaskTimeTrackingModel($this->container); - $p = new ProjectModel($this->container); - - $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))); - $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2))); - $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1))); - - $this->assertTrue($st->logStartTime(1, 1)); - $this->assertTrue($st->logStartTime(2, 1)); - - // Fake start time - $this->container['db']->table(SubtaskTimeTrackingModel::TABLE)->update(array('start' => time() - 3600)); - - $this->assertTrue($st->logEndTime(1, 1)); - $this->assertTrue($st->logEndTime(2, 1)); - - $timesheet = $st->getUserTimesheet(1); - $this->assertNotEmpty($timesheet); - $this->assertCount(2, $timesheet); - $this->assertEquals(3600, $timesheet[0]['end'] - $timesheet[0]['start'], 'Wrong timestamps', 1); - $this->assertEquals(3600, $timesheet[1]['end'] - $timesheet[1]['start'], 'Wrong timestamps', 1); - - $time = $st->calculateSubtaskTime(1); - $this->assertEquals(4.2, $time['time_spent'], 'Total spent', 0.01); - $this->assertEquals(0, $time['time_estimated'], 'Total estimated', 0.01); - - $time = $st->calculateSubtaskTime(2); - $this->assertEquals(0, $time['time_spent'], 'Total spent', 0.01); - $this->assertEquals(0, $time['time_estimated'], 'Total estimated', 0.01); - } - - public function testUpdateTaskTimeTracking() - { - $tf = new TaskFinderModel($this->container); - $tc = new TaskCreationModel($this->container); - $s = new SubtaskModel($this->container); - $st = new SubtaskTimeTrackingModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - - $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); - $this->assertEquals(2, $tc->create(array('title' => 'test 2', 'project_id' => 1, 'time_estimated' => 1.5, 'time_spent' => 0.5))); - $this->assertEquals(3, $tc->create(array('title' => 'test 3', 'project_id' => 1, 'time_estimated' => 4, 'time_spent' => 2))); - - $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_spent' => 2.2))); - $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => 1))); - - $this->assertEquals(3, $s->create(array('title' => 'subtask #3', 'task_id' => 2, 'time_spent' => 3.4))); - $this->assertEquals(4, $s->create(array('title' => 'subtask #4', 'task_id' => 2, 'time_estimated' => 1.25))); - - $this->assertEquals(5, $s->create(array('title' => 'subtask #5', 'task_id' => 3, 'time_spent' => 8))); - - $st->updateTaskTimeTracking(1); - $st->updateTaskTimeTracking(2); - $st->updateTaskTimeTracking(3); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(2.2, $task['time_spent'], 'Total spent', 0.01); - $this->assertEquals(1, $task['time_estimated'], 'Total estimated', 0.01); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(3.4, $task['time_spent'], 'Total spent', 0.01); - $this->assertEquals(1.25, $task['time_estimated'], 'Total estimated', 0.01); - - $task = $tf->getById(3); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['time_estimated']); - $this->assertEquals(8, $task['time_spent']); - - $this->assertTrue($s->remove(3)); - $this->assertTrue($s->remove(4)); - - $st->updateTaskTimeTracking(2); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['time_estimated']); - $this->assertEquals(0, $task['time_spent']); - } -} diff --git a/tests/units/Model/SwimlaneTest.php b/tests/units/Model/SwimlaneTest.php index cf0be169..4541e07f 100644 --- a/tests/units/Model/SwimlaneTest.php +++ b/tests/units/Model/SwimlaneTest.php @@ -40,7 +40,17 @@ class SwimlaneTest extends Base $this->assertEquals(2, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #2'))); $swimlane = $swimlaneModel->getFirstActiveSwimlane(1); + $this->assertEquals(0, $swimlane['id']); + $this->assertEquals('Default swimlane', $swimlane['name']); + + $this->assertTrue($swimlaneModel->disableDefault(1)); + + $swimlane = $swimlaneModel->getFirstActiveSwimlane(1); $this->assertEquals(2, $swimlane['id']); + $this->assertEquals('Swimlane #2', $swimlane['name']); + + $this->assertTrue($swimlaneModel->disable(1, 2)); + $this->assertNull($swimlaneModel->getFirstActiveSwimlane(1)); } public function testGetList() diff --git a/tests/units/Model/TagDuplicationModelTest.php b/tests/units/Model/TagDuplicationModelTest.php new file mode 100644 index 00000000..0c95c0fd --- /dev/null +++ b/tests/units/Model/TagDuplicationModelTest.php @@ -0,0 +1,31 @@ +<?php + +use Kanboard\Model\ProjectModel; +use Kanboard\Model\TagDuplicationModel; +use Kanboard\Model\TagModel; + +require_once __DIR__.'/../Base.php'; + +class TagDuplicationModelTest extends Base +{ + public function testProjectDuplication() + { + $tagModel = new TagModel($this->container); + $tagDuplicationModel = new TagDuplicationModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'P2'))); + + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + $this->assertEquals(2, $tagModel->create(1, 'Tag 2')); + $this->assertEquals(3, $tagModel->create(1, 'Tag 3')); + + $this->assertTrue($tagDuplicationModel->duplicate(1, 2)); + + $tags = $tagModel->getAllByProject(2); + $this->assertCount(2, $tags); + $this->assertEquals('Tag 2', $tags[0]['name']); + $this->assertEquals('Tag 3', $tags[1]['name']); + } +} diff --git a/tests/units/Model/TagModelTest.php b/tests/units/Model/TagModelTest.php new file mode 100644 index 00000000..f090ab4a --- /dev/null +++ b/tests/units/Model/TagModelTest.php @@ -0,0 +1,120 @@ +<?php + +use Kanboard\Model\ProjectModel; +use Kanboard\Model\TagModel; + +require_once __DIR__.'/../Base.php'; + +class TagModelTest extends Base +{ + public function testCreation() + { + $tagModel = new TagModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + $this->assertEquals(2, $tagModel->create(1, 'Tag 1')); + $this->assertEquals(3, $tagModel->create(1, 'Tag 2')); + $this->assertFalse($tagModel->create(0, 'Tag 1')); + $this->assertFalse($tagModel->create(1, 'Tag 2')); + } + + public function testGetById() + { + $tagModel = new TagModel($this->container); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + + $tag = $tagModel->getById(1); + $this->assertEquals(0, $tag['project_id']); + $this->assertEquals('Tag 1', $tag['name']); + + $tag = $tagModel->getById(3); + $this->assertEmpty($tag); + } + + public function testGetAll() + { + $tagModel = new TagModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + $this->assertEquals(2, $tagModel->create(1, 'Tag 2')); + + $tags = $tagModel->getAll(); + $this->assertCount(2, $tags); + $this->assertEquals(0, $tags[0]['project_id']); + $this->assertEquals('Tag 1', $tags[0]['name']); + + $this->assertEquals(1, $tags[1]['project_id']); + $this->assertEquals('Tag 2', $tags[1]['name']); + } + + public function testGetAllByProjectId() + { + $tagModel = new TagModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + $this->assertEquals(2, $tagModel->create(1, 'B')); + $this->assertEquals(3, $tagModel->create(1, 'A')); + + $tags = $tagModel->getAllByProject(1); + $this->assertCount(2, $tags); + $this->assertEquals(1, $tags[0]['project_id']); + $this->assertEquals('A', $tags[0]['name']); + + $this->assertEquals(1, $tags[1]['project_id']); + $this->assertEquals('B', $tags[1]['name']); + } + + public function testGetIdByName() + { + $tagModel = new TagModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + $this->assertEquals(2, $tagModel->create(1, 'Tag 1')); + $this->assertEquals(3, $tagModel->create(1, 'Tag 3')); + + $this->assertEquals(1, $tagModel->getIdByName(1, 'tag 1')); + $this->assertEquals(1, $tagModel->getIdByName(0, 'tag 1')); + $this->assertEquals(3, $tagModel->getIdByName(1, 'TaG 3')); + } + + public function testFindOrCreateTag() + { + $tagModel = new TagModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + + $this->assertEquals(2, $tagModel->findOrCreateTag(1, 'Tag 2')); + $this->assertEquals(2, $tagModel->findOrCreateTag(1, 'Tag 2')); + $this->assertEquals(1, $tagModel->findOrCreateTag(1, 'Tag 1')); + } + + public function testRemove() + { + $tagModel = new TagModel($this->container); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + + $this->assertTrue($tagModel->remove(1)); + $this->assertFalse($tagModel->remove(1)); + } + + public function testUpdate() + { + $tagModel = new TagModel($this->container); + $this->assertEquals(1, $tagModel->create(0, 'Tag 1')); + $this->assertTrue($tagModel->update(1, 'Tag Updated')); + + $tag = $tagModel->getById(1); + $this->assertEquals(0, $tag['project_id']); + $this->assertEquals('Tag Updated', $tag['name']); + } +} diff --git a/tests/units/Model/TaskCreationModelTest.php b/tests/units/Model/TaskCreationModelTest.php new file mode 100644 index 00000000..ce9996d9 --- /dev/null +++ b/tests/units/Model/TaskCreationModelTest.php @@ -0,0 +1,403 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\ConfigModel; +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\ProjectModel; + +class TaskCreationModelTest extends Base +{ + public function onCreate($event) + { + $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); + + $event_data = $event->getAll(); + $this->assertNotEmpty($event_data); + $this->assertEquals(1, $event_data['task_id']); + $this->assertEquals('test', $event_data['task']['title']); + } + + public function testNoTitle() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, function () {}); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1))); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.closure', $called); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['id']); + $this->assertEquals('Untitled', $task['title']); + $this->assertEquals(1, $task['project_id']); + } + + public function testMinimum() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $finderModel = new TaskFinderModel($this->container); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, array($this, 'onCreate')); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.TaskCreationModelTest::onCreate', $called); + + $task = $finderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertNotFalse($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals('yellow', $task['color_id']); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals(1, $task['column_id']); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(0, $task['category_id']); + $this->assertEquals(0, $task['creator_id']); + + $this->assertEquals('test', $task['title']); + $this->assertEquals('', $task['description']); + $this->assertEquals('', $task['reference']); + + $this->assertEquals(time(), $task['date_creation'], 'Wrong timestamp', 1); + $this->assertEquals(time(), $task['date_modification'], 'Wrong timestamp', 1); + $this->assertEquals(0, $task['date_due']); + $this->assertEquals(0, $task['date_completed']); + $this->assertEquals(0, $task['date_started']); + + $this->assertEquals(0, $task['time_estimated']); + $this->assertEquals(0, $task['time_spent']); + + $this->assertEquals(1, $task['position']); + $this->assertEquals(1, $task['is_active']); + $this->assertEquals(0, $task['score']); + } + + public function testColorId() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'color_id' => 'blue'))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals('blue', $task['color_id']); + } + + public function testOwnerId() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'owner_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals(1, $task['owner_id']); + } + + public function testCategoryId() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'category_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals(1, $task['category_id']); + } + + public function testCreatorId() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'creator_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals(1, $task['creator_id']); + } + + public function testThatCreatorIsDefined() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->container['sessionStorage']->user = array('id' => 1); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals(1, $task['creator_id']); + } + + public function testColumnId() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals(2, $task['column_id']); + $this->assertEquals(1, $task['position']); + } + + public function testPosition() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals(2, $task['column_id']); + $this->assertEquals(1, $task['position']); + + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2))); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + + $this->assertEquals(2, $task['id']); + $this->assertEquals(2, $task['column_id']); + $this->assertEquals(2, $task['position']); + } + + public function testDescription() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'description' => 'test'))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals('test', $task['description']); + } + + public function testReference() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'reference' => 'test'))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + + $this->assertEquals(1, $task['id']); + $this->assertEquals('test', $task['reference']); + } + + public function testDateDue() + { + $date = '2014-11-23'; + $timestamp = strtotime('+2days'); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_due' => $date))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_due' => $timestamp))); + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_due' => ''))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['id']); + $this->assertEquals($date, date('Y-m-d', $task['date_due'])); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['id']); + $this->assertEquals(date('Y-m-d 00:00', $timestamp), date('Y-m-d 00:00', $task['date_due'])); + + $task = $taskFinderModel->getById(3); + $this->assertEquals(3, $task['id']); + $this->assertEquals(0, $task['date_due']); + } + + public function testDateStarted() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + // Set only a date + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '2014-11-24'))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('2014-11-24 '.date('H:i'), date('Y-m-d H:i', $task['date_started'])); + + // Set a datetime + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '2014-11-24 16:25'))); + + $task = $taskFinderModel->getById(2); + $this->assertEquals('2014-11-24 16:25', date('Y-m-d H:i', $task['date_started'])); + + // Set a datetime + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '11/24/2014 18:25'))); + + $task = $taskFinderModel->getById(3); + $this->assertEquals('2014-11-24 18:25', date('Y-m-d H:i', $task['date_started'])); + + // Set a timestamp + $this->assertEquals(4, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_started' => time()))); + + $task = $taskFinderModel->getById(4); + $this->assertEquals(time(), $task['date_started'], '', 1); + + // Set empty string + $this->assertEquals(5, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_started' => ''))); + $task = $taskFinderModel->getById(5); + $this->assertEquals(0, $task['date_started']); + } + + public function testTime() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 1.5, 'time_spent' => 2.3))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => '', 'time_spent' => ''))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['id']); + $this->assertEquals(1.5, $task['time_estimated']); + $this->assertEquals(2.3, $task['time_spent']); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['id']); + $this->assertEquals(0, $task['time_estimated']); + $this->assertEquals(0, $task['time_spent']); + } + + public function testStripColumn() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'another_task' => '1'))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + } + + public function testScore() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'score' => '3'))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertNotFalse($task); + $this->assertEquals(3, $task['score']); + } + + public function testDefaultColor() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $configModel = new ConfigModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test1'))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals('yellow', $task['color_id']); + + $this->assertTrue($configModel->save(array('default_color' => 'orange'))); + $this->container['memoryCache']->flush(); + + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test2'))); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals('orange', $task['color_id']); + } + + public function testDueDateYear2038TimestampBug() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'date_due' => strtotime('2050-01-10 12:30')))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals('2050-01-10 00:00', date('Y-m-d H:i', $task['date_due'])); + } +} diff --git a/tests/units/Model/TaskCreationTest.php b/tests/units/Model/TaskCreationTest.php deleted file mode 100644 index b249bc0b..00000000 --- a/tests/units/Model/TaskCreationTest.php +++ /dev/null @@ -1,414 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\ConfigModel; -use Kanboard\Model\TaskModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\ProjectModel; - -class TaskCreationTest extends Base -{ - public function onCreate($event) - { - $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); - - $event_data = $event->getAll(); - $this->assertNotEmpty($event_data); - $this->assertEquals(1, $event_data['task_id']); - $this->assertEquals('test', $event_data['title']); - } - - public function testNoProjectId() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, function () {}); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(0, $tc->create(array('title' => 'test', 'project_id' => 0))); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayNotHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); - $this->assertArrayNotHasKey(TaskModel::EVENT_CREATE.'.closure', $called); - } - - public function testNoTitle() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, function () {}); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1))); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.closure', $called); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['id']); - $this->assertEquals('Untitled', $task['title']); - $this->assertEquals(1, $task['project_id']); - } - - public function testMinimum() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, array($this, 'onCreate')); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.TaskCreationTest::onCreate', $called); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertNotFalse($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals('yellow', $task['color_id']); - $this->assertEquals(1, $task['project_id']); - $this->assertEquals(1, $task['column_id']); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals(0, $task['creator_id']); - - $this->assertEquals('test', $task['title']); - $this->assertEquals('', $task['description']); - $this->assertEquals('', $task['reference']); - - $this->assertEquals(time(), $task['date_creation'], 'Wrong timestamp', 1); - $this->assertEquals(time(), $task['date_modification'], 'Wrong timestamp', 1); - $this->assertEquals(0, $task['date_due']); - $this->assertEquals(0, $task['date_completed']); - $this->assertEquals(0, $task['date_started']); - - $this->assertEquals(0, $task['time_estimated']); - $this->assertEquals(0, $task['time_spent']); - - $this->assertEquals(1, $task['position']); - $this->assertEquals(1, $task['is_active']); - $this->assertEquals(0, $task['score']); - } - - public function testColorId() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'color_id' => 'blue'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals('blue', $task['color_id']); - } - - public function testOwnerId() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'owner_id' => 1))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals(1, $task['owner_id']); - } - - public function testCategoryId() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'category_id' => 1))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals(1, $task['category_id']); - } - - public function testCreatorId() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'creator_id' => 1))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals(1, $task['creator_id']); - } - - public function testThatCreatorIsDefined() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $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'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals(1, $task['creator_id']); - } - - public function testColumnId() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals(2, $task['column_id']); - $this->assertEquals(1, $task['position']); - } - - public function testPosition() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals(2, $task['column_id']); - $this->assertEquals(1, $task['position']); - - $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2))); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - - $this->assertEquals(2, $task['id']); - $this->assertEquals(2, $task['column_id']); - $this->assertEquals(2, $task['position']); - } - - public function testDescription() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'description' => 'test'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals('test', $task['description']); - } - - public function testReference() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'reference' => 'test'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals('test', $task['reference']); - } - - public function testDateDue() - { - $date = '2014-11-23'; - $timestamp = strtotime('+2days'); - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_due' => $date))); - $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_due' => $timestamp))); - $this->assertEquals(3, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_due' => ''))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['id']); - $this->assertEquals($date, date('Y-m-d', $task['date_due'])); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['id']); - $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']); - $this->assertEquals(0, $task['date_due']); - } - - public function testDateStarted() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - - // Set only a date - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '2014-11-24'))); - - $task = $tf->getById(1); - $this->assertEquals('2014-11-24 '.date('H:i'), date('Y-m-d H:i', $task['date_started'])); - - // Set a datetime - $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '2014-11-24 16:25'))); - - $task = $tf->getById(2); - $this->assertEquals('2014-11-24 16:25', date('Y-m-d H:i', $task['date_started'])); - - // Set a datetime - $this->assertEquals(3, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '11/24/2014 18:25'))); - - $task = $tf->getById(3); - $this->assertEquals('2014-11-24 18:25', date('Y-m-d H:i', $task['date_started'])); - - // Set a timestamp - $this->assertEquals(4, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => time()))); - - $task = $tf->getById(4); - $this->assertEquals(time(), $task['date_started'], '', 1); - - // Set empty string - $this->assertEquals(5, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => ''))); - $task = $tf->getById(5); - $this->assertEquals(0, $task['date_started']); - } - - public function testTime() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 1.5, 'time_spent' => 2.3))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - - $this->assertEquals(1, $task['id']); - $this->assertEquals(1.5, $task['time_estimated']); - $this->assertEquals(2.3, $task['time_spent']); - } - - public function testStripColumn() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'another_task' => '1'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - } - - public function testScore() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'score' => '3'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertNotFalse($task); - $this->assertEquals(3, $task['score']); - } - - public function testDefaultColor() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $c = new ConfigModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test1'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $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'))); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals('orange', $task['color_id']); - } - - public function testDueDateYear2038TimestampBug() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_due' => strtotime('2050-01-10 12:30')))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals('2050-01-10 00:00', date('Y-m-d H:i', $task['date_due'])); - } -} diff --git a/tests/units/Model/TaskDuplicationModelTest.php b/tests/units/Model/TaskDuplicationModelTest.php new file mode 100644 index 00000000..1c42b6a1 --- /dev/null +++ b/tests/units/Model/TaskDuplicationModelTest.php @@ -0,0 +1,142 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskDuplicationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\CategoryModel; +use Kanboard\Model\TaskTagModel; + +class TaskDuplicationModelTest extends Base +{ + public function testThatDuplicateDefineCreator() + { + $taskDuplicationModel = new TaskDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['position']); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals(0, $task['creator_id']); + + $this->container['sessionStorage']->user = array('id' => 1); + + // We duplicate our task + $this->assertEquals(2, $taskDuplicationModel->duplicate(1)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['creator_id']); + } + + public function testDuplicateSameProject() + { + $taskDuplicationModel = new TaskDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $categoryModel = new CategoryModel($this->container); + + // We create a task and a project + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + // Some categories + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 1))); + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #2', 'project_id' => 1))); + $this->assertTrue($categoryModel->exists(1)); + $this->assertTrue($categoryModel->exists(2)); + + $this->assertEquals(1, $taskCreationModel->create(array( + 'title' => 'test', + 'project_id' => 1, + 'column_id' => 3, + 'owner_id' => 1, + 'category_id' => 2, + 'time_spent' => 4.4 + ))); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['position']); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals(3, $task['column_id']); + $this->assertEquals(2, $task['category_id']); + $this->assertEquals(4.4, $task['time_spent']); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, function () {}); + + // We duplicate our task + $this->assertEquals(2, $taskDuplicationModel->duplicate(1)); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.closure', $called); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(TaskModel::STATUS_OPEN, $task['is_active']); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals(1, $task['owner_id']); + $this->assertEquals(2, $task['category_id']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(3, $task['column_id']); + $this->assertEquals(2, $task['position']); + $this->assertEquals('test', $task['title']); + $this->assertEquals(0, $task['time_spent']); + } + + public function testDuplicateSameProjectWithTags() + { + $taskDuplicationModel = new TaskDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array( + 'title' => 'test', + 'project_id' => 1, + 'tags' => array('T1', 'T2') + ))); + + $this->assertEquals(2, $taskDuplicationModel->duplicate(1)); + + $tags = $taskTagModel->getList(2); + $this->assertCount(2, $tags); + $this->assertArrayHasKey(1, $tags); + $this->assertArrayHasKey(2, $tags); + } + + public function testDuplicateSameProjectWithPriority() + { + $taskDuplicationModel = new TaskDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(1, $taskCreationModel->create(array( + 'title' => 'test', + 'project_id' => 1, + 'priority' => 2 + ))); + + $this->assertEquals(2, $taskDuplicationModel->duplicate(1)); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['priority']); + } +} diff --git a/tests/units/Model/TaskDuplicationTest.php b/tests/units/Model/TaskDuplicationTest.php deleted file mode 100644 index 79b75e54..00000000 --- a/tests/units/Model/TaskDuplicationTest.php +++ /dev/null @@ -1,700 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Core\DateParser; -use Kanboard\Model\TaskModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskDuplicationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\ProjectModel; -use Kanboard\Model\ProjectUserRoleModel; -use Kanboard\Model\CategoryModel; -use Kanboard\Model\UserModel; -use Kanboard\Model\SwimlaneModel; -use Kanboard\Core\Security\Role; - -class TaskDuplicationTest extends Base -{ - public function testThatDuplicateDefineCreator() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['position']); - $this->assertEquals(1, $task['project_id']); - $this->assertEquals(0, $task['creator_id']); - - $this->container['sessionStorage']->user = array('id' => 1); - - // We duplicate our task - $this->assertEquals(2, $td->duplicate(1)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['creator_id']); - } - - public function testDuplicateSameProject() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $c = new CategoryModel($this->container); - - // We create a task and a project - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - - // Some categories - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 1))); - $this->assertNotFalse($c->create(array('name' => 'Category #2', 'project_id' => 1))); - $this->assertTrue($c->exists(1)); - $this->assertTrue($c->exists(2)); - - $this->assertEquals( - 1, - $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1, 'category_id' => 2, 'time_spent' => 4.4) - )); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['position']); - $this->assertEquals(1, $task['project_id']); - $this->assertEquals(3, $task['column_id']); - $this->assertEquals(2, $task['category_id']); - $this->assertEquals(4.4, $task['time_spent']); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, function () {}); - - // We duplicate our task - $this->assertEquals(2, $td->duplicate(1)); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.closure', $called); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(TaskModel::STATUS_OPEN, $task['is_active']); - $this->assertEquals(1, $task['project_id']); - $this->assertEquals(1, $task['owner_id']); - $this->assertEquals(2, $task['category_id']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(3, $task['column_id']); - $this->assertEquals(2, $task['position']); - $this->assertEquals('test', $task['title']); - $this->assertEquals(0, $task['time_spent']); - } - - public function testDuplicateAnotherProject() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $c = new CategoryModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 1))); - $this->assertTrue($c->exists(1)); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1, 'category_id' => 1))); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); - $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, function () {}); - - // We duplicate our task to the 2nd project - $this->assertEquals(2, $td->duplicateToProject(1, 2)); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.closure', $called); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - } - - public function testDuplicateAnotherProjectWithCategory() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $c = new CategoryModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 1))); - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 2))); - $this->assertTrue($c->exists(1)); - $this->assertTrue($c->exists(2)); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'category_id' => 1))); - - // We duplicate our task to the 2nd project - $this->assertEquals(2, $td->duplicateToProject(1, 2)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(2, $task['category_id']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - } - - public function testDuplicateAnotherProjectWithPredefinedCategory() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $c = new CategoryModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 1))); - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 2))); - $this->assertNotFalse($c->create(array('name' => 'Category #2', 'project_id' => 2))); - $this->assertTrue($c->exists(1)); - $this->assertTrue($c->exists(2)); - $this->assertTrue($c->exists(3)); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'category_id' => 1))); - - // We duplicate our task to the 2nd project with no category - $this->assertEquals(2, $td->duplicateToProject(1, 2, null, null, 0)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['category_id']); - - // We duplicate our task to the 2nd project with a different category - $this->assertEquals(3, $td->duplicateToProject(1, 2, null, null, 3)); - - // Check the values of the duplicated task - $task = $tf->getById(3); - $this->assertNotEmpty($task); - $this->assertEquals(3, $task['category_id']); - } - - public function testDuplicateAnotherProjectWithSwimlane() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($s->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); - $this->assertNotFalse($s->create(array('project_id' => 2, 'name' => 'Swimlane #1'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); - - // We duplicate our task to the 2nd project - $this->assertEquals(2, $td->duplicateToProject(1, 2)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals(2, $task['swimlane_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - } - - public function testDuplicateAnotherProjectWithoutSwimlane() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($s->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); - $this->assertNotFalse($s->create(array('project_id' => 2, 'name' => 'Swimlane #2'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); - - // We duplicate our task to the 2nd project - $this->assertEquals(2, $td->duplicateToProject(1, 2)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - } - - public function testDuplicateAnotherProjectWithPredefinedSwimlane() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($s->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); - $this->assertNotFalse($s->create(array('project_id' => 2, 'name' => 'Swimlane #1'))); - $this->assertNotFalse($s->create(array('project_id' => 2, 'name' => 'Swimlane #2'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); - - // We duplicate our task to the 2nd project - $this->assertEquals(2, $td->duplicateToProject(1, 2, 3)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(3, $task['swimlane_id']); - } - - public function testDuplicateAnotherProjectWithPredefinedColumn() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2))); - - // We duplicate our task to the 2nd project with a different column - $this->assertEquals(2, $td->duplicateToProject(1, 2, null, 7)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(7, $task['column_id']); - } - - public function testDuplicateAnotherProjectWithUser() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $pp = new ProjectUserRoleModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2))); - - // We duplicate our task to the 2nd project - $this->assertEquals(2, $td->duplicateToProject(1, 2)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - - // We create a new user for our project - $user = new UserModel($this->container); - $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $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)); - - // Check the values of the duplicated task - $task = $tf->getById(3); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['position']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(2, $task['owner_id']); - $this->assertEquals(2, $task['project_id']); - - // We duplicate a task with a not allowed user - $this->assertEquals(4, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 3))); - $this->assertEquals(5, $td->duplicateToProject(4, 2)); - - $task = $tf->getById(5); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['position']); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals(5, $task['column_id']); - } - - public function testDuplicateAnotherProjectWithPredefinedUser() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $pr = new ProjectUserRoleModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - // 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)); - - // Check the values of the duplicated task - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['owner_id']); - } - - public function onMoveProject($event) - { - $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); - - $event_data = $event->getAll(); - $this->assertNotEmpty($event_data); - $this->assertEquals(1, $event_data['task_id']); - $this->assertEquals('test', $event_data['title']); - } - - public function testMoveAnotherProject() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $user = new UserModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'owner_id' => 1, 'category_id' => 10, 'position' => 333))); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_MOVE_PROJECT, array($this, 'onMoveProject')); - - // We duplicate our task to the 2nd project - $this->assertTrue($td->moveToProject(1, 2)); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_MOVE_PROJECT.'.TaskDuplicationTest::onMoveProject', $called); - - // Check the values of the moved task - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals(5, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals('test', $task['title']); - } - - public function testMoveAnotherProjectWithCategory() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $c = new CategoryModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 1))); - $this->assertNotFalse($c->create(array('name' => 'Category #1', 'project_id' => 2))); - $this->assertTrue($c->exists(1)); - $this->assertTrue($c->exists(2)); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'category_id' => 1))); - - // We move our task to the 2nd project - $this->assertTrue($td->moveToProject(1, 2)); - - // Check the values of the duplicated task - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(2, $task['category_id']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - } - - public function testMoveAnotherProjectWithUser() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $pp = new ProjectUserRoleModel($this->container); - $user = new UserModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - // We create a new user for our project - $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $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))); - - // We move our task to the 2nd project - $this->assertTrue($td->moveToProject(1, 2)); - - // Check the values of the moved task - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['owner_id']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals(6, $task['column_id']); - } - - public function testMoveAnotherProjectWithForbiddenUser() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $pp = new ProjectUserRoleModel($this->container); - $user = new UserModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - // We create a new user for our project - $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); - $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))); - - // We move our task to the 2nd project - $this->assertTrue($td->moveToProject(1, 2)); - - // Check the values of the moved task - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['position']); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals(6, $task['column_id']); - } - - public function testMoveAnotherProjectWithSwimlane() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($s->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); - $this->assertNotFalse($s->create(array('project_id' => 2, 'name' => 'Swimlane #1'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); - - // We move our task to the 2nd project - $this->assertTrue($td->moveToProject(1, 2)); - - // Check the values of the moved task - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals(2, $task['swimlane_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - } - - public function testMoveAnotherProjectWithoutSwimlane() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - - $this->assertNotFalse($s->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); - $this->assertNotFalse($s->create(array('project_id' => 2, 'name' => 'Swimlane #2'))); - - // We create a task - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); - - // We move our task to the 2nd project - $this->assertTrue($td->moveToProject(1, 2)); - - // Check the values of the moved task - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(0, $task['owner_id']); - $this->assertEquals(0, $task['category_id']); - $this->assertEquals(0, $task['swimlane_id']); - $this->assertEquals(6, $task['column_id']); - $this->assertEquals(1, $task['position']); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('test', $task['title']); - } - - public function testCalculateRecurringTaskDueDate() - { - $td = new TaskDuplicationModel($this->container); - - $values = array('date_due' => 0); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(0, $values['date_due']); - - $values = array('date_due' => 0, 'recurrence_factor' => 0, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(0, $values['date_due']); - - $values = array('date_due' => 1431291376, 'recurrence_factor' => 1, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(time() + 86400, $values['date_due'], '', 1); - - $values = array('date_due' => 1431291376, 'recurrence_factor' => -2, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(time() - 2 * 86400, $values['date_due'], '', 1); - - $values = array('date_due' => 1431291376, 'recurrence_factor' => 1, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(1431291376 + 86400, $values['date_due'], '', 1); - - $values = array('date_due' => 1431291376, 'recurrence_factor' => -1, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(1431291376 - 86400, $values['date_due'], '', 1); - - $values = array('date_due' => 1431291376, 'recurrence_factor' => 2, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_MONTHS); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(1436561776, $values['date_due'], '', 1); - - $values = array('date_due' => 1431291376, 'recurrence_factor' => 2, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_YEARS); - $td->calculateRecurringTaskDueDate($values); - $this->assertEquals(1494449776, $values['date_due'], '', 1); - } - - public function testDuplicateRecurringTask() - { - $td = new TaskDuplicationModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $dp = new DateParser($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - - $this->assertEquals(1, $tc->create(array( - 'title' => 'test', - 'project_id' => 1, - 'date_due' => 1436561776, - 'recurrence_status' => TaskModel::RECURRING_STATUS_PENDING, - 'recurrence_trigger' => TaskModel::RECURRING_TRIGGER_CLOSE, - 'recurrence_factor' => 2, - 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS, - 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, - ))); - - $this->assertEquals(2, $td->duplicateRecurringTask(1)); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(TaskModel::RECURRING_STATUS_PROCESSED, $task['recurrence_status']); - $this->assertEquals(2, $task['recurrence_child']); - $this->assertEquals(1436486400, $task['date_due'], '', 2); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(TaskModel::RECURRING_STATUS_PENDING, $task['recurrence_status']); - $this->assertEquals(TaskModel::RECURRING_TRIGGER_CLOSE, $task['recurrence_trigger']); - $this->assertEquals(TaskModel::RECURRING_TIMEFRAME_DAYS, $task['recurrence_timeframe']); - $this->assertEquals(TaskModel::RECURRING_BASEDATE_TRIGGERDATE, $task['recurrence_basedate']); - $this->assertEquals(1, $task['recurrence_parent']); - $this->assertEquals(2, $task['recurrence_factor']); - $this->assertEquals($dp->removeTimeFromTimestamp(strtotime('+2 days')), $task['date_due'], '', 2); - } -} diff --git a/tests/units/Model/TaskFileTest.php b/tests/units/Model/TaskFileModelTest.php index 2faee95c..de12553f 100644 --- a/tests/units/Model/TaskFileTest.php +++ b/tests/units/Model/TaskFileModelTest.php @@ -6,7 +6,7 @@ use Kanboard\Model\TaskFileModel; use Kanboard\Model\TaskCreationModel; use Kanboard\Model\ProjectModel; -class TaskFileTest extends Base +class TaskFileModelTest extends Base { public function testCreation() { @@ -442,4 +442,17 @@ class TaskFileTest extends Base $this->assertTrue($fileModel->removeAll(1)); } + + public function testGetProjectId() + { + $projectModel = new ProjectModel($this->container); + $fileModel = new TaskFileModel($this->container); + $taskCreationModel = new TaskCreationModel($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/foobar', 10)); + $this->assertEquals(1, $fileModel->getProjectId(1)); + $this->assertEquals(0, $fileModel->getProjectId(2)); + } } diff --git a/tests/units/Model/TaskFinderModelTest.php b/tests/units/Model/TaskFinderModelTest.php new file mode 100644 index 00000000..a9f018ff --- /dev/null +++ b/tests/units/Model/TaskFinderModelTest.php @@ -0,0 +1,203 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\ColumnModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\TaskModel; + +class TaskFinderModelTest extends Base +{ + public function testGetDetails() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $categoryModel = new \Kanboard\Model\CategoryModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $categoryModel->create(array('project_id' => 1, 'name' => 'C1'))); + $this->assertEquals(1, $taskCreationModel->create(array( + 'project_id' => 1, + 'title' => 'Task #1', + 'reference' => 'test', + 'description' => 'desc', + 'owner_id' => 1, + 'category_id' => 1, + ))); + + $task = $taskFinderModel->getDetails(1); + $this->assertEquals(1, $task['id']); + $this->assertEquals('test', $task['reference']); + $this->assertEquals('Task #1', $task['title']); + $this->assertEquals('desc', $task['description']); + $this->assertEquals(time(), $task['date_creation'], 'Delta', 1); + $this->assertEquals(time(), $task['date_modification'], 'Delta', 1); + $this->assertEquals(time(), $task['date_moved'], 'Delta', 1); + $this->assertEquals(0, $task['date_completed']); + $this->assertEquals(0, $task['date_due']); + $this->assertEquals(0, $task['date_started']); + $this->assertEquals(0, $task['time_estimated']); + $this->assertEquals(0, $task['time_spent']); + $this->assertEquals('yellow', $task['color_id']); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals(1, $task['column_id']); + $this->assertEquals(1, $task['owner_id']); + $this->assertEquals(0, $task['creator_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(TaskModel::STATUS_OPEN, $task['is_active']); + $this->assertEquals(0, $task['score']); + $this->assertEquals(1, $task['category_id']); + $this->assertEquals(0, $task['priority']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(TaskModel::RECURRING_STATUS_NONE, $task['recurrence_status']); + $this->assertEquals(TaskModel::RECURRING_TRIGGER_FIRST_COLUMN, $task['recurrence_trigger']); + $this->assertEquals(0, $task['recurrence_factor']); + $this->assertEquals(TaskModel::RECURRING_TIMEFRAME_DAYS, $task['recurrence_timeframe']); + $this->assertEquals(TaskModel::RECURRING_BASEDATE_DUEDATE, $task['recurrence_basedate']); + $this->assertEquals(0, $task['recurrence_parent']); + $this->assertEquals(0, $task['recurrence_child']); + $this->assertEquals('C1', $task['category_name']); + $this->assertNull($task['swimlane_name']); + $this->assertEquals('Default swimlane', $task['default_swimlane']); + $this->assertEquals('Project #1', $task['project_name']); + $this->assertEquals('Backlog', $task['column_title']); + $this->assertEquals('admin', $task['assignee_username']); + $this->assertEquals('', $task['assignee_name']); + $this->assertEquals('', $task['creator_username']); + $this->assertEquals('', $task['creator_name']); + } + + public function testGetTasksForDashboardWithHiddenColumn() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $columnModel = new ColumnModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1))); + + $tasks = $taskFinderModel->getUserQuery(1)->findAll(); + $this->assertCount(2, $tasks); + + $this->assertTrue($columnModel->update(2, 'Test', 0, '', 1)); + + $tasks = $taskFinderModel->getUserQuery(1)->findAll(); + $this->assertCount(1, $tasks); + $this->assertEquals('Task #1', $tasks[0]['title']); + + $this->assertTrue($columnModel->update(2, 'Test', 0, '', 0)); + + $tasks = $taskFinderModel->getUserQuery(1)->findAll(); + $this->assertCount(2, $tasks); + } + + public function testGetOverdueTasks() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'date_due' => strtotime('-1 day')))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'date_due' => strtotime('+1 day')))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => 0))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1))); + + $tasks = $taskFinderModel->getOverdueTasks(); + $this->assertNotEmpty($tasks); + $this->assertTrue(is_array($tasks)); + $this->assertCount(1, $tasks); + $this->assertEquals('Task #1', $tasks[0]['title']); + } + + public function testGetOverdueTasksByProject() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project #2'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'date_due' => strtotime('-1 day')))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 2, 'date_due' => strtotime('-1 day')))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => strtotime('+1 day')))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'Task #4', 'project_id' => 1, 'date_due' => 0))); + $this->assertEquals(5, $taskCreationModel->create(array('title' => 'Task #5', 'project_id' => 1))); + + $tasks = $taskFinderModel->getOverdueTasksByProject(1); + $this->assertNotEmpty($tasks); + $this->assertTrue(is_array($tasks)); + $this->assertCount(1, $tasks); + $this->assertEquals('Task #1', $tasks[0]['title']); + } + + public function testGetOverdueTasksByUser() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project #2'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'owner_id' => 1, 'date_due' => strtotime('-1 day')))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 2, 'owner_id' => 1, 'date_due' => strtotime('-1 day')))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => strtotime('+1 day')))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'Task #4', 'project_id' => 1, 'date_due' => 0))); + $this->assertEquals(5, $taskCreationModel->create(array('title' => 'Task #5', 'project_id' => 1))); + + $tasks = $taskFinderModel->getOverdueTasksByUser(1); + $this->assertNotEmpty($tasks); + $this->assertTrue(is_array($tasks)); + $this->assertCount(2, $tasks); + + $this->assertEquals(1, $tasks[0]['id']); + $this->assertEquals('Task #1', $tasks[0]['title']); + $this->assertEquals(1, $tasks[0]['owner_id']); + $this->assertEquals(1, $tasks[0]['project_id']); + $this->assertEquals('Project #1', $tasks[0]['project_name']); + $this->assertEquals('admin', $tasks[0]['assignee_username']); + $this->assertEquals('', $tasks[0]['assignee_name']); + + $this->assertEquals('Task #2', $tasks[1]['title']); + } + + public function testCountByProject() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project #2'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 2))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 2))); + + $this->assertEquals(1, $taskFinderModel->countByProjectId(1)); + $this->assertEquals(2, $taskFinderModel->countByProjectId(2)); + } + + public function testGetProjectToken() + { + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'Project #2'))); + + $this->assertTrue($projectModel->enablePublicAccess(1)); + + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 2))); + + $project = $projectModel->getById(1); + $this->assertEquals($project['token'], $taskFinderModel->getProjectToken(1)); + $this->assertEmpty($taskFinderModel->getProjectToken(2)); + } +} diff --git a/tests/units/Model/TaskFinderTest.php b/tests/units/Model/TaskFinderTest.php deleted file mode 100644 index 46792baf..00000000 --- a/tests/units/Model/TaskFinderTest.php +++ /dev/null @@ -1,115 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\ProjectModel; - -class TaskFinderTest extends Base -{ - public function testGetOverdueTasks() - { - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'date_due' => strtotime('-1 day')))); - $this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 1, 'date_due' => strtotime('+1 day')))); - $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => 0))); - $this->assertEquals(4, $tc->create(array('title' => 'Task #3', 'project_id' => 1))); - - $tasks = $tf->getOverdueTasks(); - $this->assertNotEmpty($tasks); - $this->assertTrue(is_array($tasks)); - $this->assertCount(1, $tasks); - $this->assertEquals('Task #1', $tasks[0]['title']); - } - - public function testGetOverdueTasksByProject() - { - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(2, $p->create(array('name' => 'Project #2'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'date_due' => strtotime('-1 day')))); - $this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 2, 'date_due' => strtotime('-1 day')))); - $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => strtotime('+1 day')))); - $this->assertEquals(4, $tc->create(array('title' => 'Task #4', 'project_id' => 1, 'date_due' => 0))); - $this->assertEquals(5, $tc->create(array('title' => 'Task #5', 'project_id' => 1))); - - $tasks = $tf->getOverdueTasksByProject(1); - $this->assertNotEmpty($tasks); - $this->assertTrue(is_array($tasks)); - $this->assertCount(1, $tasks); - $this->assertEquals('Task #1', $tasks[0]['title']); - } - - public function testGetOverdueTasksByUser() - { - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(2, $p->create(array('name' => 'Project #2'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'owner_id' => 1, 'date_due' => strtotime('-1 day')))); - $this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 2, 'owner_id' => 1, 'date_due' => strtotime('-1 day')))); - $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => strtotime('+1 day')))); - $this->assertEquals(4, $tc->create(array('title' => 'Task #4', 'project_id' => 1, 'date_due' => 0))); - $this->assertEquals(5, $tc->create(array('title' => 'Task #5', 'project_id' => 1))); - - $tasks = $tf->getOverdueTasksByUser(1); - $this->assertNotEmpty($tasks); - $this->assertTrue(is_array($tasks)); - $this->assertCount(2, $tasks); - - $this->assertEquals(1, $tasks[0]['id']); - $this->assertEquals('Task #1', $tasks[0]['title']); - $this->assertEquals(1, $tasks[0]['owner_id']); - $this->assertEquals(1, $tasks[0]['project_id']); - $this->assertEquals('Project #1', $tasks[0]['project_name']); - $this->assertEquals('admin', $tasks[0]['assignee_username']); - $this->assertEquals('', $tasks[0]['assignee_name']); - - $this->assertEquals('Task #2', $tasks[1]['title']); - } - - public function testCountByProject() - { - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(2, $p->create(array('name' => 'Project #2'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1))); - $this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 2))); - $this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 2))); - - $this->assertEquals(1, $tf->countByProjectId(1)); - $this->assertEquals(2, $tf->countByProjectId(2)); - } - - public function testGetProjectToken() - { - $taskCreationModel = new TaskCreationModel($this->container); - $taskFinderModel = new TaskFinderModel($this->container); - $projectModel = new ProjectModel($this->container); - - $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); - $this->assertEquals(2, $projectModel->create(array('name' => 'Project #2'))); - - $this->assertTrue($projectModel->enablePublicAccess(1)); - - $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1))); - $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 2))); - - $project = $projectModel->getById(1); - $this->assertEquals($project['token'], $taskFinderModel->getProjectToken(1)); - $this->assertEmpty($taskFinderModel->getProjectToken(2)); - } -} diff --git a/tests/units/Model/TaskLinkModelTest.php b/tests/units/Model/TaskLinkModelTest.php new file mode 100644 index 00000000..01a7888b --- /dev/null +++ b/tests/units/Model/TaskLinkModelTest.php @@ -0,0 +1,239 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\TaskLinkModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\ProjectModel; + +class TaskLinkModelTest extends Base +{ + public function testGeyById() + { + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'B'))); + $this->assertEquals(1, $taskLinkModel->create(1, 2, 6)); + + $taskLink = $taskLinkModel->getById(1); + $this->assertEquals(1, $taskLink['id']); + $this->assertEquals(1, $taskLink['task_id']); + $this->assertEquals(2, $taskLink['opposite_task_id']); + $this->assertEquals(6, $taskLink['link_id']); + $this->assertEquals(7, $taskLink['opposite_link_id']); + $this->assertEquals('is a child of', $taskLink['label']); + + $taskLink = $taskLinkModel->getById(2); + $this->assertEquals(2, $taskLink['id']); + $this->assertEquals(2, $taskLink['task_id']); + $this->assertEquals(1, $taskLink['opposite_task_id']); + $this->assertEquals(7, $taskLink['link_id']); + $this->assertEquals(6, $taskLink['opposite_link_id']); + $this->assertEquals('is a parent of', $taskLink['label']); + } + + // Check postgres issue: "Cardinality violation: 7 ERROR: more than one row returned by a subquery used as an expression" + public function testGetTaskWithMultipleMilestoneLink() + { + $taskFinderModel = new TaskFinderModel($this->container); + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'B'))); + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'C'))); + + $this->assertNotFalse($taskLinkModel->create(1, 2, 9)); + $this->assertNotFalse($taskLinkModel->create(1, 3, 9)); + + $task = $taskFinderModel->getExtendedQuery()->findOne(); + $this->assertNotEmpty($task); + } + + public function testCreateTaskLinkWithNoOpposite() + { + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'B'))); + $this->assertEquals(1, $taskLinkModel->create(1, 2, 1)); + + $links = $taskLinkModel->getAll(1); + $this->assertNotEmpty($links); + $this->assertCount(1, $links); + $this->assertEquals('relates to', $links[0]['label']); + $this->assertEquals('B', $links[0]['title']); + $this->assertEquals(2, $links[0]['task_id']); + $this->assertEquals(1, $links[0]['is_active']); + + $links = $taskLinkModel->getAll(2); + $this->assertNotEmpty($links); + $this->assertCount(1, $links); + $this->assertEquals('relates to', $links[0]['label']); + $this->assertEquals('A', $links[0]['title']); + $this->assertEquals(1, $links[0]['task_id']); + $this->assertEquals(1, $links[0]['is_active']); + + $task_link = $taskLinkModel->getById(1); + $this->assertNotEmpty($task_link); + $this->assertEquals(1, $task_link['id']); + $this->assertEquals(1, $task_link['task_id']); + $this->assertEquals(2, $task_link['opposite_task_id']); + $this->assertEquals(1, $task_link['link_id']); + + $opposite_task_link = $taskLinkModel->getOppositeTaskLink($task_link); + $this->assertNotEmpty($opposite_task_link); + $this->assertEquals(2, $opposite_task_link['id']); + $this->assertEquals(2, $opposite_task_link['task_id']); + $this->assertEquals(1, $opposite_task_link['opposite_task_id']); + $this->assertEquals(1, $opposite_task_link['link_id']); + } + + public function testCreateTaskLinkWithOpposite() + { + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'B'))); + $this->assertEquals(1, $taskLinkModel->create(1, 2, 2)); + + $links = $taskLinkModel->getAll(1); + $this->assertNotEmpty($links); + $this->assertCount(1, $links); + $this->assertEquals('blocks', $links[0]['label']); + $this->assertEquals('B', $links[0]['title']); + $this->assertEquals(2, $links[0]['task_id']); + $this->assertEquals(1, $links[0]['is_active']); + + $links = $taskLinkModel->getAll(2); + $this->assertNotEmpty($links); + $this->assertCount(1, $links); + $this->assertEquals('is blocked by', $links[0]['label']); + $this->assertEquals('A', $links[0]['title']); + $this->assertEquals(1, $links[0]['task_id']); + $this->assertEquals(1, $links[0]['is_active']); + + $task_link = $taskLinkModel->getById(1); + $this->assertNotEmpty($task_link); + $this->assertEquals(1, $task_link['id']); + $this->assertEquals(1, $task_link['task_id']); + $this->assertEquals(2, $task_link['opposite_task_id']); + $this->assertEquals(2, $task_link['link_id']); + + $opposite_task_link = $taskLinkModel->getOppositeTaskLink($task_link); + $this->assertNotEmpty($opposite_task_link); + $this->assertEquals(2, $opposite_task_link['id']); + $this->assertEquals(2, $opposite_task_link['task_id']); + $this->assertEquals(1, $opposite_task_link['opposite_task_id']); + $this->assertEquals(3, $opposite_task_link['link_id']); + } + + public function testGroupByLabel() + { + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'B'))); + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'C'))); + + $this->assertNotFalse($taskLinkModel->create(1, 2, 2)); + $this->assertNotFalse($taskLinkModel->create(1, 3, 2)); + + $links = $taskLinkModel->getAllGroupedByLabel(1); + $this->assertCount(1, $links); + $this->assertArrayHasKey('blocks', $links); + $this->assertCount(2, $links['blocks']); + $this->assertEquals('test', $links['blocks'][0]['project_name']); + $this->assertEquals('Backlog', $links['blocks'][0]['column_title']); + $this->assertEquals('blocks', $links['blocks'][0]['label']); + } + + public function testUpdate() + { + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 2, 'title' => 'B'))); + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'C'))); + + $this->assertEquals(1, $taskLinkModel->create(1, 2, 5)); + $this->assertTrue($taskLinkModel->update(1, 1, 3, 11)); + + $links = $taskLinkModel->getAll(1); + $this->assertNotEmpty($links); + $this->assertCount(1, $links); + $this->assertEquals('is fixed by', $links[0]['label']); + $this->assertEquals('C', $links[0]['title']); + $this->assertEquals(3, $links[0]['task_id']); + + $links = $taskLinkModel->getAll(2); + $this->assertEmpty($links); + + $links = $taskLinkModel->getAll(3); + $this->assertNotEmpty($links); + $this->assertCount(1, $links); + $this->assertEquals('fixes', $links[0]['label']); + $this->assertEquals('A', $links[0]['title']); + $this->assertEquals(1, $links[0]['task_id']); + } + + public function testRemove() + { + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'B'))); + $this->assertEquals(1, $taskLinkModel->create(1, 2, 2)); + + $links = $taskLinkModel->getAll(1); + $this->assertNotEmpty($links); + $links = $taskLinkModel->getAll(2); + $this->assertNotEmpty($links); + + $this->assertTrue($taskLinkModel->remove($links[0]['id'])); + + $links = $taskLinkModel->getAll(1); + $this->assertEmpty($links); + $links = $taskLinkModel->getAll(2); + $this->assertEmpty($links); + } + + public function testGetProjectId() + { + $taskLinkModel = new TaskLinkModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'A'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'B'))); + $this->assertEquals(1, $taskLinkModel->create(1, 2, 2)); + + $this->assertEquals(1, $taskLinkModel->getProjectId(1)); + $this->assertEquals(0, $taskLinkModel->getProjectId(42)); + } +} diff --git a/tests/units/Model/TaskLinkTest.php b/tests/units/Model/TaskLinkTest.php deleted file mode 100644 index bc574731..00000000 --- a/tests/units/Model/TaskLinkTest.php +++ /dev/null @@ -1,196 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\TaskLinkModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\ProjectModel; - -class TaskLinkTest extends Base -{ - // Check postgres issue: "Cardinality violation: 7 ERROR: more than one row returned by a subquery used as an expression" - public function testGetTaskWithMultipleMilestoneLink() - { - $tf = new TaskFinderModel($this->container); - $tl = new TaskLinkModel($this->container); - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($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'))); - $this->assertEquals(3, $tc->create(array('project_id' => 1, 'title' => 'C'))); - - $this->assertNotFalse($tl->create(1, 2, 9)); - $this->assertNotFalse($tl->create(1, 3, 9)); - - $task = $tf->getExtendedQuery()->findOne(); - $this->assertNotEmpty($task); - } - - public function testCreateTaskLinkWithNoOpposite() - { - $tl = new TaskLinkModel($this->container); - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($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'))); - $this->assertEquals(1, $tl->create(1, 2, 1)); - - $links = $tl->getAll(1); - $this->assertNotEmpty($links); - $this->assertCount(1, $links); - $this->assertEquals('relates to', $links[0]['label']); - $this->assertEquals('B', $links[0]['title']); - $this->assertEquals(2, $links[0]['task_id']); - $this->assertEquals(1, $links[0]['is_active']); - - $links = $tl->getAll(2); - $this->assertNotEmpty($links); - $this->assertCount(1, $links); - $this->assertEquals('relates to', $links[0]['label']); - $this->assertEquals('A', $links[0]['title']); - $this->assertEquals(1, $links[0]['task_id']); - $this->assertEquals(1, $links[0]['is_active']); - - $task_link = $tl->getById(1); - $this->assertNotEmpty($task_link); - $this->assertEquals(1, $task_link['id']); - $this->assertEquals(1, $task_link['task_id']); - $this->assertEquals(2, $task_link['opposite_task_id']); - $this->assertEquals(1, $task_link['link_id']); - - $opposite_task_link = $tl->getOppositeTaskLink($task_link); - $this->assertNotEmpty($opposite_task_link); - $this->assertEquals(2, $opposite_task_link['id']); - $this->assertEquals(2, $opposite_task_link['task_id']); - $this->assertEquals(1, $opposite_task_link['opposite_task_id']); - $this->assertEquals(1, $opposite_task_link['link_id']); - } - - public function testCreateTaskLinkWithOpposite() - { - $tl = new TaskLinkModel($this->container); - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($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'))); - $this->assertEquals(1, $tl->create(1, 2, 2)); - - $links = $tl->getAll(1); - $this->assertNotEmpty($links); - $this->assertCount(1, $links); - $this->assertEquals('blocks', $links[0]['label']); - $this->assertEquals('B', $links[0]['title']); - $this->assertEquals(2, $links[0]['task_id']); - $this->assertEquals(1, $links[0]['is_active']); - - $links = $tl->getAll(2); - $this->assertNotEmpty($links); - $this->assertCount(1, $links); - $this->assertEquals('is blocked by', $links[0]['label']); - $this->assertEquals('A', $links[0]['title']); - $this->assertEquals(1, $links[0]['task_id']); - $this->assertEquals(1, $links[0]['is_active']); - - $task_link = $tl->getById(1); - $this->assertNotEmpty($task_link); - $this->assertEquals(1, $task_link['id']); - $this->assertEquals(1, $task_link['task_id']); - $this->assertEquals(2, $task_link['opposite_task_id']); - $this->assertEquals(2, $task_link['link_id']); - - $opposite_task_link = $tl->getOppositeTaskLink($task_link); - $this->assertNotEmpty($opposite_task_link); - $this->assertEquals(2, $opposite_task_link['id']); - $this->assertEquals(2, $opposite_task_link['task_id']); - $this->assertEquals(1, $opposite_task_link['opposite_task_id']); - $this->assertEquals(3, $opposite_task_link['link_id']); - } - - public function testGroupByLabel() - { - $tl = new TaskLinkModel($this->container); - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($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'))); - $this->assertEquals(3, $tc->create(array('project_id' => 1, 'title' => 'C'))); - - $this->assertNotFalse($tl->create(1, 2, 2)); - $this->assertNotFalse($tl->create(1, 3, 2)); - - $links = $tl->getAllGroupedByLabel(1); - $this->assertCount(1, $links); - $this->assertArrayHasKey('blocks', $links); - $this->assertCount(2, $links['blocks']); - $this->assertEquals('test', $links['blocks'][0]['project_name']); - $this->assertEquals('Backlog', $links['blocks'][0]['column_title']); - $this->assertEquals('blocks', $links['blocks'][0]['label']); - } - - public function testUpdate() - { - $tl = new TaskLinkModel($this->container); - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2'))); - $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'A'))); - $this->assertEquals(2, $tc->create(array('project_id' => 2, 'title' => 'B'))); - $this->assertEquals(3, $tc->create(array('project_id' => 1, 'title' => 'C'))); - - $this->assertEquals(1, $tl->create(1, 2, 5)); - $this->assertTrue($tl->update(1, 1, 3, 11)); - - $links = $tl->getAll(1); - $this->assertNotEmpty($links); - $this->assertCount(1, $links); - $this->assertEquals('is fixed by', $links[0]['label']); - $this->assertEquals('C', $links[0]['title']); - $this->assertEquals(3, $links[0]['task_id']); - - $links = $tl->getAll(2); - $this->assertEmpty($links); - - $links = $tl->getAll(3); - $this->assertNotEmpty($links); - $this->assertCount(1, $links); - $this->assertEquals('fixes', $links[0]['label']); - $this->assertEquals('A', $links[0]['title']); - $this->assertEquals(1, $links[0]['task_id']); - } - - public function testRemove() - { - $tl = new TaskLinkModel($this->container); - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($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'))); - $this->assertEquals(1, $tl->create(1, 2, 2)); - - $links = $tl->getAll(1); - $this->assertNotEmpty($links); - $links = $tl->getAll(2); - $this->assertNotEmpty($links); - - $this->assertTrue($tl->remove($links[0]['id'])); - - $links = $tl->getAll(1); - $this->assertEmpty($links); - $links = $tl->getAll(2); - $this->assertEmpty($links); - } -} diff --git a/tests/units/Model/TaskModelTest.php b/tests/units/Model/TaskModelTest.php new file mode 100644 index 00000000..e42b5ae6 --- /dev/null +++ b/tests/units/Model/TaskModelTest.php @@ -0,0 +1,30 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\ProjectModel; + +class TaskModelTest extends Base +{ + public function testRemove() + { + $taskModel = new TaskModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1))); + + $this->assertTrue($taskModel->remove(1)); + $this->assertFalse($taskModel->remove(1234)); + } + + public function testGetTaskIdFromText() + { + $taskModel = new TaskModel($this->container); + $this->assertEquals(123, $taskModel->getTaskIdFromText('My task #123')); + $this->assertEquals(0, $taskModel->getTaskIdFromText('My task 123')); + } +} diff --git a/tests/units/Model/TaskModificationModelTest.php b/tests/units/Model/TaskModificationModelTest.php new file mode 100644 index 00000000..f70561b3 --- /dev/null +++ b/tests/units/Model/TaskModificationModelTest.php @@ -0,0 +1,323 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskModificationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\TaskTagModel; + +class TaskModificationModelTest extends Base +{ + public function onCreateUpdate($event) + { + $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); + + $event_data = $event->getAll(); + $this->assertNotEmpty($event_data); + $this->assertEquals(1, $event_data['task_id']); + $this->assertEquals('After', $event_data['task']['title']); + $this->assertEquals('After', $event_data['changes']['title']); + } + + public function onUpdate($event) + { + $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); + + $event_data = $event->getAll(); + $this->assertNotEmpty($event_data); + $this->assertEquals(1, $event_data['task_id']); + $this->assertEquals('After', $event_data['task']['title']); + } + + public function onAssigneeChange($event) + { + $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); + + $event_data = $event->getAll(); + $this->assertNotEmpty($event_data); + $this->assertEquals(1, $event_data['task_id']); + $this->assertEquals(1, $event_data['changes']['owner_id']); + } + + public function testThatNoEventAreFiredWhenNoChanges() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, array($this, 'onCreateUpdate')); + $this->container['dispatcher']->addListener(TaskModel::EVENT_UPDATE, array($this, 'onUpdate')); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'title' => 'test'))); + + $this->assertEmpty($this->container['dispatcher']->getCalledListeners()); + } + + public function testChangeTitle() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Before', 'project_id' => 1))); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, array($this, 'onCreateUpdate')); + $this->container['dispatcher']->addListener(TaskModel::EVENT_UPDATE, array($this, 'onUpdate')); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'title' => 'After'))); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.TaskModificationModelTest::onCreateUpdate', $called); + $this->assertArrayHasKey(TaskModel::EVENT_UPDATE.'.TaskModificationModelTest::onUpdate', $called); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('After', $task['title']); + } + + public function testChangeAssignee() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['owner_id']); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_ASSIGNEE_CHANGE, array($this, 'onAssigneeChange')); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'owner_id' => 1))); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(TaskModel::EVENT_ASSIGNEE_CHANGE.'.TaskModificationModelTest::onAssigneeChange', $called); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(1, $task['owner_id']); + } + + public function testChangeDescription() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('', $task['description']); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'description' => 'test'))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('test', $task['description']); + } + + public function testChangeCategory() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['category_id']); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'category_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(1, $task['category_id']); + } + + public function testChangeColor() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('yellow', $task['color_id']); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'color_id' => 'blue'))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('blue', $task['color_id']); + } + + public function testChangeScore() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['score']); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'score' => 13))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(13, $task['score']); + } + + public function testChangeDueDate() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['date_due']); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'date_due' => '2014-11-24'))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('2014-11-24', date('Y-m-d', $task['date_due'])); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'date_due' => time()))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(date('Y-m-d'), date('Y-m-d', $task['date_due'])); + } + + public function testChangeStartedDate() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['date_started']); + + // Set only a date + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'date_started' => '2014-11-24'))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('2014-11-24 '.date('H:i'), date('Y-m-d H:i', $task['date_started'])); + + // Set a datetime + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'date_started' => '2014-11-24 16:25'))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('2014-11-24 16:25', date('Y-m-d H:i', $task['date_started'])); + + // Set a datetime + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'date_started' => '11/24/2014 18:25'))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals('2014-11-24 18:25', date('Y-m-d H:i', $task['date_started'])); + + // Set a timestamp + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'date_started' => time()))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(time(), $task['date_started'], '', 1); + } + + public function testChangeTimeEstimated() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['time_estimated']); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'time_estimated' => 13.3))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(13.3, $task['time_estimated']); + } + + public function testChangeTimeSpent() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(0, $task['time_spent']); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'time_spent' => 13.3))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(13.3, $task['time_spent']); + } + + public function testChangeTags() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'tags' => array('tag1', 'tag2')))); + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'tags' => array('tag2')))); + + $tags = $taskTagModel->getList(1); + $this->assertEquals(array(2 => 'tag2'), $tags); + } + + public function testRemoveAllTags() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskModificationModel = new TaskModificationModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test1', 'project_id' => 1, 'tags' => array('tag1', 'tag2')))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'test2', 'project_id' => 1, 'tags' => array('tag1', 'tag2')))); + + $this->assertTrue($taskModificationModel->update(array('id' => 1))); + $tags = $taskTagModel->getList(1); + $this->assertEquals(array(1 => 'tag1', 2 => 'tag2'), $tags); + + $this->assertTrue($taskModificationModel->update(array('id' => 1, 'tags' => array()))); + $tags = $taskTagModel->getList(1); + $this->assertEquals(array(), $tags); + + $this->assertTrue($taskModificationModel->update(array('id' => 2, 'tags' => array('')))); + $tags = $taskTagModel->getList(2); + $this->assertEquals(array(), $tags); + } +} diff --git a/tests/units/Model/TaskModificationTest.php b/tests/units/Model/TaskModificationTest.php deleted file mode 100644 index a8ace25a..00000000 --- a/tests/units/Model/TaskModificationTest.php +++ /dev/null @@ -1,283 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskModificationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\ProjectModel; - -class TaskModificationTest extends Base -{ - public function onCreateUpdate($event) - { - $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); - - $event_data = $event->getAll(); - $this->assertNotEmpty($event_data); - $this->assertEquals(1, $event_data['task_id']); - $this->assertEquals('Task #1', $event_data['title']); - } - - public function onUpdate($event) - { - $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); - - $event_data = $event->getAll(); - $this->assertNotEmpty($event_data); - $this->assertEquals(1, $event_data['task_id']); - $this->assertEquals('Task #1', $event_data['title']); - } - - public function onAssigneeChange($event) - { - $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); - - $event_data = $event->getAll(); - $this->assertNotEmpty($event_data); - $this->assertEquals(1, $event_data['task_id']); - $this->assertEquals(1, $event_data['owner_id']); - } - - public function testThatNoEventAreFiredWhenNoChanges() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($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(TaskModel::EVENT_CREATE_UPDATE, array($this, 'onCreateUpdate')); - $this->container['dispatcher']->addListener(TaskModel::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 ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($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(TaskModel::EVENT_CREATE_UPDATE, array($this, 'onCreateUpdate')); - $this->container['dispatcher']->addListener(TaskModel::EVENT_UPDATE, array($this, 'onUpdate')); - - $this->assertTrue($tm->update(array('id' => 1, 'title' => 'Task #1'))); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.TaskModificationTest::onCreateUpdate', $called); - $this->assertArrayHasKey(TaskModel::EVENT_UPDATE.'.TaskModificationTest::onUpdate', $called); - - $task = $tf->getById(1); - $this->assertEquals('Task #1', $task['title']); - } - - public function testChangeAssignee() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(0, $task['owner_id']); - - $this->container['dispatcher']->addListener(TaskModel::EVENT_ASSIGNEE_CHANGE, array($this, 'onAssigneeChange')); - - $this->assertTrue($tm->update(array('id' => 1, 'owner_id' => 1))); - - $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_ASSIGNEE_CHANGE.'.TaskModificationTest::onAssigneeChange', $called); - - $task = $tf->getById(1); - $this->assertEquals(1, $task['owner_id']); - } - - public function testChangeDescription() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals('', $task['description']); - - $this->assertTrue($tm->update(array('id' => 1, 'description' => 'test'))); - - $task = $tf->getById(1); - $this->assertEquals('test', $task['description']); - } - - public function testChangeCategory() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(0, $task['category_id']); - - $this->assertTrue($tm->update(array('id' => 1, 'category_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(1, $task['category_id']); - } - - public function testChangeColor() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals('yellow', $task['color_id']); - - $this->assertTrue($tm->update(array('id' => 1, 'color_id' => 'blue'))); - - $task = $tf->getById(1); - $this->assertEquals('blue', $task['color_id']); - } - - public function testChangeScore() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(0, $task['score']); - - $this->assertTrue($tm->update(array('id' => 1, 'score' => 13))); - - $task = $tf->getById(1); - $this->assertEquals(13, $task['score']); - } - - public function testChangeDueDate() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(0, $task['date_due']); - - $this->assertTrue($tm->update(array('id' => 1, 'date_due' => '2014-11-24'))); - - $task = $tf->getById(1); - $this->assertEquals('2014-11-24', date('Y-m-d', $task['date_due'])); - - $this->assertTrue($tm->update(array('id' => 1, 'date_due' => time()))); - - $task = $tf->getById(1); - $this->assertEquals(date('Y-m-d'), date('Y-m-d', $task['date_due'])); - } - - public function testChangeStartedDate() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(0, $task['date_started']); - - // Set only a date - $this->assertTrue($tm->update(array('id' => 1, 'date_started' => '2014-11-24'))); - - $task = $tf->getById(1); - $this->assertEquals('2014-11-24 '.date('H:i'), date('Y-m-d H:i', $task['date_started'])); - - // Set a datetime - $this->assertTrue($tm->update(array('id' => 1, 'date_started' => '2014-11-24 16:25'))); - - $task = $tf->getById(1); - $this->assertEquals('2014-11-24 16:25', date('Y-m-d H:i', $task['date_started'])); - - // Set a datetime - $this->assertTrue($tm->update(array('id' => 1, 'date_started' => '11/24/2014 18:25'))); - - $task = $tf->getById(1); - $this->assertEquals('2014-11-24 18:25', date('Y-m-d H:i', $task['date_started'])); - - // Set a timestamp - $this->assertTrue($tm->update(array('id' => 1, 'date_started' => time()))); - - $task = $tf->getById(1); - $this->assertEquals(time(), $task['date_started'], '', 1); - } - - public function testChangeTimeEstimated() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(0, $task['time_estimated']); - - $this->assertTrue($tm->update(array('id' => 1, 'time_estimated' => 13.3))); - - $task = $tf->getById(1); - $this->assertEquals(13.3, $task['time_estimated']); - } - - public function testChangeTimeSpent() - { - $p = new ProjectModel($this->container); - $tc = new TaskCreationModel($this->container); - $tm = new TaskModificationModel($this->container); - $tf = new TaskFinderModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'test'))); - $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(0, $task['time_spent']); - - $this->assertTrue($tm->update(array('id' => 1, 'time_spent' => 13.3))); - - $task = $tf->getById(1); - $this->assertEquals(13.3, $task['time_spent']); - } -} diff --git a/tests/units/Model/TaskPositionTest.php b/tests/units/Model/TaskPositionModelTest.php index 7ab6950e..03caf7ed 100644 --- a/tests/units/Model/TaskPositionTest.php +++ b/tests/units/Model/TaskPositionModelTest.php @@ -11,57 +11,57 @@ use Kanboard\Model\TaskFinderModel; use Kanboard\Model\ProjectModel; use Kanboard\Model\SwimlaneModel; -class TaskPositionTest extends Base +class TaskPositionModelTest extends Base { public function testGetTaskProgression() { - $t = new TaskModel($this->container); - $ts = new TaskStatusModel($this->container); - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); + $taskModel = new TaskModel($this->container); + $taskStatusModel = new TaskStatusModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); $columnModel = new ColumnModel($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), $columnModel->getList(1))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(0, $taskModel->getProgress($taskFinderModel->getById(1), $columnModel->getList(1))); - $this->assertTrue($tp->movePosition(1, 1, 2, 1)); - $this->assertEquals(25, $t->getProgress($tf->getById(1), $columnModel->getList(1))); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 2, 1)); + $this->assertEquals(25, $taskModel->getProgress($taskFinderModel->getById(1), $columnModel->getList(1))); - $this->assertTrue($tp->movePosition(1, 1, 3, 1)); - $this->assertEquals(50, $t->getProgress($tf->getById(1), $columnModel->getList(1))); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 3, 1)); + $this->assertEquals(50, $taskModel->getProgress($taskFinderModel->getById(1), $columnModel->getList(1))); - $this->assertTrue($tp->movePosition(1, 1, 4, 1)); - $this->assertEquals(75, $t->getProgress($tf->getById(1), $columnModel->getList(1))); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 4, 1)); + $this->assertEquals(75, $taskModel->getProgress($taskFinderModel->getById(1), $columnModel->getList(1))); - $this->assertTrue($ts->close(1)); - $this->assertEquals(100, $t->getProgress($tf->getById(1), $columnModel->getList(1))); + $this->assertTrue($taskStatusModel->close(1)); + $this->assertEquals(100, $taskModel->getProgress($taskFinderModel->getById(1), $columnModel->getList(1))); } public function testMoveTaskToWrongPosition() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); - $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))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); // We move the task 2 to the position 0 - $this->assertFalse($tp->movePosition(1, 1, 3, 0)); + $this->assertFalse($taskPositionModel->movePosition(1, 1, 3, 0)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); @@ -69,26 +69,26 @@ class TaskPositionTest extends Base public function testMoveTaskToGreaterPosition() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); - $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))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); // We move the task 2 to the position 42 - $this->assertTrue($tp->movePosition(1, 1, 1, 42)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 1, 42)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); @@ -96,26 +96,26 @@ class TaskPositionTest extends Base public function testMoveTaskToEmptyColumn() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); - $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))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); // We move the task 1 to the column 3 - $this->assertTrue($tp->movePosition(1, 1, 3, 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 3, 1)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(3, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); @@ -123,62 +123,62 @@ class TaskPositionTest extends Base public function testMoveTaskToAnotherColumn() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($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(2, $tc->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); - $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' => 2))); - $this->assertEquals(5, $tc->create(array('title' => 'Task #5', 'project_id' => 1, 'column_id' => 2))); - $this->assertEquals(6, $tc->create(array('title' => 'Task #6', 'project_id' => 1, 'column_id' => 2))); - $this->assertEquals(7, $tc->create(array('title' => 'Task #7', 'project_id' => 1, 'column_id' => 3))); - $this->assertEquals(8, $tc->create(array('title' => 'Task #8', 'project_id' => 1, 'column_id' => 1))); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'Task #4', 'project_id' => 1, 'column_id' => 2))); + $this->assertEquals(5, $taskCreationModel->create(array('title' => 'Task #5', 'project_id' => 1, 'column_id' => 2))); + $this->assertEquals(6, $taskCreationModel->create(array('title' => 'Task #6', 'project_id' => 1, 'column_id' => 2))); + $this->assertEquals(7, $taskCreationModel->create(array('title' => 'Task #7', 'project_id' => 1, 'column_id' => 3))); + $this->assertEquals(8, $taskCreationModel->create(array('title' => 'Task #8', 'project_id' => 1, 'column_id' => 1))); // We move the task 3 to the column 3 - $this->assertTrue($tp->movePosition(1, 3, 3, 2)); + $this->assertTrue($taskPositionModel->movePosition(1, 3, 3, 2)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); - $task = $tf->getById(3); + $task = $taskFinderModel->getById(3); $this->assertEquals(3, $task['id']); $this->assertEquals(3, $task['column_id']); $this->assertEquals(2, $task['position']); - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(5); + $task = $taskFinderModel->getById(5); $this->assertEquals(5, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(2, $task['position']); - $task = $tf->getById(6); + $task = $taskFinderModel->getById(6); $this->assertEquals(6, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(3, $task['position']); - $task = $tf->getById(7); + $task = $taskFinderModel->getById(7); $this->assertEquals(7, $task['id']); $this->assertEquals(3, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(8); + $task = $taskFinderModel->getById(8); $this->assertEquals(8, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(3, $task['position']); @@ -186,37 +186,37 @@ class TaskPositionTest extends Base public function testMoveTaskTop() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($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(2, $tc->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); - $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))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'Task #4', 'project_id' => 1, 'column_id' => 1))); // Move the last task to the top - $this->assertTrue($tp->movePosition(1, 4, 1, 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 4, 1, 1)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(3, $task['position']); - $task = $tf->getById(3); + $task = $taskFinderModel->getById(3); $this->assertEquals(3, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(4, $task['position']); - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); @@ -224,37 +224,37 @@ class TaskPositionTest extends Base public function testMoveTaskBottom() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($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(2, $tc->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); - $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))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'Task #4', 'project_id' => 1, 'column_id' => 1))); // Move the first task to the bottom - $this->assertTrue($tp->movePosition(1, 1, 1, 4)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 1, 4)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(4, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(3); + $task = $taskFinderModel->getById(3); $this->assertEquals(3, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(3, $task['position']); @@ -262,12 +262,12 @@ class TaskPositionTest extends Base public function testMovePosition() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); $counter = 1; $task_per_column = 5; @@ -280,240 +280,240 @@ class TaskPositionTest extends Base 'owner_id' => 0, ); - $this->assertEquals($counter, $tc->create($task)); + $this->assertEquals($counter, $taskCreationModel->create($task)); - $task = $tf->getById($counter); + $task = $taskFinderModel->getById($counter); $this->assertNotEmpty($task); $this->assertEquals($i, $task['position']); } } // We move task id #4, column 1, position 4 to the column 2, position 3 - $this->assertTrue($tp->movePosition(1, 4, 2, 3)); + $this->assertTrue($taskPositionModel->movePosition(1, 4, 2, 3)); // We check the new position of the task - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(3, $task['position']); // The tasks before have the correct position - $task = $tf->getById(3); + $task = $taskFinderModel->getById(3); $this->assertEquals(3, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(3, $task['position']); - $task = $tf->getById(7); + $task = $taskFinderModel->getById(7); $this->assertEquals(7, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(2, $task['position']); // The tasks after have the correct position - $task = $tf->getById(5); + $task = $taskFinderModel->getById(5); $this->assertEquals(5, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(4, $task['position']); - $task = $tf->getById(8); + $task = $taskFinderModel->getById(8); $this->assertEquals(8, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(4, $task['position']); // The number of tasks per column - $this->assertEquals($task_per_column - 1, $tf->countByColumnId(1, 1)); - $this->assertEquals($task_per_column + 1, $tf->countByColumnId(1, 2)); - $this->assertEquals($task_per_column, $tf->countByColumnId(1, 3)); - $this->assertEquals($task_per_column, $tf->countByColumnId(1, 4)); + $this->assertEquals($task_per_column - 1, $taskFinderModel->countByColumnId(1, 1)); + $this->assertEquals($task_per_column + 1, $taskFinderModel->countByColumnId(1, 2)); + $this->assertEquals($task_per_column, $taskFinderModel->countByColumnId(1, 3)); + $this->assertEquals($task_per_column, $taskFinderModel->countByColumnId(1, 4)); // We move task id #1, column 1, position 1 to the column 4, position 6 (last position) - $this->assertTrue($tp->movePosition(1, 1, 4, $task_per_column + 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 4, $task_per_column + 1)); // We check the new position of the task - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(4, $task['column_id']); $this->assertEquals($task_per_column + 1, $task['position']); // The tasks before have the correct position - $task = $tf->getById(20); + $task = $taskFinderModel->getById(20); $this->assertEquals(20, $task['id']); $this->assertEquals(4, $task['column_id']); $this->assertEquals($task_per_column, $task['position']); // The tasks after have the correct position - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); // The number of tasks per column - $this->assertEquals($task_per_column - 2, $tf->countByColumnId(1, 1)); - $this->assertEquals($task_per_column + 1, $tf->countByColumnId(1, 2)); - $this->assertEquals($task_per_column, $tf->countByColumnId(1, 3)); - $this->assertEquals($task_per_column + 1, $tf->countByColumnId(1, 4)); + $this->assertEquals($task_per_column - 2, $taskFinderModel->countByColumnId(1, 1)); + $this->assertEquals($task_per_column + 1, $taskFinderModel->countByColumnId(1, 2)); + $this->assertEquals($task_per_column, $taskFinderModel->countByColumnId(1, 3)); + $this->assertEquals($task_per_column + 1, $taskFinderModel->countByColumnId(1, 4)); // Our previous moved task should stay at the same place - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(3, $task['position']); // Test wrong position number - $this->assertFalse($tp->movePosition(1, 2, 3, 0)); - $this->assertFalse($tp->movePosition(1, 2, 3, -2)); + $this->assertFalse($taskPositionModel->movePosition(1, 2, 3, 0)); + $this->assertFalse($taskPositionModel->movePosition(1, 2, 3, -2)); // Wrong column - $this->assertFalse($tp->movePosition(1, 2, 22, 2)); + $this->assertFalse($taskPositionModel->movePosition(1, 2, 22, 2)); // Test position greater than the last position - $this->assertTrue($tp->movePosition(1, 11, 1, 22)); + $this->assertTrue($taskPositionModel->movePosition(1, 11, 1, 22)); - $task = $tf->getById(11); + $task = $taskFinderModel->getById(11); $this->assertEquals(11, $task['id']); $this->assertEquals(1, $task['column_id']); - $this->assertEquals($tf->countByColumnId(1, 1), $task['position']); + $this->assertEquals($taskFinderModel->countByColumnId(1, 1), $task['position']); - $task = $tf->getById(5); + $task = $taskFinderModel->getById(5); $this->assertEquals(5, $task['id']); $this->assertEquals(1, $task['column_id']); - $this->assertEquals($tf->countByColumnId(1, 1) - 1, $task['position']); + $this->assertEquals($taskFinderModel->countByColumnId(1, 1) - 1, $task['position']); - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(3, $task['position']); - $this->assertEquals($task_per_column - 1, $tf->countByColumnId(1, 1)); - $this->assertEquals($task_per_column + 1, $tf->countByColumnId(1, 2)); - $this->assertEquals($task_per_column - 1, $tf->countByColumnId(1, 3)); - $this->assertEquals($task_per_column + 1, $tf->countByColumnId(1, 4)); + $this->assertEquals($task_per_column - 1, $taskFinderModel->countByColumnId(1, 1)); + $this->assertEquals($task_per_column + 1, $taskFinderModel->countByColumnId(1, 2)); + $this->assertEquals($task_per_column - 1, $taskFinderModel->countByColumnId(1, 3)); + $this->assertEquals($task_per_column + 1, $taskFinderModel->countByColumnId(1, 4)); // Our previous moved task should stay at the same place - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(3, $task['position']); // Test moving task to position 1 - $this->assertTrue($tp->movePosition(1, 14, 1, 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 14, 1, 1)); - $task = $tf->getById(14); + $task = $taskFinderModel->getById(14); $this->assertEquals(14, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); - $this->assertEquals($task_per_column, $tf->countByColumnId(1, 1)); - $this->assertEquals($task_per_column + 1, $tf->countByColumnId(1, 2)); - $this->assertEquals($task_per_column - 2, $tf->countByColumnId(1, 3)); - $this->assertEquals($task_per_column + 1, $tf->countByColumnId(1, 4)); + $this->assertEquals($task_per_column, $taskFinderModel->countByColumnId(1, 1)); + $this->assertEquals($task_per_column + 1, $taskFinderModel->countByColumnId(1, 2)); + $this->assertEquals($task_per_column - 2, $taskFinderModel->countByColumnId(1, 3)); + $this->assertEquals($task_per_column + 1, $taskFinderModel->countByColumnId(1, 4)); } public function testMoveTaskSwimlane() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'test 1'))); - $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))); - $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))); - $this->assertEquals(5, $tc->create(array('title' => 'Task #5', 'project_id' => 1, 'column_id' => 1))); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'test 1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(3, $taskCreationModel->create(array('title' => 'Task #3', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'Task #4', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(5, $taskCreationModel->create(array('title' => 'Task #5', 'project_id' => 1, 'column_id' => 1))); // Move the task to the swimlane - $this->assertTrue($tp->movePosition(1, 1, 2, 1, 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 2, 1, 1)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(1, $task['swimlane_id']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(0, $task['swimlane_id']); - $task = $tf->getById(3); + $task = $taskFinderModel->getById(3); $this->assertEquals(3, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); $this->assertEquals(0, $task['swimlane_id']); - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(3, $task['position']); $this->assertEquals(0, $task['swimlane_id']); // Move the task to the swimlane - $this->assertTrue($tp->movePosition(1, 2, 2, 1, 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 2, 2, 1, 1)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(2, $task['position']); $this->assertEquals(1, $task['swimlane_id']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(1, $task['swimlane_id']); - $task = $tf->getById(3); + $task = $taskFinderModel->getById(3); $this->assertEquals(3, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(0, $task['swimlane_id']); - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); $this->assertEquals(0, $task['swimlane_id']); // Move the task 5 to the last column - $this->assertTrue($tp->movePosition(1, 5, 4, 1, 0)); + $this->assertTrue($taskPositionModel->movePosition(1, 5, 4, 1, 0)); // Check tasks position - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(2, $task['position']); $this->assertEquals(1, $task['swimlane_id']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(1, $task['swimlane_id']); - $task = $tf->getById(3); + $task = $taskFinderModel->getById(3); $this->assertEquals(3, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(0, $task['swimlane_id']); - $task = $tf->getById(4); + $task = $taskFinderModel->getById(4); $this->assertEquals(4, $task['id']); $this->assertEquals(1, $task['column_id']); $this->assertEquals(2, $task['position']); $this->assertEquals(0, $task['swimlane_id']); - $task = $tf->getById(5); + $task = $taskFinderModel->getById(5); $this->assertEquals(5, $task['id']); $this->assertEquals(4, $task['column_id']); $this->assertEquals(1, $task['position']); @@ -522,73 +522,73 @@ class TaskPositionTest extends Base public function testEvents() { - $tp = new TaskPositionModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SwimlaneModel($this->container); + $taskPositionModel = new TaskPositionModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'test 1'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'test 1'))); - $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' => 2))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); + $this->assertEquals(2, $taskCreationModel->create(array('title' => 'Task #2', 'project_id' => 1, 'column_id' => 2))); $this->container['dispatcher']->addListener(TaskModel::EVENT_MOVE_COLUMN, array($this, 'onMoveColumn')); $this->container['dispatcher']->addListener(TaskModel::EVENT_MOVE_POSITION, array($this, 'onMovePosition')); $this->container['dispatcher']->addListener(TaskModel::EVENT_MOVE_SWIMLANE, array($this, 'onMoveSwimlane')); // We move the task 1 to the column 2 - $this->assertTrue($tp->movePosition(1, 1, 2, 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 2, 1)); - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(1, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(2, $task['position']); $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_MOVE_COLUMN.'.TaskPositionTest::onMoveColumn', $called); + $this->assertArrayHasKey(TaskModel::EVENT_MOVE_COLUMN.'.TaskPositionModelTest::onMoveColumn', $called); $this->assertEquals(1, count($called)); // We move the task 1 to the position 2 - $this->assertTrue($tp->movePosition(1, 1, 2, 2)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 2, 2)); - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(2, $task['position']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(1, $task['position']); $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_MOVE_POSITION.'.TaskPositionTest::onMovePosition', $called); + $this->assertArrayHasKey(TaskModel::EVENT_MOVE_POSITION.'.TaskPositionModelTest::onMovePosition', $called); $this->assertEquals(2, count($called)); // Move to another swimlane - $this->assertTrue($tp->movePosition(1, 1, 3, 1, 1)); + $this->assertTrue($taskPositionModel->movePosition(1, 1, 3, 1, 1)); - $task = $tf->getById(1); + $task = $taskFinderModel->getById(1); $this->assertEquals(1, $task['id']); $this->assertEquals(3, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(1, $task['swimlane_id']); - $task = $tf->getById(2); + $task = $taskFinderModel->getById(2); $this->assertEquals(2, $task['id']); $this->assertEquals(2, $task['column_id']); $this->assertEquals(1, $task['position']); $this->assertEquals(0, $task['swimlane_id']); $called = $this->container['dispatcher']->getCalledListeners(); - $this->assertArrayHasKey(TaskModel::EVENT_MOVE_SWIMLANE.'.TaskPositionTest::onMoveSwimlane', $called); + $this->assertArrayHasKey(TaskModel::EVENT_MOVE_SWIMLANE.'.TaskPositionModelTest::onMoveSwimlane', $called); $this->assertEquals(3, count($called)); } diff --git a/tests/units/Model/TaskProjectDuplicationModelTest.php b/tests/units/Model/TaskProjectDuplicationModelTest.php new file mode 100644 index 00000000..9ff33bc1 --- /dev/null +++ b/tests/units/Model/TaskProjectDuplicationModelTest.php @@ -0,0 +1,377 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Core\Security\Role; +use Kanboard\Model\CategoryModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\ProjectUserRoleModel; +use Kanboard\Model\SwimlaneModel; +use Kanboard\Model\TagModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskProjectDuplicationModel; +use Kanboard\Model\TaskTagModel; +use Kanboard\Model\UserModel; + +class TaskProjectDuplicationModelTest extends Base +{ + public function testDuplicateAnotherProject() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $categoryModel = new CategoryModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 1))); + $this->assertTrue($categoryModel->exists(1)); + + $this->assertEquals(1, $taskCreationModel->create(array( + 'title' => 'test', + 'project_id' => 1, + 'column_id' => 2, + 'owner_id' => 1, + 'category_id' => 1, + 'priority' => 3, + ))); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, function () {}); + $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE, function () {}); + + // We duplicate our task to the 2nd project + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2)); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.closure', $called); + $this->assertArrayHasKey(TaskModel::EVENT_CREATE.'.closure', $called); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(0, $task['category_id']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals(3, $task['priority']); + $this->assertEquals('test', $task['title']); + } + + public function testDuplicateAnotherProjectWithCategory() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $categoryModel = new CategoryModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 1))); + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 2))); + $this->assertTrue($categoryModel->exists(1)); + $this->assertTrue($categoryModel->exists(2)); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'category_id' => 1))); + + // We duplicate our task to the 2nd project + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(2, $task['category_id']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('test', $task['title']); + } + + public function testDuplicateAnotherProjectWithPredefinedCategory() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $categoryModel = new CategoryModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 1))); + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 2))); + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #2', 'project_id' => 2))); + $this->assertTrue($categoryModel->exists(1)); + $this->assertTrue($categoryModel->exists(2)); + $this->assertTrue($categoryModel->exists(3)); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'category_id' => 1))); + + // We duplicate our task to the 2nd project with no category + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2, null, null, 0)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['category_id']); + + // We duplicate our task to the 2nd project with a different category + $this->assertEquals(3, $taskProjectDuplicationModel->duplicateToProject(1, 2, null, null, 3)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(3); + $this->assertNotEmpty($task); + $this->assertEquals(3, $task['category_id']); + } + + public function testDuplicateAnotherProjectWithSwimlane() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 2, 'name' => 'Swimlane #1'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); + + // We duplicate our task to the 2nd project + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(0, $task['category_id']); + $this->assertEquals(2, $task['swimlane_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('test', $task['title']); + } + + public function testDuplicateAnotherProjectWithoutSwimlane() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 2, 'name' => 'Swimlane #2'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); + + // We duplicate our task to the 2nd project + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(0, $task['category_id']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('test', $task['title']); + } + + public function testDuplicateAnotherProjectWithPredefinedSwimlane() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 2, 'name' => 'Swimlane #1'))); + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 2, 'name' => 'Swimlane #2'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); + + // We duplicate our task to the 2nd project + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2, 3)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(3, $task['swimlane_id']); + } + + public function testDuplicateAnotherProjectWithPredefinedColumn() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2))); + + // We duplicate our task to the 2nd project with a different column + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2, null, 7)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(7, $task['column_id']); + } + + public function testDuplicateAnotherProjectWithUser() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2))); + + // We duplicate our task to the 2nd project + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('test', $task['title']); + + // We create a new user for our project + $user = new UserModel($this->container); + $this->assertNotFalse($user->create(array('username' => 'unittest#1', 'password' => 'unittest'))); + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($projectUserRoleModel->addUser(2, 2, Role::PROJECT_MEMBER)); + + // We duplicate our task to the 2nd project + $this->assertEquals(3, $taskProjectDuplicationModel->duplicateToProject(1, 2)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(3); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['position']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(2, $task['owner_id']); + $this->assertEquals(2, $task['project_id']); + + // We duplicate a task with a not allowed user + $this->assertEquals(4, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 3))); + $this->assertEquals(5, $taskProjectDuplicationModel->duplicateToProject(4, 2)); + + $task = $taskFinderModel->getById(5); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['position']); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals(5, $task['column_id']); + } + + public function testDuplicateAnotherProjectWithPredefinedUser() + { + $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2))); + $this->assertTrue($projectUserRoleModel->addUser(2, 1, Role::PROJECT_MEMBER)); + + // We duplicate our task to the 2nd project + $this->assertEquals(2, $taskProjectDuplicationModel->duplicateToProject(1, 2, null, null, null, 1)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['owner_id']); + } + + public function testDuplicateAnotherProjectWithDifferentTags() + { + $taskProjectMoveModel = new TaskProjectDuplicationModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $tagModel = new TagModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create our tags for each projects + $this->assertEquals(1, $tagModel->create(1, 'T1')); + $this->assertEquals(2, $tagModel->create(1, 'T2')); + $this->assertEquals(3, $tagModel->create(2, 'T2')); + $this->assertEquals(4, $tagModel->create(2, 'T3')); + $this->assertEquals(5, $tagModel->create(0, 'T4')); + $this->assertEquals(6, $tagModel->create(0, 'T5')); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'tags' => array('T1', 'T2', 'T5')))); + + // We move our task to the 2nd project + $this->assertEquals(2, $taskProjectMoveModel->duplicateToProject(1, 2)); + + // Check the values of the moved task + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['id']); + $this->assertEquals(2, $task['project_id']); + + // Check tags + $tags = $taskTagModel->getList(2); + $this->assertCount(2, $tags); + $this->assertArrayHasKey(3, $tags); + $this->assertArrayHasKey(6, $tags); + } +} diff --git a/tests/units/Model/TaskProjectMoveModelTest.php b/tests/units/Model/TaskProjectMoveModelTest.php new file mode 100644 index 00000000..52f61b28 --- /dev/null +++ b/tests/units/Model/TaskProjectMoveModelTest.php @@ -0,0 +1,277 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Core\Security\Role; +use Kanboard\Model\CategoryModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\ProjectUserRoleModel; +use Kanboard\Model\SwimlaneModel; +use Kanboard\Model\TagModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskProjectMoveModel; +use Kanboard\Model\TaskTagModel; +use Kanboard\Model\UserModel; + +class TaskProjectMoveModelTest extends Base +{ + public function onMoveProject($event) + { + $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event); + + $event_data = $event->getAll(); + $this->assertNotEmpty($event_data); + $this->assertEquals(1, $event_data['task_id']); + $this->assertEquals('test', $event_data['task']['title']); + } + + public function testMoveAnotherProject() + { + $taskProjectMoveModel = new TaskProjectMoveModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array( + 'title' => 'test', + 'project_id' => 1, + 'owner_id' => 1, + 'category_id' => 10, + 'position' => 333, + 'priority' => 1, + ))); + + $this->container['dispatcher']->addListener(TaskModel::EVENT_MOVE_PROJECT, array($this, 'onMoveProject')); + + // We duplicate our task to the 2nd project + $this->assertTrue($taskProjectMoveModel->moveToProject(1, 2)); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(TaskModel::EVENT_MOVE_PROJECT.'.TaskProjectMoveModelTest::onMoveProject', $called); + + // Check the values of the moved task + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(0, $task['category_id']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals(5, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(1, $task['priority']); + $this->assertEquals('test', $task['title']); + } + + public function testMoveAnotherProjectWithCategory() + { + $taskProjectMoveModel = new TaskProjectMoveModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $categoryModel = new CategoryModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 1))); + $this->assertNotFalse($categoryModel->create(array('name' => 'Category #1', 'project_id' => 2))); + $this->assertTrue($categoryModel->exists(1)); + $this->assertTrue($categoryModel->exists(2)); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'category_id' => 1))); + + // We move our task to the 2nd project + $this->assertTrue($taskProjectMoveModel->moveToProject(1, 2)); + + // Check the values of the duplicated task + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(2, $task['category_id']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('test', $task['title']); + } + + public function testMoveAnotherProjectWithUser() + { + $taskProjectMoveModel = new TaskProjectMoveModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + $userModel = new UserModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create a new user for our project + $this->assertNotFalse($userModel->create(array('username' => 'unittest#1', 'password' => 'unittest'))); + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($projectUserRoleModel->addUser(2, 2, Role::PROJECT_MEMBER)); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2))); + + // We move our task to the 2nd project + $this->assertTrue($taskProjectMoveModel->moveToProject(1, 2)); + + // Check the values of the moved task + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['owner_id']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals(6, $task['column_id']); + } + + public function testMoveAnotherProjectWithForbiddenUser() + { + $taskProjectMoveModel = new TaskProjectMoveModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $projectUserRoleModel = new ProjectUserRoleModel($this->container); + $userModel = new UserModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create a new user for our project + $this->assertNotFalse($userModel->create(array('username' => 'unittest#1', 'password' => 'unittest'))); + $this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MEMBER)); + $this->assertTrue($projectUserRoleModel->addUser(2, 2, Role::PROJECT_MEMBER)); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 3))); + + // We move our task to the 2nd project + $this->assertTrue($taskProjectMoveModel->moveToProject(1, 2)); + + // Check the values of the moved task + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['position']); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals(6, $task['column_id']); + } + + public function testMoveAnotherProjectWithSwimlane() + { + $taskProjectMoveModel = new TaskProjectMoveModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 2, 'name' => 'Swimlane #1'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); + + // We move our task to the 2nd project + $this->assertTrue($taskProjectMoveModel->moveToProject(1, 2)); + + // Check the values of the moved task + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(0, $task['category_id']); + $this->assertEquals(2, $task['swimlane_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('test', $task['title']); + } + + public function testMoveAnotherProjectWithoutSwimlane() + { + $taskProjectMoveModel = new TaskProjectMoveModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $swimlaneModel = new SwimlaneModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1'))); + $this->assertNotFalse($swimlaneModel->create(array('project_id' => 2, 'name' => 'Swimlane #2'))); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2, 'swimlane_id' => 1))); + + // We move our task to the 2nd project + $this->assertTrue($taskProjectMoveModel->moveToProject(1, 2)); + + // Check the values of the moved task + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(0, $task['owner_id']); + $this->assertEquals(0, $task['category_id']); + $this->assertEquals(0, $task['swimlane_id']); + $this->assertEquals(6, $task['column_id']); + $this->assertEquals(1, $task['position']); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('test', $task['title']); + } + + public function testMoveAnotherProjectWithDifferentTags() + { + $taskProjectMoveModel = new TaskProjectMoveModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $tagModel = new TagModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + // We create 2 projects + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); + + // We create our tags for each projects + $this->assertEquals(1, $tagModel->create(1, 'T1')); + $this->assertEquals(2, $tagModel->create(1, 'T2')); + $this->assertEquals(3, $tagModel->create(2, 'T3')); + $this->assertEquals(4, $tagModel->create(2, 'T4')); + $this->assertEquals(5, $tagModel->create(0, 'T5')); + $this->assertEquals(6, $tagModel->create(0, 'T6')); + + // We create a task + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'test', 'project_id' => 1, 'tags' => array('T1', 'T5', 'T6')))); + + // We move our task to the 2nd project + $this->assertTrue($taskProjectMoveModel->moveToProject(1, 2)); + + // Check the values of the moved task + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + + // Check tags + $tags = $taskTagModel->getList(1); + $this->assertCount(2, $tags); + $this->assertArrayHasKey(5, $tags); + $this->assertArrayHasKey(6, $tags); + } +} diff --git a/tests/units/Model/TaskRecurrenceModelTest.php b/tests/units/Model/TaskRecurrenceModelTest.php new file mode 100644 index 00000000..85d77bc2 --- /dev/null +++ b/tests/units/Model/TaskRecurrenceModelTest.php @@ -0,0 +1,126 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Core\DateParser; +use Kanboard\Model\ProjectModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\TaskModel; +use Kanboard\Model\TaskRecurrenceModel; +use Kanboard\Model\TaskTagModel; + +class TaskRecurrenceModelTest extends Base +{ + public function testRecurrenceSettings() + { + $taskRecurrenceModel = new TaskRecurrenceModel($this->container); + + $statuses = $taskRecurrenceModel->getRecurrenceStatusList(); + $this->assertCount(2, $statuses); + $this->assertArrayHasKey(TaskModel::RECURRING_STATUS_NONE, $statuses); + $this->assertArrayHasKey(TaskModel::RECURRING_STATUS_PENDING, $statuses); + $this->assertArrayNotHasKey(TaskModel::RECURRING_STATUS_PROCESSED, $statuses); + + $triggers = $taskRecurrenceModel->getRecurrenceTriggerList(); + $this->assertCount(3, $triggers); + $this->assertArrayHasKey(TaskModel::RECURRING_TRIGGER_FIRST_COLUMN, $triggers); + $this->assertArrayHasKey(TaskModel::RECURRING_TRIGGER_LAST_COLUMN, $triggers); + $this->assertArrayHasKey(TaskModel::RECURRING_TRIGGER_CLOSE, $triggers); + + $dates = $taskRecurrenceModel->getRecurrenceBasedateList(); + $this->assertCount(2, $dates); + $this->assertArrayHasKey(TaskModel::RECURRING_BASEDATE_DUEDATE, $dates); + $this->assertArrayHasKey(TaskModel::RECURRING_BASEDATE_TRIGGERDATE, $dates); + + $timeframes = $taskRecurrenceModel->getRecurrenceTimeframeList(); + $this->assertCount(3, $timeframes); + $this->assertArrayHasKey(TaskModel::RECURRING_TIMEFRAME_DAYS, $timeframes); + $this->assertArrayHasKey(TaskModel::RECURRING_TIMEFRAME_MONTHS, $timeframes); + $this->assertArrayHasKey(TaskModel::RECURRING_TIMEFRAME_YEARS, $timeframes); + } + + public function testCalculateRecurringTaskDueDate() + { + $taskRecurrenceModel = new TaskRecurrenceModel($this->container); + + $values = array('date_due' => 0); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(0, $values['date_due']); + + $values = array('date_due' => 0, 'recurrence_factor' => 0, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(0, $values['date_due']); + + $values = array('date_due' => 1431291376, 'recurrence_factor' => 1, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(time() + 86400, $values['date_due'], '', 1); + + $values = array('date_due' => 1431291376, 'recurrence_factor' => -2, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(time() - 2 * 86400, $values['date_due'], '', 1); + + $values = array('date_due' => 1431291376, 'recurrence_factor' => 1, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(1431291376 + 86400, $values['date_due'], '', 1); + + $values = array('date_due' => 1431291376, 'recurrence_factor' => -1, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(1431291376 - 86400, $values['date_due'], '', 1); + + $values = array('date_due' => 1431291376, 'recurrence_factor' => 2, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_MONTHS); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(1436561776, $values['date_due'], '', 1); + + $values = array('date_due' => 1431291376, 'recurrence_factor' => 2, 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_DUEDATE, 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_YEARS); + $taskRecurrenceModel->calculateRecurringTaskDueDate($values); + $this->assertEquals(1494449776, $values['date_due'], '', 1); + } + + public function testDuplicateRecurringTask() + { + $taskRecurrenceModel = new TaskRecurrenceModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $dateParser = new DateParser($this->container); + $taskTagModel = new TaskTagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array( + 'title' => 'test', + 'project_id' => 1, + 'date_due' => 1436561776, + 'recurrence_status' => TaskModel::RECURRING_STATUS_PENDING, + 'recurrence_trigger' => TaskModel::RECURRING_TRIGGER_CLOSE, + 'recurrence_factor' => 2, + 'recurrence_timeframe' => TaskModel::RECURRING_TIMEFRAME_DAYS, + 'recurrence_basedate' => TaskModel::RECURRING_BASEDATE_TRIGGERDATE, + 'tags' => array('T1', 'T2'), + ))); + + $this->assertEquals(2, $taskRecurrenceModel->duplicateRecurringTask(1)); + + $task = $taskFinderModel->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(TaskModel::RECURRING_STATUS_PROCESSED, $task['recurrence_status']); + $this->assertEquals(2, $task['recurrence_child']); + $this->assertEquals(1436486400, $task['date_due'], '', 2); + + $task = $taskFinderModel->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(TaskModel::RECURRING_STATUS_PENDING, $task['recurrence_status']); + $this->assertEquals(TaskModel::RECURRING_TRIGGER_CLOSE, $task['recurrence_trigger']); + $this->assertEquals(TaskModel::RECURRING_TIMEFRAME_DAYS, $task['recurrence_timeframe']); + $this->assertEquals(TaskModel::RECURRING_BASEDATE_TRIGGERDATE, $task['recurrence_basedate']); + $this->assertEquals(1, $task['recurrence_parent']); + $this->assertEquals(2, $task['recurrence_factor']); + $this->assertEquals($dateParser->removeTimeFromTimestamp(strtotime('+2 days')), $task['date_due'], '', 2); + + $tags = $taskTagModel->getList(2); + $this->assertCount(2, $tags); + $this->assertArrayHasKey(1, $tags); + $this->assertArrayHasKey(2, $tags); + } +} diff --git a/tests/units/Model/TaskTagModelTest.php b/tests/units/Model/TaskTagModelTest.php new file mode 100644 index 00000000..3485368e --- /dev/null +++ b/tests/units/Model/TaskTagModelTest.php @@ -0,0 +1,148 @@ +<?php + +use Kanboard\Model\ProjectModel; +use Kanboard\Model\TagModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskTagModel; + +require_once __DIR__.'/../Base.php'; + +class TaskTagModelTest extends Base +{ + public function testAssociationAndDissociation() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + $tagModel = new TagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->assertEquals(1, $tagModel->create(0, 'My tag 1')); + $this->assertEquals(2, $tagModel->create(0, 'My tag 2')); + + $this->assertTrue($taskTagModel->save(1, 1, array('My tag 1', 'My tag 2', '', 'My tag 3'))); + + $tags = $taskTagModel->getTagsByTask(1); + $this->assertCount(3, $tags); + + $this->assertEquals(1, $tags[0]['id']); + $this->assertEquals('My tag 1', $tags[0]['name']); + + $this->assertEquals(2, $tags[1]['id']); + $this->assertEquals('My tag 2', $tags[1]['name']); + + $this->assertEquals(3, $tags[2]['id']); + $this->assertEquals('My tag 3', $tags[2]['name']); + + $this->assertTrue($taskTagModel->save(1, 1, array('My tag 3', 'My tag 1', 'My tag 4'))); + + $tags = $taskTagModel->getTagsByTask(1); + $this->assertCount(3, $tags); + + $this->assertEquals(1, $tags[0]['id']); + $this->assertEquals('My tag 1', $tags[0]['name']); + + $this->assertEquals(3, $tags[1]['id']); + $this->assertEquals('My tag 3', $tags[1]['name']); + + $this->assertEquals(4, $tags[2]['id']); + $this->assertEquals('My tag 4', $tags[2]['name']); + + $tags = $tagModel->getAll(); + $this->assertCount(4, $tags); + $this->assertEquals('My tag 1', $tags[0]['name']); + $this->assertEquals(0, $tags[0]['project_id']); + + $this->assertEquals('My tag 2', $tags[1]['name']); + $this->assertEquals(0, $tags[1]['project_id']); + + $this->assertEquals('My tag 3', $tags[2]['name']); + $this->assertEquals(1, $tags[2]['project_id']); + + $this->assertEquals('My tag 4', $tags[3]['name']); + $this->assertEquals(1, $tags[3]['project_id']); + } + + public function testGetTagsForTasks() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test1'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test2'))); + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test3'))); + + $this->assertTrue($taskTagModel->save(1, 1, array('My tag 1', 'My tag 2', 'My tag 3'))); + $this->assertTrue($taskTagModel->save(1, 2, array('My tag 3'))); + + $tags = $taskTagModel->getTagsByTasks(array(1, 2, 3)); + + $expected = array( + 1 => array( + array( + 'id' => 1, + 'name' => 'My tag 1', + 'task_id' => 1 + ), + array( + 'id' => 2, + 'name' => 'My tag 2', + 'task_id' => 1 + ), + array( + 'id' => 3, + 'name' => 'My tag 3', + 'task_id' => 1 + ), + ), + 2 => array( + array( + 'id' => 3, + 'name' => 'My tag 3', + 'task_id' => 2, + ) + ) + ); + + $this->assertEquals($expected, $tags); + } + + public function testGetTagsForTasksWithEmptyList() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'Test'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test1'))); + $this->assertTrue($taskTagModel->save(1, 1, array('My tag 1', 'My tag 2', 'My tag 3'))); + + $tags = $taskTagModel->getTagsByTasks(array()); + $this->assertEquals(array(), $tags); + } + + public function testGetTagIdNotAvailableInDestinationProject() + { + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskTagModel = new TaskTagModel($this->container); + $tagModel = new TagModel($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'P1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'P2'))); + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test1'))); + + $this->assertEquals(1, $tagModel->create(0, 'T0')); + $this->assertEquals(2, $tagModel->create(2, 'T1')); + $this->assertEquals(3, $tagModel->create(2, 'T3')); + $this->assertEquals(4, $tagModel->create(1, 'T2')); + $this->assertEquals(5, $tagModel->create(1, 'T3')); + $this->assertTrue($taskTagModel->save(1, 1, array('T0', 'T2', 'T3'))); + + $this->assertEquals(array(4, 5), $taskTagModel->getTagIdsByTaskNotAvailableInProject(1, 2)); + } +} diff --git a/tests/units/Model/TaskTest.php b/tests/units/Model/TaskTest.php deleted file mode 100644 index 89fc4dc1..00000000 --- a/tests/units/Model/TaskTest.php +++ /dev/null @@ -1,58 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\TaskModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\ProjectModel; - -class TaskTest extends Base -{ - public function testRemove() - { - $t = new TaskModel($this->container); - $tc = new TaskCreationModel($this->container); - $p = new ProjectModel($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1))); - - $this->assertTrue($t->remove(1)); - $this->assertFalse($t->remove(1234)); - } - - public function testGetTaskIdFromText() - { - $t = new TaskModel($this->container); - $this->assertEquals(123, $t->getTaskIdFromText('My task #123')); - $this->assertEquals(0, $t->getTaskIdFromText('My task 123')); - } - - public function testRecurrenceSettings() - { - $t = new TaskModel($this->container); - - $statuses = $t->getRecurrenceStatusList(); - $this->assertCount(2, $statuses); - $this->assertArrayHasKey(TaskModel::RECURRING_STATUS_NONE, $statuses); - $this->assertArrayHasKey(TaskModel::RECURRING_STATUS_PENDING, $statuses); - $this->assertArrayNotHasKey(TaskModel::RECURRING_STATUS_PROCESSED, $statuses); - - $triggers = $t->getRecurrenceTriggerList(); - $this->assertCount(3, $triggers); - $this->assertArrayHasKey(TaskModel::RECURRING_TRIGGER_FIRST_COLUMN, $triggers); - $this->assertArrayHasKey(TaskModel::RECURRING_TRIGGER_LAST_COLUMN, $triggers); - $this->assertArrayHasKey(TaskModel::RECURRING_TRIGGER_CLOSE, $triggers); - - $dates = $t->getRecurrenceBasedateList(); - $this->assertCount(2, $dates); - $this->assertArrayHasKey(TaskModel::RECURRING_BASEDATE_DUEDATE, $dates); - $this->assertArrayHasKey(TaskModel::RECURRING_BASEDATE_TRIGGERDATE, $dates); - - $timeframes = $t->getRecurrenceTimeframeList(); - $this->assertCount(3, $timeframes); - $this->assertArrayHasKey(TaskModel::RECURRING_TIMEFRAME_DAYS, $timeframes); - $this->assertArrayHasKey(TaskModel::RECURRING_TIMEFRAME_MONTHS, $timeframes); - $this->assertArrayHasKey(TaskModel::RECURRING_TIMEFRAME_YEARS, $timeframes); - } -} diff --git a/tests/units/Model/UserModelTest.php b/tests/units/Model/UserModelTest.php new file mode 100644 index 00000000..a0c9c575 --- /dev/null +++ b/tests/units/Model/UserModelTest.php @@ -0,0 +1,384 @@ +<?php + +require_once __DIR__.'/../Base.php'; + +use Kanboard\Model\UserModel; +use Kanboard\Model\SubtaskModel; +use Kanboard\Model\CommentModel; +use Kanboard\Model\TaskCreationModel; +use Kanboard\Model\TaskFinderModel; +use Kanboard\Model\ProjectModel; +use Kanboard\Core\Security\Role; + +class UserModelTest extends Base +{ + public function testGetByEmail() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1', 'password' => '123456', 'email' => 'user1@localhost'))); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'password' => '123456', 'email' => ''))); + + $this->assertNotEmpty($userModel->getByEmail('user1@localhost')); + $this->assertEmpty($userModel->getByEmail('')); + } + + public function testGetByExternalId() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1', 'password' => '123456', 'gitlab_id' => '1234'))); + + $this->assertNotEmpty($userModel->getByExternalId('gitlab_id', '1234')); + $this->assertEmpty($userModel->getByExternalId('gitlab_id', '')); + + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'password' => '123456', 'github_id' => 'plop'))); + $this->assertNotFalse($userModel->create(array('username' => 'user3', 'password' => '123456', 'github_id' => ''))); + + $this->assertNotEmpty($userModel->getByExternalId('github_id', 'plop')); + $this->assertEmpty($userModel->getByExternalId('github_id', '')); + + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user4', 'password' => '123456', 'google_id' => '1234'))); + $this->assertNotFalse($userModel->create(array('username' => 'user5', 'password' => '123456', 'google_id' => ''))); + + $this->assertNotEmpty($userModel->getByExternalId('google_id', '1234')); + $this->assertEmpty($userModel->getByExternalId('google_id', '')); + } + + public function testGetByToken() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1', 'token' => 'random'))); + $this->assertNotFalse($userModel->create(array('username' => 'user2', 'token' => ''))); + + $this->assertNotEmpty($userModel->getByToken('random')); + $this->assertEmpty($userModel->getByToken('')); + } + + public function testGetByUsername() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + + $this->assertNotEmpty($userModel->getByUsername('user1')); + $this->assertEmpty($userModel->getByUsername('user2')); + $this->assertEmpty($userModel->getByUsername('')); + } + + public function testExists() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + + $this->assertTrue($userModel->exists(1)); + $this->assertTrue($userModel->exists(2)); + $this->assertFalse($userModel->exists(3)); + } + + public function testCount() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'user1'))); + $this->assertEquals(2, $userModel->count()); + } + + public function testGetAll() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'you'))); + $this->assertEquals(3, $userModel->create(array('username' => 'me', 'name' => 'Me'))); + + $users = $userModel->getAll(); + $this->assertCount(3, $users); + $this->assertEquals('admin', $users[0]['username']); + $this->assertEquals('me', $users[1]['username']); + $this->assertEquals('you', $users[2]['username']); + } + + public function testGetActiveUsersList() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'you'))); + $this->assertEquals(3, $userModel->create(array('username' => 'me', 'name' => 'Me too'))); + $this->assertEquals(4, $userModel->create(array('username' => 'foobar', 'is_active' => 0))); + + $users = $userModel->getActiveUsersList(); + + $expected = array( + 1 => 'admin', + 3 => 'Me too', + 2 => 'you', + ); + + $this->assertEquals($expected, $users); + + $users = $userModel->getActiveUsersList(true); + + $expected = array( + UserModel::EVERYBODY_ID => 'Everybody', + 1 => 'admin', + 3 => 'Me too', + 2 => 'you', + ); + + $this->assertEquals($expected, $users); + } + + public function testIsAdmin() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'user1'))); + + $this->assertTrue($userModel->isAdmin(1)); + $this->assertFalse($userModel->isAdmin(2)); + } + + public function testPassword() + { + $password = 'test123'; + $hash = password_hash($password, PASSWORD_BCRYPT); + + $this->assertNotEmpty($hash); + $this->assertTrue(password_verify($password, $hash)); + } + + public function testPrepare() + { + $userModel = new UserModel($this->container); + + $input = array( + 'username' => 'user1', + 'password' => '1234', + 'confirmation' => '1234', + 'name' => 'me', + 'role' => Role::APP_ADMIN, + ); + + $userModel->prepare($input); + $this->assertArrayNotHasKey('confirmation', $input); + + $this->assertArrayHasKey('password', $input); + $this->assertNotEquals('1234', $input['password']); + $this->assertNotEmpty($input['password']); + + $input = array( + 'username' => 'user1', + 'password' => '1234', + 'current_password' => 'bla', + 'confirmation' => '1234', + 'name' => 'me', + 'is_ldap_user' => '1', + ); + + $userModel->prepare($input); + $this->assertArrayNotHasKey('confirmation', $input); + $this->assertArrayNotHasKey('current_password', $input); + + $this->assertArrayHasKey('password', $input); + $this->assertNotEquals('1234', $input['password']); + $this->assertNotEmpty($input['password']); + + $this->assertArrayHasKey('is_ldap_user', $input); + $this->assertEquals(1, $input['is_ldap_user']); + + $input = array( + 'id' => 2, + 'name' => 'me', + ); + + $userModel->prepare($input); + $this->assertEquals(array('id' => 2, 'name' => 'me'), $input); + + $input = array( + 'gitlab_id' => '1234', + ); + + $userModel->prepare($input); + $this->assertEquals(array('gitlab_id' => 1234), $input); + + $input = array( + 'gitlab_id' => '', + ); + + $userModel->prepare($input); + $this->assertEquals(array('gitlab_id' => null), $input); + + $input = array( + 'gitlab_id' => 'something', + ); + + $userModel->prepare($input); + $this->assertEquals(array('gitlab_id' => 0), $input); + + $input = array( + 'username' => 'something', + 'password' => '' + ); + + $userModel->prepare($input); + $this->assertEquals(array('username' => 'something'), $input); + } + + public function testCreate() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'user #1', 'password' => '123456', 'name' => 'User'))); + $this->assertEquals(3, $userModel->create(array('username' => 'user #2', 'is_ldap_user' => 1))); + $this->assertEquals(4, $userModel->create(array('username' => 'user #3', 'role' => Role::APP_MANAGER))); + $this->assertEquals(5, $userModel->create(array('username' => 'user #4', 'gitlab_id' => '', 'role' => Role::APP_ADMIN))); + $this->assertEquals(6, $userModel->create(array('username' => 'user #5', 'gitlab_id' => '1234'))); + $this->assertFalse($userModel->create(array('username' => 'user #1'))); + + $user = $userModel->getById(1); + $this->assertEquals('admin', $user['username']); + $this->assertEquals('', $user['name']); + $this->assertEquals(Role::APP_ADMIN, $user['role']); + $this->assertEquals(0, $user['is_ldap_user']); + + $user = $userModel->getById(2); + $this->assertEquals('user #1', $user['username']); + $this->assertEquals('User', $user['name']); + $this->assertEquals(Role::APP_USER, $user['role']); + $this->assertEquals(0, $user['is_ldap_user']); + + $user = $userModel->getById(3); + $this->assertEquals('user #2', $user['username']); + $this->assertEquals('', $user['name']); + $this->assertEquals(Role::APP_USER, $user['role']); + $this->assertEquals(1, $user['is_ldap_user']); + + $user = $userModel->getById(4); + $this->assertEquals('user #3', $user['username']); + $this->assertEquals(Role::APP_MANAGER, $user['role']); + + $user = $userModel->getById(5); + $this->assertEquals('user #4', $user['username']); + $this->assertEquals('', $user['gitlab_id']); + $this->assertEquals(Role::APP_ADMIN, $user['role']); + + $user = $userModel->getById(6); + $this->assertEquals('user #5', $user['username']); + $this->assertEquals('1234', $user['gitlab_id']); + $this->assertEquals(Role::APP_USER, $user['role']); + } + + public function testUpdate() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); + $this->assertEquals(3, $userModel->create(array('username' => 'plop', 'gitlab_id' => '123'))); + + $this->assertTrue($userModel->update(array('id' => 2, 'username' => 'biloute'))); + $this->assertTrue($userModel->update(array('id' => 3, 'gitlab_id' => ''))); + + $user = $userModel->getById(2); + $this->assertEquals('biloute', $user['username']); + $this->assertEquals('Toto', $user['name']); + $this->assertEquals(Role::APP_USER, $user['role']); + $this->assertEquals(0, $user['is_ldap_user']); + + $user = $userModel->getById(3); + $this->assertNotEmpty($user); + $this->assertEquals(null, $user['gitlab_id']); + } + + public function testRemove() + { + $userModel = new UserModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $subtaskModel = new SubtaskModel($this->container); + $commentModel = new CommentModel($this->container); + + $this->assertNotFalse($userModel->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); + $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); + $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1, 'owner_id' => 2))); + $this->assertEquals(1, $subtaskModel->create(array('title' => 'Subtask #1', 'user_id' => 2, 'task_id' => 1))); + $this->assertEquals(1, $commentModel->create(array('comment' => 'foobar', 'user_id' => 2, 'task_id' => 1))); + + $task = $taskFinderModel->getById(1); + $this->assertEquals(1, $task['id']); + $this->assertEquals(2, $task['owner_id']); + + $this->assertTrue($userModel->remove(1)); + $this->assertTrue($userModel->remove(2)); + $this->assertFalse($userModel->remove(2)); + $this->assertFalse($userModel->remove(55)); + + // Make sure that assigned tasks are unassigned after removing the user + $task = $taskFinderModel->getById(1); + $this->assertEquals(1, $task['id']); + $this->assertEquals(0, $task['owner_id']); + + // Make sure that assigned subtasks are unassigned after removing the user + $subtask = $subtaskModel->getById(1); + $this->assertEquals(1, $subtask['id']); + $this->assertEquals(0, $subtask['user_id']); + + // Make sure that comments are not related to the user anymore + $comment = $commentModel->getById(1); + $this->assertEquals(1, $comment['id']); + $this->assertEquals(0, $comment['user_id']); + + // Make sure that private projects are also removed + $user_id1 = $userModel->create(array('username' => 'toto1', 'password' => '123456', 'name' => 'Toto')); + $user_id2 = $userModel->create(array('username' => 'toto2', 'password' => '123456', 'name' => 'Toto')); + $this->assertNotFalse($user_id1); + $this->assertNotFalse($user_id2); + $this->assertEquals(2, $projectModel->create(array('name' => 'Private project #1', 'is_private' => 1), $user_id1, true)); + $this->assertEquals(3, $projectModel->create(array('name' => 'Private project #2', 'is_private' => 1), $user_id2, true)); + + $this->assertTrue($userModel->remove($user_id1)); + + $this->assertNotEmpty($projectModel->getById(1)); + $this->assertNotEmpty($projectModel->getById(3)); + + $this->assertEmpty($projectModel->getById(2)); + } + + public function testEnableDisablePublicAccess() + { + $userModel = new UserModel($this->container); + $this->assertNotFalse($userModel->create(array('username' => 'toto', 'password' => '123456'))); + + $user = $userModel->getById(2); + $this->assertNotEmpty($user); + $this->assertEquals('toto', $user['username']); + $this->assertEmpty($user['token']); + + $this->assertTrue($userModel->enablePublicAccess(2)); + + $user = $userModel->getById(2); + $this->assertNotEmpty($user); + $this->assertEquals('toto', $user['username']); + $this->assertNotEmpty($user['token']); + + $this->assertTrue($userModel->disablePublicAccess(2)); + + $user = $userModel->getById(2); + $this->assertNotEmpty($user); + $this->assertEquals('toto', $user['username']); + $this->assertEmpty($user['token']); + } + + public function testEnableDisable() + { + $userModel = new UserModel($this->container); + $this->assertEquals(2, $userModel->create(array('username' => 'toto'))); + + $this->assertTrue($userModel->isActive(2)); + $user = $userModel->getById(2); + $this->assertEquals(1, $user['is_active']); + + $this->assertTrue($userModel->disable(2)); + $user = $userModel->getById(2); + $this->assertEquals(0, $user['is_active']); + $this->assertFalse($userModel->isActive(2)); + + $this->assertTrue($userModel->enable(2)); + $user = $userModel->getById(2); + $this->assertEquals(1, $user['is_active']); + $this->assertTrue($userModel->isActive(2)); + } +} diff --git a/tests/units/Model/UserTest.php b/tests/units/Model/UserTest.php deleted file mode 100644 index 0be6172e..00000000 --- a/tests/units/Model/UserTest.php +++ /dev/null @@ -1,400 +0,0 @@ -<?php - -require_once __DIR__.'/../Base.php'; - -use Kanboard\Model\UserModel; -use Kanboard\Model\SubtaskModel; -use Kanboard\Model\CommentModel; -use Kanboard\Model\TaskCreationModel; -use Kanboard\Model\TaskFinderModel; -use Kanboard\Model\ProjectModel; -use Kanboard\Core\Security\Role; - -class UserTest extends Base -{ - public function testGetByEmail() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'password' => '123456', 'email' => 'user1@localhost'))); - $this->assertNotFalse($u->create(array('username' => 'user2', 'password' => '123456', 'email' => ''))); - - $this->assertNotEmpty($u->getByEmail('user1@localhost')); - $this->assertEmpty($u->getByEmail('')); - } - - public function testGetByExternalId() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'password' => '123456', 'gitlab_id' => '1234'))); - - $this->assertNotEmpty($u->getByExternalId('gitlab_id', '1234')); - $this->assertEmpty($u->getByExternalId('gitlab_id', '')); - - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user2', 'password' => '123456', 'github_id' => 'plop'))); - $this->assertNotFalse($u->create(array('username' => 'user3', 'password' => '123456', 'github_id' => ''))); - - $this->assertNotEmpty($u->getByExternalId('github_id', 'plop')); - $this->assertEmpty($u->getByExternalId('github_id', '')); - - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user4', 'password' => '123456', 'google_id' => '1234'))); - $this->assertNotFalse($u->create(array('username' => 'user5', 'password' => '123456', 'google_id' => ''))); - - $this->assertNotEmpty($u->getByExternalId('google_id', '1234')); - $this->assertEmpty($u->getByExternalId('google_id', '')); - } - - public function testGetByToken() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1', 'token' => 'random'))); - $this->assertNotFalse($u->create(array('username' => 'user2', 'token' => ''))); - - $this->assertNotEmpty($u->getByToken('random')); - $this->assertEmpty($u->getByToken('')); - } - - public function testGetByUsername() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1'))); - - $this->assertNotEmpty($u->getByUsername('user1')); - $this->assertEmpty($u->getByUsername('user2')); - $this->assertEmpty($u->getByUsername('')); - } - - public function testExists() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1'))); - - $this->assertTrue($u->exists(1)); - $this->assertTrue($u->exists(2)); - $this->assertFalse($u->exists(3)); - } - - public function testCount() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'user1'))); - $this->assertEquals(2, $u->count()); - } - - public function testGetAll() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'you'))); - $this->assertEquals(3, $u->create(array('username' => 'me', 'name' => 'Me'))); - - $users = $u->getAll(); - $this->assertCount(3, $users); - $this->assertEquals('admin', $users[0]['username']); - $this->assertEquals('me', $users[1]['username']); - $this->assertEquals('you', $users[2]['username']); - } - - public function testGetActiveUsersList() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'you'))); - $this->assertEquals(3, $u->create(array('username' => 'me', 'name' => 'Me too'))); - $this->assertEquals(4, $u->create(array('username' => 'foobar', 'is_active' => 0))); - - $users = $u->getActiveUsersList(); - - $expected = array( - 1 => 'admin', - 3 => 'Me too', - 2 => 'you', - ); - - $this->assertEquals($expected, $users); - - $users = $u->getActiveUsersList(true); - - $expected = array( - UserModel::EVERYBODY_ID => 'Everybody', - 1 => 'admin', - 3 => 'Me too', - 2 => 'you', - ); - - $this->assertEquals($expected, $users); - } - - public function testGetFullname() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'user1'))); - $this->assertEquals(3, $u->create(array('username' => 'user2', 'name' => 'User #2'))); - - $user1 = $u->getById(2); - $user2 = $u->getById(3); - - $this->assertNotEmpty($user1); - $this->assertNotEmpty($user2); - - $this->assertEquals('user1', $u->getFullname($user1)); - $this->assertEquals('User #2', $u->getFullname($user2)); - } - - public function testIsAdmin() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'user1'))); - - $this->assertTrue($u->isAdmin(1)); - $this->assertFalse($u->isAdmin(2)); - } - - public function testPassword() - { - $password = 'test123'; - $hash = password_hash($password, PASSWORD_BCRYPT); - - $this->assertNotEmpty($hash); - $this->assertTrue(password_verify($password, $hash)); - } - - public function testPrepare() - { - $u = new UserModel($this->container); - - $input = array( - 'username' => 'user1', - 'password' => '1234', - 'confirmation' => '1234', - 'name' => 'me', - 'role' => Role::APP_ADMIN, - ); - - $u->prepare($input); - $this->assertArrayNotHasKey('confirmation', $input); - - $this->assertArrayHasKey('password', $input); - $this->assertNotEquals('1234', $input['password']); - $this->assertNotEmpty($input['password']); - - $input = array( - 'username' => 'user1', - 'password' => '1234', - 'current_password' => 'bla', - 'confirmation' => '1234', - 'name' => 'me', - 'is_ldap_user' => '1', - ); - - $u->prepare($input); - $this->assertArrayNotHasKey('confirmation', $input); - $this->assertArrayNotHasKey('current_password', $input); - - $this->assertArrayHasKey('password', $input); - $this->assertNotEquals('1234', $input['password']); - $this->assertNotEmpty($input['password']); - - $this->assertArrayHasKey('is_ldap_user', $input); - $this->assertEquals(1, $input['is_ldap_user']); - - $input = array( - 'id' => 2, - 'name' => 'me', - ); - - $u->prepare($input); - $this->assertEquals(array('id' => 2, 'name' => 'me'), $input); - - $input = array( - 'gitlab_id' => '1234', - ); - - $u->prepare($input); - $this->assertEquals(array('gitlab_id' => 1234), $input); - - $input = array( - 'gitlab_id' => '', - ); - - $u->prepare($input); - $this->assertEquals(array('gitlab_id' => null), $input); - - $input = array( - 'gitlab_id' => 'something', - ); - - $u->prepare($input); - $this->assertEquals(array('gitlab_id' => 0), $input); - - $input = array( - 'username' => 'something', - 'password' => '' - ); - - $u->prepare($input); - $this->assertEquals(array('username' => 'something'), $input); - } - - public function testCreate() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'user #1', 'password' => '123456', 'name' => 'User'))); - $this->assertEquals(3, $u->create(array('username' => 'user #2', 'is_ldap_user' => 1))); - $this->assertEquals(4, $u->create(array('username' => 'user #3', 'role' => Role::APP_MANAGER))); - $this->assertEquals(5, $u->create(array('username' => 'user #4', 'gitlab_id' => '', 'role' => Role::APP_ADMIN))); - $this->assertEquals(6, $u->create(array('username' => 'user #5', 'gitlab_id' => '1234'))); - $this->assertFalse($u->create(array('username' => 'user #1'))); - - $user = $u->getById(1); - $this->assertEquals('admin', $user['username']); - $this->assertEquals('', $user['name']); - $this->assertEquals(Role::APP_ADMIN, $user['role']); - $this->assertEquals(0, $user['is_ldap_user']); - - $user = $u->getById(2); - $this->assertEquals('user #1', $user['username']); - $this->assertEquals('User', $user['name']); - $this->assertEquals(Role::APP_USER, $user['role']); - $this->assertEquals(0, $user['is_ldap_user']); - - $user = $u->getById(3); - $this->assertEquals('user #2', $user['username']); - $this->assertEquals('', $user['name']); - $this->assertEquals(Role::APP_USER, $user['role']); - $this->assertEquals(1, $user['is_ldap_user']); - - $user = $u->getById(4); - $this->assertEquals('user #3', $user['username']); - $this->assertEquals(Role::APP_MANAGER, $user['role']); - - $user = $u->getById(5); - $this->assertEquals('user #4', $user['username']); - $this->assertEquals('', $user['gitlab_id']); - $this->assertEquals(Role::APP_ADMIN, $user['role']); - - $user = $u->getById(6); - $this->assertEquals('user #5', $user['username']); - $this->assertEquals('1234', $user['gitlab_id']); - $this->assertEquals(Role::APP_USER, $user['role']); - } - - public function testUpdate() - { - $u = new UserModel($this->container); - $this->assertEquals(2, $u->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); - $this->assertEquals(3, $u->create(array('username' => 'plop', 'gitlab_id' => '123'))); - - $this->assertTrue($u->update(array('id' => 2, 'username' => 'biloute'))); - $this->assertTrue($u->update(array('id' => 3, 'gitlab_id' => ''))); - - $user = $u->getById(2); - $this->assertEquals('biloute', $user['username']); - $this->assertEquals('Toto', $user['name']); - $this->assertEquals(Role::APP_USER, $user['role']); - $this->assertEquals(0, $user['is_ldap_user']); - - $user = $u->getById(3); - $this->assertNotEmpty($user); - $this->assertEquals(null, $user['gitlab_id']); - } - - public function testRemove() - { - $u = new UserModel($this->container); - $tc = new TaskCreationModel($this->container); - $tf = new TaskFinderModel($this->container); - $p = new ProjectModel($this->container); - $s = new SubtaskModel($this->container); - $c = new CommentModel($this->container); - - $this->assertNotFalse($u->create(array('username' => 'toto', 'password' => '123456', 'name' => 'Toto'))); - $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); - $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'owner_id' => 2))); - $this->assertEquals(1, $s->create(array('title' => 'Subtask #1', 'user_id' => 2, 'task_id' => 1))); - $this->assertEquals(1, $c->create(array('comment' => 'foobar', 'user_id' => 2, 'task_id' => 1))); - - $task = $tf->getById(1); - $this->assertEquals(1, $task['id']); - $this->assertEquals(2, $task['owner_id']); - - $this->assertTrue($u->remove(1)); - $this->assertTrue($u->remove(2)); - $this->assertFalse($u->remove(2)); - $this->assertFalse($u->remove(55)); - - // Make sure that assigned tasks are unassigned after removing the user - $task = $tf->getById(1); - $this->assertEquals(1, $task['id']); - $this->assertEquals(0, $task['owner_id']); - - // Make sure that assigned subtasks are unassigned after removing the user - $subtask = $s->getById(1); - $this->assertEquals(1, $subtask['id']); - $this->assertEquals(0, $subtask['user_id']); - - // Make sure that comments are not related to the user anymore - $comment = $c->getById(1); - $this->assertEquals(1, $comment['id']); - $this->assertEquals(0, $comment['user_id']); - - // Make sure that private projects are also removed - $user_id1 = $u->create(array('username' => 'toto1', 'password' => '123456', 'name' => 'Toto')); - $user_id2 = $u->create(array('username' => 'toto2', 'password' => '123456', 'name' => 'Toto')); - $this->assertNotFalse($user_id1); - $this->assertNotFalse($user_id2); - $this->assertEquals(2, $p->create(array('name' => 'Private project #1', 'is_private' => 1), $user_id1, true)); - $this->assertEquals(3, $p->create(array('name' => 'Private project #2', 'is_private' => 1), $user_id2, true)); - - $this->assertTrue($u->remove($user_id1)); - - $this->assertNotEmpty($p->getById(1)); - $this->assertNotEmpty($p->getById(3)); - - $this->assertEmpty($p->getById(2)); - } - - public function testEnableDisablePublicAccess() - { - $u = new UserModel($this->container); - $this->assertNotFalse($u->create(array('username' => 'toto', 'password' => '123456'))); - - $user = $u->getById(2); - $this->assertNotEmpty($user); - $this->assertEquals('toto', $user['username']); - $this->assertEmpty($user['token']); - - $this->assertTrue($u->enablePublicAccess(2)); - - $user = $u->getById(2); - $this->assertNotEmpty($user); - $this->assertEquals('toto', $user['username']); - $this->assertNotEmpty($user['token']); - - $this->assertTrue($u->disablePublicAccess(2)); - - $user = $u->getById(2); - $this->assertNotEmpty($user); - $this->assertEquals('toto', $user['username']); - $this->assertEmpty($user['token']); - } - - public function testEnableDisable() - { - $userModel = new UserModel($this->container); - $this->assertEquals(2, $userModel->create(array('username' => 'toto'))); - - $this->assertTrue($userModel->isActive(2)); - $user = $userModel->getById(2); - $this->assertEquals(1, $user['is_active']); - - $this->assertTrue($userModel->disable(2)); - $user = $userModel->getById(2); - $this->assertEquals(0, $user['is_active']); - $this->assertFalse($userModel->isActive(2)); - - $this->assertTrue($userModel->enable(2)); - $user = $userModel->getById(2); - $this->assertEquals(1, $user['is_active']); - $this->assertTrue($userModel->isActive(2)); - } -} |