Add event subtask.delete

This commit is contained in:
Frederic Guillot
2015-09-14 21:37:30 -04:00
parent 63426c5374
commit eaff957839
3 changed files with 162 additions and 10 deletions

View File

@@ -19,6 +19,7 @@ Improvements:
* Avoid scrollbar in Gantt chart for row title on Windows platform * Avoid scrollbar in Gantt chart for row title on Windows platform
* Remove unnecessary margin for calendar header * Remove unnecessary margin for calendar header
* Show localized documentation if available * Show localized documentation if available
* Add event subtask.delete
Bug fixes: Bug fixes:

View File

@@ -49,6 +49,7 @@ class Subtask extends Base
*/ */
const EVENT_UPDATE = 'subtask.update'; const EVENT_UPDATE = 'subtask.update';
const EVENT_CREATE = 'subtask.create'; const EVENT_CREATE = 'subtask.create';
const EVENT_DELETE = 'subtask.delete';
/** /**
* Get available status * Get available status
@@ -173,6 +174,23 @@ class Subtask extends Base
$this->resetFields($values, array('time_estimated', 'time_spent')); $this->resetFields($values, array('time_estimated', 'time_spent'));
} }
/**
* 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 * Get the position of the last column for a given project
* *
@@ -198,9 +216,7 @@ class Subtask extends Base
*/ */
public function create(array $values) public function create(array $values)
{ {
$this->prepare($values); $this->prepareCreation($values);
$values['position'] = $this->getLastPosition($values['task_id']) + 1;
$subtask_id = $this->persist(self::TABLE, $values); $subtask_id = $this->persist(self::TABLE, $values);
if ($subtask_id) { if ($subtask_id) {
@@ -223,14 +239,13 @@ class Subtask extends Base
public function update(array $values) public function update(array $values)
{ {
$this->prepare($values); $this->prepare($values);
$subtask = $this->getById($values['id']);
$result = $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values); $result = $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values);
if ($result) { if ($result) {
$event = $subtask;
$this->container['dispatcher']->dispatch( $event['changes'] = array_diff_assoc($values, $subtask);
self::EVENT_UPDATE, $this->container['dispatcher']->dispatch(self::EVENT_UPDATE, new SubtaskEvent($event));
new SubtaskEvent($values)
);
} }
return $result; return $result;
@@ -302,7 +317,6 @@ class Subtask extends Base
$positions = array_flip($subtasks); $positions = array_flip($subtasks);
if (isset($subtasks[$subtask_id]) && $subtasks[$subtask_id] < count($subtasks)) { if (isset($subtasks[$subtask_id]) && $subtasks[$subtask_id] < count($subtasks)) {
$position = ++$subtasks[$subtask_id]; $position = ++$subtasks[$subtask_id];
$subtasks[$positions[$position]]--; $subtasks[$positions[$position]]--;
@@ -402,7 +416,14 @@ class Subtask extends Base
*/ */
public function remove($subtask_id) 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;
} }
/** /**

View File

@@ -13,6 +13,136 @@ use Model\UserSession;
class SubTaskTest extends Base 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() public function testToggleStatusWithoutSession()
{ {
$tc = new TaskCreation($this->container); $tc = new TaskCreation($this->container);