Add unit tests for last automatic actions

This commit is contained in:
Frederic Guillot 2016-01-31 19:51:59 -05:00
parent 505f62e6ca
commit 0bc51620c7
9 changed files with 247 additions and 93 deletions

View File

@ -2,8 +2,6 @@
namespace Kanboard\Controller;
use Kanboard\Model\Project as ProjectModel;
/**
* Task Duplication controller
*

View File

@ -193,11 +193,12 @@ class Category extends Base
*/
public function duplicate($src_project_id, $dst_project_id)
{
$categories = $this->db->table(self::TABLE)
->columns('name')
->eq('project_id', $src_project_id)
->asc('name')
->findAll();
$categories = $this->db
->table(self::TABLE)
->columns('name')
->eq('project_id', $src_project_id)
->asc('name')
->findAll();
foreach ($categories as $category) {
$category['project_id'] = $dst_project_id;

View File

@ -39,14 +39,14 @@ class LastLogin extends Base
$this->cleanup($user_id);
return $this->db
->table(self::TABLE)
->insert(array(
'auth_type' => $auth_type,
'user_id' => $user_id,
'ip' => $ip,
'user_agent' => substr($user_agent, 0, 255),
'date_creation' => time(),
));
->table(self::TABLE)
->insert(array(
'auth_type' => $auth_type,
'user_id' => $user_id,
'ip' => $ip,
'user_agent' => substr($user_agent, 0, 255),
'date_creation' => time(),
));
}
/**

View File

@ -17,7 +17,7 @@ class ProjectDuplication extends Base
* Get list of optional models to duplicate
*
* @access public
* @return array
* @return string[]
*/
public function getOptionalSelection()
{
@ -28,7 +28,7 @@ class ProjectDuplication extends Base
* Get list of all possible models to duplicate
*
* @access public
* @return array
* @return string[]
*/
public function getPossibleSelection()
{
@ -71,7 +71,7 @@ class ProjectDuplication extends Base
// Get the cloned project Id
$dst_project_id = $this->copy($src_project_id, $owner_id, $name, $private);
if (! $dst_project_id) {
if ($dst_project_id === false) {
$this->db->cancelTransaction();
return false;
}

View File

@ -435,10 +435,10 @@ class Subtask extends Base
return $this->db->transaction(function (Database $db) use ($src_task_id, $dst_task_id) {
$subtasks = $db->table(Subtask::TABLE)
->columns('title', 'time_estimated', 'position')
->eq('task_id', $src_task_id)
->asc('position')
->findAll();
->columns('title', 'time_estimated', 'position')
->eq('task_id', $src_task_id)
->asc('position')
->findAll();
foreach ($subtasks as &$subtask) {
$subtask['task_id'] = $dst_task_id;

View File

@ -302,11 +302,11 @@ class SubtaskTimeTracking extends Base
{
$hook = 'model:subtask-time-tracking:calculate:time-spent';
$start_time = $this->db
->table(self::TABLE)
->eq('subtask_id', $subtask_id)
->eq('user_id', $user_id)
->eq('end', 0)
->findOneColumn('start');
->table(self::TABLE)
->eq('subtask_id', $subtask_id)
->eq('user_id', $user_id)
->eq('end', 0)
->findOneColumn('start');
if (empty($start_time)) {
return 0;

View File

@ -96,10 +96,11 @@ class Swimlane extends Base
*/
public function getDefault($project_id)
{
$result = $this->db->table(Project::TABLE)
->eq('id', $project_id)
->columns('id', 'default_swimlane', 'show_default_swimlane')
->findOne();
$result = $this->db
->table(Project::TABLE)
->eq('id', $project_id)
->columns('id', 'default_swimlane', 'show_default_swimlane')
->findOne();
if ($result['default_swimlane'] === 'Default swimlane') {
$result['default_swimlane'] = t($result['default_swimlane']);
@ -117,10 +118,11 @@ class Swimlane extends Base
*/
public function getAll($project_id)
{
return $this->db->table(self::TABLE)
->eq('project_id', $project_id)
->orderBy('position', 'asc')
->findAll();
return $this->db
->table(self::TABLE)
->eq('project_id', $project_id)
->orderBy('position', 'asc')
->findAll();
}
/**
@ -133,9 +135,10 @@ class Swimlane extends Base
*/
public function getAllByStatus($project_id, $status = self::ACTIVE)
{
$query = $this->db->table(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', $status);
$query = $this->db
->table(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', $status);
if ($status == self::ACTIVE) {
$query->asc('position');
@ -155,17 +158,19 @@ class Swimlane extends Base
*/
public function getSwimlanes($project_id)
{
$swimlanes = $this->db->table(self::TABLE)
->columns('id', 'name', 'description')
->eq('project_id', $project_id)
->eq('is_active', self::ACTIVE)
->orderBy('position', 'asc')
->findAll();
$swimlanes = $this->db
->table(self::TABLE)
->columns('id', 'name', 'description')
->eq('project_id', $project_id)
->eq('is_active', self::ACTIVE)
->orderBy('position', 'asc')
->findAll();
$default_swimlane = $this->db->table(Project::TABLE)
->eq('id', $project_id)
->eq('show_default_swimlane', 1)
->findOneColumn('default_swimlane');
$default_swimlane = $this->db
->table(Project::TABLE)
->eq('id', $project_id)
->eq('show_default_swimlane', 1)
->findOneColumn('default_swimlane');
if ($default_swimlane) {
if ($default_swimlane === 'Default swimlane') {
@ -200,11 +205,12 @@ class Swimlane extends Base
$swimlanes[0] = $default === 'Default swimlane' ? t($default) : $default;
}
return $swimlanes + $this->db->hashtable(self::TABLE)
->eq('project_id', $project_id)
->in('is_active', $only_active ? array(self::ACTIVE) : array(self::ACTIVE, self::INACTIVE))
->orderBy('position', 'asc')
->getAll('id', 'name');
return $swimlanes + $this->db
->hashtable(self::TABLE)
->eq('project_id', $project_id)
->in('is_active', $only_active ? array(self::ACTIVE) : array(self::ACTIVE, self::INACTIVE))
->orderBy('position', 'asc')
->getAll('id', 'name');
}
/**
@ -232,9 +238,10 @@ class Swimlane extends Base
*/
public function update(array $values)
{
return $this->db->table(self::TABLE)
->eq('id', $values['id'])
->update($values);
return $this->db
->table(self::TABLE)
->eq('id', $values['id'])
->update($values);
}
/**
@ -247,12 +254,12 @@ class Swimlane extends Base
public function updateDefault(array $values)
{
return $this->db
->table(Project::TABLE)
->eq('id', $values['id'])
->update(array(
'default_swimlane' => $values['default_swimlane'],
'show_default_swimlane' => $values['show_default_swimlane'],
));
->table(Project::TABLE)
->eq('id', $values['id'])
->update(array(
'default_swimlane' => $values['default_swimlane'],
'show_default_swimlane' => $values['show_default_swimlane'],
));
}
/**
@ -264,10 +271,11 @@ class Swimlane extends Base
*/
public function getLastPosition($project_id)
{
return $this->db->table(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', 1)
->count() + 1;
return $this->db
->table(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', 1)
->count() + 1;
}
/**
@ -281,12 +289,12 @@ class Swimlane extends Base
public function disable($project_id, $swimlane_id)
{
$result = $this->db
->table(self::TABLE)
->eq('id', $swimlane_id)
->update(array(
'is_active' => self::INACTIVE,
'position' => 0,
));
->table(self::TABLE)
->eq('id', $swimlane_id)
->update(array(
'is_active' => self::INACTIVE,
'position' => 0,
));
if ($result) {
// Re-order positions
@ -307,12 +315,12 @@ class Swimlane extends Base
public function enable($project_id, $swimlane_id)
{
return $this->db
->table(self::TABLE)
->eq('id', $swimlane_id)
->update(array(
'is_active' => self::ACTIVE,
'position' => $this->getLastPosition($project_id),
));
->table(self::TABLE)
->eq('id', $swimlane_id)
->update(array(
'is_active' => self::ACTIVE,
'position' => $this->getLastPosition($project_id),
));
}
/**
@ -353,11 +361,12 @@ class Swimlane extends Base
public function updatePositions($project_id)
{
$position = 0;
$swimlanes = $this->db->table(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', 1)
->asc('position')
->findAllByColumn('id');
$swimlanes = $this->db
->table(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', 1)
->asc('position')
->findAllByColumn('id');
if (! $swimlanes) {
return false;
@ -365,8 +374,8 @@ class Swimlane extends Base
foreach ($swimlanes as $swimlane_id) {
$this->db->table(self::TABLE)
->eq('id', $swimlane_id)
->update(array('position' => ++$position));
->eq('id', $swimlane_id)
->update(array('position' => ++$position));
}
return true;
@ -383,10 +392,10 @@ class Swimlane extends Base
public function moveDown($project_id, $swimlane_id)
{
$swimlanes = $this->db->hashtable(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', self::ACTIVE)
->asc('position')
->getAll('id', 'position');
->eq('project_id', $project_id)
->eq('is_active', self::ACTIVE)
->asc('position')
->getAll('id', 'position');
$positions = array_flip($swimlanes);
@ -416,10 +425,10 @@ class Swimlane extends Base
public function moveUp($project_id, $swimlane_id)
{
$swimlanes = $this->db->hashtable(self::TABLE)
->eq('project_id', $project_id)
->eq('is_active', self::ACTIVE)
->asc('position')
->getAll('id', 'position');
->eq('project_id', $project_id)
->eq('is_active', self::ACTIVE)
->asc('position')
->getAll('id', 'position');
$positions = array_flip($swimlanes);

View File

@ -0,0 +1,43 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Event\TaskListEvent;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\TaskFinder;
use Kanboard\Model\Project;
use Kanboard\Model\Task;
use Kanboard\Action\TaskCloseNoActivity;
class TaskCloseNoActivityTest extends Base
{
public function testClose()
{
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$taskFinderModel = new TaskFinder($this->container);
$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_modification' => strtotime('-10days')));
$tasks = $taskFinderModel->getAll(1);
$event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1));
$action = new TaskCloseNoActivity($this->container);
$action->setProjectId(1);
$action->setParam('duration', 2);
$this->assertTrue($action->execute($event, Task::EVENT_DAILY_CRONJOB));
$task = $taskFinderModel->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['is_active']);
$task = $taskFinderModel->getById(2);
$this->assertNotEmpty($task);
$this->assertEquals(1, $task['is_active']);
}
}

View File

@ -0,0 +1,103 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Event\TaskListEvent;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\TaskFinder;
use Kanboard\Model\Project;
use Kanboard\Model\Task;
use Kanboard\Model\User;
use Kanboard\Action\TaskEmailNoActivity;
class TaskEmailNoActivityTest extends Base
{
public function testSendEmail()
{
$userModel = new User($this->container);
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$taskFinderModel = new TaskFinder($this->container);
$this->assertEquals(2, $userModel->create(array('username' => 'test', 'email' => 'chuck@norris', 'name' => 'Chuck Norris')));
$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_modification' => strtotime('-10days')));
$tasks = $taskFinderModel->getAll(1);
$event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1));
$action = new TaskEmailNoActivity($this->container);
$action->setProjectId(1);
$action->setParam('user_id', 2);
$action->setParam('subject', 'Old tasks');
$action->setParam('duration', 2);
$this->container['emailClient']
->expects($this->once())
->method('send')
->with('chuck@norris', 'Chuck Norris', 'Old tasks', $this->anything());
$this->assertTrue($action->execute($event, Task::EVENT_DAILY_CRONJOB));
}
public function testUserWithNoEmail()
{
$userModel = new User($this->container);
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$taskFinderModel = new TaskFinder($this->container);
$this->assertEquals(2, $userModel->create(array('username' => 'test', 'name' => 'Chuck Norris')));
$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_modification' => strtotime('-10days')));
$tasks = $taskFinderModel->getAll(1);
$event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1));
$action = new TaskEmailNoActivity($this->container);
$action->setProjectId(1);
$action->setParam('user_id', 2);
$action->setParam('subject', 'Old tasks');
$action->setParam('duration', 2);
$this->container['emailClient']
->expects($this->never())
->method('send');
$this->assertFalse($action->execute($event, Task::EVENT_DAILY_CRONJOB));
}
public function testTooRecent()
{
$userModel = new User($this->container);
$projectModel = new Project($this->container);
$taskCreationModel = new TaskCreation($this->container);
$taskFinderModel = new TaskFinder($this->container);
$this->assertEquals(2, $userModel->create(array('username' => 'test', 'email' => 'chuck@norris', 'name' => 'Chuck Norris')));
$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')));
$tasks = $taskFinderModel->getAll(1);
$event = new TaskListEvent(array('tasks' => $tasks, 'project_id' => 1));
$action = new TaskEmailNoActivity($this->container);
$action->setProjectId(1);
$action->setParam('user_id', 2);
$action->setParam('subject', 'Old tasks');
$action->setParam('duration', 2);
$this->container['emailClient']
->expects($this->never())
->method('send');
$this->assertFalse($action->execute($event, Task::EVENT_DAILY_CRONJOB));
}
}