浏览代码

Fix counter

mscherer 5 年之前
父节点
当前提交
2ee7f1df26
共有 2 个文件被更改,包括 33 次插入8 次删除
  1. 6 2
      src/View/Helper/FormatHelper.php
  2. 27 6
      tests/TestCase/View/Helper/FormatHelperTest.php

+ 6 - 2
src/View/Helper/FormatHelper.php

@@ -495,12 +495,16 @@ class FormatHelper extends Helper {
 		$currentPage = $paginator['page'];
 		$pageCount = $paginator['pageCount'];
 		$totalCount = $paginator['count'];
-		$limit = $paginator['limit'];
+		$limit = $paginator['perPage'];
 		$step = isset($paginator['step']) ? $paginator['step'] : 1;
 
 		if ($dir === 'DESC') {
+			$count = $limit - $count + 1;
+		}
+
+		if ($dir === 'DESC') {
 			$currentCount = $count + ($pageCount - $currentPage) * $limit * $step;
-			if ($currentPage != $pageCount && $pageCount > 1) {
+			if ($pageCount > 1) {
 				$currentCount -= $pageCount * $limit * $step - $totalCount;
 			}
 		} else {

+ 27 - 6
tests/TestCase/View/Helper/FormatHelperTest.php

@@ -219,8 +219,6 @@ class FormatHelperTest extends TestCase {
 	}
 
 	/**
-	 * FormatHelperTest::testPad()
-	 *
 	 * @return void
 	 */
 	public function testPad() {
@@ -234,8 +232,6 @@ class FormatHelperTest extends TestCase {
 	}
 
 	/**
-	 * FormatHelperTest::testAbsolutePaginateCount()
-	 *
 	 * @return void
 	 */
 	public function testAbsolutePaginateCount() {
@@ -243,10 +239,35 @@ class FormatHelperTest extends TestCase {
 			'page' => 1,
 			'pageCount' => 3,
 			'count' => 25,
-			'limit' => 10,
+			'perPage' => 10,
 		];
 		$result = $this->Format->absolutePaginateCount($paginator, 2);
-		$this->assertEquals(2, $result);
+		$this->assertSame(2, $result);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testAbsolutePaginateCountDesc() {
+		// 2nd element on page 1
+		$paginator = [
+			'page' => 1,
+			'pageCount' => 3,
+			'count' => 25,
+			'perPage' => 10,
+		];
+		$result = $this->Format->absolutePaginateCount($paginator, 2, 'DESC');
+		$this->assertSame(24, $result);
+
+		// 3rd element on page 3
+		$paginator = [
+			'page' => 3,
+			'pageCount' => 3,
+			'count' => 25,
+			'perPage' => 10,
+		];
+		$result = $this->Format->absolutePaginateCount($paginator, 3, 'DESC');
+		$this->assertSame(3, $result);
 	}
 
 	/**