From 7283bfaef6aeeb62aed2478e201f28d7361633fd Mon Sep 17 00:00:00 2001 From: Rafael de Camargo Date: Tue, 23 Jul 2019 17:19:12 -0300 Subject: Implements check for duplicate default categories --- app/Model/CategoryModel.php | 14 +++++--------- tests/units/Model/CategoryModelTest.php | 3 ++- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/Model/CategoryModel.php b/app/Model/CategoryModel.php index 95e9e97e..441cc4a7 100644 --- a/app/Model/CategoryModel.php +++ b/app/Model/CategoryModel.php @@ -137,17 +137,13 @@ class CategoryModel extends Base public function createDefaultCategories($project_id) { $results = array(); - $categories = explode(',', $this->configModel->get('project_categories')); + $categories = array_unique(explode_csv_field($this->configModel->get('project_categories'))); foreach ($categories as $category) { - $category = trim($category); - - if (! empty($category)) { - $results[] = $this->db->table(self::TABLE)->insert(array( - 'project_id' => $project_id, - 'name' => $category, - )); - } + $results[] = $this->db->table(self::TABLE)->insert(array( + 'project_id' => $project_id, + 'name' => $category, + )); } return in_array(false, $results, true); diff --git a/tests/units/Model/CategoryModelTest.php b/tests/units/Model/CategoryModelTest.php index 6147b3a5..9916341b 100644 --- a/tests/units/Model/CategoryModelTest.php +++ b/tests/units/Model/CategoryModelTest.php @@ -156,7 +156,8 @@ class CategoryModelTest extends Base $categoryModel = new CategoryModel($this->container); $configModel = new ConfigModel($this->container); - $this->assertTrue($configModel->save(array('project_categories' => 'C1, C2, C3'))); + // Custom categories: spaces should be trimed, no empty and no duplicates + $this->assertTrue($configModel->save(array('project_categories' => 'C1, C2, C2 , C3, C1, '))); $this->assertEquals(1, $projectModel->create(array('name' => 'Project #1'))); $this->assertTrue($categoryModel->createDefaultCategories(1)); -- cgit v1.2.3