summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/Model/Board.php58
-rw-r--r--tests/units/BoardTest.php80
-rw-r--r--tests/units/ProjectActivityTest.php2
3 files changed, 125 insertions, 15 deletions
diff --git a/app/Model/Board.php b/app/Model/Board.php
index 711f3dd9..712a3534 100644
--- a/app/Model/Board.php
+++ b/app/Model/Board.php
@@ -146,6 +146,46 @@ class Board extends Base
}
/**
+ * Get columns with consecutive positions
+ *
+ * If you remove a column, the positions are not anymore consecutives
+ *
+ * @access public
+ * @param integer $project_id
+ * @return array
+ */
+ public function getNormalizedColumnPositions($project_id)
+ {
+ $columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position');
+ $position = 1;
+
+ foreach ($columns as $column_id => $column_position) {
+ $columns[$column_id] = $position++;
+ }
+
+ return $columns;
+ }
+
+ /**
+ * Save the new positions for a set of columns
+ *
+ * @access public
+ * @param array $columns Hashmap of column_id/column_position
+ * @return boolean
+ */
+ public function saveColumnPositions(array $columns)
+ {
+ return $this->db->transaction(function ($db) use ($columns) {
+
+ foreach ($columns as $column_id => $position) {
+ if (! $db->table(self::TABLE)->eq('id', $column_id)->update(array('position' => $position))) {
+ return false;
+ }
+ }
+ });
+ }
+
+ /**
* Move a column down, increment the column position value
*
* @access public
@@ -155,7 +195,7 @@ class Board extends Base
*/
public function moveDown($project_id, $column_id)
{
- $columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position');
+ $columns = $this->getNormalizedColumnPositions($project_id);
$positions = array_flip($columns);
if (isset($columns[$column_id]) && $columns[$column_id] < count($columns)) {
@@ -163,12 +203,7 @@ class Board extends Base
$position = ++$columns[$column_id];
$columns[$positions[$position]]--;
- $this->db->startTransaction();
- $this->db->table(self::TABLE)->eq('id', $column_id)->update(array('position' => $position));
- $this->db->table(self::TABLE)->eq('id', $positions[$position])->update(array('position' => $columns[$positions[$position]]));
- $this->db->closeTransaction();
-
- return true;
+ return $this->saveColumnPositions($columns);
}
return false;
@@ -184,7 +219,7 @@ class Board extends Base
*/
public function moveUp($project_id, $column_id)
{
- $columns = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->asc('position')->getAll('id', 'position');
+ $columns = $this->getNormalizedColumnPositions($project_id);
$positions = array_flip($columns);
if (isset($columns[$column_id]) && $columns[$column_id] > 1) {
@@ -192,12 +227,7 @@ class Board extends Base
$position = --$columns[$column_id];
$columns[$positions[$position]]++;
- $this->db->startTransaction();
- $this->db->table(self::TABLE)->eq('id', $column_id)->update(array('position' => $position));
- $this->db->table(self::TABLE)->eq('id', $positions[$position])->update(array('position' => $columns[$positions[$position]]));
- $this->db->closeTransaction();
-
- return true;
+ return $this->saveColumnPositions($columns);
}
return false;
diff --git a/tests/units/BoardTest.php b/tests/units/BoardTest.php
index 23276718..ebd4f655 100644
--- a/tests/units/BoardTest.php
+++ b/tests/units/BoardTest.php
@@ -289,4 +289,84 @@ class BoardTest extends Base
$this->assertEquals(4, $columns[3]['position']);
$this->assertEquals($columns_id[3], $columns[3]['id']);
}
+
+ public function testMoveUpAndRemoveColumn()
+ {
+ $p = new Project($this->container);
+ $b = new Board($this->container);
+
+ // We create a project
+ $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
+
+ // We remove the second column
+ $this->assertTrue($b->removeColumn(2));
+
+ $columns = $b->getColumns(1);
+ $this->assertNotEmpty($columns);
+ $this->assertCount(3, $columns);
+
+ $this->assertEquals(1, $columns[0]['position']);
+ $this->assertEquals(3, $columns[1]['position']);
+ $this->assertEquals(4, $columns[2]['position']);
+
+ $this->assertEquals(1, $columns[0]['id']);
+ $this->assertEquals(3, $columns[1]['id']);
+ $this->assertEquals(4, $columns[2]['id']);
+
+ // We move up the second column
+ $this->assertTrue($b->moveUp(1, $columns[1]['id']));
+
+ // Check the new positions
+ $columns = $b->getColumns(1);
+ $this->assertNotEmpty($columns);
+ $this->assertCount(3, $columns);
+
+ $this->assertEquals(1, $columns[0]['position']);
+ $this->assertEquals(2, $columns[1]['position']);
+ $this->assertEquals(3, $columns[2]['position']);
+
+ $this->assertEquals(3, $columns[0]['id']);
+ $this->assertEquals(1, $columns[1]['id']);
+ $this->assertEquals(4, $columns[2]['id']);
+ }
+
+ public function testMoveDownAndRemoveColumn()
+ {
+ $p = new Project($this->container);
+ $b = new Board($this->container);
+
+ // We create a project
+ $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
+
+ // We remove the second column
+ $this->assertTrue($b->removeColumn(2));
+
+ $columns = $b->getColumns(1);
+ $this->assertNotEmpty($columns);
+ $this->assertCount(3, $columns);
+
+ $this->assertEquals(1, $columns[0]['position']);
+ $this->assertEquals(3, $columns[1]['position']);
+ $this->assertEquals(4, $columns[2]['position']);
+
+ $this->assertEquals(1, $columns[0]['id']);
+ $this->assertEquals(3, $columns[1]['id']);
+ $this->assertEquals(4, $columns[2]['id']);
+
+ // We move up the second column
+ $this->assertTrue($b->moveDown(1, $columns[0]['id']));
+
+ // Check the new positions
+ $columns = $b->getColumns(1);
+ $this->assertNotEmpty($columns);
+ $this->assertCount(3, $columns);
+
+ $this->assertEquals(1, $columns[0]['position']);
+ $this->assertEquals(2, $columns[1]['position']);
+ $this->assertEquals(3, $columns[2]['position']);
+
+ $this->assertEquals(3, $columns[0]['id']);
+ $this->assertEquals(1, $columns[1]['id']);
+ $this->assertEquals(4, $columns[2]['id']);
+ }
}
diff --git a/tests/units/ProjectActivityTest.php b/tests/units/ProjectActivityTest.php
index e70dfb0e..f0b27649 100644
--- a/tests/units/ProjectActivityTest.php
+++ b/tests/units/ProjectActivityTest.php
@@ -41,7 +41,7 @@ class ProjectActivityTest extends Base
$this->assertNotEmpty($events);
$this->assertTrue(is_array($events));
$this->assertEquals(2, count($events));
- $this->assertEquals(time(), $events[0]['date_creation']);
+ $this->assertEquals(time(), $events[0]['date_creation'], '', 1);
$this->assertEquals(Task::EVENT_UPDATE, $events[0]['event_name']);
$this->assertEquals(Task::EVENT_CLOSE, $events[1]['event_name']);
}