From 748eff0fca429d93752f7bbcd36fdcc7de08c3c2 Mon Sep 17 00:00:00 2001
From: Susanne Moog <look@susi.dev>
Date: Wed, 25 Mar 2020 18:12:16 +0100
Subject: [PATCH] [BUGFIX] Reset number of results in QueryResult after offset
 changes

Using offsetSet or unset allows manipulating the QueryResult manually.
As the result of `count` is cached, offset changes need to reset that
cache to accurately return the correct offset.

Resolves: #76310
Releases: master, 9.5
Change-Id: Ic43f3810766bdb1ee02800b07f6853c81ba404f5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63919
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
---
 .../Persistence/Generic/QueryResult.php        |  2 ++
 .../Persistence/Generic/QueryResultTest.php    | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/typo3/sysext/extbase/Classes/Persistence/Generic/QueryResult.php b/typo3/sysext/extbase/Classes/Persistence/Generic/QueryResult.php
index ab933ad46fbd..4d84610a4663 100644
--- a/typo3/sysext/extbase/Classes/Persistence/Generic/QueryResult.php
+++ b/typo3/sysext/extbase/Classes/Persistence/Generic/QueryResult.php
@@ -193,6 +193,7 @@ class QueryResult implements QueryResultInterface
     public function offsetSet($offset, $value)
     {
         $this->initialize();
+        $this->numberOfResults = null;
         $this->queryResult[$offset] = $value;
     }
 
@@ -205,6 +206,7 @@ class QueryResult implements QueryResultInterface
     public function offsetUnset($offset)
     {
         $this->initialize();
+        $this->numberOfResults = null;
         unset($this->queryResult[$offset]);
     }
 
diff --git a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryResultTest.php b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryResultTest.php
index 1432b9614e8c..01319cad3c62 100644
--- a/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryResultTest.php
+++ b/typo3/sysext/extbase/Tests/Unit/Persistence/Generic/QueryResultTest.php
@@ -159,6 +159,24 @@ class QueryResultTest extends UnitTestCase
         self::assertEquals(2, $this->queryResult->count());
     }
 
+    /**
+     * @test
+     */
+    public function countCallsGetObjectCountByQueryIfOffsetChanges()
+    {
+        $this->mockPersistenceManager->expects(self::once())->method('getObjectCountByQuery')->willReturn(2);
+        $firstCount = $this->queryResult->count();
+        $this->queryResult->offsetSet(3, new \stdClass());
+        $this->queryResult->offsetSet(4, new \stdClass());
+        $secondCount = $this->queryResult->count();
+        $this->queryResult->offsetUnset(1);
+        $thirdCount = $this->queryResult->count();
+
+        self::assertSame(2, $firstCount);
+        self::assertSame(4, $secondCount);
+        self::assertSame(3, $thirdCount);
+    }
+
     /**
      * @test
      */
-- 
GitLab