Automatic actions

This commit is contained in:
Frédéric Guillot
2014-03-09 23:21:23 -04:00
parent 7bd4697dfc
commit 7749b8ed56
49 changed files with 1678 additions and 190 deletions

View File

@@ -1,24 +1,18 @@
<?php
require_once __DIR__.'/../models/base.php';
require_once __DIR__.'/../models/acl.php';
require_once __DIR__.'/base.php';
use Model\Acl;
class AclTest extends PHPUnit_Framework_TestCase
class AclTest extends Base
{
public function setUp()
{
defined('DB_FILENAME') or define('DB_FILENAME', ':memory:');
}
public function testAllowedAction()
{
$acl_rules = array(
'controller1' => array('action1', 'action3'),
);
$acl = new Acl;
$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'));
@@ -28,7 +22,7 @@ class AclTest extends PHPUnit_Framework_TestCase
public function testIsAdmin()
{
$acl = new Acl;
$acl = new Acl($this->db, $this->event);
$_SESSION = array();
$this->assertFalse($acl->isAdminUser());
@@ -51,7 +45,7 @@ class AclTest extends PHPUnit_Framework_TestCase
public function testIsUser()
{
$acl = new Acl;
$acl = new Acl($this->db, $this->event);
$_SESSION = array();
$this->assertFalse($acl->isRegularUser());
@@ -74,7 +68,7 @@ class AclTest extends PHPUnit_Framework_TestCase
public function testIsPageAllowed()
{
$acl = new Acl;
$acl = new Acl($this->db, $this->event);
// Public access
$_SESSION = array();

164
tests/ActionTest.php Normal file
View File

@@ -0,0 +1,164 @@
<?php
require_once __DIR__.'/base.php';
use Model\Action;
use Model\Project;
use Model\Board;
use Model\Task;
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 testExecuteAction()
{
$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);
// Our task should be closed
$t1 = $task->getById(1);
$this->assertEquals(4, $t1['column_id']);
$this->assertEquals(0, $t1['is_active']);
}*/
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->assertEquals(Task::EVENT_CREATE, $this->event->getLastTriggeredEvent());
// 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']);
}
}

37
tests/Base.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
require_once __DIR__.'/../vendor/PicoDb/Database.php';
require_once __DIR__.'/../core/event.php';
require_once __DIR__.'/../core/translator.php';
require_once __DIR__.'/../models/schema.php';
require_once __DIR__.'/../models/task.php';
require_once __DIR__.'/../models/acl.php';
require_once __DIR__.'/../models/comment.php';
require_once __DIR__.'/../models/project.php';
require_once __DIR__.'/../models/user.php';
require_once __DIR__.'/../models/board.php';
require_once __DIR__.'/../models/action.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(10)) {
return $db;
}
else {
die('Unable to migrate database schema!');
}
}
}

View File

@@ -1,24 +1,19 @@
<?php
require_once __DIR__.'/../lib/translator.php';
require_once __DIR__.'/../models/base.php';
require_once __DIR__.'/../models/board.php';
require_once __DIR__.'/../models/user.php';
require_once __DIR__.'/../models/project.php';
require_once __DIR__.'/base.php';
use Model\Project;
use Model\User;
use Model\Task;
use Model\Acl;
use Model\Board;
class ProjectTest extends PHPUnit_Framework_TestCase
class ProjectTest extends Base
{
public function setUp()
{
defined('DB_FILENAME') or define('DB_FILENAME', ':memory:');
}
public function testCreation()
{
$p = new Project;
$p = new Project($this->db, $this->event);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertNotEmpty($p->getById(1));
}
@@ -26,10 +21,10 @@ class ProjectTest extends PHPUnit_Framework_TestCase
public function testAllowEverybody()
{
// We create a regular user
$user = new User;
$user = new User($this->db, $this->event);
$user->create(array('username' => 'unittest', 'password' => 'unittest'));
$p = new Project;
$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
@@ -37,7 +32,12 @@ class ProjectTest extends PHPUnit_Framework_TestCase
public function testAllowUser()
{
$p = new Project;
$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));
@@ -58,7 +58,13 @@ class ProjectTest extends PHPUnit_Framework_TestCase
public function testRevokeUser()
{
$p = new Project;
$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));
@@ -107,7 +113,13 @@ class ProjectTest extends PHPUnit_Framework_TestCase
public function testUsersList()
{
$p = new Project;
$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(

View File

@@ -1,20 +1,15 @@
<?php
require_once __DIR__.'/../models/base.php';
require_once __DIR__.'/../models/task.php';
require_once __DIR__.'/base.php';
use Model\Task;
use Model\Project;
class TaskTest extends PHPUnit_Framework_TestCase
class TaskTest extends Base
{
public function setUp()
{
defined('DB_FILENAME') or define('DB_FILENAME', ':memory:');
}
public function testDateFormat()
{
$t = new Task;
$t = new Task($this->db, $this->event);
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getTimestampFromDate('05/03/2014', 'd/m/Y')));
$this->assertEquals('2014-03-05', date('Y-m-d', $t->getTimestampFromDate('03/05/2014', 'm/d/Y')));
@@ -24,4 +19,58 @@ class TaskTest extends PHPUnit_Framework_TestCase
$this->assertEquals(0, $t->getTimestampFromDate('5/3/14', 'd/m/Y'));
$this->assertEquals(0, $t->getTimestampFromDate('5-3-2014', 'd/m/Y'));
}
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)));
// We duplicate our task to the 2nd project
$this->assertEquals(2, $t->duplicateToAnotherProject(1, 2));
$this->assertEquals(Task::EVENT_CREATE, $this->event->getLastTriggeredEvent());
}
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->assertEquals(Task::EVENT_CREATE, $this->event->getLastTriggeredEvent());
// We update a task
$this->assertTrue($t->update(array('title' => 'test2', 'id' => 1)));
$this->assertEquals(Task::EVENT_UPDATE, $this->event->getLastTriggeredEvent());
// We close our task
$this->assertTrue($t->close(1));
$this->assertEquals(Task::EVENT_CLOSE, $this->event->getLastTriggeredEvent());
// We open our task
$this->assertTrue($t->open(1));
$this->assertEquals(Task::EVENT_OPEN, $this->event->getLastTriggeredEvent());
// We change the column of our task
$this->assertTrue($t->move(1, 2, 1));
$this->assertEquals(Task::EVENT_MOVE_COLUMN, $this->event->getLastTriggeredEvent());
// We change the position of our task
$this->assertTrue($t->move(1, 2, 2));
$this->assertEquals(Task::EVENT_MOVE_POSITION, $this->event->getLastTriggeredEvent());
// We change the column and the position of our task
$this->assertTrue($t->move(1, 1, 3));
$this->assertEquals(Task::EVENT_MOVE_COLUMN, $this->event->getLastTriggeredEvent());
}
}