Add some tests

This commit is contained in:
Frederic Guillot
2015-09-18 22:37:00 -04:00
parent 1fa72295f2
commit e8228c3975
11 changed files with 208 additions and 87 deletions

View File

@@ -7,7 +7,8 @@ New features:
* Add swimlane description * Add swimlane description
* New plugin system (alpha) * New plugin system (alpha)
* Added Bahasa Indonesia translation * Added Bahasa Indonesia translation
* Api changes: new getMyOverdueTasks, new getOverdueTasksByProject, allow getProjectActivity for user-api, new GetMyProjects * Added API procedures: getMyOverdueTasks, getOverdueTasksByProject and GetMyProjects
* Added user API access for procedure getProjectActivity()
Breaking changes: Breaking changes:

View File

@@ -19,8 +19,8 @@ abstract class Base extends \Core\Base
'getMyActivityStream', 'getMyActivityStream',
'createMyPrivateProject', 'createMyPrivateProject',
'getMyProjectsList', 'getMyProjectsList',
'getMyOverdueTasks',
'getMyProjects', 'getMyProjects',
'getMyOverdueTasks',
); );
private $both_allowed_procedures = array( private $both_allowed_procedures = array(
@@ -70,7 +70,7 @@ abstract class Base extends \Core\Base
} }
} }
protected function formatTask($task) protected function formatTask(array $task)
{ {
if (! empty($task)) { if (! empty($task)) {
$task['url'] = $this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), '', true); $task['url'] = $this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), '', true);
@@ -80,7 +80,7 @@ abstract class Base extends \Core\Base
return $task; return $task;
} }
protected function formatTasks($tasks) protected function formatTasks(array $tasks)
{ {
if (! empty($tasks)) { if (! empty($tasks)) {
foreach ($tasks as &$task) { foreach ($tasks as &$task) {
@@ -91,7 +91,7 @@ abstract class Base extends \Core\Base
return $tasks; return $tasks;
} }
protected function formatProject($project) protected function formatProject(array $project)
{ {
if (! empty($project)) { if (! empty($project)) {
$project['url'] = array( $project['url'] = array(
@@ -104,7 +104,7 @@ abstract class Base extends \Core\Base
return $project; return $project;
} }
protected function formatProjects($projects) protected function formatProjects(array $projects)
{ {
if (! empty($projects)) { if (! empty($projects)) {
foreach ($projects as &$project) { foreach ($projects as &$project) {

View File

@@ -33,7 +33,8 @@ class Me extends Base
public function getMyActivityStream() public function getMyActivityStream()
{ {
return $this->projectActivity->getProjects($this->projectPermission->getActiveMemberProjectIds($this->userSession->getId()), 100); $project_ids = $this->projectPermission->getActiveMemberProjectIds($this->userSession->getId());
return $this->projectActivity->getProjects($project_ids, 100);
} }
public function createMyPrivateProject($name, $description = null) public function createMyPrivateProject($name, $description = null)
@@ -60,6 +61,9 @@ class Me extends Base
public function getMyProjects() public function getMyProjects()
{ {
return $this->formatProjects($this->project->getAllByIds($this->projectPermission->getActiveMemberProjectIds($this->userSession->getId()))); $project_ids = $this->projectPermission->getActiveMemberProjectIds($this->userSession->getId());
$projects = $this->project->getAllByIds($project_ids);
return $this->formatProjects($projects);
} }
} }

View File

@@ -177,11 +177,15 @@ class Project extends Base
* Get all projects with given Ids * Get all projects with given Ids
* *
* @access public * @access public
* @param integer[] $project_ids Projects id * @param integer[] $project_ids
* @return array * @return array
*/ */
public function getAllByIds($project_ids) public function getAllByIds(array $project_ids)
{ {
if (empty($project_ids)) {
return array();
}
return $this->db->table(self::TABLE)->in('id', $project_ids)->asc('name')->findAll(); return $this->db->table(self::TABLE)->in('id', $project_ids)->asc('name')->findAll();
} }

View File

@@ -176,6 +176,34 @@ class TaskFinder extends Base
->findAll(); ->findAll();
} }
/**
* Get overdue tasks query
*
* @access public
* @return \PicoDb\Table
*/
public function getOverdueTasksQuery()
{
return $this->db->table(Task::TABLE)
->columns(
Task::TABLE.'.id',
Task::TABLE.'.title',
Task::TABLE.'.date_due',
Task::TABLE.'.project_id',
Task::TABLE.'.creator_id',
Task::TABLE.'.owner_id',
Project::TABLE.'.name AS project_name',
User::TABLE.'.username AS assignee_username',
User::TABLE.'.name AS assignee_name'
)
->join(Project::TABLE, 'id', 'project_id')
->join(User::TABLE, 'id', 'owner_id')
->eq(Project::TABLE.'.is_active', 1)
->eq(Task::TABLE.'.is_active', 1)
->neq(Task::TABLE.'.date_due', 0)
->lte(Task::TABLE.'.date_due', mktime(23, 59, 59));
}
/** /**
* Get a list of overdue tasks for all projects * Get a list of overdue tasks for all projects
* *
@@ -184,91 +212,31 @@ class TaskFinder extends Base
*/ */
public function getOverdueTasks() public function getOverdueTasks()
{ {
$tasks = $this->db->table(Task::TABLE) return $this->getOverdueTasksQuery()->findAll();
->columns(
Task::TABLE.'.id',
Task::TABLE.'.title',
Task::TABLE.'.date_due',
Task::TABLE.'.project_id',
Task::TABLE.'.creator_id',
Task::TABLE.'.owner_id',
Project::TABLE.'.name AS project_name',
User::TABLE.'.username AS assignee_username',
User::TABLE.'.name AS assignee_name'
)
->join(Project::TABLE, 'id', 'project_id')
->join(User::TABLE, 'id', 'owner_id')
->eq(Project::TABLE.'.is_active', 1)
->eq(Task::TABLE.'.is_active', 1)
->neq(Task::TABLE.'.date_due', 0)
->lte(Task::TABLE.'.date_due', mktime(23, 59, 59))
->findAll();
return $tasks;
} }
/** /**
* Get a list of overdue tasks by project * Get a list of overdue tasks by project
* *
* @access public * @access public
* @param integer $project_id
* @return array * @return array
*/ */
public function getOverdueTasksByProject($project_id) public function getOverdueTasksByProject($project_id)
{ {
$tasks = $this->db->table(Task::TABLE) return $this->getOverdueTasksQuery()->eq(Task::TABLE.'.project_id', $project_id)->findAll();
->columns(
Task::TABLE.'.id',
Task::TABLE.'.title',
Task::TABLE.'.date_due',
Task::TABLE.'.project_id',
Task::TABLE.'.creator_id',
Task::TABLE.'.owner_id',
Project::TABLE.'.name AS project_name',
User::TABLE.'.username AS assignee_username',
User::TABLE.'.name AS assignee_name'
)
->join(Project::TABLE, 'id', 'project_id')
->join(User::TABLE, 'id', 'owner_id')
->eq(Project::TABLE.'.is_active', 1)
->eq(Task::TABLE.'.project_id', $project_id)
->eq(Task::TABLE.'.is_active', 1)
->neq(Task::TABLE.'.date_due', 0)
->lte(Task::TABLE.'.date_due', mktime(23, 59, 59))
->findAll();
return $tasks;
} }
/** /**
* Get a list of overdue tasks by user * Get a list of overdue tasks by user
* *
* @access public * @access public
* @param integer $user_id
* @return array * @return array
*/ */
public function getOverdueTasksByUser($user_id) public function getOverdueTasksByUser($user_id)
{ {
$tasks = $this->db->table(Task::TABLE) return $this->getOverdueTasksQuery()->eq(Task::TABLE.'.owner_id', $user_id)->findAll();
->columns(
Task::TABLE.'.id',
Task::TABLE.'.title',
Task::TABLE.'.date_due',
Task::TABLE.'.project_id',
Task::TABLE.'.creator_id',
Task::TABLE.'.owner_id',
Project::TABLE.'.name AS project_name',
User::TABLE.'.username AS assignee_username',
User::TABLE.'.name AS assignee_name'
)
->join(Project::TABLE, 'id', 'project_id')
->join(User::TABLE, 'id', 'owner_id')
->eq(Project::TABLE.'.is_active', 1)
->eq(Task::TABLE.'.owner_id', $user_id)
->eq(Task::TABLE.'.is_active', 1)
->neq(Task::TABLE.'.date_due', 0)
->lte(Task::TABLE.'.date_due', mktime(23, 59, 59))
->findAll();
return $tasks;
} }
/** /**

View File

@@ -7,7 +7,7 @@
"eluceo/ical": "0.8.0", "eluceo/ical": "0.8.0",
"erusev/parsedown" : "1.5.4", "erusev/parsedown" : "1.5.4",
"fabiang/xmpp" : "0.6.1", "fabiang/xmpp" : "0.6.1",
"fguillot/json-rpc" : "1.0.2", "fguillot/json-rpc" : "1.0.3",
"fguillot/picodb" : "1.0.2", "fguillot/picodb" : "1.0.2",
"fguillot/simpleLogger" : "1.0.0", "fguillot/simpleLogger" : "1.0.0",
"fguillot/simple-validator" : "1.0.0", "fguillot/simple-validator" : "1.0.0",

12
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "10de302e730ebc3076393a3a38d1964f", "hash": "66d81a6b6a317dfb617413cf9d971bc0",
"packages": [ "packages": [
{ {
"name": "christian-riesen/base32", "name": "christian-riesen/base32",
@@ -260,16 +260,16 @@
}, },
{ {
"name": "fguillot/json-rpc", "name": "fguillot/json-rpc",
"version": "v1.0.2", "version": "v1.0.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/fguillot/JsonRPC.git", "url": "https://github.com/fguillot/JsonRPC.git",
"reference": "265cf039c2823f684349de78c0c03a597992bea9" "reference": "0a77cd311783431c851e4c8eed33858663c17277"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/fguillot/JsonRPC/zipball/265cf039c2823f684349de78c0c03a597992bea9", "url": "https://api.github.com/repos/fguillot/JsonRPC/zipball/0a77cd311783431c851e4c8eed33858663c17277",
"reference": "265cf039c2823f684349de78c0c03a597992bea9", "reference": "0a77cd311783431c851e4c8eed33858663c17277",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -292,7 +292,7 @@
], ],
"description": "Simple Json-RPC client/server library that just works", "description": "Simple Json-RPC client/server library that just works",
"homepage": "https://github.com/fguillot/JsonRPC", "homepage": "https://github.com/fguillot/JsonRPC",
"time": "2015-09-12 16:27:13" "time": "2015-09-19 02:27:10"
}, },
{ {
"name": "fguillot/picodb", "name": "fguillot/picodb",

View File

@@ -1055,4 +1055,31 @@ class Api extends PHPUnit_Framework_TestCase
$this->assertEquals('TICKET-1234', $task['reference']); $this->assertEquals('TICKET-1234', $task['reference']);
$this->assertEquals('http://127.0.0.1:8000/?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'], $task['url']); $this->assertEquals('http://127.0.0.1:8000/?controller=task&action=show&task_id='.$task['id'].'&project_id='.$task['project_id'], $task['url']);
} }
public function testCreateOverdueTask()
{
$this->assertNotFalse($this->client->createTask(array(
'title' => 'overdue task',
'project_id' => 1,
'date_due' => date('Y-m-d', strtotime('-2days')),
)));
}
public function testGetOverdueTasksByProject()
{
$tasks = $this->client->getOverdueTasksByProject(1);
$this->assertNotEmpty($tasks);
$this->assertCount(1, $tasks);
$this->assertEquals('overdue task', $tasks[0]['title']);
$this->assertEquals('API test', $tasks[0]['project_name']);
}
public function testGetOverdueTasks()
{
$tasks = $this->client->getOverdueTasks();
$this->assertNotEmpty($tasks);
$this->assertCount(1, $tasks);
$this->assertEquals('overdue task', $tasks[0]['title']);
$this->assertEquals('API test', $tasks[0]['project_name']);
}
} }

View File

@@ -121,6 +121,7 @@ class UserApi extends PHPUnit_Framework_TestCase
{ {
$profile = $this->user->getMe(); $profile = $this->user->getMe();
$this->assertNotEmpty($profile); $this->assertNotEmpty($profile);
$this->assertEquals(2, $profile['id']);
$this->assertEquals('user', $profile['username']); $this->assertEquals('user', $profile['username']);
} }
@@ -138,6 +139,18 @@ class UserApi extends PHPUnit_Framework_TestCase
$this->assertEquals('my project', $projects[2]); $this->assertEquals('my project', $projects[2]);
} }
public function testGetMyProjects()
{
$projects = $this->user->getMyProjects();
$this->assertNotEmpty($projects);
$this->assertCount(1, $projects);
$this->assertEquals(2, $projects[0]['id']);
$this->assertEquals('my project', $projects[0]['name']);
$this->assertNotEmpty($projects[0]['url']['calendar']);
$this->assertNotEmpty($projects[0]['url']['board']);
$this->assertNotEmpty($projects[0]['url']['list']);
}
public function testGetProjectById() public function testGetProjectById()
{ {
$project = $this->user->getProjectById(2); $project = $this->user->getProjectById(2);
@@ -172,6 +185,20 @@ class UserApi extends PHPUnit_Framework_TestCase
$this->user->getTask(2); $this->user->getTask(2);
} }
/**
* @expectedException JsonRPC\AccessDeniedException
*/
public function testGetProjectActivityDenied()
{
$this->user->getProjectActivity(1);
}
public function testGetProjectActivityAllowed()
{
$activity = $this->user->getProjectActivity(2);
$this->assertNotEmpty($activity);
}
public function testGetMyActivityStream() public function testGetMyActivityStream()
{ {
$activity = $this->user->getMyActivityStream(); $activity = $this->user->getMyActivityStream();
@@ -222,4 +249,32 @@ class UserApi extends PHPUnit_Framework_TestCase
{ {
$this->assertNotEmpty($this->user->getBoard(2)); $this->assertNotEmpty($this->user->getBoard(2));
} }
public function testCreateOverdueTask()
{
$this->assertNotFalse($this->user->createTask(array(
'title' => 'overdue task',
'project_id' => 2,
'date_due' => date('Y-m-d', strtotime('-2days')),
'owner_id' => 2,
)));
}
public function testGetMyOverdueTasks()
{
$tasks = $this->user->getMyOverdueTasks();
$this->assertNotEmpty($tasks);
$this->assertCount(1, $tasks);
$this->assertEquals('overdue task', $tasks[0]['title']);
$this->assertEquals('my project', $tasks[0]['project_name']);
}
public function testGetOverdueTasksByProject()
{
$tasks = $this->user->getOverdueTasksByProject(2);
$this->assertNotEmpty($tasks);
$this->assertCount(1, $tasks);
$this->assertEquals('overdue task', $tasks[0]['title']);
$this->assertEquals('my project', $tasks[0]['project_name']);
}
} }

View File

@@ -133,6 +133,17 @@ class ProjectTest extends Base
$this->assertGreaterThan($now, $project['last_modified']); $this->assertGreaterThan($now, $project['last_modified']);
} }
public function testGetAllIds()
{
$p = new Project($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertEmpty($p->getAllByIds(array()));
$this->assertNotEmpty($p->getAllByIds(array(1, 2)));
$this->assertCount(1, $p->getAllByIds(array(1)));
}
public function testIsLastModified() public function testIsLastModified()
{ {
$p = new Project($this->container); $p = new Project($this->container);

View File

@@ -30,4 +30,55 @@ class TaskFinderTest extends Base
$this->assertEquals(1, count($tasks)); $this->assertEquals(1, count($tasks));
$this->assertEquals('Task #1', $tasks[0]['title']); $this->assertEquals('Task #1', $tasks[0]['title']);
} }
public function testGetOverdueTasksByProject()
{
$tc = new TaskCreation($this->container);
$tf = new TaskFinder($this->container);
$p = new Project($this->container);
$this->assertEquals(1, $p->create(array('name' => 'Project #1')));
$this->assertEquals(2, $p->create(array('name' => 'Project #2')));
$this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'date_due' => strtotime('-1 day'))));
$this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 2, 'date_due' => strtotime('-1 day'))));
$this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => strtotime('+1 day'))));
$this->assertEquals(4, $tc->create(array('title' => 'Task #4', 'project_id' => 1, 'date_due' => 0)));
$this->assertEquals(5, $tc->create(array('title' => 'Task #5', 'project_id' => 1)));
$tasks = $tf->getOverdueTasksByProject(1);
$this->assertNotEmpty($tasks);
$this->assertTrue(is_array($tasks));
$this->assertEquals(1, count($tasks));
$this->assertEquals('Task #1', $tasks[0]['title']);
}
public function testGetOverdueTasksByUser()
{
$tc = new TaskCreation($this->container);
$tf = new TaskFinder($this->container);
$p = new Project($this->container);
$this->assertEquals(1, $p->create(array('name' => 'Project #1')));
$this->assertEquals(2, $p->create(array('name' => 'Project #2')));
$this->assertEquals(1, $tc->create(array('title' => 'Task #1', 'project_id' => 1, 'owner_id' => 1, 'date_due' => strtotime('-1 day'))));
$this->assertEquals(2, $tc->create(array('title' => 'Task #2', 'project_id' => 2, 'owner_id' => 1, 'date_due' => strtotime('-1 day'))));
$this->assertEquals(3, $tc->create(array('title' => 'Task #3', 'project_id' => 1, 'date_due' => strtotime('+1 day'))));
$this->assertEquals(4, $tc->create(array('title' => 'Task #4', 'project_id' => 1, 'date_due' => 0)));
$this->assertEquals(5, $tc->create(array('title' => 'Task #5', 'project_id' => 1)));
$tasks = $tf->getOverdueTasksByUser(1);
$this->assertNotEmpty($tasks);
$this->assertTrue(is_array($tasks));
$this->assertEquals(2, count($tasks));
$this->assertEquals(1, $tasks[0]['id']);
$this->assertEquals('Task #1', $tasks[0]['title']);
$this->assertEquals(1, $tasks[0]['owner_id']);
$this->assertEquals(1, $tasks[0]['project_id']);
$this->assertEquals('Project #1', $tasks[0]['project_name']);
$this->assertEquals('admin', $tasks[0]['assignee_username']);
$this->assertEquals('', $tasks[0]['assignee_name']);
$this->assertEquals('Task #2', $tasks[1]['title']);
}
} }