[BUGFIX] Correct handling of ObjectStorage objects in PaginateController 49/51849/2
authorNicole Cordes <typo3@cordes.co>
Mon, 30 Jan 2017 18:01:32 +0000 (19:01 +0100)
committerNicole Cordes <typo3@cordes.co>
Sat, 25 Feb 2017 10:58:15 +0000 (11:58 +0100)
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/51849
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/fluid/Classes/ViewHelpers/Widget/Controller/PaginateController.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php

index 9626356..070fea1 100644 (file)
@@ -189,9 +189,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)) {
index 8d0d2f0..d2ef138 100644 (file)
@@ -222,6 +222,25 @@ class PaginateControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     /**
      * @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
+     */
     public function prepareObjectsSliceReturnsCorrectPortionForArrayAndFirstPage()
     {
         $objects = [];