From 18ed87cbd3ce7c10e777d727785e0c2ea6791d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sat, 4 Mar 2023 13:59:27 -0800 Subject: [PATCH] Filtering by category does not show results when the category name is a number Fixes #4789 --- app/Filter/TaskCategoryFilter.php | 3 + tests/units/Filter/TaskCategoryFilterTest.php | 105 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/units/Filter/TaskCategoryFilterTest.php diff --git a/app/Filter/TaskCategoryFilter.php b/app/Filter/TaskCategoryFilter.php index c8ca54ade..514d7a5aa 100644 --- a/app/Filter/TaskCategoryFilter.php +++ b/app/Filter/TaskCategoryFilter.php @@ -34,7 +34,10 @@ class TaskCategoryFilter extends BaseFilter implements FilterInterface public function apply() { if (is_int($this->value) || ctype_digit((string) $this->value)) { + $this->query->beginOr(); $this->query->eq(TaskModel::TABLE.'.category_id', $this->value); + $this->query->eq(CategoryModel::TABLE.'.name', $this->value); + $this->query->closeOr(); } elseif ($this->value === 'none') { $this->query->eq(TaskModel::TABLE.'.category_id', 0); } else { diff --git a/tests/units/Filter/TaskCategoryFilterTest.php b/tests/units/Filter/TaskCategoryFilterTest.php new file mode 100644 index 000000000..d38a2e4ed --- /dev/null +++ b/tests/units/Filter/TaskCategoryFilterTest.php @@ -0,0 +1,105 @@ +container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $categoryModel = new CategoryModel($this->container); + $query = $taskFinderModel->getExtendedQuery(); + + $this->assertEquals(1, $projectModel->create(['name' => 'Test'])); + $this->assertEquals(1, $categoryModel->create(['name' => 'Some category', 'project_id' => 1])); + $this->assertEquals(1, $taskCreationModel->create(['project_id' => 1, 'title' => 'test1'])); + $this->assertEquals(2, $taskCreationModel->create(['project_id' => 1, 'title' => 'test2', 'category_id' => 1])); + + $filter = new TaskCategoryFilter(); + $filter->withQuery($query); + $filter->withValue('Some category'); + $filter->apply(); + + $tasks = $query->findAll(); + $this->assertCount(1, $tasks); + $this->assertEquals('test2', $tasks[0]['title']); + } + + public function testFilterByCategoryID() + { + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $categoryModel = new CategoryModel($this->container); + $query = $taskFinderModel->getExtendedQuery(); + + $this->assertEquals(1, $projectModel->create(['name' => 'Test'])); + $this->assertEquals(1, $categoryModel->create(['name' => 'Some category', 'project_id' => 1])); + $this->assertEquals(1, $taskCreationModel->create(['project_id' => 1, 'title' => 'test1'])); + $this->assertEquals(2, $taskCreationModel->create(['project_id' => 1, 'title' => 'test2', 'category_id' => 1])); + + $filter = new TaskCategoryFilter(); + $filter->withQuery($query); + $filter->withValue('1'); + $filter->apply(); + + $tasks = $query->findAll(); + $this->assertCount(1, $tasks); + $this->assertEquals('test2', $tasks[0]['title']); + } + + public function testFilterByNoCategory() + { + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $categoryModel = new CategoryModel($this->container); + $query = $taskFinderModel->getExtendedQuery(); + + $this->assertEquals(1, $projectModel->create(['name' => 'Test'])); + $this->assertEquals(1, $categoryModel->create(['name' => 'Some category', 'project_id' => 1])); + $this->assertEquals(1, $taskCreationModel->create(['project_id' => 1, 'title' => 'test1'])); + $this->assertEquals(2, $taskCreationModel->create(['project_id' => 1, 'title' => 'test2', 'category_id' => 1])); + + $filter = new TaskCategoryFilter(); + $filter->withQuery($query); + $filter->withValue('none'); + $filter->apply(); + + $tasks = $query->findAll(); + $this->assertCount(1, $tasks); + $this->assertEquals('test1', $tasks[0]['title']); + } + + public function testFilterByNumericCategoryName() + { + $taskFinderModel = new TaskFinderModel($this->container); + $projectModel = new ProjectModel($this->container); + $taskCreationModel = new TaskCreationModel($this->container); + $categoryModel = new CategoryModel($this->container); + $query = $taskFinderModel->getExtendedQuery(); + + $this->assertEquals(1, $projectModel->create(['name' => 'Test'])); + $this->assertEquals(1, $categoryModel->create(['name' => '1234', 'project_id' => 1])); + $this->assertEquals(1, $taskCreationModel->create(['project_id' => 1, 'title' => 'test1'])); + $this->assertEquals(2, $taskCreationModel->create(['project_id' => 1, 'title' => 'test2', 'category_id' => 1])); + + $filter = new TaskCategoryFilter(); + $filter->withQuery($query); + $filter->withValue('1234'); + $filter->apply(); + + $tasks = $query->findAll(); + $this->assertCount(1, $tasks); + $this->assertEquals('test2', $tasks[0]['title']); + } +}