Skip to content

Commit

Permalink
Fix yiisoft#20055: Fix Response header X-Pagination-Total-Count is al…
Browse files Browse the repository at this point in the history
…ways 0
  • Loading branch information
lav45 authored May 16, 2024
1 parent 8861f7b commit 90c0eb0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3)
- Bug #19691: Allow using custom class to style error summary (skepticspriggan)
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
- 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)
Expand Down
1 change: 0 additions & 1 deletion framework/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ protected function prepareModels()
}
$query = clone $this->query;
if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount();
if ($pagination->totalCount === 0) {
return [];
}
Expand Down
10 changes: 3 additions & 7 deletions framework/data/ArrayDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,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;
}

Expand Down
15 changes: 9 additions & 6 deletions framework/data/BaseDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ public function getCount()
*/
public function getTotalCount()
{
if ($this->getPagination() === false) {
if ($this->_pagination === false) {
return $this->getCount();
} elseif ($this->_totalCount === null) {
}
if ($this->_totalCount === null) {
$this->_totalCount = $this->prepareTotalCount();
}

return $this->_totalCount;
}

Expand All @@ -194,7 +194,6 @@ public function getPagination()
if ($this->_pagination === null) {
$this->setPagination([]);
}

return $this->_pagination;
}

Expand All @@ -218,9 +217,13 @@ public function setPagination($value)
$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->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.');
}
Expand Down
1 change: 0 additions & 1 deletion framework/data/SqlDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ protected function prepareModels()
}

if ($pagination !== false) {
$pagination->totalCount = $this->getTotalCount();
$limit = $pagination->getLimit();
$offset = $pagination->getOffset();
}
Expand Down
3 changes: 1 addition & 2 deletions tests/framework/data/ActiveDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
24 changes: 24 additions & 0 deletions tests/framework/rest/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down

0 comments on commit 90c0eb0

Please sign in to comment.