From c58478c0abfb418048d2ee9c6297a30793f10f80 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Fri, 15 Jan 2016 21:03:19 -0500 Subject: Move analytic logic to separate classes --- app/Analytic/EstimatedTimeComparisonAnalytic.php | 50 +++++++++++++++++++++ app/Analytic/TaskDistributionAnalytic.php | 48 ++++++++++++++++++++ app/Analytic/UserDistributionAnalytic.php | 56 ++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 app/Analytic/EstimatedTimeComparisonAnalytic.php create mode 100644 app/Analytic/TaskDistributionAnalytic.php create mode 100644 app/Analytic/UserDistributionAnalytic.php (limited to 'app/Analytic') diff --git a/app/Analytic/EstimatedTimeComparisonAnalytic.php b/app/Analytic/EstimatedTimeComparisonAnalytic.php new file mode 100644 index 00000000..490bcd50 --- /dev/null +++ b/app/Analytic/EstimatedTimeComparisonAnalytic.php @@ -0,0 +1,50 @@ +db->table(Task::TABLE) + ->columns('SUM(time_estimated) AS time_estimated', 'SUM(time_spent) AS time_spent', 'is_active') + ->eq('project_id', $project_id) + ->groupBy('is_active') + ->findAll(); + + $metrics = array( + 'open' => array( + 'time_spent' => 0, + 'time_estimated' => 0, + ), + 'closed' => array( + 'time_spent' => 0, + 'time_estimated' => 0, + ), + ); + + foreach ($rows as $row) { + $key = $row['is_active'] == Task::STATUS_OPEN ? 'open' : 'closed'; + $metrics[$key]['time_spent'] = $row['time_spent']; + $metrics[$key]['time_estimated'] = $row['time_estimated']; + } + + return $metrics; + } +} diff --git a/app/Analytic/TaskDistributionAnalytic.php b/app/Analytic/TaskDistributionAnalytic.php new file mode 100644 index 00000000..614c5f72 --- /dev/null +++ b/app/Analytic/TaskDistributionAnalytic.php @@ -0,0 +1,48 @@ +board->getColumns($project_id); + + foreach ($columns as $column) { + $nb_tasks = $this->taskFinder->countByColumnId($project_id, $column['id']); + $total += $nb_tasks; + + $metrics[] = array( + 'column_title' => $column['title'], + 'nb_tasks' => $nb_tasks, + ); + } + + if ($total === 0) { + return array(); + } + + foreach ($metrics as &$metric) { + $metric['percentage'] = round(($metric['nb_tasks'] * 100) / $total, 2); + } + + return $metrics; + } +} diff --git a/app/Analytic/UserDistributionAnalytic.php b/app/Analytic/UserDistributionAnalytic.php new file mode 100644 index 00000000..e1815f9c --- /dev/null +++ b/app/Analytic/UserDistributionAnalytic.php @@ -0,0 +1,56 @@ +taskFinder->getAll($project_id); + $users = $this->projectUserRole->getAssignableUsersList($project_id); + + foreach ($tasks as $task) { + $user = isset($users[$task['owner_id']]) ? $users[$task['owner_id']] : $users[0]; + $total++; + + if (! isset($metrics[$user])) { + $metrics[$user] = array( + 'nb_tasks' => 0, + 'percentage' => 0, + 'user' => $user, + ); + } + + $metrics[$user]['nb_tasks']++; + } + + if ($total === 0) { + return array(); + } + + foreach ($metrics as &$metric) { + $metric['percentage'] = round(($metric['nb_tasks'] * 100) / $total, 2); + } + + ksort($metrics); + + return array_values($metrics); + } +} -- cgit v1.2.3 From 73ff5ec89b711791b3993f9158dc9554a623602c Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 16 Jan 2016 17:01:56 -0500 Subject: Remove ProjectAnalytic class --- app/Analytic/AverageLeadCycleTimeAnalytic.php | 112 +++++++++++++++ app/Analytic/AverageTimeSpentColumnAnalytic.php | 151 +++++++++++++++++++++ app/Controller/Analytic.php | 6 +- app/Core/Base.php | 2 + app/Locale/fr_FR/translations.php | 4 +- app/Model/ProjectAnalytic.php | 106 --------------- app/Model/ProjectDailyStats.php | 2 +- app/ServiceProvider/ClassProvider.php | 3 +- .../Analytic/AverageLeadCycleTimeAnalyticTest.php | 73 ++++++++++ .../AverageTimeSpentColumnAnalyticTest.php | 116 ++++++++++++++++ 10 files changed, 461 insertions(+), 114 deletions(-) create mode 100644 app/Analytic/AverageLeadCycleTimeAnalytic.php create mode 100644 app/Analytic/AverageTimeSpentColumnAnalytic.php delete mode 100644 app/Model/ProjectAnalytic.php create mode 100644 tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php create mode 100644 tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php (limited to 'app/Analytic') diff --git a/app/Analytic/AverageLeadCycleTimeAnalytic.php b/app/Analytic/AverageLeadCycleTimeAnalytic.php new file mode 100644 index 00000000..96c803cc --- /dev/null +++ b/app/Analytic/AverageLeadCycleTimeAnalytic.php @@ -0,0 +1,112 @@ + 0, + 'total_lead_time' => 0, + 'total_cycle_time' => 0, + 'avg_lead_time' => 0, + 'avg_cycle_time' => 0, + ); + + foreach ($this->getTasks($project_id) as &$task) { + $stats['count']++; + $stats['total_lead_time'] += $this->calculateLeadTime($task); + $stats['total_cycle_time'] += $this->calculateCycleTime($task); + } + + $stats['avg_lead_time'] = $this->calculateAverage($stats, 'total_lead_time'); + $stats['avg_cycle_time'] = $this->calculateAverage($stats, 'total_cycle_time'); + + return $stats; + } + + /** + * Calculate average + * + * @access private + * @param array &$stats + * @param string $field + * @return float + */ + private function calculateAverage(array &$stats, $field) + { + if ($stats['count'] > 0) { + return (int) ($stats[$field] / $stats['count']); + } + + return 0; + } + + /** + * Calculate lead time + * + * @access private + * @param array &$task + * @return integer + */ + private function calculateLeadTime(array &$task) + { + $end = $task['date_completed'] ?: time(); + $start = $task['date_creation']; + + return $end - $start; + } + + /** + * Calculate cycle time + * + * @access private + * @param array &$task + * @return integer + */ + private function calculateCycleTime(array &$task) + { + if (empty($task['date_started'])) { + return 0; + } + + $end = $task['date_completed'] ?: time(); + $start = $task['date_started']; + + return $end - $start; + } + + /** + * Get the 1000 last created tasks + * + * @access private + * @return array + */ + private function getTasks($project_id) + { + return $this->db + ->table(Task::TABLE) + ->columns('date_completed', 'date_creation', 'date_started') + ->eq('project_id', $project_id) + ->desc('id') + ->limit(1000) + ->findAll(); + } +} diff --git a/app/Analytic/AverageTimeSpentColumnAnalytic.php b/app/Analytic/AverageTimeSpentColumnAnalytic.php new file mode 100644 index 00000000..820db801 --- /dev/null +++ b/app/Analytic/AverageTimeSpentColumnAnalytic.php @@ -0,0 +1,151 @@ +initialize($project_id); + + $this->processTasks($stats, $project_id); + $this->calculateAverage($stats); + + return $stats; + } + + /** + * Initialize default values for each column + * + * @access private + * @param integer $project_id + * @return array + */ + private function initialize($project_id) + { + $stats = array(); + $columns = $this->board->getColumnsList($project_id); + + foreach ($columns as $column_id => $column_title) { + $stats[$column_id] = array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => $column_title, + ); + } + + return $stats; + } + + /** + * Calculate time spent for each tasks for each columns + * + * @access private + * @param array $stats + * @param integer $project_id + */ + private function processTasks(array &$stats, $project_id) + { + foreach ($this->getTasks($project_id) as &$task) { + foreach ($this->getTaskTimeByColumns($task) as $column_id => $time_spent) { + if (isset($stats[$column_id])) { + $stats[$column_id]['count']++; + $stats[$column_id]['time_spent'] += $time_spent; + } + } + } + } + + /** + * Calculate averages + * + * @access private + * @param array $stats + */ + private function calculateAverage(array &$stats) + { + foreach ($stats as &$column) { + $this->calculateColumnAverage($column); + } + } + + /** + * Calculate column average + * + * @access private + * @param array $column + */ + private function calculateColumnAverage(array &$column) + { + if ($column['count'] > 0) { + $column['average'] = (int) ($column['time_spent'] / $column['count']); + } + } + + /** + * Get time spent for each column for a given task + * + * @access private + * @param array $task + * @return array + */ + private function getTaskTimeByColumns(array &$task) + { + $columns = $this->transition->getTimeSpentByTask($task['id']); + + if (! isset($columns[$task['column_id']])) { + $columns[$task['column_id']] = 0; + } + + $columns[$task['column_id']] += $this->getTaskTimeSpentInCurrentColumn($task); + + return $columns; + } + + /** + * Calculate time spent of a task in the current column + * + * @access private + * @param array $task + */ + private function getTaskTimeSpentInCurrentColumn(array &$task) + { + $end = $task['date_completed'] ?: time(); + return $end - $task['date_moved']; + } + + /** + * Fetch the last 1000 tasks + * + * @access private + * @param integer $project_id + * @return array + */ + private function getTasks($project_id) + { + return $this->db + ->table(Task::TABLE) + ->columns('id', 'date_completed', 'date_moved', 'column_id') + ->eq('project_id', $project_id) + ->desc('id') + ->limit(1000) + ->findAll(); + } +} diff --git a/app/Controller/Analytic.php b/app/Controller/Analytic.php index f9ea511b..26386029 100644 --- a/app/Controller/Analytic.php +++ b/app/Controller/Analytic.php @@ -37,8 +37,6 @@ class Analytic extends Base $project = $this->getProject(); $values = $this->request->getValues(); - $this->projectDailyStats->updateTotals($project['id'], date('Y-m-d')); - $from = $this->request->getStringParam('from', date('Y-m-d', strtotime('-1week'))); $to = $this->request->getStringParam('to', date('Y-m-d')); @@ -53,7 +51,7 @@ class Analytic extends Base 'to' => $to, ), 'project' => $project, - 'average' => $this->projectAnalytic->getAverageLeadAndCycleTime($project['id']), + 'average' => $this->averageLeadCycleTimeAnalytic->build($project['id']), 'metrics' => $this->projectDailyStats->getRawMetrics($project['id'], $from, $to), 'date_format' => $this->config->get('application_date_format'), 'date_formats' => $this->dateParser->getAvailableFormats(), @@ -72,7 +70,7 @@ class Analytic extends Base $this->response->html($this->layout('analytic/avg_time_columns', array( 'project' => $project, - 'metrics' => $this->projectAnalytic->getAverageTimeSpentByColumn($project['id']), + 'metrics' => $this->averageTimeSpentColumnAnalytic->build($project['id']), 'title' => t('Average time spent into each column for "%s"', $project['name']), ))); } diff --git a/app/Core/Base.php b/app/Core/Base.php index ea8c0abf..2821e5ae 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -13,6 +13,8 @@ use Pimple\Container; * @property \Kanboard\Analytic\TaskDistributionAnalytic $taskDistributionAnalytic * @property \Kanboard\Analytic\UserDistributionAnalytic $userDistributionAnalytic * @property \Kanboard\Analytic\EstimatedTimeComparisonAnalytic $estimatedTimeComparisonAnalytic + * @property \Kanboard\Analytic\AverageLeadCycleTimeAnalytic $averageLeadCycleTimeAnalytic + * @property \Kanboard\Analytic\AverageTimeSpentColumnAnalytic $averageTimeSpentColumnAnalytic * @property \Kanboard\Core\Action\ActionManager $actionManager * @property \Kanboard\Core\Cache\MemoryCache $memoryCache * @property \Kanboard\Core\Event\EventManager $eventManager diff --git a/app/Locale/fr_FR/translations.php b/app/Locale/fr_FR/translations.php index 016c16d7..2724c300 100644 --- a/app/Locale/fr_FR/translations.php +++ b/app/Locale/fr_FR/translations.php @@ -1074,8 +1074,8 @@ return array( 'You were mentioned in a comment on the task #%d' => 'Vous avez été mentionné dans un commentaire de la tâche n°%d', 'Mentioned' => 'Mentionné', 'Compare Estimated Time vs Actual Time' => 'Comparer le temps estimé et le temps actuel', - 'Estimated hours: ' => 'Heures estimées :', - 'Actual hours: ' => 'Heures actuelles :', + 'Estimated hours: ' => 'Heures estimées : ', + 'Actual hours: ' => 'Heures actuelles : ', 'Hours Spent' => 'Heures passées', 'Hours Estimated' => 'Heures estimées', 'Estimated Time' => 'Temps estimé', diff --git a/app/Model/ProjectAnalytic.php b/app/Model/ProjectAnalytic.php deleted file mode 100644 index 5753a5ae..00000000 --- a/app/Model/ProjectAnalytic.php +++ /dev/null @@ -1,106 +0,0 @@ - 0, - 'total_lead_time' => 0, - 'total_cycle_time' => 0, - 'avg_lead_time' => 0, - 'avg_cycle_time' => 0, - ); - - $tasks = $this->db - ->table(Task::TABLE) - ->columns('date_completed', 'date_creation', 'date_started') - ->eq('project_id', $project_id) - ->desc('id') - ->limit(1000) - ->findAll(); - - foreach ($tasks as &$task) { - $stats['count']++; - $stats['total_lead_time'] += ($task['date_completed'] ?: time()) - $task['date_creation']; - $stats['total_cycle_time'] += empty($task['date_started']) ? 0 : ($task['date_completed'] ?: time()) - $task['date_started']; - } - - $stats['avg_lead_time'] = $stats['count'] > 0 ? (int) ($stats['total_lead_time'] / $stats['count']) : 0; - $stats['avg_cycle_time'] = $stats['count'] > 0 ? (int) ($stats['total_cycle_time'] / $stats['count']) : 0; - - return $stats; - } - - /** - * Get the average time spent into each column - * - * @access public - * @param integer $project_id - * @return array - */ - public function getAverageTimeSpentByColumn($project_id) - { - $stats = array(); - $columns = $this->board->getColumnsList($project_id); - - // Get the time spent of the last move for each tasks - $tasks = $this->db - ->table(Task::TABLE) - ->columns('id', 'date_completed', 'date_moved', 'column_id') - ->eq('project_id', $project_id) - ->desc('id') - ->limit(1000) - ->findAll(); - - // Init values - foreach ($columns as $column_id => $column_title) { - $stats[$column_id] = array( - 'count' => 0, - 'time_spent' => 0, - 'average' => 0, - 'title' => $column_title, - ); - } - - // Get time spent foreach task/column and take into account the last move - foreach ($tasks as &$task) { - $sums = $this->transition->getTimeSpentByTask($task['id']); - - if (! isset($sums[$task['column_id']])) { - $sums[$task['column_id']] = 0; - } - - $sums[$task['column_id']] += ($task['date_completed'] ?: time()) - $task['date_moved']; - - foreach ($sums as $column_id => $time_spent) { - if (isset($stats[$column_id])) { - $stats[$column_id]['count']++; - $stats[$column_id]['time_spent'] += $time_spent; - } - } - } - - // Calculate average for each column - foreach ($columns as $column_id => $column_title) { - $stats[$column_id]['average'] = $stats[$column_id]['count'] > 0 ? (int) ($stats[$column_id]['time_spent'] / $stats[$column_id]['count']) : 0; - } - - return $stats; - } -} diff --git a/app/Model/ProjectDailyStats.php b/app/Model/ProjectDailyStats.php index e79af372..12fecbe6 100644 --- a/app/Model/ProjectDailyStats.php +++ b/app/Model/ProjectDailyStats.php @@ -29,7 +29,7 @@ class ProjectDailyStats extends Base { $this->db->startTransaction(); - $lead_cycle_time = $this->projectAnalytic->getAverageLeadAndCycleTime($project_id); + $lead_cycle_time = $this->averageLeadCycleTimeAnalytic->build($project_id); $exists = $this->db->table(ProjectDailyStats::TABLE) ->eq('day', $date) diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index ed7f4c08..31cbfa8d 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -18,6 +18,8 @@ class ClassProvider implements ServiceProviderInterface 'TaskDistributionAnalytic', 'UserDistributionAnalytic', 'EstimatedTimeComparisonAnalytic', + 'AverageLeadCycleTimeAnalytic', + 'AverageTimeSpentColumnAnalytic', ), 'Model' => array( 'Action', @@ -39,7 +41,6 @@ class ClassProvider implements ServiceProviderInterface 'PasswordReset', 'Project', 'ProjectActivity', - 'ProjectAnalytic', 'ProjectDuplication', 'ProjectDailyColumnStats', 'ProjectDailyStats', diff --git a/tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php b/tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php new file mode 100644 index 00000000..9c445dca --- /dev/null +++ b/tests/units/Analytic/AverageLeadCycleTimeAnalyticTest.php @@ -0,0 +1,73 @@ +container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageLeadCycleTimeAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(3, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(4, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + // LT=3600 CT=1800 + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600, 'date_started' => $now + 1800)); + + // LT=1800 CT=900 + $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800, 'date_started' => $now + 900)); + + // LT=3600 CT=0 + $this->container['db']->table(Task::TABLE)->eq('id', 3)->update(array('date_completed' => $now + 3600)); + + // LT=2*3600 CT=0 + $this->container['db']->table(Task::TABLE)->eq('id', 4)->update(array('date_completed' => $now + 2 * 3600)); + + $stats = $averageLeadCycleTimeAnalytic->build(1); + $expected = array( + 'count' => 4, + 'total_lead_time' => 3600 + 1800 + 3600 + 2*3600, + 'total_cycle_time' => 1800 + 900, + 'avg_lead_time' => (3600 + 1800 + 3600 + 2*3600) / 4, + 'avg_cycle_time' => (1800 + 900) / 4, + ); + + $this->assertEquals($expected, $stats); + } + + public function testBuildWithNoTasks() + { + $taskCreationModel = new TaskCreation($this->container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageLeadCycleTimeAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + $this->assertEquals(2, $projectModel->create(array('name' => 'test1'))); + + $stats = $averageLeadCycleTimeAnalytic->build(1); + + $expected = array( + 'count' => 0, + 'total_lead_time' => 0, + 'total_cycle_time' => 0, + 'avg_lead_time' => 0, + 'avg_cycle_time' => 0, + ); + + $this->assertEquals($expected, $stats); + } +} diff --git a/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php new file mode 100644 index 00000000..75cb181d --- /dev/null +++ b/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php @@ -0,0 +1,116 @@ +container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600)); + $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800)); + + $stats = $averageLeadCycleTimeAnalytic->build(1); + $expected = array( + 1 => array( + 'count' => 2, + 'time_spent' => 3600+1800, + 'average' => (int) ((3600+1800)/2), + 'title' => 'Backlog', + ), + 2 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Ready', + ), + 3 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Work in progress', + ), + 4 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Done', + ) + ); + + $this->assertEquals($expected, $stats); + } + + public function testAverageWithTransitions() + { + $transitionModel = new Transition($this->container); + $taskFinderModel = new TaskFinder($this->container); + $taskCreationModel = new TaskCreation($this->container); + $projectModel = new Project($this->container); + $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container); + $now = time(); + + $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); + + $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test'))); + + $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600)); + $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800)); + + foreach (array(1, 2) as $task_id) { + $task = $taskFinderModel->getById($task_id); + $task['task_id'] = $task['id']; + $task['date_moved'] = $now - 900; + $task['src_column_id'] = 3; + $task['dst_column_id'] = 1; + $this->assertTrue($transitionModel->save(1, $task)); + } + + $stats = $averageLeadCycleTimeAnalytic->build(1); + $expected = array( + 1 => array( + 'count' => 2, + 'time_spent' => 3600+1800, + 'average' => (int) ((3600+1800)/2), + 'title' => 'Backlog', + ), + 2 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Ready', + ), + 3 => array( + 'count' => 2, + 'time_spent' => 1800, + 'average' => 900, + 'title' => 'Work in progress', + ), + 4 => array( + 'count' => 0, + 'time_spent' => 0, + 'average' => 0, + 'title' => 'Done', + ) + ); + + $this->assertEquals($expected, $stats); + } +} -- cgit v1.2.3 From 2e4c2b6e0571690e8b8d0488dbb53e3373a86ff5 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 16 Jan 2016 19:19:05 -0500 Subject: Improve class ProjectDailyColumnStats --- ChangeLog | 3 + app/Analytic/AverageLeadCycleTimeAnalytic.php | 4 +- app/Controller/Analytic.php | 79 +++--- app/Model/ProjectDailyColumnStats.php | 280 ++++++++++++--------- app/Subscriber/ProjectDailySummarySubscriber.php | 4 +- tests/units/Model/ProjectDailyColumnStatsTest.php | 290 ++++++++++++++++------ 6 files changed, 427 insertions(+), 233 deletions(-) (limited to 'app/Analytic') diff --git a/ChangeLog b/ChangeLog index 1c200c2f..f11edbac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,9 @@ Improvements: * Add dropdown menu for subtasks, categories, swimlanes, columns, custom filters, task links and groups * Add new template hooks * Application settings are not cached anymore in the session +* Move validators to a separate namespace +* Improve and write unit tests for reports +* Reduce the number of SQL queries for project daily column stats Bug fixes: diff --git a/app/Analytic/AverageLeadCycleTimeAnalytic.php b/app/Analytic/AverageLeadCycleTimeAnalytic.php index 96c803cc..fd85f864 100644 --- a/app/Analytic/AverageLeadCycleTimeAnalytic.php +++ b/app/Analytic/AverageLeadCycleTimeAnalytic.php @@ -30,7 +30,9 @@ class AverageLeadCycleTimeAnalytic extends Base 'avg_cycle_time' => 0, ); - foreach ($this->getTasks($project_id) as &$task) { + $tasks = $this->getTasks($project_id); + + foreach ($tasks as &$task) { $stats['count']++; $stats['total_lead_time'] += $this->calculateLeadTime($task); $stats['total_cycle_time'] += $this->calculateCycleTime($task); diff --git a/app/Controller/Analytic.php b/app/Controller/Analytic.php index 26386029..d203fb8e 100644 --- a/app/Controller/Analytic.php +++ b/app/Controller/Analytic.php @@ -35,15 +35,7 @@ class Analytic extends Base public function leadAndCycleTime() { $project = $this->getProject(); - $values = $this->request->getValues(); - - $from = $this->request->getStringParam('from', date('Y-m-d', strtotime('-1week'))); - $to = $this->request->getStringParam('to', date('Y-m-d')); - - if (! empty($values)) { - $from = $values['from']; - $to = $values['to']; - } + list($from, $to) = $this->getDates(); $this->response->html($this->layout('analytic/lead_cycle_time', array( 'values' => array( @@ -59,6 +51,32 @@ class Analytic extends Base ))); } + /** + * Show comparison between actual and estimated hours chart + * + * @access public + */ + public function compareHours() + { + $project = $this->getProject(); + $params = $this->getProjectFilters('analytic', 'compareHours'); + $query = $this->taskFilter->create()->filterByProject($params['project']['id'])->getQuery(); + + $paginator = $this->paginator + ->setUrl('analytic', 'compareHours', array('project_id' => $project['id'])) + ->setMax(30) + ->setOrder(TaskModel::TABLE.'.id') + ->setQuery($query) + ->calculate(); + + $this->response->html($this->layout('analytic/compare_hours', array( + 'project' => $project, + 'paginator' => $paginator, + 'metrics' => $this->estimatedTimeComparisonAnalytic->build($project['id']), + 'title' => t('Compare hours for "%s"', $project['name']), + ))); + } + /** * Show average time spent by column * @@ -138,17 +156,7 @@ class Analytic extends Base private function commonAggregateMetrics($template, $column, $title) { $project = $this->getProject(); - $values = $this->request->getValues(); - - $this->projectDailyColumnStats->updateTotals($project['id'], date('Y-m-d')); - - $from = $this->request->getStringParam('from', date('Y-m-d', strtotime('-1week'))); - $to = $this->request->getStringParam('to', date('Y-m-d')); - - if (! empty($values)) { - $from = $values['from']; - $to = $values['to']; - } + list($from, $to) = $this->getDates(); $display_graph = $this->projectDailyColumnStats->countDays($project['id'], $from, $to) >= 2; @@ -166,29 +174,18 @@ class Analytic extends Base ))); } - /** - * Show comparison between actual and estimated hours chart - * - * @access public - */ - public function compareHours() + private function getDates() { - $project = $this->getProject(); - $params = $this->getProjectFilters('analytic', 'compareHours'); - $query = $this->taskFilter->create()->filterByProject($params['project']['id'])->getQuery(); + $values = $this->request->getValues(); - $paginator = $this->paginator - ->setUrl('analytic', 'compareHours', array('project_id' => $project['id'])) - ->setMax(30) - ->setOrder(TaskModel::TABLE.'.id') - ->setQuery($query) - ->calculate(); + $from = $this->request->getStringParam('from', date('Y-m-d', strtotime('-1week'))); + $to = $this->request->getStringParam('to', date('Y-m-d')); - $this->response->html($this->layout('analytic/compare_hours', array( - 'project' => $project, - 'paginator' => $paginator, - 'metrics' => $this->estimatedTimeComparisonAnalytic->build($project['id']), - 'title' => t('Compare hours for "%s"', $project['name']), - ))); + if (! empty($values)) { + $from = $values['from']; + $to = $values['to']; + } + + return array($from, $to); } } diff --git a/app/Model/ProjectDailyColumnStats.php b/app/Model/ProjectDailyColumnStats.php index 7c89283d..c4a3f28e 100644 --- a/app/Model/ProjectDailyColumnStats.php +++ b/app/Model/ProjectDailyColumnStats.php @@ -18,7 +18,7 @@ class ProjectDailyColumnStats extends Base const TABLE = 'project_daily_column_stats'; /** - * Update daily totals for the project and foreach column + * Update daily totals for the project and for each column * * "total" is the number open of tasks in the column * "score" is the sum of tasks score in the column @@ -30,48 +30,17 @@ class ProjectDailyColumnStats extends Base */ public function updateTotals($project_id, $date) { - $status = $this->config->get('cfd_include_closed_tasks') == 1 ? array(Task::STATUS_OPEN, Task::STATUS_CLOSED) : array(Task::STATUS_OPEN); - $this->db->startTransaction(); - - $column_ids = $this->db->table(Board::TABLE)->eq('project_id', $project_id)->findAllByColumn('id'); - - foreach ($column_ids as $column_id) { - - $exists = $this->db->table(ProjectDailyColumnStats::TABLE) - ->eq('project_id', $project_id) - ->eq('column_id', $column_id) - ->eq('day', $date) - ->exists(); - - $score = $this->db->table(Task::TABLE) - ->eq('project_id', $project_id) - ->eq('column_id', $column_id) - ->eq('is_active', Task::STATUS_OPEN) - ->sum('score'); - - $total = $this->db->table(Task::TABLE) - ->eq('project_id', $project_id) - ->eq('column_id', $column_id) - ->in('is_active', $status) - ->count(); - - if ($exists) { - $this->db->table(ProjectDailyColumnStats::TABLE) - ->eq('project_id', $project_id) - ->eq('column_id', $column_id) - ->eq('day', $date) - ->update(array('score' => $score, 'total' => $total)); - - } else { - $this->db->table(ProjectDailyColumnStats::TABLE)->insert(array( - 'day' => $date, - 'project_id' => $project_id, - 'column_id' => $column_id, - 'total' => $total, - 'score' => $score, - )); - } + $this->db->table(self::TABLE)->eq('project_id', $project_id)->eq('day', $date)->remove(); + + foreach ($this->getStatsByColumns($project_id) as $column_id => $column) { + $this->db->table(self::TABLE)->insert(array( + 'day' => $date, + 'project_id' => $project_id, + 'column_id' => $column_id, + 'total' => $column['total'], + 'score' => $column['score'], + )); } $this->db->closeTransaction(); @@ -90,43 +59,38 @@ class ProjectDailyColumnStats extends Base */ public function countDays($project_id, $from, $to) { - $rq = $this->db->execute( - 'SELECT COUNT(DISTINCT day) FROM '.self::TABLE.' WHERE day >= ? AND day <= ? AND project_id=?', - array($from, $to, $project_id) - ); - - return $rq !== false ? $rq->fetchColumn(0) : 0; + return $this->db->table(self::TABLE) + ->eq('project_id', $project_id) + ->gte('day', $from) + ->lte('day', $to) + ->findOneColumn('COUNT(DISTINCT day)'); } /** - * Get raw metrics for the project within a data range + * Get aggregated metrics for the project within a data range + * + * [ + * ['Date', 'Column1', 'Column2'], + * ['2014-11-16', 2, 5], + * ['2014-11-17', 20, 15], + * ] * * @access public * @param integer $project_id Project id * @param string $from Start date (ISO format YYYY-MM-DD) * @param string $to End date + * @param string $field Column to aggregate * @return array */ - public function getRawMetrics($project_id, $from, $to) + public function getAggregatedMetrics($project_id, $from, $to, $field = 'total') { - return $this->db->table(ProjectDailyColumnStats::TABLE) - ->columns( - ProjectDailyColumnStats::TABLE.'.column_id', - ProjectDailyColumnStats::TABLE.'.day', - ProjectDailyColumnStats::TABLE.'.total', - ProjectDailyColumnStats::TABLE.'.score', - Board::TABLE.'.title AS column_title' - ) - ->join(Board::TABLE, 'id', 'column_id') - ->eq(ProjectDailyColumnStats::TABLE.'.project_id', $project_id) - ->gte('day', $from) - ->lte('day', $to) - ->asc(ProjectDailyColumnStats::TABLE.'.day') - ->findAll(); + $columns = $this->board->getColumnsList($project_id); + $metrics = $this->getMetrics($project_id, $from, $to); + return $this->buildAggregate($metrics, $columns, $field); } /** - * Get raw metrics for the project within a data range grouped by day + * Fetch metrics * * @access public * @param integer $project_id Project id @@ -134,72 +98,158 @@ class ProjectDailyColumnStats extends Base * @param string $to End date * @return array */ - public function getRawMetricsByDay($project_id, $from, $to) + public function getMetrics($project_id, $from, $to) { - return $this->db->table(ProjectDailyColumnStats::TABLE) - ->columns( - ProjectDailyColumnStats::TABLE.'.day', - 'SUM('.ProjectDailyColumnStats::TABLE.'.total) AS total', - 'SUM('.ProjectDailyColumnStats::TABLE.'.score) AS score' - ) - ->eq(ProjectDailyColumnStats::TABLE.'.project_id', $project_id) - ->gte('day', $from) - ->lte('day', $to) - ->asc(ProjectDailyColumnStats::TABLE.'.day') - ->groupBy(ProjectDailyColumnStats::TABLE.'.day') - ->findAll(); + return $this->db->table(self::TABLE) + ->eq('project_id', $project_id) + ->gte('day', $from) + ->lte('day', $to) + ->asc(self::TABLE.'.day') + ->findAll(); } /** - * Get aggregated metrics for the project within a data range - * - * [ - * ['Date', 'Column1', 'Column2'], - * ['2014-11-16', 2, 5], - * ['2014-11-17', 20, 15], - * ] + * Build aggregate * - * @access public - * @param integer $project_id Project id - * @param string $from Start date (ISO format YYYY-MM-DD) - * @param string $to End date - * @param string $column Column to aggregate + * @access private + * @param array $metrics + * @param array $columns + * @param string $field * @return array */ - public function getAggregatedMetrics($project_id, $from, $to, $column = 'total') + private function buildAggregate(array &$metrics, array &$columns, $field) { - $columns = $this->board->getColumnsList($project_id); $column_ids = array_keys($columns); - $metrics = array(array_merge(array(e('Date')), array_values($columns))); - $aggregates = array(); - - // Fetch metrics for the project - $records = $this->db->table(ProjectDailyColumnStats::TABLE) - ->eq('project_id', $project_id) - ->gte('day', $from) - ->lte('day', $to) - ->findAll(); - - // Aggregate by day - foreach ($records as $record) { - if (! isset($aggregates[$record['day']])) { - $aggregates[$record['day']] = array($record['day']); - } + $days = array_unique(array_column($metrics, 'day')); + $rows = array(array_merge(array(e('Date')), array_values($columns))); - $aggregates[$record['day']][$record['column_id']] = $record[$column]; + foreach ($days as $day) { + $rows[] = $this->buildRowAggregate($metrics, $column_ids, $day, $field); } - // Aggregate by row - foreach ($aggregates as $aggregate) { - $row = array($aggregate[0]); + return $rows; + } + + /** + * Build one row of the aggregate + * + * @access private + * @param array $metrics + * @param array $column_ids + * @param string $day + * @param string $field + * @return array + */ + private function buildRowAggregate(array &$metrics, array &$column_ids, $day, $field) + { + $row = array($day); + + foreach ($column_ids as $column_id) { + $row[] = $this->findValueInMetrics($metrics, $day, $column_id, $field); + } + + return $row; + } - foreach ($column_ids as $column_id) { - $row[] = (int) $aggregate[$column_id]; + /** + * Find the value in the metrics + * + * @access private + * @param array $metrics + * @param string $day + * @param string $column_id + * @param string $field + * @return array + */ + private function findValueInMetrics(array &$metrics, $day, $column_id, $field) + { + foreach ($metrics as $metric) { + if ($metric['day'] === $day && $metric['column_id'] == $column_id) { + return $metric[$field]; } + } - $metrics[] = $row; + return 0; + } + + /** + * Get number of tasks and score by columns + * + * @access private + * @param integer $project_id + * @return array + */ + private function getStatsByColumns($project_id) + { + $totals = $this->getTotalByColumns($project_id); + $scores = $this->getScoreByColumns($project_id); + $columns = array(); + + foreach ($totals as $column_id => $total) { + $columns[$column_id] = array('total' => $total); + } + + foreach ($scores as $column_id => $score) { + if (isset($columns[$column_id])) { + $columns[$column_id]['score'] = $score; + } else { + $columns[$column_id] = array('score' => $score); + } + } + + return $columns; + } + + /** + * Get number of tasks and score by columns + * + * @access private + * @param integer $project_id + * @return array + */ + private function getScoreByColumns($project_id) + { + $stats = $this->db->table(Task::TABLE) + ->columns('column_id', 'SUM(score) AS score') + ->eq('project_id', $project_id) + ->eq('is_active', Task::STATUS_OPEN) + ->groupBy('column_id') + ->findAll(); + + return array_column($stats, 'score', 'column_id'); + } + + /** + * Get number of tasks and score by columns + * + * @access private + * @param integer $project_id + * @return array + */ + private function getTotalByColumns($project_id) + { + $stats = $this->db->table(Task::TABLE) + ->columns('column_id', 'COUNT(*) AS total') + ->eq('project_id', $project_id) + ->in('is_active', $this->getTaskStatusConfig()) + ->groupBy('column_id') + ->findAll(); + + return array_column($stats, 'total', 'column_id'); + } + + /** + * Get task status to use for total calculation + * + * @access private + * @return array + */ + private function getTaskStatusConfig() + { + if ($this->config->get('cfd_include_closed_tasks') == 1) { + return array(Task::STATUS_OPEN, Task::STATUS_CLOSED); } - return $metrics; + return array(Task::STATUS_OPEN); } } diff --git a/app/Subscriber/ProjectDailySummarySubscriber.php b/app/Subscriber/ProjectDailySummarySubscriber.php index bfa6cd42..d9239264 100644 --- a/app/Subscriber/ProjectDailySummarySubscriber.php +++ b/app/Subscriber/ProjectDailySummarySubscriber.php @@ -11,11 +11,11 @@ class ProjectDailySummarySubscriber extends \Kanboard\Core\Base implements Event public static function getSubscribedEvents() { return array( - Task::EVENT_CREATE => array('execute', 0), - Task::EVENT_UPDATE => array('execute', 0), + Task::EVENT_CREATE_UPDATE => array('execute', 0), Task::EVENT_CLOSE => array('execute', 0), Task::EVENT_OPEN => array('execute', 0), Task::EVENT_MOVE_COLUMN => array('execute', 0), + Task::EVENT_MOVE_SWIMLANE => array('execute', 0), ); } diff --git a/tests/units/Model/ProjectDailyColumnStatsTest.php b/tests/units/Model/ProjectDailyColumnStatsTest.php index 4c801e02..f8a64054 100644 --- a/tests/units/Model/ProjectDailyColumnStatsTest.php +++ b/tests/units/Model/ProjectDailyColumnStatsTest.php @@ -4,6 +4,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Project; use Kanboard\Model\ProjectDailyColumnStats; +use Kanboard\Model\Config; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskStatus; @@ -12,79 +13,220 @@ class ProjectDailyColumnStatsTest extends Base { public function testUpdateTotals() { - $p = new Project($this->container); - $pds = new ProjectDailyColumnStats($this->container); - $tc = new TaskCreation($this->container); - $ts = new TaskStatus($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest'))); - $this->assertEquals(0, $pds->countDays(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d'))); - - for ($i = 0; $i < 10; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 1))); - } - - for ($i = 0; $i < 5; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 4))); - } - - $pds->updateTotals(1, date('Y-m-d', strtotime('-2days'))); - - for ($i = 0; $i < 15; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 3))); - } - - for ($i = 0; $i < 25; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 2))); - } - - $pds->updateTotals(1, date('Y-m-d', strtotime('-1 day'))); - - $this->assertNotFalse($ts->close(1)); - $this->assertNotFalse($ts->close(2)); - - for ($i = 0; $i < 3; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 3))); - } - - for ($i = 0; $i < 5; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 2))); - } - - for ($i = 0; $i < 4; $i++) { - $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 4))); - } - - $pds->updateTotals(1, date('Y-m-d')); - - $this->assertEquals(3, $pds->countDays(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d'))); - $metrics = $pds->getAggregatedMetrics(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d')); - - $this->assertNotEmpty($metrics); - $this->assertEquals(4, count($metrics)); - $this->assertEquals(5, count($metrics[0])); - $this->assertEquals('Date', $metrics[0][0]); - $this->assertEquals('Backlog', $metrics[0][1]); - $this->assertEquals('Ready', $metrics[0][2]); - $this->assertEquals('Work in progress', $metrics[0][3]); - $this->assertEquals('Done', $metrics[0][4]); - - $this->assertEquals(date('Y-m-d', strtotime('-2days')), $metrics[1][0]); - $this->assertEquals(10, $metrics[1][1]); - $this->assertEquals(0, $metrics[1][2]); - $this->assertEquals(0, $metrics[1][3]); - $this->assertEquals(5, $metrics[1][4]); - - $this->assertEquals(date('Y-m-d', strtotime('-1day')), $metrics[2][0]); - $this->assertEquals(10, $metrics[2][1]); - $this->assertEquals(25, $metrics[2][2]); - $this->assertEquals(15, $metrics[2][3]); - $this->assertEquals(5, $metrics[2][4]); - - $this->assertEquals(date('Y-m-d'), $metrics[3][0]); - $this->assertEquals(10, $metrics[3][1]); - $this->assertEquals(30, $metrics[3][2]); - $this->assertEquals(18, $metrics[3][3]); - $this->assertEquals(9, $metrics[3][4]); + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->createTasks(1, 2, 1); + $this->createTasks(1, 3, 0); + + $this->createTasks(2, 5, 1); + $this->createTasks(2, 8, 1); + $this->createTasks(2, 0, 0); + $this->createTasks(2, 0, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(1, 9, 1); + $this->createTasks(1, 7, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + + $stats = $this->container['db']->table(ProjectDailyColumnStats::TABLE) + ->asc('day') + ->asc('column_id') + ->columns('day', 'project_id', 'column_id', 'total', 'score') + ->findAll(); + + $expected = array( + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 4, + 'score' => 11, + ), + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 4, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 4, + 'score' => 11, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 4, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 3, + 'total' => 1, + 'score' => 0, + ), + ); + + $this->assertEquals($expected, $stats); + } + + public function testUpdateTotalsWithOnlyOpenTasks() + { + $configModel = new Config($this->container); + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($configModel->save(array('cfd_include_closed_tasks' => 0))); + $this->container['memoryCache']->flush(); + + $this->createTasks(1, 2, 1); + $this->createTasks(1, 3, 0); + + $this->createTasks(2, 5, 1); + $this->createTasks(2, 8, 1); + $this->createTasks(2, 0, 0); + $this->createTasks(2, 0, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(1, 9, 1); + $this->createTasks(1, 7, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + + $stats = $this->container['db']->table(ProjectDailyColumnStats::TABLE) + ->asc('day') + ->asc('column_id') + ->columns('day', 'project_id', 'column_id', 'total', 'score') + ->findAll(); + + $expected = array( + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 2, + 'score' => 11, + ), + array( + 'day' => '2016-01-16', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 2, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 1, + 'total' => 2, + 'score' => 11, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 2, + 'total' => 2, + 'score' => 13, + ), + array( + 'day' => '2016-01-17', + 'project_id' => 1, + 'column_id' => 3, + 'total' => 1, + 'score' => 0, + ), + ); + + $this->assertEquals($expected, $stats); + } + + public function testCountDays() + { + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->createTasks(1, 2, 1); + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + $this->assertEquals(1, $projectDailyColumnStats->countDays(1, '2016-01-16', '2016-01-17')); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + $this->assertEquals(2, $projectDailyColumnStats->countDays(1, '2016-01-16', '2016-01-17')); + } + + public function testGetAggregatedMetrics() + { + $projectModel = new Project($this->container); + $projectDailyColumnStats = new ProjectDailyColumnStats($this->container); + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->createTasks(1, 2, 1); + $this->createTasks(1, 3, 0); + + $this->createTasks(2, 5, 1); + $this->createTasks(2, 8, 1); + $this->createTasks(2, 0, 0); + $this->createTasks(2, 0, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(1, 9, 1); + $this->createTasks(1, 7, 0); + + $projectDailyColumnStats->updateTotals(1, '2016-01-16'); + + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-17'); + + $this->createTasks(2, 1, 1); + $this->createTasks(3, 1, 1); + $this->createTasks(3, 0, 1); + + $projectDailyColumnStats->updateTotals(1, '2016-01-18'); + + $expected = array( + array('Date', 'Backlog', 'Ready', 'Work in progress', 'Done'), + array('2016-01-16', 4, 4, 0, 0), + array('2016-01-17', 4, 4, 1, 0), + array('2016-01-18', 4, 5, 3, 0), + ); + + $this->assertEquals($expected, $projectDailyColumnStats->getAggregatedMetrics(1, '2016-01-16', '2016-01-18')); + + $expected = array( + array('Date', 'Backlog', 'Ready', 'Work in progress', 'Done'), + array('2016-01-16', 11, 13, 0, 0), + array('2016-01-17', 11, 13, 0, 0), + array('2016-01-18', 11, 14, 1, 0), + ); + + $this->assertEquals($expected, $projectDailyColumnStats->getAggregatedMetrics(1, '2016-01-16', '2016-01-18', 'score')); + } + + private function createTasks($column_id, $score, $is_active) + { + $taskCreationModel = new TaskCreation($this->container); + $this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => $column_id, 'score' => $score, 'is_active' => $is_active))); } } -- cgit v1.2.3 From 81e4c3199edd4e92f66fe7db6bc36d30d16ee96d Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 16 Jan 2016 19:40:26 -0500 Subject: Fix unit test (PHP 5.4 regression) --- app/Analytic/AverageTimeSpentColumnAnalytic.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/Analytic') diff --git a/app/Analytic/AverageTimeSpentColumnAnalytic.php b/app/Analytic/AverageTimeSpentColumnAnalytic.php index 820db801..c3cff548 100644 --- a/app/Analytic/AverageTimeSpentColumnAnalytic.php +++ b/app/Analytic/AverageTimeSpentColumnAnalytic.php @@ -63,7 +63,9 @@ class AverageTimeSpentColumnAnalytic extends Base */ private function processTasks(array &$stats, $project_id) { - foreach ($this->getTasks($project_id) as &$task) { + $tasks = $this->getTasks($project_id); + + foreach ($tasks as &$task) { foreach ($this->getTaskTimeByColumns($task) as $column_id => $time_spent) { if (isset($stats[$column_id])) { $stats[$column_id]['count']++; -- cgit v1.2.3 From fc468088c3b39bc4e9d185683442db1d36b61e23 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 20 Feb 2016 15:08:18 -0500 Subject: Split Board model into multiple classes --- ChangeLog | 1 + app/Action/CommentCreationMoveTaskColumn.php | 2 +- app/Action/TaskDuplicateAnotherProject.php | 2 +- app/Analytic/AverageTimeSpentColumnAnalytic.php | 2 +- app/Analytic/TaskDistributionAnalytic.php | 2 +- app/Api/Board.php | 35 ---- app/Api/Column.php | 42 ++++ app/Controller/Action.php | 4 +- app/Controller/BoardPopover.php | 4 +- app/Controller/Column.php | 16 +- app/Controller/Gantt.php | 2 +- app/Controller/Task.php | 4 +- app/Controller/Taskcreation.php | 2 +- app/Controller/Taskduplication.php | 2 +- app/Formatter/TaskFilterGanttFormatter.php | 2 +- app/Helper/Task.php | 2 +- app/Model/ActionParameter.php | 4 +- app/Model/Board.php | 268 +----------------------- app/Model/Column.php | 145 +++++++++++++ app/Model/Project.php | 4 +- app/Model/ProjectDailyColumnStats.php | 2 +- app/Model/TaskAnalytic.php | 2 +- app/Model/TaskCreation.php | 2 +- app/Model/TaskDuplication.php | 8 +- app/Model/TaskFilter.php | 2 +- app/Model/TaskFinder.php | 10 +- app/Model/TaskImport.php | 2 +- app/Model/TaskLink.php | 6 +- app/Model/Transition.php | 8 +- app/Subscriber/RecurringTaskSubscriber.php | 4 +- jsonrpc.php | 2 + tests/integration/ApiTest.php | 70 ------- tests/integration/BoardTest.php | 21 ++ tests/integration/ColumnTest.php | 65 ++++++ tests/units/Model/ActionTest.php | 6 +- tests/units/Model/BoardTest.php | 227 +------------------- tests/units/Model/ColumnTest.php | 168 +++++++++++++++ tests/units/Model/ProjectTest.php | 2 - tests/units/Model/TaskPositionTest.php | 14 +- 39 files changed, 511 insertions(+), 655 deletions(-) create mode 100644 app/Api/Column.php create mode 100644 tests/integration/BoardTest.php create mode 100644 tests/integration/ColumnTest.php (limited to 'app/Analytic') diff --git a/ChangeLog b/ChangeLog index ae7919bf..190a57ad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,7 @@ Improvements: * Improve image thumbnails and files table * Add confirmation inline popup to remove custom filter * Increase client_max_body_size value for Nginx +* Split Board model into multiple classes Bug fixes: diff --git a/app/Action/CommentCreationMoveTaskColumn.php b/app/Action/CommentCreationMoveTaskColumn.php index 4473cf91..11224d67 100644 --- a/app/Action/CommentCreationMoveTaskColumn.php +++ b/app/Action/CommentCreationMoveTaskColumn.php @@ -71,7 +71,7 @@ class CommentCreationMoveTaskColumn extends Base return false; } - $column = $this->board->getColumn($data['column_id']); + $column = $this->column->getById($data['column_id']); return (bool) $this->comment->create(array( 'comment' => t('Moved to column %s', $column['title']), diff --git a/app/Action/TaskDuplicateAnotherProject.php b/app/Action/TaskDuplicateAnotherProject.php index 5bcdce08..5f05136e 100644 --- a/app/Action/TaskDuplicateAnotherProject.php +++ b/app/Action/TaskDuplicateAnotherProject.php @@ -74,7 +74,7 @@ class TaskDuplicateAnotherProject extends Base */ public function doAction(array $data) { - $destination_column_id = $this->board->getFirstColumn($this->getParam('project_id')); + $destination_column_id = $this->column->getFirstColumnId($this->getParam('project_id')); return (bool) $this->taskDuplication->duplicateToProject($data['task_id'], $this->getParam('project_id'), null, $destination_column_id); } diff --git a/app/Analytic/AverageTimeSpentColumnAnalytic.php b/app/Analytic/AverageTimeSpentColumnAnalytic.php index c3cff548..bef55419 100644 --- a/app/Analytic/AverageTimeSpentColumnAnalytic.php +++ b/app/Analytic/AverageTimeSpentColumnAnalytic.php @@ -40,7 +40,7 @@ class AverageTimeSpentColumnAnalytic extends Base private function initialize($project_id) { $stats = array(); - $columns = $this->board->getColumnsList($project_id); + $columns = $this->column->getList($project_id); foreach ($columns as $column_id => $column_title) { $stats[$column_id] = array( diff --git a/app/Analytic/TaskDistributionAnalytic.php b/app/Analytic/TaskDistributionAnalytic.php index 614c5f72..838652e3 100644 --- a/app/Analytic/TaskDistributionAnalytic.php +++ b/app/Analytic/TaskDistributionAnalytic.php @@ -23,7 +23,7 @@ class TaskDistributionAnalytic extends Base { $metrics = array(); $total = 0; - $columns = $this->board->getColumns($project_id); + $columns = $this->column->getAll($project_id); foreach ($columns as $column) { $nb_tasks = $this->taskFinder->countByColumnId($project_id, $column['id']); diff --git a/app/Api/Board.php b/app/Api/Board.php index d615b1dc..185ac51a 100644 --- a/app/Api/Board.php +++ b/app/Api/Board.php @@ -15,39 +15,4 @@ class Board extends Base $this->checkProjectPermission($project_id); return $this->board->getBoard($project_id); } - - public function getColumns($project_id) - { - return $this->board->getColumns($project_id); - } - - public function getColumn($column_id) - { - return $this->board->getColumn($column_id); - } - - public function moveColumnUp($project_id, $column_id) - { - return $this->board->moveUp($project_id, $column_id); - } - - public function moveColumnDown($project_id, $column_id) - { - return $this->board->moveDown($project_id, $column_id); - } - - public function updateColumn($column_id, $title, $task_limit = 0, $description = '') - { - return $this->board->updateColumn($column_id, $title, $task_limit, $description); - } - - public function addColumn($project_id, $title, $task_limit = 0, $description = '') - { - return $this->board->addColumn($project_id, $title, $task_limit, $description); - } - - public function removeColumn($column_id) - { - return $this->board->removeColumn($column_id); - } } diff --git a/app/Api/Column.php b/app/Api/Column.php new file mode 100644 index 00000000..ddc3a5d0 --- /dev/null +++ b/app/Api/Column.php @@ -0,0 +1,42 @@ +column->getAll($project_id); + } + + public function getColumn($column_id) + { + return $this->column->getById($column_id); + } + + public function updateColumn($column_id, $title, $task_limit = 0, $description = '') + { + return $this->column->update($column_id, $title, $task_limit, $description); + } + + public function addColumn($project_id, $title, $task_limit = 0, $description = '') + { + return $this->column->create($project_id, $title, $task_limit, $description); + } + + public function removeColumn($column_id) + { + return $this->column->remove($column_id); + } + + public function changeColumnPosition($project_id, $column_id, $position) + { + return $this->column->changePosition($project_id, $column_id, $position); + } +} diff --git a/app/Controller/Action.php b/app/Controller/Action.php index 482a210b..6c324324 100644 --- a/app/Controller/Action.php +++ b/app/Controller/Action.php @@ -27,7 +27,7 @@ class Action extends Base 'available_actions' => $this->actionManager->getAvailableActions(), 'available_events' => $this->eventManager->getAll(), 'available_params' => $this->actionManager->getAvailableParameters($actions), - 'columns_list' => $this->board->getColumnsList($project['id']), + 'columns_list' => $this->column->getList($project['id']), 'users_list' => $this->projectUserRole->getAssignableUsersList($project['id']), 'projects_list' => $this->projectUserRole->getProjectsByUser($this->userSession->getId()), 'colors_list' => $this->color->getList(), @@ -86,7 +86,7 @@ class Action extends Base $this->response->html($this->helper->layout->project('action/params', array( 'values' => $values, 'action_params' => $action_params, - 'columns_list' => $this->board->getColumnsList($project['id']), + 'columns_list' => $this->column->getList($project['id']), 'users_list' => $this->projectUserRole->getAssignableUsersList($project['id']), 'projects_list' => $projects_list, 'colors_list' => $this->color->getList(), diff --git a/app/Controller/BoardPopover.php b/app/Controller/BoardPopover.php index 965669ff..63dab302 100644 --- a/app/Controller/BoardPopover.php +++ b/app/Controller/BoardPopover.php @@ -112,7 +112,7 @@ class BoardPopover extends Base $this->response->html($this->template->render('board/popover_close_all_tasks_column', array( 'project' => $project, 'nb_tasks' => $this->taskFinder->countByColumnAndSwimlaneId($project['id'], $column_id, $swimlane_id), - 'column' => $this->board->getColumnTitleById($column_id), + 'column' => $this->column->getColumnTitleById($column_id), 'swimlane' => $this->swimlane->getNameById($swimlane_id) ?: t($project['default_swimlane']), 'values' => array('column_id' => $column_id, 'swimlane_id' => $swimlane_id), ))); @@ -129,7 +129,7 @@ class BoardPopover extends Base $values = $this->request->getValues(); $this->taskStatus->closeTasksBySwimlaneAndColumn($values['swimlane_id'], $values['column_id']); - $this->flash->success(t('All tasks of the column "%s" and the swimlane "%s" have been closed successfully.', $this->board->getColumnTitleById($values['column_id']), $this->swimlane->getNameById($values['swimlane_id']) ?: t($project['default_swimlane']))); + $this->flash->success(t('All tasks of the column "%s" and the swimlane "%s" have been closed successfully.', $this->column->getColumnTitleById($values['column_id']), $this->swimlane->getNameById($values['swimlane_id']) ?: t($project['default_swimlane']))); $this->response->redirect($this->helper->url->to('board', 'show', array('project_id' => $project['id']))); } } diff --git a/app/Controller/Column.php b/app/Controller/Column.php index 329413ef..e02c7dcb 100644 --- a/app/Controller/Column.php +++ b/app/Controller/Column.php @@ -18,7 +18,7 @@ class Column extends Base public function index() { $project = $this->getProject(); - $columns = $this->board->getColumns($project['id']); + $columns = $this->column->getAll($project['id']); $this->response->html($this->helper->layout->project('column/index', array( 'columns' => $columns, @@ -35,7 +35,7 @@ class Column extends Base public function create(array $values = array(), array $errors = array()) { $project = $this->getProject(); - $columns = $this->board->getColumnsList($project['id']); + $columns = $this->column->getList($project['id']); if (empty($values)) { $values = array('project_id' => $project['id']); @@ -62,7 +62,7 @@ class Column extends Base list($valid, $errors) = $this->columnValidator->validateCreation($values); if ($valid) { - if ($this->board->addColumn($project['id'], $values['title'], $values['task_limit'], $values['description'])) { + if ($this->column->create($project['id'], $values['title'], $values['task_limit'], $values['description'])) { $this->flash->success(t('Column created successfully.')); return $this->response->redirect($this->helper->url->to('column', 'index', array('project_id' => $project['id'])), true); } else { @@ -81,7 +81,7 @@ class Column extends Base public function edit(array $values = array(), array $errors = array()) { $project = $this->getProject(); - $column = $this->board->getColumn($this->request->getIntegerParam('column_id')); + $column = $this->column->getById($this->request->getIntegerParam('column_id')); $this->response->html($this->helper->layout->project('column/edit', array( 'errors' => $errors, @@ -105,7 +105,7 @@ class Column extends Base list($valid, $errors) = $this->columnValidator->validateModification($values); if ($valid) { - if ($this->board->updateColumn($values['id'], $values['title'], $values['task_limit'], $values['description'])) { + if ($this->column->update($values['id'], $values['title'], $values['task_limit'], $values['description'])) { $this->flash->success(t('Board updated successfully.')); $this->response->redirect($this->helper->url->to('column', 'index', array('project_id' => $project['id']))); } else { @@ -144,7 +144,7 @@ class Column extends Base $project = $this->getProject(); $this->response->html($this->helper->layout->project('column/remove', array( - 'column' => $this->board->getColumn($this->request->getIntegerParam('column_id')), + 'column' => $this->column->getById($this->request->getIntegerParam('column_id')), 'project' => $project, 'title' => t('Remove a column from a board') ))); @@ -159,9 +159,9 @@ class Column extends Base { $project = $this->getProject(); $this->checkCSRFParam(); - $column = $this->board->getColumn($this->request->getIntegerParam('column_id')); + $column_id = $this->request->getIntegerParam('column_id'); - if (! empty($column) && $this->board->removeColumn($column['id'])) { + if ($this->column->remove($column_id)) { $this->flash->success(t('Column removed successfully.')); } else { $this->flash->failure(t('Unable to remove this column.')); diff --git a/app/Controller/Gantt.php b/app/Controller/Gantt.php index 5dbd1243..9ffa277f 100644 --- a/app/Controller/Gantt.php +++ b/app/Controller/Gantt.php @@ -103,7 +103,7 @@ class Gantt extends Base $values = $values + array( 'project_id' => $project['id'], - 'column_id' => $this->board->getFirstColumn($project['id']), + 'column_id' => $this->column->getFirstColumnId($project['id']), 'position' => 1 ); diff --git a/app/Controller/Task.php b/app/Controller/Task.php index 98b7a041..539d377b 100644 --- a/app/Controller/Task.php +++ b/app/Controller/Task.php @@ -36,7 +36,7 @@ class Task extends Base 'subtasks' => $this->subtask->getAll($task['id']), 'links' => $this->taskLink->getAllGroupedByLabel($task['id']), 'task' => $task, - 'columns_list' => $this->board->getColumnsList($task['project_id']), + 'columns_list' => $this->column->getList($task['project_id']), 'colors_list' => $this->color->getList(), 'title' => $task['title'], 'no_layout' => true, @@ -74,7 +74,7 @@ class Task extends Base 'task' => $task, 'values' => $values, 'link_label_list' => $this->link->getList(0, false), - 'columns_list' => $this->board->getColumnsList($task['project_id']), + 'columns_list' => $this->column->getList($task['project_id']), 'colors_list' => $this->color->getList(), 'users_list' => $this->projectUserRole->getAssignableUsersList($task['project_id'], true, false, false), 'title' => $task['project_name'].' > '.$task['title'], diff --git a/app/Controller/Taskcreation.php b/app/Controller/Taskcreation.php index f1ac7272..1d8a0e29 100644 --- a/app/Controller/Taskcreation.php +++ b/app/Controller/Taskcreation.php @@ -36,7 +36,7 @@ class Taskcreation extends Base 'project' => $project, 'errors' => $errors, 'values' => $values + array('project_id' => $project['id']), - 'columns_list' => $this->board->getColumnsList($project['id']), + 'columns_list' => $this->column->getList($project['id']), 'users_list' => $this->projectUserRole->getAssignableUsersList($project['id'], true, false, true), 'colors_list' => $this->color->getList(), 'categories_list' => $this->category->getList($project['id']), diff --git a/app/Controller/Taskduplication.php b/app/Controller/Taskduplication.php index 7e7fccd6..7641a48d 100644 --- a/app/Controller/Taskduplication.php +++ b/app/Controller/Taskduplication.php @@ -115,7 +115,7 @@ class Taskduplication extends Base $dst_project_id = $this->request->getIntegerParam('dst_project_id', key($projects_list)); $swimlanes_list = $this->swimlane->getList($dst_project_id, false, true); - $columns_list = $this->board->getColumnsList($dst_project_id); + $columns_list = $this->column->getList($dst_project_id); $categories_list = $this->category->getList($dst_project_id); $users_list = $this->projectUserRole->getAssignableUsersList($dst_project_id); diff --git a/app/Formatter/TaskFilterGanttFormatter.php b/app/Formatter/TaskFilterGanttFormatter.php index 08059d4c..a4eef1ee 100644 --- a/app/Formatter/TaskFilterGanttFormatter.php +++ b/app/Formatter/TaskFilterGanttFormatter.php @@ -47,7 +47,7 @@ class TaskFilterGanttFormatter extends TaskFilter implements FormatterInterface private function formatTask(array $task) { if (! isset($this->columns[$task['project_id']])) { - $this->columns[$task['project_id']] = $this->board->getColumnsList($task['project_id']); + $this->columns[$task['project_id']] = $this->column->getList($task['project_id']); } $start = $task['date_started'] ?: time(); diff --git a/app/Helper/Task.php b/app/Helper/Task.php index e85d6e30..6058c099 100644 --- a/app/Helper/Task.php +++ b/app/Helper/Task.php @@ -178,7 +178,7 @@ class Task extends Base public function getProgress($task) { if (! isset($this->columns[$task['project_id']])) { - $this->columns[$task['project_id']] = $this->board->getColumnsList($task['project_id']); + $this->columns[$task['project_id']] = $this->column->getList($task['project_id']); } return $this->task->getProgress($task, $this->columns[$task['project_id']]); diff --git a/app/Model/ActionParameter.php b/app/Model/ActionParameter.php index 62b03142..53edcbc8 100644 --- a/app/Model/ActionParameter.php +++ b/app/Model/ActionParameter.php @@ -150,8 +150,8 @@ class ActionParameter extends Base case 'dest_column_id': case 'dst_column_id': case 'column_id': - $column = $this->board->getColumn($value); - return empty($column) ? false : $this->board->getColumnIdByTitle($project_id, $column['title']) ?: false; + $column = $this->column->getById($value); + return empty($column) ? false : $this->column->getColumnIdByTitle($project_id, $column['title']) ?: false; case 'user_id': case 'owner_id': return $this->projectPermission->isAssignable($project_id, $value) ? $value : false; diff --git a/app/Model/Board.php b/app/Model/Board.php index 0f980f68..f677266f 100644 --- a/app/Model/Board.php +++ b/app/Model/Board.php @@ -12,13 +12,6 @@ use PicoDb\Database; */ class Board extends Base { - /** - * SQL table name - * - * @var string - */ - const TABLE = 'columns'; - /** * Get Kanboard default columns * @@ -73,7 +66,7 @@ class Board extends Base 'description' => $column['description'], ); - if (! $this->db->table(self::TABLE)->save($values)) { + if (! $this->db->table(Column::TABLE)->save($values)) { return false; } } @@ -91,7 +84,7 @@ class Board extends Base */ public function duplicate($project_from, $project_to) { - $columns = $this->db->table(Board::TABLE) + $columns = $this->db->table(Column::TABLE) ->columns('title', 'task_limit', 'description') ->eq('project_id', $project_from) ->asc('position') @@ -100,134 +93,6 @@ class Board extends Base return $this->board->create($project_to, $columns); } - /** - * Add a new column to the board - * - * @access public - * @param integer $project_id Project id - * @param string $title Column title - * @param integer $task_limit Task limit - * @param string $description Column description - * @return boolean|integer - */ - public function addColumn($project_id, $title, $task_limit = 0, $description = '') - { - $values = array( - 'project_id' => $project_id, - 'title' => $title, - 'task_limit' => intval($task_limit), - 'position' => $this->getLastColumnPosition($project_id) + 1, - 'description' => $description, - ); - - return $this->persist(self::TABLE, $values); - } - - /** - * Update a column - * - * @access public - * @param integer $column_id Column id - * @param string $title Column title - * @param integer $task_limit Task limit - * @param string $description Optional description - * @return boolean - */ - public function updateColumn($column_id, $title, $task_limit = 0, $description = '') - { - return $this->db->table(self::TABLE)->eq('id', $column_id)->update(array( - 'title' => $title, - 'task_limit' => intval($task_limit), - 'description' => $description, - )); - } - - /** - * Get columns with consecutive positions - * - * If you remove a column, the positions are not anymore consecutives - * - * @access public - * @param integer $project_id - * @return array - */ - public function getNormalizedColumnPositions($project_id) - { - $columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position'); - $position = 1; - - foreach ($columns as $column_id => $column_position) { - $columns[$column_id] = $position++; - } - - return $columns; - } - - /** - * Save the new positions for a set of columns - * - * @access public - * @param array $columns Hashmap of column_id/column_position - * @return boolean - */ - public function saveColumnPositions(array $columns) - { - return $this->db->transaction(function (Database $db) use ($columns) { - - foreach ($columns as $column_id => $position) { - if (! $db->table(Board::TABLE)->eq('id', $column_id)->update(array('position' => $position))) { - return false; - } - } - }); - } - - /** - * Move a column down, increment the column position value - * - * @access public - * @param integer $project_id Project id - * @param integer $column_id Column id - * @return boolean - */ - public function moveDown($project_id, $column_id) - { - $columns = $this->getNormalizedColumnPositions($project_id); - $positions = array_flip($columns); - - if (isset($columns[$column_id]) && $columns[$column_id] < count($columns)) { - $position = ++$columns[$column_id]; - $columns[$positions[$position]]--; - - return $this->saveColumnPositions($columns); - } - - return false; - } - - /** - * Move a column up, decrement the column position value - * - * @access public - * @param integer $project_id Project id - * @param integer $column_id Column id - * @return boolean - */ - public function moveUp($project_id, $column_id) - { - $columns = $this->getNormalizedColumnPositions($project_id); - $positions = array_flip($columns); - - if (isset($columns[$column_id]) && $columns[$column_id] > 1) { - $position = --$columns[$column_id]; - $columns[$positions[$position]]++; - - return $this->saveColumnPositions($columns); - } - - return false; - } - /** * Get all tasks sorted by columns and swimlanes * @@ -239,7 +104,7 @@ class Board extends Base public function getBoard($project_id, $callback = null) { $swimlanes = $this->swimlane->getSwimlanes($project_id); - $columns = $this->getColumns($project_id); + $columns = $this->column->getAll($project_id); $nb_columns = count($columns); for ($i = 0, $ilen = count($swimlanes); $i < $ilen; $i++) { @@ -307,131 +172,4 @@ class Board extends Base return $prepend ? array(-1 => t('All columns')) + $listing : $listing; } - - /** - * Get the first column id for a given project - * - * @access public - * @param integer $project_id Project id - * @return integer - */ - public function getFirstColumn($project_id) - { - return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findOneColumn('id'); - } - - /** - * Get the last column id for a given project - * - * @access public - * @param integer $project_id Project id - * @return integer - */ - public function getLastColumn($project_id) - { - return $this->db->table(self::TABLE)->eq('project_id', $project_id)->desc('position')->findOneColumn('id'); - } - - /** - * Get the list of columns sorted by position [ column_id => title ] - * - * @access public - * @param integer $project_id Project id - * @param boolean $prepend Prepend a default value - * @return array - */ - public function getColumnsList($project_id, $prepend = false) - { - $listing = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'title'); - return $prepend ? array(-1 => t('All columns')) + $listing : $listing; - } - - /** - * Get all columns sorted by position for a given project - * - * @access public - * @param integer $project_id Project id - * @return array - */ - public function getColumns($project_id) - { - return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findAll(); - } - - /** - * Get the number of columns for a given project - * - * @access public - * @param integer $project_id Project id - * @return integer - */ - public function countColumns($project_id) - { - return $this->db->table(self::TABLE)->eq('project_id', $project_id)->count(); - } - - /** - * Get a column by the id - * - * @access public - * @param integer $column_id Column id - * @return array - */ - public function getColumn($column_id) - { - return $this->db->table(self::TABLE)->eq('id', $column_id)->findOne(); - } - - /** - * Get a column id by the name - * - * @access public - * @param integer $project_id - * @param string $title - * @return integer - */ - public function getColumnIdByTitle($project_id, $title) - { - return (int) $this->db->table(self::TABLE)->eq('project_id', $project_id)->eq('title', $title)->findOneColumn('id'); - } - - /** - * Get a column title by the id - * - * @access public - * @param integer $column_id - * @return integer - */ - public function getColumnTitleById($column_id) - { - return $this->db->table(self::TABLE)->eq('id', $column_id)->findOneColumn('title'); - } - - /** - * Get the position of the last column for a given project - * - * @access public - * @param integer $project_id Project id - * @return integer - */ - public function getLastColumnPosition($project_id) - { - return (int) $this->db - ->table(self::TABLE) - ->eq('project_id', $project_id) - ->desc('position') - ->findOneColumn('position'); - } - - /** - * Remove a column and all tasks associated to this column - * - * @access public - * @param integer $column_id Column id - * @return boolean - */ - public function removeColumn($column_id) - { - return $this->db->table(self::TABLE)->eq('id', $column_id)->remove(); - } } diff --git a/app/Model/Column.php b/app/Model/Column.php index 286b6140..ccdcb049 100644 --- a/app/Model/Column.php +++ b/app/Model/Column.php @@ -17,6 +17,83 @@ class Column extends Base */ const TABLE = 'columns'; + /** + * Get a column by the id + * + * @access public + * @param integer $column_id Column id + * @return array + */ + public function getById($column_id) + { + return $this->db->table(self::TABLE)->eq('id', $column_id)->findOne(); + } + + /** + * Get the first column id for a given project + * + * @access public + * @param integer $project_id Project id + * @return integer + */ + public function getFirstColumnId($project_id) + { + return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findOneColumn('id'); + } + + /** + * Get the last column id for a given project + * + * @access public + * @param integer $project_id Project id + * @return integer + */ + public function getLastColumnId($project_id) + { + return $this->db->table(self::TABLE)->eq('project_id', $project_id)->desc('position')->findOneColumn('id'); + } + + /** + * Get the position of the last column for a given project + * + * @access public + * @param integer $project_id Project id + * @return integer + */ + public function getLastColumnPosition($project_id) + { + return (int) $this->db + ->table(self::TABLE) + ->eq('project_id', $project_id) + ->desc('position') + ->findOneColumn('position'); + } + + /** + * Get a column id by the name + * + * @access public + * @param integer $project_id + * @param string $title + * @return integer + */ + public function getColumnIdByTitle($project_id, $title) + { + return (int) $this->db->table(self::TABLE)->eq('project_id', $project_id)->eq('title', $title)->findOneColumn('id'); + } + + /** + * Get a column title by the id + * + * @access public + * @param integer $column_id + * @return integer + */ + public function getColumnTitleById($column_id) + { + return $this->db->table(self::TABLE)->eq('id', $column_id)->findOneColumn('title'); + } + /** * Get all columns sorted by position for a given project * @@ -29,6 +106,74 @@ class Column extends Base return $this->db->table(self::TABLE)->eq('project_id', $project_id)->asc('position')->findAll(); } + /** + * Get the list of columns sorted by position [ column_id => title ] + * + * @access public + * @param integer $project_id Project id + * @param boolean $prepend Prepend a default value + * @return array + */ + public function getList($project_id, $prepend = false) + { + $listing = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'title'); + return $prepend ? array(-1 => t('All columns')) + $listing : $listing; + } + + /** + * Add a new column to the board + * + * @access public + * @param integer $project_id Project id + * @param string $title Column title + * @param integer $task_limit Task limit + * @param string $description Column description + * @return boolean|integer + */ + public function create($project_id, $title, $task_limit = 0, $description = '') + { + $values = array( + 'project_id' => $project_id, + 'title' => $title, + 'task_limit' => intval($task_limit), + 'position' => $this->getLastColumnPosition($project_id) + 1, + 'description' => $description, + ); + + return $this->persist(self::TABLE, $values); + } + + /** + * Update a column + * + * @access public + * @param integer $column_id Column id + * @param string $title Column title + * @param integer $task_limit Task limit + * @param string $description Optional description + * @return boolean + */ + public function update($column_id, $title, $task_limit = 0, $description = '') + { + return $this->db->table(self::TABLE)->eq('id', $column_id)->update(array( + 'title' => $title, + 'task_limit' => intval($task_limit), + 'description' => $description, + )); + } + + /** + * Remove a column and all tasks associated to this column + * + * @access public + * @param integer $column_id Column id + * @return boolean + */ + public function remove($column_id) + { + return $this->db->table(self::TABLE)->eq('id', $column_id)->remove(); + } + /** * Change column position * diff --git a/app/Model/Project.php b/app/Model/Project.php index d0a8bfc8..a79e46a1 100644 --- a/app/Model/Project.php +++ b/app/Model/Project.php @@ -241,7 +241,7 @@ class Project extends Base { $stats = array(); $stats['nb_active_tasks'] = 0; - $columns = $this->board->getColumns($project_id); + $columns = $this->column->getAll($project_id); $column_stats = $this->board->getColumnStats($project_id); foreach ($columns as &$column) { @@ -265,7 +265,7 @@ class Project extends Base */ public function getColumnStats(array &$project) { - $project['columns'] = $this->board->getColumns($project['id']); + $project['columns'] = $this->column->getAll($project['id']); $stats = $this->board->getColumnStats($project['id']); foreach ($project['columns'] as &$column) { diff --git a/app/Model/ProjectDailyColumnStats.php b/app/Model/ProjectDailyColumnStats.php index cf79be84..2bcc4d55 100644 --- a/app/Model/ProjectDailyColumnStats.php +++ b/app/Model/ProjectDailyColumnStats.php @@ -84,7 +84,7 @@ class ProjectDailyColumnStats extends Base */ public function getAggregatedMetrics($project_id, $from, $to, $field = 'total') { - $columns = $this->board->getColumnsList($project_id); + $columns = $this->column->getList($project_id); $metrics = $this->getMetrics($project_id, $from, $to); return $this->buildAggregate($metrics, $columns, $field); } diff --git a/app/Model/TaskAnalytic.php b/app/Model/TaskAnalytic.php index bdfec3cb..cff56744 100644 --- a/app/Model/TaskAnalytic.php +++ b/app/Model/TaskAnalytic.php @@ -48,7 +48,7 @@ class TaskAnalytic extends Base public function getTimeSpentByColumn(array $task) { $result = array(); - $columns = $this->board->getColumnsList($task['project_id']); + $columns = $this->column->getList($task['project_id']); $sums = $this->transition->getTimeSpentByTask($task['id']); foreach ($columns as $column_id => $column_title) { diff --git a/app/Model/TaskCreation.php b/app/Model/TaskCreation.php index f7d981fa..576eb18c 100644 --- a/app/Model/TaskCreation.php +++ b/app/Model/TaskCreation.php @@ -56,7 +56,7 @@ class TaskCreation extends Base $this->resetFields($values, array('date_started', 'creator_id', 'owner_id', 'swimlane_id', 'date_due', 'score', 'category_id', 'time_estimated')); if (empty($values['column_id'])) { - $values['column_id'] = $this->board->getFirstColumn($values['project_id']); + $values['column_id'] = $this->column->getFirstColumnId($values['project_id']); } if (empty($values['color_id'])) { diff --git a/app/Model/TaskDuplication.php b/app/Model/TaskDuplication.php index e81fb232..b081aac1 100644 --- a/app/Model/TaskDuplication.php +++ b/app/Model/TaskDuplication.php @@ -64,7 +64,7 @@ class TaskDuplication extends Base if ($values['recurrence_status'] == Task::RECURRING_STATUS_PENDING) { $values['recurrence_parent'] = $task_id; - $values['column_id'] = $this->board->getFirstColumn($values['project_id']); + $values['column_id'] = $this->column->getFirstColumnId($values['project_id']); $this->calculateRecurringTaskDueDate($values); $recurring_task_id = $this->save($task_id, $values); @@ -181,12 +181,12 @@ class TaskDuplication extends Base // Check if the column exists for the destination project if ($values['column_id'] > 0) { - $values['column_id'] = $this->board->getColumnIdByTitle( + $values['column_id'] = $this->column->getColumnIdByTitle( $values['project_id'], - $this->board->getColumnTitleById($values['column_id']) + $this->column->getColumnTitleById($values['column_id']) ); - $values['column_id'] = $values['column_id'] ?: $this->board->getFirstColumn($values['project_id']); + $values['column_id'] = $values['column_id'] ?: $this->column->getFirstColumnId($values['project_id']); } return $values; diff --git a/app/Model/TaskFilter.php b/app/Model/TaskFilter.php index 7ceb4a97..1883298d 100644 --- a/app/Model/TaskFilter.php +++ b/app/Model/TaskFilter.php @@ -469,7 +469,7 @@ class TaskFilter extends Base $this->query->beginOr(); foreach ($values as $project) { - $this->query->ilike(Board::TABLE.'.title', $project); + $this->query->ilike(Column::TABLE.'.title', $project); } $this->query->closeOr(); diff --git a/app/Model/TaskFinder.php b/app/Model/TaskFinder.php index 95ddc12f..0492a9bf 100644 --- a/app/Model/TaskFinder.php +++ b/app/Model/TaskFinder.php @@ -38,14 +38,14 @@ class TaskFinder extends Base Task::TABLE.'.time_spent', Task::TABLE.'.time_estimated', Project::TABLE.'.name AS project_name', - Board::TABLE.'.title AS column_name', + Column::TABLE.'.title AS column_name', User::TABLE.'.username AS assignee_username', User::TABLE.'.name AS assignee_name' ) ->eq(Task::TABLE.'.is_active', $is_active) ->in(Project::TABLE.'.id', $project_ids) ->join(Project::TABLE, 'id', 'project_id') - ->join(Board::TABLE, 'id', 'column_id', Task::TABLE) + ->join(Column::TABLE, 'id', 'column_id', Task::TABLE) ->join(User::TABLE, 'id', 'owner_id', Task::TABLE); } @@ -129,15 +129,15 @@ class TaskFinder extends Base User::TABLE.'.name AS assignee_name', Category::TABLE.'.name AS category_name', Category::TABLE.'.description AS category_description', - Board::TABLE.'.title AS column_name', - Board::TABLE.'.position AS column_position', + Column::TABLE.'.title AS column_name', + Column::TABLE.'.position AS column_position', Swimlane::TABLE.'.name AS swimlane_name', Project::TABLE.'.default_swimlane', Project::TABLE.'.name AS project_name' ) ->join(User::TABLE, 'id', 'owner_id', Task::TABLE) ->join(Category::TABLE, 'id', 'category_id', Task::TABLE) - ->join(Board::TABLE, 'id', 'column_id', Task::TABLE) + ->join(Column::TABLE, 'id', 'column_id', Task::TABLE) ->join(Swimlane::TABLE, 'id', 'swimlane_id', Task::TABLE) ->join(Project::TABLE, 'id', 'project_id', Task::TABLE); } diff --git a/app/Model/TaskImport.php b/app/Model/TaskImport.php index e8dd1946..ccab0152 100644 --- a/app/Model/TaskImport.php +++ b/app/Model/TaskImport.php @@ -111,7 +111,7 @@ class TaskImport extends Base } if (! empty($row['column'])) { - $values['column_id'] = $this->board->getColumnIdByTitle($this->projectId, $row['column']); + $values['column_id'] = $this->column->getColumnIdByTitle($this->projectId, $row['column']); } if (! empty($row['category'])) { diff --git a/app/Model/TaskLink.php b/app/Model/TaskLink.php index 034fcf45..a57bf3b0 100644 --- a/app/Model/TaskLink.php +++ b/app/Model/TaskLink.php @@ -81,17 +81,17 @@ class TaskLink extends Base Task::TABLE.'.owner_id AS task_assignee_id', User::TABLE.'.username AS task_assignee_username', User::TABLE.'.name AS task_assignee_name', - Board::TABLE.'.title AS column_title', + Column::TABLE.'.title AS column_title', Project::TABLE.'.name AS project_name' ) ->eq(self::TABLE.'.task_id', $task_id) ->join(Link::TABLE, 'id', 'link_id') ->join(Task::TABLE, 'id', 'opposite_task_id') - ->join(Board::TABLE, 'id', 'column_id', Task::TABLE) + ->join(Column::TABLE, 'id', 'column_id', Task::TABLE) ->join(User::TABLE, 'id', 'owner_id', Task::TABLE) ->join(Project::TABLE, 'id', 'project_id', Task::TABLE) ->asc(Link::TABLE.'.id') - ->desc(Board::TABLE.'.position') + ->desc(Column::TABLE.'.position') ->desc(Task::TABLE.'.is_active') ->asc(Task::TABLE.'.position') ->asc(Task::TABLE.'.id') diff --git a/app/Model/Transition.php b/app/Model/Transition.php index b1f8f678..aa76d58f 100644 --- a/app/Model/Transition.php +++ b/app/Model/Transition.php @@ -76,8 +76,8 @@ class Transition extends Base ->eq('task_id', $task_id) ->desc('date') ->join(User::TABLE, 'id', 'user_id') - ->join(Board::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src') - ->join(Board::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst') + ->join(Column::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src') + ->join(Column::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst') ->findAll(); } @@ -118,8 +118,8 @@ class Transition extends Base ->desc('date') ->join(Task::TABLE, 'id', 'task_id') ->join(User::TABLE, 'id', 'user_id') - ->join(Board::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src') - ->join(Board::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst') + ->join(Column::TABLE.' as src', 'id', 'src_column_id', self::TABLE, 'src') + ->join(Column::TABLE.' as dst', 'id', 'dst_column_id', self::TABLE, 'dst') ->findAll(); } diff --git a/app/Subscriber/RecurringTaskSubscriber.php b/app/Subscriber/RecurringTaskSubscriber.php index 6d5aee89..09a5665a 100644 --- a/app/Subscriber/RecurringTaskSubscriber.php +++ b/app/Subscriber/RecurringTaskSubscriber.php @@ -21,9 +21,9 @@ class RecurringTaskSubscriber extends BaseSubscriber implements EventSubscriberI $this->logger->debug('Subscriber executed: '.__METHOD__); if ($event['recurrence_status'] == Task::RECURRING_STATUS_PENDING) { - if ($event['recurrence_trigger'] == Task::RECURRING_TRIGGER_FIRST_COLUMN && $this->board->getFirstColumn($event['project_id']) == $event['src_column_id']) { + if ($event['recurrence_trigger'] == Task::RECURRING_TRIGGER_FIRST_COLUMN && $this->column->getFirstColumnId($event['project_id']) == $event['src_column_id']) { $this->taskDuplication->duplicateRecurringTask($event['task_id']); - } elseif ($event['recurrence_trigger'] == Task::RECURRING_TRIGGER_LAST_COLUMN && $this->board->getLastColumn($event['project_id']) == $event['dst_column_id']) { + } elseif ($event['recurrence_trigger'] == Task::RECURRING_TRIGGER_LAST_COLUMN && $this->column->getLastColumnId($event['project_id']) == $event['dst_column_id']) { $this->taskDuplication->duplicateRecurringTask($event['task_id']); } } diff --git a/jsonrpc.php b/jsonrpc.php index 1d59d4ea..d2163347 100644 --- a/jsonrpc.php +++ b/jsonrpc.php @@ -8,6 +8,7 @@ use Kanboard\Api\Me; use Kanboard\Api\Action; use Kanboard\Api\App; use Kanboard\Api\Board; +use Kanboard\Api\Column; use Kanboard\Api\Category; use Kanboard\Api\Comment; use Kanboard\Api\File; @@ -30,6 +31,7 @@ $server->attach(new Me($container)); $server->attach(new Action($container)); $server->attach(new App($container)); $server->attach(new Board($container)); +$server->attach(new Column($container)); $server->attach(new Category($container)); $server->attach(new Comment($container)); $server->attach(new File($container)); diff --git a/tests/integration/ApiTest.php b/tests/integration/ApiTest.php index 8b970a6c..679238a2 100644 --- a/tests/integration/ApiTest.php +++ b/tests/integration/ApiTest.php @@ -170,76 +170,6 @@ class Api extends PHPUnit_Framework_TestCase $this->assertCount(0, $activities); } - public function testGetBoard() - { - $board = $this->client->getBoard(1); - $this->assertTrue(is_array($board)); - $this->assertEquals(1, count($board)); - $this->assertEquals('Default swimlane', $board[0]['name']); - $this->assertEquals(4, count($board[0]['columns'])); - } - - public function testGetColumns() - { - $columns = $this->client->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals(4, count($columns)); - $this->assertEquals('Done', $columns[3]['title']); - } - - public function testMoveColumnUp() - { - $this->assertTrue($this->client->moveColumnUp(1, 4)); - - $columns = $this->client->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals('Done', $columns[2]['title']); - $this->assertEquals('Work in progress', $columns[3]['title']); - } - - public function testMoveColumnDown() - { - $this->assertTrue($this->client->moveColumnDown(1, 4)); - - $columns = $this->client->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals('Work in progress', $columns[2]['title']); - $this->assertEquals('Done', $columns[3]['title']); - } - - public function testUpdateColumn() - { - $this->assertTrue($this->client->updateColumn(4, 'Boo', 2)); - - $columns = $this->client->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals('Boo', $columns[3]['title']); - $this->assertEquals(2, $columns[3]['task_limit']); - } - - public function testAddColumn() - { - $column_id = $this->client->addColumn(1, 'New column'); - - $this->assertNotFalse($column_id); - $this->assertInternalType('int', $column_id); - $this->assertTrue($column_id > 0); - - $columns = $this->client->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals(5, count($columns)); - $this->assertEquals('New column', $columns[4]['title']); - } - - public function testRemoveColumn() - { - $this->assertTrue($this->client->removeColumn(5)); - - $columns = $this->client->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals(4, count($columns)); - } - public function testGetDefaultSwimlane() { $swimlane = $this->client->getDefaultSwimlane(1); diff --git a/tests/integration/BoardTest.php b/tests/integration/BoardTest.php new file mode 100644 index 00000000..bf8d50b9 --- /dev/null +++ b/tests/integration/BoardTest.php @@ -0,0 +1,21 @@ +assertEquals(1, $this->app->createProject('A project')); + } + + public function testGetBoard() + { + $board = $this->app->getBoard(1); + $this->assertCount(1, $board); + $this->assertEquals('Default swimlane', $board[0]['name']); + + $this->assertCount(4, $board[0]['columns']); + $this->assertEquals('Ready', $board[0]['columns'][1]['title']); + } +} diff --git a/tests/integration/ColumnTest.php b/tests/integration/ColumnTest.php new file mode 100644 index 00000000..6d02afc0 --- /dev/null +++ b/tests/integration/ColumnTest.php @@ -0,0 +1,65 @@ +assertEquals(1, $this->app->createProject('A project')); + } + + public function testGetColumns() + { + $columns = $this->app->getColumns($this->getProjectId()); + $this->assertCount(4, $columns); + $this->assertEquals('Done', $columns[3]['title']); + } + + public function testUpdateColumn() + { + $this->assertTrue($this->app->updateColumn(4, 'Boo', 2)); + + $columns = $this->app->getColumns($this->getProjectId()); + $this->assertEquals('Boo', $columns[3]['title']); + $this->assertEquals(2, $columns[3]['task_limit']); + } + + public function testAddColumn() + { + $column_id = $this->app->addColumn($this->getProjectId(), 'New column'); + + $this->assertNotFalse($column_id); + $this->assertInternalType('int', $column_id); + $this->assertTrue($column_id > 0); + + $columns = $this->app->getColumns($this->getProjectId()); + $this->assertCount(5, $columns); + $this->assertEquals('New column', $columns[4]['title']); + } + + public function testRemoveColumn() + { + $this->assertTrue($this->app->removeColumn(5)); + + $columns = $this->app->getColumns($this->getProjectId()); + $this->assertCount(4, $columns); + } + + public function testChangeColumnPosition() + { + $this->assertTrue($this->app->changeColumnPosition($this->getProjectId(), 1, 3)); + + $columns = $this->app->getColumns($this->getProjectId()); + $this->assertCount(4, $columns); + + $this->assertEquals('Ready', $columns[0]['title']); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals('Work in progress', $columns[1]['title']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals('Backlog', $columns[2]['title']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals('Boo', $columns[3]['title']); + $this->assertEquals(4, $columns[3]['position']); + } +} diff --git a/tests/units/Model/ActionTest.php b/tests/units/Model/ActionTest.php index 8d574115..ed687846 100644 --- a/tests/units/Model/ActionTest.php +++ b/tests/units/Model/ActionTest.php @@ -6,7 +6,7 @@ use Kanboard\Model\Action; use Kanboard\Model\Project; use Kanboard\Model\Task; use Kanboard\Model\User; -use Kanboard\Model\Board; +use Kanboard\Model\Column; use Kanboard\Model\Category; use Kanboard\Model\ProjectUserRole; use Kanboard\Core\Security\Role; @@ -260,12 +260,12 @@ class ActionTest extends Base { $projectModel = new Project($this->container); $actionModel = new Action($this->container); - $boardModel = new Board($this->container); + $columnModel = new Column($this->container); $this->assertEquals(1, $projectModel->create(array('name' => 'test1'))); $this->assertEquals(2, $projectModel->create(array('name' => 'test2'))); - $this->assertTrue($boardModel->updateColumn(2, 'My unique column')); + $this->assertTrue($columnModel->update(2, 'My unique column')); $this->assertEquals(1, $actionModel->create(array( 'project_id' => 1, diff --git a/tests/units/Model/BoardTest.php b/tests/units/Model/BoardTest.php index bb6c4b76..bb0778ce 100644 --- a/tests/units/Model/BoardTest.php +++ b/tests/units/Model/BoardTest.php @@ -4,6 +4,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Project; use Kanboard\Model\Board; +use Kanboard\Model\Column; use Kanboard\Model\Config; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskFinder; @@ -15,12 +16,13 @@ class BoardTest extends Base { $p = new Project($this->container); $b = new Board($this->container); + $columnModel = new Column($this->container); $c = new Config($this->container); // Default columns $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $columns = $b->getColumnsList(1); + $columns = $columnModel->getList(1); $this->assertTrue(is_array($columns)); $this->assertEquals(4, count($columns)); @@ -37,7 +39,7 @@ class BoardTest extends Base $this->assertEquals($input, $c->get('board_columns')); $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); - $columns = $b->getColumnsList(2); + $columns = $columnModel->getList(2); $this->assertTrue(is_array($columns)); $this->assertEquals(2, count($columns)); @@ -161,225 +163,4 @@ class BoardTest extends Base $this->assertEquals(1, $board[1]['columns'][3]['tasks'][0]['position']); $this->assertEquals(1, $board[1]['columns'][3]['tasks'][0]['swimlane_id']); } - - public function testGetColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - $column = $b->getColumn(3); - $this->assertNotEmpty($column); - $this->assertEquals('Work in progress', $column['title']); - - $column = $b->getColumn(33); - $this->assertEmpty($column); - } - - public function testRemoveColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertTrue($b->removeColumn(3)); - $this->assertFalse($b->removeColumn(322)); - - $columns = $b->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals(3, count($columns)); - } - - public function testUpdateColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - $this->assertTrue($b->updateColumn(3, 'blah', 5)); - $this->assertTrue($b->updateColumn(2, 'boo')); - - $column = $b->getColumn(3); - $this->assertNotEmpty($column); - $this->assertEquals('blah', $column['title']); - $this->assertEquals(5, $column['task_limit']); - - $column = $b->getColumn(2); - $this->assertNotEmpty($column); - $this->assertEquals('boo', $column['title']); - $this->assertEquals(0, $column['task_limit']); - } - - public function testAddColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertNotFalse($b->addColumn(1, 'another column')); - $this->assertNotFalse($b->addColumn(1, 'one more', 3, 'one more description')); - - $columns = $b->getColumns(1); - $this->assertTrue(is_array($columns)); - $this->assertEquals(6, count($columns)); - - $this->assertEquals('another column', $columns[4]['title']); - $this->assertEquals(0, $columns[4]['task_limit']); - $this->assertEquals(5, $columns[4]['position']); - - $this->assertEquals('one more', $columns[5]['title']); - $this->assertEquals(3, $columns[5]['task_limit']); - $this->assertEquals(6, $columns[5]['position']); - $this->assertEquals('one more description', $columns[5]['description']); - } - - public function testMoveColumns() - { - $p = new Project($this->container); - $b = new Board($this->container); - - // We create 2 projects - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - $this->assertEquals(2, $p->create(array('name' => 'UnitTest2'))); - - // We get the columns of the project 2 - $columns = $b->getColumns(2); - $columns_id = array_keys($b->getColumnsList(2)); - $this->assertNotEmpty($columns); - - // Initial order: 5, 6, 7, 8 - - // Move the column 1 down - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[0], $columns[0]['id']); - - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals($columns_id[1], $columns[1]['id']); - - $this->assertTrue($b->moveDown(2, $columns[0]['id'])); - $columns = $b->getColumns(2); // Sorted by position - - // New order: 6, 5, 7, 8 - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[1], $columns[0]['id']); - - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals($columns_id[0], $columns[1]['id']); - - // Move the column 3 up - $this->assertTrue($b->moveUp(2, $columns[2]['id'])); - $columns = $b->getColumns(2); - - // New order: 6, 7, 5, 8 - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[1], $columns[0]['id']); - - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals($columns_id[2], $columns[1]['id']); - - $this->assertEquals(3, $columns[2]['position']); - $this->assertEquals($columns_id[0], $columns[2]['id']); - - // Move column 1 up (must do nothing because it's the first column) - $this->assertFalse($b->moveUp(2, $columns[0]['id'])); - $columns = $b->getColumns(2); - - // Order: 6, 7, 5, 8 - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals($columns_id[1], $columns[0]['id']); - - // Move column 4 down (must do nothing because it's the last column) - $this->assertFalse($b->moveDown(2, $columns[3]['id'])); - $columns = $b->getColumns(2); - - // Order: 6, 7, 5, 8 - - $this->assertEquals(4, $columns[3]['position']); - $this->assertEquals($columns_id[3], $columns[3]['id']); - } - - public function testMoveUpAndRemoveColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - // We remove the second column - $this->assertTrue($b->removeColumn(2)); - - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(3, $columns[1]['position']); - $this->assertEquals(4, $columns[2]['position']); - - $this->assertEquals(1, $columns[0]['id']); - $this->assertEquals(3, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - - // We move up the second column - $this->assertTrue($b->moveUp(1, $columns[1]['id'])); - - // Check the new positions - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals(3, $columns[2]['position']); - - $this->assertEquals(3, $columns[0]['id']); - $this->assertEquals(1, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - } - - public function testMoveDownAndRemoveColumn() - { - $p = new Project($this->container); - $b = new Board($this->container); - - // We create a project - $this->assertEquals(1, $p->create(array('name' => 'UnitTest1'))); - - // We remove the second column - $this->assertTrue($b->removeColumn(2)); - - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(3, $columns[1]['position']); - $this->assertEquals(4, $columns[2]['position']); - - $this->assertEquals(1, $columns[0]['id']); - $this->assertEquals(3, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - - // We move up the second column - $this->assertTrue($b->moveDown(1, $columns[0]['id'])); - - // Check the new positions - $columns = $b->getColumns(1); - $this->assertNotEmpty($columns); - $this->assertCount(3, $columns); - - $this->assertEquals(1, $columns[0]['position']); - $this->assertEquals(2, $columns[1]['position']); - $this->assertEquals(3, $columns[2]['position']); - - $this->assertEquals(3, $columns[0]['id']); - $this->assertEquals(1, $columns[1]['id']); - $this->assertEquals(4, $columns[2]['id']); - } } diff --git a/tests/units/Model/ColumnTest.php b/tests/units/Model/ColumnTest.php index a03c3717..e40f89c6 100644 --- a/tests/units/Model/ColumnTest.php +++ b/tests/units/Model/ColumnTest.php @@ -7,6 +7,174 @@ use Kanboard\Model\Column; class ColumnTest extends Base { + public function testGetColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $column = $columnModel->getById(3); + $this->assertNotEmpty($column); + $this->assertEquals('Work in progress', $column['title']); + + $column = $columnModel->getById(33); + $this->assertEmpty($column); + } + + public function testGetFirstColumnId() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(1, $columnModel->getFirstColumnId(1)); + } + + public function testGetLastColumnId() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(4, $columnModel->getLastColumnId(1)); + } + + public function testGetLastColumnPosition() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(4, $columnModel->getLastColumnPosition(1)); + } + + public function testGetColumnIdByTitle() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals(2, $columnModel->getColumnIdByTitle(1, 'Ready')); + } + + public function testGetTitleByColumnId() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertEquals('Work in progress', $columnModel->getColumnTitleById(3)); + } + + public function testGetAll() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $columns = $columnModel->getAll(1); + $this->assertCount(4, $columns); + + $this->assertEquals(1, $columns[0]['id']); + $this->assertEquals(1, $columns[0]['position']); + $this->assertEquals('Backlog', $columns[0]['title']); + + $this->assertEquals(2, $columns[1]['id']); + $this->assertEquals(2, $columns[1]['position']); + $this->assertEquals('Ready', $columns[1]['title']); + + $this->assertEquals(3, $columns[2]['id']); + $this->assertEquals(3, $columns[2]['position']); + $this->assertEquals('Work in progress', $columns[2]['title']); + + $this->assertEquals(4, $columns[3]['id']); + $this->assertEquals(4, $columns[3]['position']); + $this->assertEquals('Done', $columns[3]['title']); + } + + public function testGetList() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $columns = $columnModel->getList(1); + $this->assertCount(4, $columns); + $this->assertEquals('Backlog', $columns[1]); + $this->assertEquals('Ready', $columns[2]); + $this->assertEquals('Work in progress', $columns[3]); + $this->assertEquals('Done', $columns[4]); + + $columns = $columnModel->getList(1, true); + $this->assertCount(5, $columns); + $this->assertEquals('All columns', $columns[-1]); + $this->assertEquals('Backlog', $columns[1]); + $this->assertEquals('Ready', $columns[2]); + $this->assertEquals('Work in progress', $columns[3]); + $this->assertEquals('Done', $columns[4]); + } + + public function testAddColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertNotFalse($columnModel->create(1, 'another column')); + $this->assertNotFalse($columnModel->create(1, 'one more', 3, 'one more description')); + + $columns = $columnModel->getAll(1); + $this->assertTrue(is_array($columns)); + $this->assertEquals(6, count($columns)); + + $this->assertEquals('another column', $columns[4]['title']); + $this->assertEquals(0, $columns[4]['task_limit']); + $this->assertEquals(5, $columns[4]['position']); + + $this->assertEquals('one more', $columns[5]['title']); + $this->assertEquals(3, $columns[5]['task_limit']); + $this->assertEquals(6, $columns[5]['position']); + $this->assertEquals('one more description', $columns[5]['description']); + } + + public function testUpdateColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + + $this->assertTrue($columnModel->update(3, 'blah', 5)); + $this->assertTrue($columnModel->update(2, 'boo')); + + $column = $columnModel->getById(3); + $this->assertNotEmpty($column); + $this->assertEquals('blah', $column['title']); + $this->assertEquals(5, $column['task_limit']); + + $column = $columnModel->getById(2); + $this->assertNotEmpty($column); + $this->assertEquals('boo', $column['title']); + $this->assertEquals(0, $column['task_limit']); + } + + public function testRemoveColumn() + { + $projectModel = new Project($this->container); + $columnModel = new Column($this->container); + + $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest'))); + $this->assertTrue($columnModel->remove(3)); + $this->assertFalse($columnModel->remove(322)); + + $columns = $columnModel->getAll(1); + $this->assertTrue(is_array($columns)); + $this->assertEquals(3, count($columns)); + } + public function testChangePosition() { $projectModel = new Project($this->container); diff --git a/tests/units/Model/ProjectTest.php b/tests/units/Model/ProjectTest.php index cadb42a6..5478fa40 100644 --- a/tests/units/Model/ProjectTest.php +++ b/tests/units/Model/ProjectTest.php @@ -8,8 +8,6 @@ use Kanboard\Model\Project; use Kanboard\Model\User; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; -use Kanboard\Model\Acl; -use Kanboard\Model\Board; use Kanboard\Model\Config; use Kanboard\Model\Category; diff --git a/tests/units/Model/TaskPositionTest.php b/tests/units/Model/TaskPositionTest.php index 5f045768..28145a66 100644 --- a/tests/units/Model/TaskPositionTest.php +++ b/tests/units/Model/TaskPositionTest.php @@ -3,7 +3,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\Task; -use Kanboard\Model\Board; +use Kanboard\Model\Column; use Kanboard\Model\TaskStatus; use Kanboard\Model\TaskPosition; use Kanboard\Model\TaskCreation; @@ -21,23 +21,23 @@ class TaskPositionTest extends Base $tc = new TaskCreation($this->container); $tf = new TaskFinder($this->container); $p = new Project($this->container); - $b = new Board($this->container); + $columnModel = new Column($this->container); $this->assertEquals(1, $p->create(array('name' => 'Project #1'))); $this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'column_id' => 1))); - $this->assertEquals(0, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(0, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($tp->movePosition(1, 1, 2, 1)); - $this->assertEquals(25, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(25, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($tp->movePosition(1, 1, 3, 1)); - $this->assertEquals(50, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(50, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($tp->movePosition(1, 1, 4, 1)); - $this->assertEquals(75, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(75, $t->getProgress($tf->getById(1), $columnModel->getList(1))); $this->assertTrue($ts->close(1)); - $this->assertEquals(100, $t->getProgress($tf->getById(1), $b->getColumnsList(1))); + $this->assertEquals(100, $t->getProgress($tf->getById(1), $columnModel->getList(1))); } public function testMoveTaskToWrongPosition() -- cgit v1.2.3