From 2000ff3385ebdd66f074f379857edd3711cd7c86 Mon Sep 17 00:00:00 2001
From: Georg Ringer <georg.ringer@gmail.com>
Date: Tue, 11 Jun 2024 19:34:00 +0200
Subject: [PATCH] [BUGFIX] Check for existing category before processing it

The CategoryCollection might be called without an existing category uid
which must not lead to an exception to notices.

Resolves: #99989
Releases: main, 12.4
Change-Id: I6c0bb34d9f5b460f73b8c2933994d94b54d8f01f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/84624
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Classes/Category/Collection/CategoryCollection.php    | 8 ++++++++
 .../Category/Collection/CategoryCollectionTest.php        | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/typo3/sysext/core/Classes/Category/Collection/CategoryCollection.php b/typo3/sysext/core/Classes/Category/Collection/CategoryCollection.php
index 2b890c94a767..6d5d422ccc90 100644
--- a/typo3/sysext/core/Classes/Category/Collection/CategoryCollection.php
+++ b/typo3/sysext/core/Classes/Category/Collection/CategoryCollection.php
@@ -116,6 +116,14 @@ class CategoryCollection extends AbstractRecordCollection implements EditableCol
             ->executeQuery()
             ->fetchAssociative();
 
+        if ($collectionRecord === false) {
+            return GeneralUtility::makeInstance(
+                self::class,
+                $tableName,
+                $fieldName
+            );
+        }
+
         $collectionRecord['table_name'] = $tableName;
         $collectionRecord['field_name'] = $fieldName;
 
diff --git a/typo3/sysext/core/Tests/Functional/Category/Collection/CategoryCollectionTest.php b/typo3/sysext/core/Tests/Functional/Category/Collection/CategoryCollectionTest.php
index e00c3a5e107d..d436452b6882 100644
--- a/typo3/sysext/core/Tests/Functional/Category/Collection/CategoryCollectionTest.php
+++ b/typo3/sysext/core/Tests/Functional/Category/Collection/CategoryCollectionTest.php
@@ -152,6 +152,14 @@ final class CategoryCollectionTest extends FunctionalTestCase
         self::assertEquals(0, $collection->count());
     }
 
+    #[Test]
+    public function canLoadADummyCollectionFromNotExistingCategory(): void
+    {
+        $collection = CategoryCollection::load(-1, false, 'tx_test_test');
+        // Check the number of record
+        self::assertEquals(0, $collection->count());
+    }
+
     #[Test]
     public function canLoadADummyCollectionFromDatabaseAfterRemoveOneRelation(): void
     {
-- 
GitLab