First API implementation

This commit is contained in:
Frédéric Guillot
2014-06-20 15:41:05 -03:00
parent efdc959c55
commit 7c5b900bd8
28 changed files with 1285 additions and 63 deletions

112
tests/units/AclTest.php Normal file
View File

@@ -0,0 +1,112 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Acl;
class AclTest extends Base
{
public function testAllowedAction()
{
$acl_rules = array(
'controller1' => array('action1', 'action3'),
);
$acl = new Acl($this->db, $this->event);
$this->assertTrue($acl->isAllowedAction($acl_rules, 'controller1', 'action1'));
$this->assertTrue($acl->isAllowedAction($acl_rules, 'controller1', 'action3'));
$this->assertFalse($acl->isAllowedAction($acl_rules, 'controller1', 'action2'));
$this->assertFalse($acl->isAllowedAction($acl_rules, 'controller2', 'action2'));
$this->assertFalse($acl->isAllowedAction($acl_rules, 'controller2', 'action3'));
}
public function testIsAdmin()
{
$acl = new Acl($this->db, $this->event);
$_SESSION = array();
$this->assertFalse($acl->isAdminUser());
$_SESSION = array('user' => array());
$this->assertFalse($acl->isAdminUser());
$_SESSION = array('user' => array('is_admin' => '1'));
$this->assertFalse($acl->isAdminUser());
$_SESSION = array('user' => array('is_admin' => false));
$this->assertFalse($acl->isAdminUser());
$_SESSION = array('user' => array('is_admin' => '2'));
$this->assertFalse($acl->isAdminUser());
$_SESSION = array('user' => array('is_admin' => true));
$this->assertTrue($acl->isAdminUser());
}
public function testIsUser()
{
$acl = new Acl($this->db, $this->event);
$_SESSION = array();
$this->assertFalse($acl->isRegularUser());
$_SESSION = array('user' => array());
$this->assertFalse($acl->isRegularUser());
$_SESSION = array('user' => array('is_admin' => true));
$this->assertFalse($acl->isRegularUser());
$_SESSION = array('user' => array('is_admin' => true));
$this->assertFalse($acl->isRegularUser());
$_SESSION = array('user' => array('is_admin' => '2'));
$this->assertFalse($acl->isRegularUser());
$_SESSION = array('user' => array('is_admin' => false));
$this->assertTrue($acl->isRegularUser());
}
public function testIsPageAllowed()
{
$acl = new Acl($this->db, $this->event);
// Public access
$_SESSION = array();
$this->assertFalse($acl->isPageAccessAllowed('user', 'create'));
$this->assertFalse($acl->isPageAccessAllowed('user', 'save'));
$this->assertFalse($acl->isPageAccessAllowed('user', 'remove'));
$this->assertFalse($acl->isPageAccessAllowed('user', 'confirm'));
$this->assertFalse($acl->isPageAccessAllowed('app', 'index'));
$this->assertFalse($acl->isPageAccessAllowed('user', 'index'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'login'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'check'));
$this->assertTrue($acl->isPageAccessAllowed('task', 'add'));
$this->assertTrue($acl->isPageAccessAllowed('board', 'readonly'));
// Regular user
$_SESSION = array('user' => array('is_admin' => false));
$this->assertFalse($acl->isPageAccessAllowed('user', 'create'));
$this->assertFalse($acl->isPageAccessAllowed('user', 'save'));
$this->assertFalse($acl->isPageAccessAllowed('user', 'remove'));
$this->assertFalse($acl->isPageAccessAllowed('user', 'confirm'));
$this->assertTrue($acl->isPageAccessAllowed('app', 'index'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'index'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'login'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'check'));
$this->assertTrue($acl->isPageAccessAllowed('task', 'add'));
$this->assertTrue($acl->isPageAccessAllowed('board', 'readonly'));
// Admin user
$_SESSION = array('user' => array('is_admin' => true));
$this->assertTrue($acl->isPageAccessAllowed('user', 'create'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'save'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'remove'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'confirm'));
$this->assertTrue($acl->isPageAccessAllowed('app', 'index'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'index'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'login'));
$this->assertTrue($acl->isPageAccessAllowed('user', 'check'));
$this->assertTrue($acl->isPageAccessAllowed('task', 'add'));
$this->assertTrue($acl->isPageAccessAllowed('board', 'readonly'));
}
}

View File

@@ -0,0 +1,76 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
use Model\Category;
class ActionTaskAssignColorCategory extends Base
{
public function testBadProject()
{
$action = new Action\TaskAssignColorCategory(3, new Task($this->db, $this->event));
$event = array(
'project_id' => 2,
'task_id' => 3,
'column_id' => 5,
);
$this->assertFalse($action->isExecutable($event));
$this->assertFalse($action->execute($event));
}
public function testExecute()
{
$action = new Action\TaskAssignColorCategory(1, new Task($this->db, $this->event));
$action->setParam('category_id', 1);
$action->setParam('color_id', 'blue');
// We create a task in the first column
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$c = new Category($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $c->create(array('name' => 'c1')));
$this->assertEquals(2, $c->create(array('name' => 'c2')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1, 'color_id' => 'green', 'category_id' => 2)));
// We create an event but we don't do anything
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 1,
'category_id' => 2,
'position' => 2,
);
// Our event should NOT be executed
$this->assertFalse($action->execute($event));
// Our task should be assigned to the ategory_id=1 and have the green color
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(2, $task['category_id']);
$this->assertEquals('green', $task['color_id']);
// We create an event to move the task
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 1,
'position' => 5,
'category_id' => 1,
);
// Our event should be executed
$this->assertTrue($action->execute($event));
// Our task should have the blue color
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals('blue', $task['color_id']);
}
}

View File

@@ -0,0 +1,70 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
class ActionTaskAssignColorUser extends Base
{
public function testBadProject()
{
$action = new Action\TaskAssignColorUser(3, new Task($this->db, $this->event));
$event = array(
'project_id' => 2,
'task_id' => 3,
'column_id' => 5,
);
$this->assertFalse($action->isExecutable($event));
$this->assertFalse($action->execute($event));
}
public function testExecute()
{
$action = new Action\TaskAssignColorUser(1, new Task($this->db, $this->event));
$action->setParam('user_id', 1);
$action->setParam('color_id', 'blue');
// We create a task in the first column
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1, 'color_id' => 'green')));
// We create an event to move the task to the 2nd column with a user id 5
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 2,
'owner_id' => 5,
);
// Our event should NOT be executed
$this->assertFalse($action->execute($event));
// Our task should be assigned to nobody and have the green color
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['owner_id']);
$this->assertEquals('green', $task['color_id']);
// We create an event to move the task to the 2nd column with a user id 1
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 2,
'owner_id' => 1,
);
// Our event should be executed
$this->assertTrue($action->execute($event));
// Our task should be assigned to nobody and have the blue color
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['owner_id']);
$this->assertEquals('blue', $task['color_id']);
}
}

View File

@@ -0,0 +1,73 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
use Model\Acl;
class ActionTaskAssignCurrentUser extends Base
{
public function testBadProject()
{
$action = new Action\TaskAssignCurrentUser(3, new Task($this->db, $this->event), new Acl($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 2,
'task_id' => 3,
'column_id' => 5,
);
$this->assertFalse($action->isExecutable($event));
$this->assertFalse($action->execute($event));
}
public function testBadColumn()
{
$action = new Action\TaskAssignCurrentUser(3, new Task($this->db, $this->event), new Acl($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 3,
'task_id' => 3,
'column_id' => 3,
);
$this->assertFalse($action->execute($event));
}
public function testExecute()
{
$action = new Action\TaskAssignCurrentUser(1, new Task($this->db, $this->event), new Acl($this->db, $this->event));
$action->setParam('column_id', 2);
$_SESSION = array(
'user' => array('id' => 5)
);
// We create a task in the first column
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$a = new Acl($this->db, $this->event);
$this->assertEquals(5, $a->getUserId());
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
// We create an event to move the task to the 2nd column
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 2,
);
// Our event should be executed
$this->assertTrue($action->execute($event));
// Our task should be assigned to the user 5 (from the session)
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(1, $task['id']);
$this->assertEquals(5, $task['owner_id']);
}
}

View File

@@ -0,0 +1,66 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
class ActionTaskAssignSpecificUser extends Base
{
public function testBadProject()
{
$action = new Action\TaskAssignSpecificUser(3, new Task($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 2,
'task_id' => 3,
'column_id' => 5,
);
$this->assertFalse($action->isExecutable($event));
$this->assertFalse($action->execute($event));
}
public function testBadColumn()
{
$action = new Action\TaskAssignSpecificUser(3, new Task($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 3,
'task_id' => 3,
'column_id' => 3,
);
$this->assertFalse($action->execute($event));
}
public function testExecute()
{
$action = new Action\TaskAssignSpecificUser(1, new Task($this->db, $this->event));
$action->setParam('column_id', 2);
$action->setParam('user_id', 1);
// We create a task in the first column
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
// We create an event to move the task to the 2nd column
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 2,
);
// Our event should be executed
$this->assertTrue($action->execute($event));
// Our task should be assigned to the user 1
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(1, $task['owner_id']);
}
}

View File

@@ -0,0 +1,65 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
class ActionTaskCloseTest extends Base
{
public function testBadProject()
{
$action = new Action\TaskClose(3, new Task($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 2,
'task_id' => 3,
'column_id' => 5,
);
$this->assertFalse($action->isExecutable($event));
$this->assertFalse($action->execute($event));
}
public function testBadColumn()
{
$action = new Action\TaskClose(3, new Task($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 3,
'task_id' => 3,
'column_id' => 3,
);
$this->assertFalse($action->execute($event));
}
public function testExecute()
{
$action = new Action\TaskClose(1, new Task($this->db, $this->event));
$action->setParam('column_id', 2);
// We create a task in the first column
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
// We create an event to move the task to the 2nd column
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 2,
);
// Our event should be executed
$this->assertTrue($action->execute($event));
// Our task should be closed
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['is_active']);
}
}

View File

@@ -0,0 +1,89 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
class ActionTaskDuplicateAnotherProject extends Base
{
public function testBadProject()
{
$action = new Action\TaskDuplicateAnotherProject(3, new Task($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 2,
'task_id' => 3,
'column_id' => 5,
);
$this->assertFalse($action->isExecutable($event));
$this->assertFalse($action->execute($event));
}
public function testBadColumn()
{
$action = new Action\TaskDuplicateAnotherProject(3, new Task($this->db, $this->event));
$action->setParam('column_id', 5);
$event = array(
'project_id' => 3,
'task_id' => 3,
'column_id' => 3,
);
$this->assertFalse($action->execute($event));
}
public function testExecute()
{
$action = new Action\TaskDuplicateAnotherProject(1, new Task($this->db, $this->event));
// We create a task in the first column
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'project 1')));
$this->assertEquals(2, $p->create(array('name' => 'project 2')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
// We create an event to move the task to the 2nd column
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 2,
);
// Our event should NOT be executed because we define the same project
$action->setParam('column_id', 2);
$action->setParam('project_id', 1);
$this->assertFalse($action->execute($event));
// Our task should be assigned to the project 1
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(1, $task['project_id']);
// We create an event to move the task to the 2nd column
$event = array(
'project_id' => 1,
'task_id' => 1,
'column_id' => 2,
);
// Our event should be executed because we define a different project
$action->setParam('column_id', 2);
$action->setParam('project_id', 2);
$this->assertTrue($action->execute($event));
// Our task should be assigned to the project 1
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(1, $task['project_id']);
// We should have another task assigned to the project 2
$task = $t->getById(2);
$this->assertNotEmpty($task);
$this->assertEquals(2, $task['project_id']);
}
}

244
tests/units/ActionTest.php Normal file
View File

@@ -0,0 +1,244 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Action;
use Model\Project;
use Model\Board;
use Model\Task;
use Model\Category;
class ActionTest extends Base
{
public function testFetchActions()
{
$action = new Action($this->db, $this->event);
$board = new Board($this->db, $this->event);
$project = new Project($this->db, $this->event);
$this->assertEquals(1, $project->create(array('name' => 'unit_test')));
// We should have nothing
$this->assertEmpty($action->getAll());
$this->assertEmpty($action->getAllByProject(1));
// We create a new action
$this->assertTrue($action->create(array(
'project_id' => 1,
'event_name' => Task::EVENT_MOVE_COLUMN,
'action_name' => 'TaskClose',
'params' => array(
'column_id' => 4,
)
)));
// We should have our action
$this->assertNotEmpty($action->getAll());
$this->assertEquals($action->getAll(), $action->getAllByProject(1));
$actions = $action->getAll();
$this->assertEquals(1, count($actions));
$this->assertEquals(1, $actions[0]['project_id']);
$this->assertEquals(Task::EVENT_MOVE_COLUMN, $actions[0]['event_name']);
$this->assertEquals('TaskClose', $actions[0]['action_name']);
$this->assertEquals('column_id', $actions[0]['params'][0]['name']);
$this->assertEquals(4, $actions[0]['params'][0]['value']);
}
public function testEventMoveColumn()
{
$task = new Task($this->db, $this->event);
$board = new Board($this->db, $this->event);
$project = new Project($this->db, $this->event);
$action = new Action($this->db, $this->event);
// We create a project
$this->assertEquals(1, $project->create(array('name' => 'unit_test')));
// We create a task
$this->assertEquals(1, $task->create(array(
'title' => 'unit_test',
'project_id' => 1,
'owner_id' => 1,
'color_id' => 'red',
'column_id' => 1,
)));
// We create a new action
$this->assertTrue($action->create(array(
'project_id' => 1,
'event_name' => Task::EVENT_MOVE_COLUMN,
'action_name' => 'TaskClose',
'params' => array(
'column_id' => 4,
)
)));
// We bind events
$action->attachEvents();
// Our task should be open
$t1 = $task->getById(1);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals(1, $t1['column_id']);
// We move our task
$task->move(1, 4, 1);
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_MOVE_COLUMN));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_UPDATE));
// Our task should be closed
$t1 = $task->getById(1);
$this->assertEquals(4, $t1['column_id']);
$this->assertEquals(0, $t1['is_active']);
}
public function testEventMovePosition()
{
$task = new Task($this->db, $this->event);
$board = new Board($this->db, $this->event);
$project = new Project($this->db, $this->event);
$action = new Action($this->db, $this->event);
// We create a project
$this->assertEquals(1, $project->create(array('name' => 'unit_test')));
// We create a task
$this->assertEquals(1, $task->create(array(
'title' => 'unit_test 0',
'project_id' => 1,
'owner_id' => 1,
'color_id' => 'red',
'column_id' => 1,
'category_id' => 1,
)));
$this->assertEquals(2, $task->create(array(
'title' => 'unit_test 1',
'project_id' => 1,
'owner_id' => 1,
'color_id' => 'yellow',
'column_id' => 1,
'category_id' => 1,
)));
// We create a new action, when the category_id=2 then the color_id should be green
$this->assertTrue($action->create(array(
'project_id' => 1,
'event_name' => Task::EVENT_MOVE_POSITION,
'action_name' => 'TaskAssignColorCategory',
'params' => array(
'category_id' => 1,
'color_id' => 'green',
)
)));
// We bind events
$action->attachEvents();
$this->assertTrue($this->event->hasListener(Task::EVENT_MOVE_POSITION, 'Action\TaskAssignColorCategory'));
// Our task should have the color red and position=0
$t1 = $task->getById(1);
$this->assertEquals(0, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('red', $t1['color_id']);
$t1 = $task->getById(2);
$this->assertEquals(1, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('yellow', $t1['color_id']);
// We move our tasks
$task->move(1, 1, 1); // task #1 to position 1
$task->move(2, 1, 0); // task #2 to position 0
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_MOVE_POSITION));
// Both tasks should be green
$t1 = $task->getById(1);
$this->assertEquals(1, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('green', $t1['color_id']);
$t1 = $task->getById(2);
$this->assertEquals(0, $t1['position']);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals('green', $t1['color_id']);
}
public function testExecuteMultipleActions()
{
$task = new Task($this->db, $this->event);
$board = new Board($this->db, $this->event);
$project = new Project($this->db, $this->event);
$action = new Action($this->db, $this->event);
// We create 2 projects
$this->assertEquals(1, $project->create(array('name' => 'unit_test1')));
$this->assertEquals(2, $project->create(array('name' => 'unit_test2')));
// We create a task
$this->assertEquals(1, $task->create(array(
'title' => 'unit_test',
'project_id' => 1,
'owner_id' => 1,
'color_id' => 'red',
'column_id' => 1,
)));
// We create 2 actions
$this->assertTrue($action->create(array(
'project_id' => 1,
'event_name' => Task::EVENT_CLOSE,
'action_name' => 'TaskDuplicateAnotherProject',
'params' => array(
'column_id' => 4,
'project_id' => 2,
)
)));
$this->assertTrue($action->create(array(
'project_id' => 1,
'event_name' => Task::EVENT_MOVE_COLUMN,
'action_name' => 'TaskClose',
'params' => array(
'column_id' => 4,
)
)));
// We bind events
$action->attachEvents();
// Events should be attached
$this->assertTrue($this->event->hasListener(Task::EVENT_CLOSE, 'Action\TaskDuplicateAnotherProject'));
$this->assertTrue($this->event->hasListener(Task::EVENT_MOVE_COLUMN, 'Action\TaskClose'));
// Our task should be open, linked to the first project and in the first column
$t1 = $task->getById(1);
$this->assertEquals(1, $t1['is_active']);
$this->assertEquals(1, $t1['column_id']);
$this->assertEquals(1, $t1['project_id']);
// We move our task
$task->move(1, 4, 1);
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_CLOSE));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_MOVE_COLUMN));
// Our task should be closed
$t1 = $task->getById(1);
$this->assertEquals(4, $t1['column_id']);
$this->assertEquals(0, $t1['is_active']);
// Our task should be duplicated to the 2nd project
$t2 = $task->getById(2);
$this->assertNotEmpty($t2);
$this->assertNotEquals(4, $t2['column_id']);
$this->assertEquals(1, $t2['is_active']);
$this->assertEquals(2, $t2['project_id']);
$this->assertEquals('unit_test', $t2['title']);
}
}

57
tests/units/Base.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
if (version_compare(PHP_VERSION, '5.5.0', '<')) {
require __DIR__.'/../../vendor/password.php';
}
require_once __DIR__.'/../../app/Core/Security.php';
require_once __DIR__.'/../../vendor/PicoDb/Database.php';
require_once __DIR__.'/../../app/Schema/Sqlite.php';
require_once __DIR__.'/../../app/Core/Listener.php';
require_once __DIR__.'/../../app/Core/Event.php';
require_once __DIR__.'/../../app/Core/Translator.php';
require_once __DIR__.'/../../app/translator.php';
require_once __DIR__.'/../../app/Model/Base.php';
require_once __DIR__.'/../../app/Model/Task.php';
require_once __DIR__.'/../../app/Model/Acl.php';
require_once __DIR__.'/../../app/Model/Comment.php';
require_once __DIR__.'/../../app/Model/Project.php';
require_once __DIR__.'/../../app/Model/User.php';
require_once __DIR__.'/../../app/Model/Board.php';
require_once __DIR__.'/../../app/Model/Action.php';
require_once __DIR__.'/../../app/Model/Category.php';
require_once __DIR__.'/../../app/Action/Base.php';
require_once __DIR__.'/../../app/Action/TaskClose.php';
require_once __DIR__.'/../../app/Action/TaskAssignSpecificUser.php';
require_once __DIR__.'/../../app/Action/TaskAssignColorUser.php';
require_once __DIR__.'/../../app/Action/TaskAssignColorCategory.php';
require_once __DIR__.'/../../app/Action/TaskAssignCurrentUser.php';
require_once __DIR__.'/../../app/Action/TaskDuplicateAnotherProject.php';
abstract class Base extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->db = $this->getDbConnection();
$this->event = new \Core\Event;
}
public function getDbConnection()
{
$db = new \PicoDb\Database(array(
'driver' => 'sqlite',
'filename' => ':memory:'
));
if ($db->schema()->check(\Schema\VERSION)) {
return $db;
}
else {
die('Unable to migrate database schema!');
}
}
}

77
tests/units/BoardTest.php Normal file
View File

@@ -0,0 +1,77 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Project;
use Model\Board;
class BoardTest extends Base
{
public function testMoveColumns()
{
$p = new Project($this->db, $this->event);
$b = new Board($this->db, $this->event);
// 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']);
}
}

116
tests/units/CommentTest.php Normal file
View File

@@ -0,0 +1,116 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
use Model\Comment;
class CommentTest extends Base
{
public function testCreate()
{
$c = new Comment($this->db, $this->event);
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));
$this->assertTrue($c->create(array('task_id' => 1, 'comment' => 'bla bla', 'user_id' => 1)));
$comment = $c->getById(1);
$this->assertNotEmpty($comment);
$this->assertEquals('bla bla', $comment['comment']);
$this->assertEquals(1, $comment['task_id']);
$this->assertEquals(1, $comment['user_id']);
$this->assertEquals('admin', $comment['username']);
$this->assertNotEmpty($comment['date']);
}
public function testGetAll()
{
$c = new Comment($this->db, $this->event);
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));
$this->assertTrue($c->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1)));
$this->assertTrue($c->create(array('task_id' => 1, 'comment' => 'c2', 'user_id' => 1)));
$this->assertTrue($c->create(array('task_id' => 1, 'comment' => 'c3', 'user_id' => 1)));
$comments = $c->getAll(1);
$this->assertNotEmpty($comments);
$this->assertEquals(3, count($comments));
$this->assertEquals(1, $comments[0]['id']);
$this->assertEquals(2, $comments[1]['id']);
$this->assertEquals(3, $comments[2]['id']);
}
public function testUpdate()
{
$c = new Comment($this->db, $this->event);
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));
$this->assertTrue($c->create(array('task_id' => 1, 'comment' => 'c1', 'user_id' => 1)));
$this->assertTrue($c->update(array('id' => 1, 'comment' => 'bla')));
$comment = $c->getById(1);
$this->assertNotEmpty($comment);
$this->assertEquals('bla', $comment['comment']);
}
public function testValidateCreation()
{
$c = new Comment($this->db, $this->event);
$result = $c->validateCreation(array('user_id' => 1, 'task_id' => 1, 'comment' => 'bla'));
$this->assertTrue($result[0]);
$result = $c->validateCreation(array('user_id' => 1, 'task_id' => 1, 'comment' => ''));
$this->assertFalse($result[0]);
$result = $c->validateCreation(array('user_id' => 1, 'task_id' => 'a', 'comment' => 'bla'));
$this->assertFalse($result[0]);
$result = $c->validateCreation(array('user_id' => 'b', 'task_id' => 1, 'comment' => 'bla'));
$this->assertFalse($result[0]);
$result = $c->validateCreation(array('user_id' => 1, 'comment' => 'bla'));
$this->assertFalse($result[0]);
$result = $c->validateCreation(array('task_id' => 1, 'comment' => 'bla'));
$this->assertFalse($result[0]);
$result = $c->validateCreation(array('comment' => 'bla'));
$this->assertFalse($result[0]);
$result = $c->validateCreation(array());
$this->assertFalse($result[0]);
}
public function testValidateModification()
{
$c = new Comment($this->db, $this->event);
$result = $c->validateModification(array('id' => 1, 'comment' => 'bla'));
$this->assertTrue($result[0]);
$result = $c->validateModification(array('id' => 1, 'comment' => ''));
$this->assertFalse($result[0]);
$result = $c->validateModification(array('comment' => 'bla'));
$this->assertFalse($result[0]);
$result = $c->validateModification(array('id' => 'b', 'comment' => 'bla'));
$this->assertFalse($result[0]);
$result = $c->validateModification(array());
$this->assertFalse($result[0]);
}
}

162
tests/units/ProjectTest.php Normal file
View File

@@ -0,0 +1,162 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Project;
use Model\User;
use Model\Task;
use Model\Acl;
use Model\Board;
class ProjectTest extends Base
{
public function testCreation()
{
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertNotEmpty($p->getById(1));
}
public function testAllowEverybody()
{
// We create a regular user
$user = new User($this->db, $this->event);
$user->create(array('username' => 'unittest', 'password' => 'unittest'));
$p = new Project($this->db, $this->event);
$this->assertEmpty($p->getAllowedUsers(1)); // Nobody is specified for the given project
$this->assertTrue($p->isUserAllowed(1, 1)); // Everybody should be allowed
$this->assertTrue($p->isUserAllowed(1, 2)); // Everybody should be allowed
}
public function testAllowUser()
{
$p = new Project($this->db, $this->event);
$user = new User($this->db, $this->event);
$user->create(array('username' => 'unittest', 'password' => 'unittest'));
// We create a project
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
// We allow the admin user
$this->assertTrue($p->allowUser(1, 1));
// Non-existant project
$this->assertFalse($p->allowUser(50, 1));
// Non-existant user
$this->assertFalse($p->allowUser(1, 50));
// Our admin user should be allowed
$this->assertEquals(array('1' => 'admin'), $p->getAllowedUsers(1));
$this->assertTrue($p->isUserAllowed(1, 1));
// Our regular user should be forbidden
$this->assertFalse($p->isUserAllowed(1, 2));
}
public function testRevokeUser()
{
$p = new Project($this->db, $this->event);
$user = new User($this->db, $this->event);
$user->create(array('username' => 'unittest', 'password' => 'unittest'));
// We create a project
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
// We revoke our admin user
$this->assertTrue($p->revokeUser(1, 1));
// We should have nobody in the users list
$this->assertEmpty($p->getAllowedUsers(1));
// Our admin user and our regular user should be allowed
$this->assertTrue($p->isUserAllowed(1, 1));
$this->assertTrue($p->isUserAllowed(1, 2));
// We allow only the regular user
$this->assertTrue($p->allowUser(1, 2));
// All users should be allowed (admin and regular)
$this->assertTrue($p->isUserAllowed(1, 1));
$this->assertTrue($p->isUserAllowed(1, 2));
// However, we should have only our regular user in the list
$this->assertEquals(array('2' => 'unittest'), $p->getAllowedUsers(1));
// We allow our admin, we should have both in the list
$this->assertTrue($p->allowUser(1, 1));
$this->assertEquals(array('1' => 'admin', '2' => 'unittest'), $p->getAllowedUsers(1));
$this->assertTrue($p->isUserAllowed(1, 1));
$this->assertTrue($p->isUserAllowed(1, 2));
// We revoke the regular user
$this->assertTrue($p->revokeUser(1, 2));
// Only admin should be allowed
$this->assertTrue($p->isUserAllowed(1, 1));
$this->assertFalse($p->isUserAllowed(1, 2));
// We should have only admin in the list
$this->assertEquals(array('1' => 'admin'), $p->getAllowedUsers(1));
// We revoke the admin user
$this->assertTrue($p->revokeUser(1, 1));
$this->assertEmpty($p->getAllowedUsers(1));
// Everybody should be allowed again
$this->assertTrue($p->isUserAllowed(1, 1));
$this->assertTrue($p->isUserAllowed(1, 2));
}
public function testUsersList()
{
$p = new Project($this->db, $this->event);
$user = new User($this->db, $this->event);
$user->create(array('username' => 'unittest', 'password' => 'unittest'));
// We create project
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
// No restriction, we should have everybody
$this->assertEquals(
array('Unassigned', 'admin', 'unittest'),
$p->getUsersList(1)
);
// We allow only the regular user
$this->assertTrue($p->allowUser(1, 2));
$this->assertEquals(
array(0 => 'Unassigned', 2 => 'unittest'),
$p->getUsersList(1)
);
// We allow the admin user
$this->assertTrue($p->allowUser(1, 1));
$this->assertEquals(
array(0 => 'Unassigned', 1 => 'admin', 2 => 'unittest'),
$p->getUsersList(1)
);
// We revoke only the regular user
$this->assertTrue($p->revokeUser(1, 2));
$this->assertEquals(
array(0 => 'Unassigned', 1 => 'admin'),
$p->getUsersList(1)
);
// We revoke only the admin user, we should have everybody
$this->assertTrue($p->revokeUser(1, 1));
$this->assertEquals(
array(0 => 'Unassigned', 1 => 'admin', 2 => 'unittest'),
$p->getUsersList(1)
);
}
}

187
tests/units/TaskTest.php Normal file
View File

@@ -0,0 +1,187 @@
<?php
require_once __DIR__.'/Base.php';
use Model\Task;
use Model\Project;
class TaskTest extends Base
{
public function testFilter()
{
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(1, $t->create(array('title' => 'test a', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1, 'description' => 'biloute')));
$this->assertEquals(2, $t->create(array('title' => 'test b', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2, 'description' => 'toto et titi sont dans un bateau')));
$tasks = $t->find(array(array('column' => 'project_id', 'operator' => 'eq', 'value' => '1')));
$this->assertNotFalse($tasks);
$this->assertEquals(2, count($tasks));
$this->assertEquals(1, $tasks[0]['id']);
$this->assertEquals(2, $tasks[1]['id']);
$tasks = $t->find(array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'),
array('column' => 'owner_id', 'operator' => 'eq', 'value' => '2'),
));
$this->assertEquals(1, count($tasks));
$this->assertEquals(2, $tasks[0]['id']);
$tasks = $t->find(array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'),
array('column' => 'title', 'operator' => 'like', 'value' => '%b%'),
));
$this->assertEquals(1, count($tasks));
$this->assertEquals(2, $tasks[0]['id']);
// Condition with OR
$search = 'bateau';
$filters = array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => 1),
'or' => array(
array('column' => 'title', 'operator' => 'like', 'value' => '%'.$search.'%'),
array('column' => 'description', 'operator' => 'like', 'value' => '%'.$search.'%'),
)
);
$tasks = $t->find($filters);
$this->assertEquals(1, count($tasks));
$this->assertEquals(2, $tasks[0]['id']);
$search = 'toto et titi';
$filters = array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => 1),
'or' => array(
array('column' => 'title', 'operator' => 'like', 'value' => '%'.$search.'%'),
array('column' => 'description', 'operator' => 'like', 'value' => '%'.$search.'%'),
)
);
$tasks = $t->find($filters);
$this->assertEquals(1, count($tasks));
$this->assertEquals(2, $tasks[0]['id']);
$search = 'john';
$filters = array(
array('column' => 'project_id', 'operator' => 'eq', 'value' => 1),
'or' => array(
array('column' => 'title', 'operator' => 'like', 'value' => '%'.$search.'%'),
array('column' => 'description', 'operator' => 'like', 'value' => '%'.$search.'%'),
)
);
$tasks = $t->find($filters);
$this->assertEquals(0, count($tasks));
}
public function testDateFormat()
{
$t = new Task($this->db, $this->event);
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('2014-03-05', 'Y-m-d')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('2014_03_05', 'Y_m_d')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('05/03/2014', 'd/m/Y')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('03/05/2014', 'm/d/Y')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('3/5/2014', 'm/d/Y')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('5/3/2014', 'd/m/Y')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getValidDate('5/3/14', 'd/m/y')));
$this->assertEquals(0, $t->getValidDate('5/3/14', 'd/m/Y'));
$this->assertEquals(0, $t->getValidDate('5-3-2014', 'd/m/Y'));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->parseDate('2014-03-05')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->parseDate('2014_03_05')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->parseDate('03/05/2014')));
}
public function testDuplicateTask()
{
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
// We create a task and a project
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1, 'category_id' => 2)));
$task = $t->getById(1);
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['position']);
// We duplicate our task
$this->assertEquals(2, $t->duplicate(1));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_CREATE));
// Check the values of the duplicated task
$task = $t->getById(2);
$this->assertNotEmpty($task);
$this->assertEquals(Task::STATUS_OPEN, $task['is_active']);
$this->assertEquals(1, $task['project_id']);
$this->assertEquals(1, $task['owner_id']);
$this->assertEquals(1, $task['position']);
$this->assertEquals(2, $task['category_id']);
}
public function testDuplicateToAnotherProject()
{
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
// We create 2 projects
$this->assertEquals(1, $p->create(array('name' => 'test1')));
$this->assertEquals(2, $p->create(array('name' => 'test2')));
// We create a task
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1, 'category_id' => 1)));
// We duplicate our task to the 2nd project
$this->assertEquals(2, $t->duplicateToAnotherProject(1, 2));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_CREATE));
// Check the values of the duplicated task
$task = $t->getById(2);
$this->assertNotEmpty($task);
$this->assertEquals(0, $task['owner_id']);
$this->assertEquals(0, $task['category_id']);
$this->assertEquals(2, $task['project_id']);
$this->assertEquals('test', $task['title']);
}
public function testEvents()
{
$t = new Task($this->db, $this->event);
$p = new Project($this->db, $this->event);
// We create a project
$this->assertEquals(1, $p->create(array('name' => 'test')));
// We create task
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_CREATE));
// We update a task
$this->assertTrue($t->update(array('title' => 'test2', 'id' => 1)));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_UPDATE));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_CREATE_UPDATE));
// We close our task
$this->assertTrue($t->close(1));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_CLOSE));
// We open our task
$this->assertTrue($t->open(1));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_OPEN));
// We change the column of our task
$this->assertTrue($t->move(1, 2, 1));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_MOVE_COLUMN));
// We change the position of our task
$this->assertTrue($t->move(1, 2, 2));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_MOVE_POSITION));
// We change the column and the position of our task
$this->assertTrue($t->move(1, 1, 3));
$this->assertTrue($this->event->isEventTriggered(Task::EVENT_MOVE_COLUMN));
}
}