From a39ee10af5dd14632d6fc20799e22569e5ab5878 Mon Sep 17 00:00:00 2001 From: lav45 Date: Fri, 17 Nov 2023 15:52:50 +0300 Subject: [PATCH 1/8] #20055 Fix X-Pagination-Total-Count is always 0 --- framework/data/ActiveDataProvider.php | 1 - framework/data/ArrayDataProvider.php | 10 +++------- framework/data/BaseDataProvider.php | 9 +++++---- framework/data/SqlDataProvider.php | 1 - 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/framework/data/ActiveDataProvider.php b/framework/data/ActiveDataProvider.php index 3a129aa7293..6343e320473 100644 --- a/framework/data/ActiveDataProvider.php +++ b/framework/data/ActiveDataProvider.php @@ -102,7 +102,6 @@ protected function prepareModels() } $query = clone $this->query; if (($pagination = $this->getPagination()) !== false) { - $pagination->totalCount = $this->getTotalCount(); if ($pagination->totalCount === 0) { return []; } diff --git a/framework/data/ArrayDataProvider.php b/framework/data/ArrayDataProvider.php index 657bcd2a1c3..feddc16ab11 100644 --- a/framework/data/ArrayDataProvider.php +++ b/framework/data/ArrayDataProvider.php @@ -86,14 +86,10 @@ protected function prepareModels() $models = $this->sortModels($models, $sort); } - if (($pagination = $this->getPagination()) !== false) { - $pagination->totalCount = $this->getTotalCount(); - - if ($pagination->getPageSize() > 0) { - $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true); - } + $pagination = $this->getPagination(); + if ($pagination !== false && $pagination->getPageSize() > 0) { + $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true); } - return $models; } diff --git a/framework/data/BaseDataProvider.php b/framework/data/BaseDataProvider.php index 82af0aa7c47..4e2a3c5ef98 100644 --- a/framework/data/BaseDataProvider.php +++ b/framework/data/BaseDataProvider.php @@ -165,10 +165,10 @@ public function getTotalCount() { if ($this->getPagination() === false) { return $this->getCount(); - } elseif ($this->_totalCount === null) { + } + if ($this->_totalCount === null) { $this->_totalCount = $this->prepareTotalCount(); } - return $this->_totalCount; } @@ -190,9 +190,10 @@ public function setTotalCount($value) public function getPagination() { if ($this->_pagination === null) { - $this->setPagination([]); + $this->setPagination([ + 'totalCount' => $this->getTotalCount() + ]); } - return $this->_pagination; } diff --git a/framework/data/SqlDataProvider.php b/framework/data/SqlDataProvider.php index 5a8f9631de5..12a1d2c0c78 100644 --- a/framework/data/SqlDataProvider.php +++ b/framework/data/SqlDataProvider.php @@ -127,7 +127,6 @@ protected function prepareModels() } if ($pagination !== false) { - $pagination->totalCount = $this->getTotalCount(); $limit = $pagination->getLimit(); $offset = $pagination->getOffset(); } From d17a212805d474d1eeb0799e19f4a226222f481e Mon Sep 17 00:00:00 2001 From: lav45 Date: Fri, 17 Nov 2023 16:11:06 +0300 Subject: [PATCH 2/8] Fix loop) --- framework/data/BaseDataProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/data/BaseDataProvider.php b/framework/data/BaseDataProvider.php index 4e2a3c5ef98..a36edbe2115 100644 --- a/framework/data/BaseDataProvider.php +++ b/framework/data/BaseDataProvider.php @@ -163,7 +163,7 @@ public function getCount() */ public function getTotalCount() { - if ($this->getPagination() === false) { + if ($this->_pagination === false) { return $this->getCount(); } if ($this->_totalCount === null) { From 7fa103cf8ba43bead0845f273fe1578a32ea813e Mon Sep 17 00:00:00 2001 From: lav45 Date: Fri, 17 Nov 2023 16:16:19 +0300 Subject: [PATCH 3/8] Fix \yii\data\BaseDataProvider::setPagination() --- framework/data/BaseDataProvider.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/framework/data/BaseDataProvider.php b/framework/data/BaseDataProvider.php index a36edbe2115..1ec7dda2e18 100644 --- a/framework/data/BaseDataProvider.php +++ b/framework/data/BaseDataProvider.php @@ -190,9 +190,7 @@ public function setTotalCount($value) public function getPagination() { if ($this->_pagination === null) { - $this->setPagination([ - 'totalCount' => $this->getTotalCount() - ]); + $this->setPagination([]); } return $this->_pagination; } @@ -212,7 +210,10 @@ public function getPagination() public function setPagination($value) { if (is_array($value)) { - $config = ['class' => Pagination::className()]; + $config = [ + 'class' => Pagination::className(), + 'totalCount' => $this->getTotalCount(), + ]; if ($this->id !== null) { $config['pageParam'] = $this->id . '-page'; $config['pageSizeParam'] = $this->id . '-per-page'; From 4a7639a9e188185b0a4a9418bfd2f235bcd8102a Mon Sep 17 00:00:00 2001 From: lav45 Date: Fri, 17 Nov 2023 16:25:52 +0300 Subject: [PATCH 4/8] Fix ActiveDataProviderTest::testPaginationBeforeModels --- framework/data/BaseDataProvider.php | 11 +++++------ tests/framework/data/ActiveDataProviderTest.php | 3 +-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/framework/data/BaseDataProvider.php b/framework/data/BaseDataProvider.php index 1ec7dda2e18..871f80515a3 100644 --- a/framework/data/BaseDataProvider.php +++ b/framework/data/BaseDataProvider.php @@ -210,16 +210,15 @@ public function getPagination() public function setPagination($value) { if (is_array($value)) { - $config = [ - 'class' => Pagination::className(), - 'totalCount' => $this->getTotalCount(), - ]; + $config = ['class' => Pagination::className()]; if ($this->id !== null) { $config['pageParam'] = $this->id . '-page'; $config['pageSizeParam'] = $this->id . '-per-page'; } - $this->_pagination = Yii::createObject(array_merge($config, $value)); - } elseif ($value instanceof Pagination || $value === false) { + $value = Yii::createObject(array_merge($config, $value)); + } + if ($value instanceof Pagination || $value === false) { + $value->totalCount = $this->getTotalCount(); $this->_pagination = $value; } else { throw new InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.'); diff --git a/tests/framework/data/ActiveDataProviderTest.php b/tests/framework/data/ActiveDataProviderTest.php index 57f866ee809..9849fd5cb4e 100644 --- a/tests/framework/data/ActiveDataProviderTest.php +++ b/tests/framework/data/ActiveDataProviderTest.php @@ -170,9 +170,8 @@ public function testPaginationBeforeModels() 'query' => $query->from('order')->orderBy('id'), ]); $pagination = $provider->getPagination(); - $this->assertEquals(0, $pagination->getPageCount()); - $this->assertCount(3, $provider->getModels()); $this->assertEquals(1, $pagination->getPageCount()); + $this->assertCount(3, $provider->getModels()); $provider->getPagination()->pageSize = 2; $this->assertCount(3, $provider->getModels()); From fa856d62f61fe98c775efc288ef9fafdb91aec16 Mon Sep 17 00:00:00 2001 From: lav45 Date: Fri, 17 Nov 2023 16:38:47 +0300 Subject: [PATCH 5/8] Fix ListViewTest::testRenderSummaryWhenPaginationIsFalseAndSummaryIsNull --- framework/data/BaseDataProvider.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/data/BaseDataProvider.php b/framework/data/BaseDataProvider.php index 871f80515a3..b631c0b7973 100644 --- a/framework/data/BaseDataProvider.php +++ b/framework/data/BaseDataProvider.php @@ -217,9 +217,11 @@ public function setPagination($value) } $value = Yii::createObject(array_merge($config, $value)); } - if ($value instanceof Pagination || $value === false) { + if ($value instanceof Pagination) { $value->totalCount = $this->getTotalCount(); $this->_pagination = $value; + } elseif ($value === false) { + $this->_pagination = false; } else { throw new InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.'); } From 65cfa512073da046d28339a36e79950e611c6f22 Mon Sep 17 00:00:00 2001 From: lav45 Date: Wed, 6 Dec 2023 09:37:57 +0300 Subject: [PATCH 6/8] update CHANGELOG.md [skip ci] --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 6645863bfd7..24e394f8a91 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -6,6 +6,7 @@ Yii Framework 2 Change Log - Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw) +- Bug #20055: Fix Response header X-Pagination-Total-Count is always 0 (lav45) - Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw) - Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir) - Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher) From 1e634b36dc8286d92b27b0574b449f28697086f8 Mon Sep 17 00:00:00 2001 From: lav45 Date: Wed, 6 Dec 2023 09:45:45 +0300 Subject: [PATCH 7/8] add mock to unit test to test (xicond) --- tests/framework/rest/SerializerTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/framework/rest/SerializerTest.php b/tests/framework/rest/SerializerTest.php index 217d7fbf7a9..02003eccb25 100644 --- a/tests/framework/rest/SerializerTest.php +++ b/tests/framework/rest/SerializerTest.php @@ -439,6 +439,30 @@ public function testHeadSerializeDataProvider($dataProvider, $expectedResult, $s ]); $serializer->preserveKeys = $saveKeys; $this->assertEmpty($serializer->serialize($dataProvider)); + $this->assertNotEmpty($serializer->response->getHeaders()->get($serializer->totalCountHeader)); + + $arrayDataProviderMock = $this->getMockBuilder(ArrayDataProvider::className()) + ->disableOriginalConstructor() + ->getMock(); + + // stub getModels to prevent empty + $arrayDataProviderMock + ->method('getModels') + ->willReturn($expectedResult); + + // stub getPagination for header + $arrayDataProviderMock + ->method('getPagination') + ->willReturn($dataProvider->getPagination()); + + // assert normal HEAD is empty response + $this->assertEmpty($serializer->serialize($arrayDataProviderMock)); + + // Test #20002: Set up the expectation for the getModels method + $arrayDataProviderMock->expects($this->never()) + ->method('getModels'); + + // reset Method unset($_POST[$request->methodParam], $_SERVER['REQUEST_METHOD']); } From 90b60c193271741db78858fec7a8bb3f84c51f10 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 13 Dec 2023 19:56:47 +0300 Subject: [PATCH 8/8] Update CHANGELOG.md --- framework/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 24e394f8a91..39296c78ffb 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -6,7 +6,7 @@ Yii Framework 2 Change Log - Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw) -- Bug #20055: Fix Response header X-Pagination-Total-Count is always 0 (lav45) +- Bug #20055: Fix Response header X-Pagination-Total-Count is always 0 (lav45, xicond) - Bug #20005: Fix `yii\console\controllers\ServeController` to specify the router script (terabytesoftw) - Bug #19060: Fix `yii\widgets\Menu` bug when using Closure for active item and adding additional tests in `tests\framework\widgets\MenuTest` (atrandafir) - Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher)