[BUGFIX] Fluid paginate widget wrong number of links 36/25536/2
authorKlaas Johan Kooistra <k.kooistra@drecomm.nl>
Fri, 8 Nov 2013 15:42:09 +0000 (16:42 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Wed, 20 Nov 2013 18:13:59 +0000 (19:13 +0100)
If the maximumNumberOfLinks configuration option is set to 8
the widget will render 10 links.

Solved the problem by changing a plus to a minus in the calculation
of the display range end and added unit tests for several cases.

Resolves: #53458
Releases: 6.2, 6.1, 6.0
Change-Id: Ic1c55b0bf925b546d119c67433f63e662493a08b
Reviewed-on: https://review.typo3.org/25536
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
typo3/sysext/fluid/Classes/ViewHelpers/Widget/Controller/PaginateController.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php [new file with mode: 0644]

index 68b8b36..5b2e9d5 100644 (file)
@@ -100,7 +100,7 @@ class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetCont
                }
                $delta = floor($maximumNumberOfLinks / 2);
                $this->displayRangeStart = $this->currentPage - $delta;
-               $this->displayRangeEnd = $this->currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
+               $this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
                if ($this->displayRangeStart < 1) {
                        $this->displayRangeEnd -= $this->displayRangeStart - 1;
                }
@@ -141,4 +141,4 @@ class PaginateController extends \TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetCont
        }
 }
 
-?>
\ No newline at end of file
+?>
diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Widget/Controller/PaginateControllerTest.php
new file mode 100644 (file)
index 0000000..67b30e7
--- /dev/null
@@ -0,0 +1,113 @@
+<?php
+namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller;
+
+/*                                                                        *
+ * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU Lesser General Public License, either version 3   *
+ *  of the License, or (at your option) any later version.                *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * Testcase for PaginateController
+ */
+class PaginateControllerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController
+        */
+       protected $controller;
+
+       /**
+        * Sets up this test case
+        *
+        * @return void
+        */
+       public function setUp() {
+               $this->controller = $this->getAccessibleMock('TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController', array('dummy'), array(), '', FALSE);
+       }
+
+       /**
+        * Cleans up this test case
+        *
+        * @return void
+        */
+       public function tearDown() {
+               unset($this->controller);
+       }
+
+       /**
+        * @test
+        */
+       public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinks() {
+               $this->controller->_set('maximumNumberOfLinks', 8);
+               $this->controller->_set('numberOfPages', 100);
+               $this->controller->_set('currentPage', 50);
+               $this->controller->_call('calculateDisplayRange');
+               $this->assertSame(46, $this->controller->_get('displayRangeStart'));
+               $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
+       }
+
+       /**
+        * @test
+        */
+       public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinks() {
+               $this->controller->_set('maximumNumberOfLinks', 7);
+               $this->controller->_set('numberOfPages', 100);
+               $this->controller->_set('currentPage', 50);
+               $this->controller->_call('calculateDisplayRange');
+               $this->assertSame(47, $this->controller->_get('displayRangeStart'));
+               $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
+       }
+
+       /**
+        * @test
+        */
+       public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnFirstPage() {
+               $this->controller->_set('maximumNumberOfLinks', 8);
+               $this->controller->_set('numberOfPages', 100);
+               $this->controller->_set('currentPage', 1);
+               $this->controller->_call('calculateDisplayRange');
+               $this->assertSame(1, $this->controller->_get('displayRangeStart'));
+               $this->assertSame(8, $this->controller->_get('displayRangeEnd'));
+       }
+
+       /**
+        * @test
+        */
+       public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnFirstPage() {
+               $this->controller->_set('maximumNumberOfLinks', 7);
+               $this->controller->_set('numberOfPages', 100);
+               $this->controller->_set('currentPage', 1);
+               $this->controller->_call('calculateDisplayRange');
+               $this->assertSame(1, $this->controller->_get('displayRangeStart'));
+               $this->assertSame(7, $this->controller->_get('displayRangeEnd'));
+       }
+
+       /**
+        * @test
+        */
+       public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnLastPage() {
+               $this->controller->_set('maximumNumberOfLinks', 8);
+               $this->controller->_set('numberOfPages', 100);
+               $this->controller->_set('currentPage', 100);
+               $this->controller->_call('calculateDisplayRange');
+               $this->assertSame(93, $this->controller->_get('displayRangeStart'));
+               $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
+       }
+
+       /**
+        * @test
+        */
+       public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnLastPage() {
+               $this->controller->_set('maximumNumberOfLinks', 7);
+               $this->controller->_set('numberOfPages', 100);
+               $this->controller->_set('currentPage', 100);
+               $this->controller->_call('calculateDisplayRange');
+               $this->assertSame(94, $this->controller->_get('displayRangeStart'));
+               $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
+       }
+}
\ No newline at end of file