diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-09-14 21:37:30 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-09-14 21:37:30 -0400 |
commit | eaff957839ad2cfaf3b5913f99dd56ca85c7c1e1 (patch) | |
tree | 988b89e6e32b97d4e68762e34137d5a2d87c6a29 | |
parent | 63426c5374341e0aed47c69ce14df58e285effc4 (diff) |
Add event subtask.delete
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | app/Model/Subtask.php | 41 | ||||
-rw-r--r-- | tests/units/Model/SubtaskTest.php | 130 |
3 files changed, 162 insertions, 10 deletions
@@ -19,6 +19,7 @@ Improvements: * Avoid scrollbar in Gantt chart for row title on Windows platform * Remove unnecessary margin for calendar header * Show localized documentation if available +* Add event subtask.delete Bug fixes: diff --git a/app/Model/Subtask.php b/app/Model/Subtask.php index d8a44aff..24508c91 100644 --- a/app/Model/Subtask.php +++ b/app/Model/Subtask.php @@ -49,6 +49,7 @@ class Subtask extends Base */ const EVENT_UPDATE = 'subtask.update'; const EVENT_CREATE = 'subtask.create'; + const EVENT_DELETE = 'subtask.delete'; /** * Get available status @@ -174,6 +175,23 @@ class Subtask extends Base } /** + * Prepare data before insert + * + * @access public + * @param array $values Form values + */ + public function prepareCreation(array &$values) + { + $this->prepare($values); + + $values['position'] = $this->getLastPosition($values['task_id']) + 1; + $values['status'] = isset($values['status']) ? $values['status'] : self::STATUS_TODO; + $values['time_estimated'] = isset($values['time_estimated']) ? $values['time_estimated'] : 0; + $values['time_spent'] = isset($values['time_spent']) ? $values['time_spent'] : 0; + $values['user_id'] = isset($values['user_id']) ? $values['user_id'] : 0; + } + + /** * Get the position of the last column for a given project * * @access public @@ -198,9 +216,7 @@ class Subtask extends Base */ public function create(array $values) { - $this->prepare($values); - $values['position'] = $this->getLastPosition($values['task_id']) + 1; - + $this->prepareCreation($values); $subtask_id = $this->persist(self::TABLE, $values); if ($subtask_id) { @@ -223,14 +239,13 @@ class Subtask extends Base public function update(array $values) { $this->prepare($values); + $subtask = $this->getById($values['id']); $result = $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values); if ($result) { - - $this->container['dispatcher']->dispatch( - self::EVENT_UPDATE, - new SubtaskEvent($values) - ); + $event = $subtask; + $event['changes'] = array_diff_assoc($values, $subtask); + $this->container['dispatcher']->dispatch(self::EVENT_UPDATE, new SubtaskEvent($event)); } return $result; @@ -302,7 +317,6 @@ class Subtask extends Base $positions = array_flip($subtasks); if (isset($subtasks[$subtask_id]) && $subtasks[$subtask_id] < count($subtasks)) { - $position = ++$subtasks[$subtask_id]; $subtasks[$positions[$position]]--; @@ -402,7 +416,14 @@ class Subtask extends Base */ public function remove($subtask_id) { - return $this->db->table(self::TABLE)->eq('id', $subtask_id)->remove(); + $subtask = $this->getById($subtask_id); + $result = $this->db->table(self::TABLE)->eq('id', $subtask_id)->remove(); + + if ($result) { + $this->container['dispatcher']->dispatch(self::EVENT_DELETE, new SubtaskEvent($subtask)); + } + + return $result; } /** diff --git a/tests/units/Model/SubtaskTest.php b/tests/units/Model/SubtaskTest.php index 1ef04c32..5773893c 100644 --- a/tests/units/Model/SubtaskTest.php +++ b/tests/units/Model/SubtaskTest.php @@ -13,6 +13,136 @@ use Model\UserSession; class SubTaskTest extends Base { + public function onSubtaskCreated($event) + { + $this->assertInstanceOf('Event\SubtaskEvent', $event); + $data = $event->getAll(); + + $this->assertArrayHasKey('id', $data); + $this->assertArrayHasKey('title', $data); + $this->assertArrayHasKey('status', $data); + $this->assertArrayHasKey('time_estimated', $data); + $this->assertArrayHasKey('time_spent', $data); + $this->assertArrayHasKey('status', $data); + $this->assertArrayHasKey('task_id', $data); + $this->assertArrayHasKey('user_id', $data); + $this->assertArrayHasKey('position', $data); + $this->assertNotEmpty($data['task_id']); + $this->assertNotEmpty($data['id']); + } + + public function onSubtaskUpdated($event) + { + $this->assertInstanceOf('Event\SubtaskEvent', $event); + $data = $event->getAll(); + + $this->assertArrayHasKey('id', $data); + $this->assertArrayHasKey('title', $data); + $this->assertArrayHasKey('status', $data); + $this->assertArrayHasKey('time_estimated', $data); + $this->assertArrayHasKey('time_spent', $data); + $this->assertArrayHasKey('status', $data); + $this->assertArrayHasKey('task_id', $data); + $this->assertArrayHasKey('user_id', $data); + $this->assertArrayHasKey('position', $data); + $this->assertArrayHasKey('changes', $data); + $this->assertArrayHasKey('user_id', $data['changes']); + $this->assertArrayHasKey('status', $data['changes']); + + $this->assertEquals(Subtask::STATUS_INPROGRESS, $data['changes']['status']); + $this->assertEquals(1, $data['changes']['user_id']); + } + + public function onSubtaskDeleted($event) + { + $this->assertInstanceOf('Event\SubtaskEvent', $event); + $data = $event->getAll(); + + $this->assertArrayHasKey('id', $data); + $this->assertArrayHasKey('title', $data); + $this->assertArrayHasKey('status', $data); + $this->assertArrayHasKey('time_estimated', $data); + $this->assertArrayHasKey('time_spent', $data); + $this->assertArrayHasKey('status', $data); + $this->assertArrayHasKey('task_id', $data); + $this->assertArrayHasKey('user_id', $data); + $this->assertArrayHasKey('position', $data); + $this->assertNotEmpty($data['task_id']); + $this->assertNotEmpty($data['id']); + } + + public function testCreation() + { + $tc = new TaskCreation($this->container); + $s = new Subtask($this->container); + $p = new Project($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); + + $this->container['dispatcher']->addListener(Subtask::EVENT_CREATE, array($this, 'onSubtaskCreated')); + + $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1))); + + $subtask = $s->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(1, $subtask['id']); + $this->assertEquals(1, $subtask['task_id']); + $this->assertEquals('subtask #1', $subtask['title']); + $this->assertEquals(Subtask::STATUS_TODO, $subtask['status']); + $this->assertEquals(0, $subtask['time_estimated']); + $this->assertEquals(0, $subtask['time_spent']); + $this->assertEquals(0, $subtask['user_id']); + $this->assertEquals(1, $subtask['position']); + } + + public function testModification() + { + $tc = new TaskCreation($this->container); + $s = new Subtask($this->container); + $p = new Project($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); + + $this->container['dispatcher']->addListener(Subtask::EVENT_UPDATE, array($this, 'onSubtaskUpdated')); + + $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1))); + $this->assertTrue($s->update(array('id' => 1, 'user_id' => 1, 'status' => Subtask::STATUS_INPROGRESS))); + + $subtask = $s->getById(1); + $this->assertNotEmpty($subtask); + $this->assertEquals(1, $subtask['id']); + $this->assertEquals(1, $subtask['task_id']); + $this->assertEquals('subtask #1', $subtask['title']); + $this->assertEquals(Subtask::STATUS_INPROGRESS, $subtask['status']); + $this->assertEquals(0, $subtask['time_estimated']); + $this->assertEquals(0, $subtask['time_spent']); + $this->assertEquals(1, $subtask['user_id']); + $this->assertEquals(1, $subtask['position']); + } + + public function testRemove() + { + $tc = new TaskCreation($this->container); + $s = new Subtask($this->container); + $p = new Project($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1))); + $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1))); + + $this->container['dispatcher']->addListener(Subtask::EVENT_DELETE, array($this, 'onSubtaskDeleted')); + + $subtask = $s->getById(1); + $this->assertNotEmpty($subtask); + + $this->assertTrue($s->remove(1)); + + $subtask = $s->getById(1); + $this->assertEmpty($subtask); + } + public function testToggleStatusWithoutSession() { $tc = new TaskCreation($this->container); |