diff --git a/application/inc/Http/Controllers/Admin/CategoryController.php b/application/inc/Http/Controllers/Admin/CategoryController.php index ffc0c4c5..24c4b55e 100644 --- a/application/inc/Http/Controllers/Admin/CategoryController.php +++ b/application/inc/Http/Controllers/Admin/CategoryController.php @@ -66,7 +66,7 @@ public function create(Request $request): JsonResponse throw new InvalidInput(_('You must enter a title and choose a location for the new category.')); } - $icon = app('orm')->getOne(File::class, $iconId); + $icon = $iconId ? app('orm')->getOne(File::class, $iconId) : null; $parent = app('orm')->getOne(Category::class, $parentId); $category = new Category([ diff --git a/tests/Feature/Http/Controllers/Admin/CategoryControllerTest.php b/tests/Feature/Http/Controllers/Admin/CategoryControllerTest.php index 267cc2e9..fd780264 100644 --- a/tests/Feature/Http/Controllers/Admin/CategoryControllerTest.php +++ b/tests/Feature/Http/Controllers/Admin/CategoryControllerTest.php @@ -44,6 +44,31 @@ public function testCreate(): void ); } + public function testCreateNoIcon(): void + { + $data = [ + 'title' => 'New title', + 'parentId' => -1, + 'render_mode' => 2, + 'email' => 'mail@example.com', + 'icon_id' => null, + ]; + + $this->json('POST', '/admin/categories/', $data) + ->assertResponseStatus(200); + + $this->assertDatabaseHas( + 'kat', + [ + 'navn' => $data['title'], + 'bind' => $data['parentId'], + 'vis' => $data['render_mode'], + 'email' => $data['email'], + 'icon_id' => $data['icon_id'], + ] + ); + } + public function testCreateNoTitle(): void { $data = [ diff --git a/tests/TestCase.php b/tests/TestCase.php index 0027b949..699fff2d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -245,6 +245,11 @@ private function isInDatabase(string $table, array $data): bool { $sets = []; foreach ($data as $filedName => $value) { + if ($value === null) { + $sets[] = '`' . $filedName . '` IS NULL'; + continue; + } + $sets[] = '`' . $filedName . '` = ' . app('db')->quote($value); } $query = 'SELECT * FROM `' . $table . '` WHERE ' . implode(' AND ', $sets);