From 5fe68d4d499a8496229763369b50d71c9fa16200 Mon Sep 17 00:00:00 2001
From: Frederic Guillot <fred@kanboard.net>
Date: Sat, 20 Feb 2016 18:11:08 -0500
Subject: Add drag and drop to change swimlane positions

---
 tests/integration/ApiTest.php      | 128 -----------------
 tests/integration/SwimlaneTest.php | 103 ++++++++++++++
 tests/units/Model/SwimlaneTest.php | 273 ++++++++++++++-----------------------
 3 files changed, 209 insertions(+), 295 deletions(-)
 create mode 100644 tests/integration/SwimlaneTest.php

(limited to 'tests')

diff --git a/tests/integration/ApiTest.php b/tests/integration/ApiTest.php
index 679238a2..5fed0368 100644
--- a/tests/integration/ApiTest.php
+++ b/tests/integration/ApiTest.php
@@ -170,122 +170,6 @@ class Api extends PHPUnit_Framework_TestCase
         $this->assertCount(0, $activities);
     }
 
-    public function testGetDefaultSwimlane()
-    {
-        $swimlane = $this->client->getDefaultSwimlane(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals('Default swimlane', $swimlane['default_swimlane']);
-    }
-
-    public function testAddSwimlane()
-    {
-        $swimlane_id = $this->client->addSwimlane(1, 'Swimlane 1');
-        $this->assertNotFalse($swimlane_id);
-        $this->assertInternalType('int', $swimlane_id);
-
-        $swimlane = $this->client->getSwimlaneById($swimlane_id);
-        $this->assertNotEmpty($swimlane);
-        $this->assertInternalType('array', $swimlane);
-        $this->assertEquals('Swimlane 1', $swimlane['name']);
-    }
-
-    public function testGetSwimlane()
-    {
-        $swimlane = $this->client->getSwimlane(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertInternalType('array', $swimlane);
-        $this->assertEquals('Swimlane 1', $swimlane['name']);
-    }
-
-    public function testUpdateSwimlane()
-    {
-        $swimlane = $this->client->getSwimlaneByName(1, 'Swimlane 1');
-        $this->assertNotEmpty($swimlane);
-        $this->assertInternalType('array', $swimlane);
-        $this->assertEquals(1, $swimlane['id']);
-        $this->assertEquals('Swimlane 1', $swimlane['name']);
-
-        $this->assertTrue($this->client->updateSwimlane($swimlane['id'], 'Another swimlane'));
-
-        $swimlane = $this->client->getSwimlaneById($swimlane['id']);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals('Another swimlane', $swimlane['name']);
-    }
-
-    public function testDisableSwimlane()
-    {
-        $this->assertTrue($this->client->disableSwimlane(1, 1));
-
-        $swimlane = $this->client->getSwimlaneById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(0, $swimlane['is_active']);
-    }
-
-    public function testEnableSwimlane()
-    {
-        $this->assertTrue($this->client->enableSwimlane(1, 1));
-
-        $swimlane = $this->client->getSwimlaneById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-    }
-
-    public function testGetAllSwimlanes()
-    {
-        $this->assertNotFalse($this->client->addSwimlane(1, 'Swimlane A'));
-
-        $swimlanes = $this->client->getAllSwimlanes(1);
-        $this->assertNotEmpty($swimlanes);
-        $this->assertCount(2, $swimlanes);
-        $this->assertEquals('Another swimlane', $swimlanes[0]['name']);
-        $this->assertEquals('Swimlane A', $swimlanes[1]['name']);
-    }
-
-    public function testGetActiveSwimlane()
-    {
-        $this->assertTrue($this->client->disableSwimlane(1, 1));
-
-        $swimlanes = $this->client->getActiveSwimlanes(1);
-        $this->assertNotEmpty($swimlanes);
-        $this->assertCount(2, $swimlanes);
-        $this->assertEquals('Default swimlane', $swimlanes[0]['name']);
-        $this->assertEquals('Swimlane A', $swimlanes[1]['name']);
-    }
-
-    public function testMoveSwimlaneUp()
-    {
-        $this->assertTrue($this->client->enableSwimlane(1, 1));
-        $this->assertTrue($this->client->moveSwimlaneUp(1, 1));
-
-        $swimlanes = $this->client->getActiveSwimlanes(1);
-        $this->assertNotEmpty($swimlanes);
-        $this->assertCount(3, $swimlanes);
-        $this->assertEquals('Default swimlane', $swimlanes[0]['name']);
-        $this->assertEquals('Another swimlane', $swimlanes[1]['name']);
-        $this->assertEquals('Swimlane A', $swimlanes[2]['name']);
-
-        $this->assertTrue($this->client->moveSwimlaneUp(1, 2));
-
-        $swimlanes = $this->client->getActiveSwimlanes(1);
-        $this->assertNotEmpty($swimlanes);
-        $this->assertCount(3, $swimlanes);
-        $this->assertEquals('Default swimlane', $swimlanes[0]['name']);
-        $this->assertEquals('Swimlane A', $swimlanes[1]['name']);
-        $this->assertEquals('Another swimlane', $swimlanes[2]['name']);
-    }
-
-    public function testMoveSwimlaneDown()
-    {
-        $this->assertTrue($this->client->moveSwimlaneDown(1, 2));
-
-        $swimlanes = $this->client->getActiveSwimlanes(1);
-        $this->assertNotEmpty($swimlanes);
-        $this->assertCount(3, $swimlanes);
-        $this->assertEquals('Default swimlane', $swimlanes[0]['name']);
-        $this->assertEquals('Another swimlane', $swimlanes[1]['name']);
-        $this->assertEquals('Swimlane A', $swimlanes[2]['name']);
-    }
-
     public function testCreateTaskWithWrongMember()
     {
         $task = array(
@@ -394,18 +278,6 @@ class Api extends PHPUnit_Framework_TestCase
         $this->assertNotEquals($moved_timestamp, $task['date_moved']);
     }
 
-    public function testRemoveSwimlane()
-    {
-        $this->assertTrue($this->client->removeSwimlane(1, 2));
-
-        $task = $this->client->getTask($this->getTaskId());
-        $this->assertNotFalse($task);
-        $this->assertTrue(is_array($task));
-        $this->assertEquals(1, $task['position']);
-        $this->assertEquals(4, $task['column_id']);
-        $this->assertEquals(0, $task['swimlane_id']);
-    }
-
     public function testUpdateTask()
     {
         $task = $this->client->getTask(1);
diff --git a/tests/integration/SwimlaneTest.php b/tests/integration/SwimlaneTest.php
new file mode 100644
index 00000000..88747204
--- /dev/null
+++ b/tests/integration/SwimlaneTest.php
@@ -0,0 +1,103 @@
+<?php
+
+require_once __DIR__.'/Base.php';
+
+class SwimlaneTest extends Base
+{
+    public function testCreateProject()
+    {
+        $this->assertEquals(1, $this->app->createProject('A project'));
+    }
+
+    public function testGetDefaultSwimlane()
+    {
+        $swimlane = $this->app->getDefaultSwimlane(1);
+        $this->assertNotEmpty($swimlane);
+        $this->assertEquals('Default swimlane', $swimlane['default_swimlane']);
+    }
+
+    public function testAddSwimlane()
+    {
+        $swimlane_id = $this->app->addSwimlane(1, 'Swimlane 1');
+        $this->assertNotFalse($swimlane_id);
+        $this->assertInternalType('int', $swimlane_id);
+
+        $swimlane = $this->app->getSwimlaneById($swimlane_id);
+        $this->assertNotEmpty($swimlane);
+        $this->assertInternalType('array', $swimlane);
+        $this->assertEquals('Swimlane 1', $swimlane['name']);
+    }
+
+    public function testGetSwimlane()
+    {
+        $swimlane = $this->app->getSwimlane(1);
+        $this->assertInternalType('array', $swimlane);
+        $this->assertEquals('Swimlane 1', $swimlane['name']);
+    }
+
+    public function testUpdateSwimlane()
+    {
+        $swimlane = $this->app->getSwimlaneByName(1, 'Swimlane 1');
+        $this->assertInternalType('array', $swimlane);
+        $this->assertEquals(1, $swimlane['id']);
+        $this->assertEquals('Swimlane 1', $swimlane['name']);
+
+        $this->assertTrue($this->app->updateSwimlane($swimlane['id'], 'Another swimlane'));
+
+        $swimlane = $this->app->getSwimlaneById($swimlane['id']);
+        $this->assertEquals('Another swimlane', $swimlane['name']);
+    }
+
+    public function testDisableSwimlane()
+    {
+        $this->assertTrue($this->app->disableSwimlane(1, 1));
+
+        $swimlane = $this->app->getSwimlaneById(1);
+        $this->assertEquals(0, $swimlane['is_active']);
+    }
+
+    public function testEnableSwimlane()
+    {
+        $this->assertTrue($this->app->enableSwimlane(1, 1));
+
+        $swimlane = $this->app->getSwimlaneById(1);
+        $this->assertEquals(1, $swimlane['is_active']);
+    }
+
+    public function testGetAllSwimlanes()
+    {
+        $this->assertNotFalse($this->app->addSwimlane(1, 'Swimlane A'));
+
+        $swimlanes = $this->app->getAllSwimlanes(1);
+        $this->assertCount(2, $swimlanes);
+        $this->assertEquals('Another swimlane', $swimlanes[0]['name']);
+        $this->assertEquals('Swimlane A', $swimlanes[1]['name']);
+    }
+
+    public function testGetActiveSwimlane()
+    {
+        $this->assertTrue($this->app->disableSwimlane(1, 1));
+
+        $swimlanes = $this->app->getActiveSwimlanes(1);
+        $this->assertCount(2, $swimlanes);
+        $this->assertEquals('Default swimlane', $swimlanes[0]['name']);
+        $this->assertEquals('Swimlane A', $swimlanes[1]['name']);
+    }
+
+    public function testRemoveSwimlane()
+    {
+        $this->assertTrue($this->app->removeSwimlane(1, 2));
+    }
+
+    public function testChangePosition()
+    {
+        $this->assertNotFalse($this->app->addSwimlane(1, 'Swimlane 1'));
+        $this->assertNotFalse($this->app->addSwimlane(1, 'Swimlane 2'));
+
+        $swimlanes = $this->app->getAllSwimlanes(1);
+        $this->assertCount(3, $swimlanes);
+
+        $this->assertTrue($this->app->changeSwimlanePosition(1, 1, 3));
+        $this->assertFalse($this->app->changeSwimlanePosition(1, 1, 6));
+    }
+}
diff --git a/tests/units/Model/SwimlaneTest.php b/tests/units/Model/SwimlaneTest.php
index 3d048abd..f8b496cf 100644
--- a/tests/units/Model/SwimlaneTest.php
+++ b/tests/units/Model/SwimlaneTest.php
@@ -86,7 +86,23 @@ class SwimlaneTest extends Base
         $this->assertEquals(0, $default['show_default_swimlane']);
     }
 
-    public function testDisable()
+    public function testDisableEnableDefaultSwimlane()
+    {
+        $projectModel = new Project($this->container);
+        $swimlaneModel = new Swimlane($this->container);
+
+        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
+
+        $this->assertTrue($swimlaneModel->disableDefault(1));
+        $default = $swimlaneModel->getDefault(1);
+        $this->assertEquals(0, $default['show_default_swimlane']);
+
+        $this->assertTrue($swimlaneModel->enableDefault(1));
+        $default = $swimlaneModel->getDefault(1);
+        $this->assertEquals(1, $default['show_default_swimlane']);
+    }
+
+    public function testDisableEnable()
     {
         $p = new Project($this->container);
         $s = new Swimlane($this->container);
@@ -210,172 +226,6 @@ class SwimlaneTest extends Base
         $this->assertEquals(1, $swimlane['position']);
     }
 
-    public function testMoveUp()
-    {
-        $p = new Project($this->container);
-        $s = new Swimlane($this->container);
-
-        $this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
-        $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'Swimlane #1')));
-        $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'Swimlane #2')));
-        $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'Swimlane #3')));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(3, $swimlane['position']);
-
-        // Move the swimlane 3 up
-        $this->assertTrue($s->moveUp(1, 3));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(3, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-
-        // First swimlane can be moved up
-        $this->assertFalse($s->moveUp(1, 1));
-
-        // Move with a disabled swimlane
-        $this->assertTrue($s->disable(1, 1));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(0, $swimlane['is_active']);
-        $this->assertEquals(0, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        // Move the 2nd swimlane up
-        $this->assertTrue($s->moveUp(1, 2));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(0, $swimlane['is_active']);
-        $this->assertEquals(0, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-    }
-
-    public function testMoveDown()
-    {
-        $p = new Project($this->container);
-        $s = new Swimlane($this->container);
-
-        $this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
-        $this->assertEquals(1, $s->create(array('project_id' => 1, 'name' => 'Swimlane #1')));
-        $this->assertEquals(2, $s->create(array('project_id' => 1, 'name' => 'Swimlane #2')));
-        $this->assertEquals(3, $s->create(array('project_id' => 1, 'name' => 'Swimlane #3')));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(3, $swimlane['position']);
-
-        // Move the swimlane 1 down
-        $this->assertTrue($s->moveDown(1, 1));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(3, $swimlane['position']);
-
-        // Last swimlane can be moved down
-        $this->assertFalse($s->moveDown(1, 3));
-
-        // Move with a disabled swimlane
-        $this->assertTrue($s->disable(1, 3));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(0, $swimlane['is_active']);
-        $this->assertEquals(0, $swimlane['position']);
-
-        // Move the 2st swimlane down
-        $this->assertTrue($s->moveDown(1, 2));
-
-        $swimlane = $s->getById(1);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(1, $swimlane['position']);
-
-        $swimlane = $s->getById(2);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(1, $swimlane['is_active']);
-        $this->assertEquals(2, $swimlane['position']);
-
-        $swimlane = $s->getById(3);
-        $this->assertNotEmpty($swimlane);
-        $this->assertEquals(0, $swimlane['is_active']);
-        $this->assertEquals(0, $swimlane['position']);
-    }
-
     public function testDuplicateSwimlane()
     {
         $p = new Project($this->container);
@@ -406,4 +256,93 @@ class SwimlaneTest extends Base
         $new_default = $s->getDefault(2);
         $this->assertEquals('New Default', $new_default['default_swimlane']);
     }
+
+    public function testChangePosition()
+    {
+        $projectModel = new Project($this->container);
+        $swimlaneModel = new Swimlane($this->container);
+
+        $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+        $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1')));
+        $this->assertEquals(2, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #2')));
+        $this->assertEquals(3, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #3')));
+        $this->assertEquals(4, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #4')));
+
+        $swimlanes = $swimlaneModel->getAllByStatus(1);
+        $this->assertEquals(1, $swimlanes[0]['position']);
+        $this->assertEquals(1, $swimlanes[0]['id']);
+        $this->assertEquals(2, $swimlanes[1]['position']);
+        $this->assertEquals(2, $swimlanes[1]['id']);
+        $this->assertEquals(3, $swimlanes[2]['position']);
+        $this->assertEquals(3, $swimlanes[2]['id']);
+
+        $this->assertTrue($swimlaneModel->changePosition(1, 3, 2));
+
+        $swimlanes = $swimlaneModel->getAllByStatus(1);
+        $this->assertEquals(1, $swimlanes[0]['position']);
+        $this->assertEquals(1, $swimlanes[0]['id']);
+        $this->assertEquals(2, $swimlanes[1]['position']);
+        $this->assertEquals(3, $swimlanes[1]['id']);
+        $this->assertEquals(3, $swimlanes[2]['position']);
+        $this->assertEquals(2, $swimlanes[2]['id']);
+
+        $this->assertTrue($swimlaneModel->changePosition(1, 2, 1));
+
+        $swimlanes = $swimlaneModel->getAllByStatus(1);
+        $this->assertEquals(1, $swimlanes[0]['position']);
+        $this->assertEquals(2, $swimlanes[0]['id']);
+        $this->assertEquals(2, $swimlanes[1]['position']);
+        $this->assertEquals(1, $swimlanes[1]['id']);
+        $this->assertEquals(3, $swimlanes[2]['position']);
+        $this->assertEquals(3, $swimlanes[2]['id']);
+
+        $this->assertTrue($swimlaneModel->changePosition(1, 2, 2));
+
+        $swimlanes = $swimlaneModel->getAllByStatus(1);
+        $this->assertEquals(1, $swimlanes[0]['position']);
+        $this->assertEquals(1, $swimlanes[0]['id']);
+        $this->assertEquals(2, $swimlanes[1]['position']);
+        $this->assertEquals(2, $swimlanes[1]['id']);
+        $this->assertEquals(3, $swimlanes[2]['position']);
+        $this->assertEquals(3, $swimlanes[2]['id']);
+
+        $this->assertTrue($swimlaneModel->changePosition(1, 4, 1));
+
+        $swimlanes = $swimlaneModel->getAllByStatus(1);
+        $this->assertEquals(1, $swimlanes[0]['position']);
+        $this->assertEquals(4, $swimlanes[0]['id']);
+        $this->assertEquals(2, $swimlanes[1]['position']);
+        $this->assertEquals(1, $swimlanes[1]['id']);
+        $this->assertEquals(3, $swimlanes[2]['position']);
+        $this->assertEquals(2, $swimlanes[2]['id']);
+
+        $this->assertFalse($swimlaneModel->changePosition(1, 2, 0));
+        $this->assertFalse($swimlaneModel->changePosition(1, 2, 5));
+    }
+
+    public function testChangePositionWithInactiveSwimlane()
+    {
+        $projectModel = new Project($this->container);
+        $swimlaneModel = new Swimlane($this->container);
+
+        $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
+        $this->assertEquals(1, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #1')));
+        $this->assertEquals(2, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #2', 'is_active' => 0)));
+        $this->assertEquals(3, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #3', 'is_active' => 0)));
+        $this->assertEquals(4, $swimlaneModel->create(array('project_id' => 1, 'name' => 'Swimlane #4')));
+
+        $swimlanes = $swimlaneModel->getAllByStatus(1);
+        $this->assertEquals(1, $swimlanes[0]['position']);
+        $this->assertEquals(1, $swimlanes[0]['id']);
+        $this->assertEquals(2, $swimlanes[1]['position']);
+        $this->assertEquals(4, $swimlanes[1]['id']);
+
+        $this->assertTrue($swimlaneModel->changePosition(1, 4, 1));
+
+        $swimlanes = $swimlaneModel->getAllByStatus(1);
+        $this->assertEquals(1, $swimlanes[0]['position']);
+        $this->assertEquals(4, $swimlanes[0]['id']);
+        $this->assertEquals(2, $swimlanes[1]['position']);
+        $this->assertEquals(1, $swimlanes[1]['id']);
+    }
 }
-- 
cgit v1.2.3