diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-07-20 21:40:27 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-07-20 21:40:27 -0400 |
commit | 7b100cc57b58a1ef5c2c6cef28904e1ba79f0c49 (patch) | |
tree | 9cdd956135dc3083d522b77989c12e94664b7069 | |
parent | 3042504f9983e79e792e2df4e981ae310ab7ebeb (diff) |
Move logic of PR #2496 to GroupModel
-rw-r--r-- | app/Controller/ProjectPermissionController.php | 8 | ||||
-rw-r--r-- | app/Model/GroupModel.php | 19 | ||||
-rw-r--r-- | tests/units/Model/GroupModelTest.php (renamed from tests/units/Model/GroupTest.php) | 11 |
3 files changed, 30 insertions, 8 deletions
diff --git a/app/Controller/ProjectPermissionController.php b/app/Controller/ProjectPermissionController.php index cbcdf77d..99f556e8 100644 --- a/app/Controller/ProjectPermissionController.php +++ b/app/Controller/ProjectPermissionController.php @@ -147,13 +147,7 @@ class ProjectPermissionController extends BaseController $values = $this->request->getValues(); if (empty($values['group_id']) && ! empty($values['external_id'])) { - $group = $this->groupModel->getByExternalId($values['external_id']); - if ($group) { - $values['group_id'] = $group['id']; - } - else { - $values['group_id'] = $this->groupModel->create($values['name'], $values['external_id']); - } + $values['group_id'] = $this->groupModel->getOrCreateExternalGroupId($values['name'], $values['external_id']); } if ($this->projectGroupRoleModel->addGroup($project['id'], $values['group_id'], $values['role'])) { diff --git a/app/Model/GroupModel.php b/app/Model/GroupModel.php index 0a975570..b43423b3 100644 --- a/app/Model/GroupModel.php +++ b/app/Model/GroupModel.php @@ -116,4 +116,23 @@ class GroupModel extends Base { return $this->db->table(self::TABLE)->eq('id', $values['id'])->update($values); } + + /** + * Get groupId from externalGroupId and create the group if not found + * + * @access public + * @param string $name + * @param string $external_id + * @return bool|integer + */ + public function getOrCreateExternalGroupId($name, $external_id) + { + $group_id = $this->db->table(self::TABLE)->eq('external_id', $external_id)->findOneColumn('id'); + + if (empty($group_id)) { + $group_id = $this->create($name, $external_id); + } + + return $group_id; + } } diff --git a/tests/units/Model/GroupTest.php b/tests/units/Model/GroupModelTest.php index 85c2c5d9..4ad0a167 100644 --- a/tests/units/Model/GroupTest.php +++ b/tests/units/Model/GroupModelTest.php @@ -4,7 +4,7 @@ require_once __DIR__.'/../Base.php'; use Kanboard\Model\GroupModel; -class GroupTest extends Base +class GroupModelTest extends Base { public function testCreation() { @@ -57,4 +57,13 @@ class GroupTest extends Base $this->assertTrue($groupModel->remove(1)); $this->assertEmpty($groupModel->getById(1)); } + + public function testGetOrCreateExternalGroupId() + { + $groupModel = new GroupModel($this->container); + $this->assertEquals(1, $groupModel->create('Group 1', 'ExternalId1')); + $this->assertEquals(1, $groupModel->getOrCreateExternalGroupId('Group 1', 'ExternalId1')); + $this->assertEquals(1, $groupModel->getOrCreateExternalGroupId('Group 2', 'ExternalId1')); + $this->assertEquals(2, $groupModel->getOrCreateExternalGroupId('Group 2', 'ExternalId2')); + } } |