From 7749b8ed569f6d27b0bb2ed4c2040e8b61ed4422 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 9 Mar 2014 23:21:23 -0400 Subject: Automatic actions --- tests/AclTest.php | 18 ++---- tests/ActionTest.php | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests/Base.php | 37 ++++++++++++ tests/ProjectTest.php | 46 ++++++++------ tests/TaskTest.php | 67 ++++++++++++++++++--- 5 files changed, 294 insertions(+), 38 deletions(-) create mode 100644 tests/ActionTest.php create mode 100644 tests/Base.php (limited to 'tests') diff --git a/tests/AclTest.php b/tests/AclTest.php index 0996a51f..566d7245 100644 --- a/tests/AclTest.php +++ b/tests/AclTest.php @@ -1,24 +1,18 @@ 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(); diff --git a/tests/ActionTest.php b/tests/ActionTest.php new file mode 100644 index 00000000..de7f2c9f --- /dev/null +++ b/tests/ActionTest.php @@ -0,0 +1,164 @@ +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']); + } +} diff --git a/tests/Base.php b/tests/Base.php new file mode 100644 index 00000000..6efb92e5 --- /dev/null +++ b/tests/Base.php @@ -0,0 +1,37 @@ +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!'); + } + } +} diff --git a/tests/ProjectTest.php b/tests/ProjectTest.php index e6725b99..c04c7ff0 100644 --- a/tests/ProjectTest.php +++ b/tests/ProjectTest.php @@ -1,24 +1,19 @@ 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( diff --git a/tests/TaskTest.php b/tests/TaskTest.php index 415faede..a3417e91 100644 --- a/tests/TaskTest.php +++ b/tests/TaskTest.php @@ -1,20 +1,15 @@ 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()); + } } -- cgit v1.2.3