From 3b3f6296ee75e30a2282a6334fd1b5a66cf49b3c Mon Sep 17 00:00:00 2001
From: Nicole Cordes <typo3@cordes.co>
Date: Mon, 30 Jan 2017 19:01:32 +0100
Subject: [PATCH] [BUGFIX] Correct handling of ObjectStorage objects in
 PaginateController
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The current implementation for getting paginated items from ObjectStorage
objects in TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController
enforces null items if the count of objects is lower than the
itemsPerPage count. This patch calculates the correct count and returns
valid items only.

Resolves: #79559
Releases: master, 7.6
Change-Id: I5746d83120474d6246da949f67096459ed1076d7
Reviewed-on: https://review.typo3.org/51475
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Jasmina Ließmann <code@frauliessmann.de>
Tested-by: Jasmina Ließmann <code@frauliessmann.de>
Reviewed-by: Christian Matthes
Tested-by: Christian Matthes
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
---
 .../Widget/Controller/PaginateController.php  |  5 +++--
 .../Controller/PaginateControllerTest.php     | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Widget/Controller/PaginateController.php b/typo3/sysext/fluid/Classes/ViewHelpers/Widget/Controller/PaginateController.php
index 7c5f47247350..556035930183 100644
--- a/typo3/sysext/fluid/Classes/ViewHelpers/Widget/Controller/PaginateController.php
+++ b/typo3/sysext/fluid/Classes/ViewHelpers/Widget/Controller/PaginateController.php
@@ -182,9 +182,10 @@ class PaginateController extends AbstractWidgetController
             return $modifiedObjects;
         } elseif ($this->objects instanceof ObjectStorage) {
             $modifiedObjects = [];
-            $endOfRange = $offset + $itemsPerPage;
+            $objectArray = $this->objects->toArray();
+            $endOfRange = min($offset + $itemsPerPage, count($objectArray));
             for ($i = $offset; $i < $endOfRange; $i++) {
-                $modifiedObjects[] = $this->objects->toArray()[$i];
+                $modifiedObjects[] = $objectArray[$i];
             }
             return $modifiedObjects;
         } elseif (is_array($this->objects)) {
diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php
index af2b26ddda65..48b041595454 100644
--- a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php
+++ b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php
@@ -220,6 +220,25 @@ class PaginateControllerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestC
         $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
     }
 
+    /**
+     * @test
+     */
+    public function prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndLastPage()
+    {
+        $this->controller->_set('currentPage', 3);
+        $objects = new ObjectStorage();
+        for ($i = 0; $i <= 25; $i++) {
+            $item = new \stdClass;
+            $objects->attach($item);
+        }
+        $this->controller->_set('objects', $objects);
+        $expectedPortion = [];
+        for ($j = 20; $j <= 25; $j++) {
+            $expectedPortion[] = $objects->toArray()[$j];
+        }
+        $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 20));
+    }
+
     /**
      * @test
      */
-- 
GitLab