Skip to content

Commit

Permalink
Revert "Fix #20055: Fix Response header X-Pagination-Total-Count is a…
Browse files Browse the repository at this point in the history
…lways 0".

This reverts commit 90c0eb0.
  • Loading branch information
terabytesoftw committed Jul 9, 2024
1 parent 5f5ef64 commit 41176f8
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 41 deletions.
3 changes: 3 additions & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Yii Framework 2 Change Log
- Bug #17181: Improved `BaseUrl::isRelative($url)` performance (sammousa, bizley, rob006)
- Bug #17191: Fixed `BaseUrl::isRelative($url)` method in `yii\helpers\BaseUrl` (ggh2e3)
- 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 #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 #19691: Allow using custom class to style error summary (skepticspriggan)
- Bug #19817: Add MySQL Query `addCheck()` and `dropCheck()` (@bobonov)
Expand Down
1 change: 1 addition & 0 deletions framework/data/ActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ 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: 7 additions & 3 deletions framework/data/ArrayDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ protected function prepareModels()
$models = $this->sortModels($models, $sort);
}

$pagination = $this->getPagination();
if ($pagination !== false && $pagination->getPageSize() > 0) {
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
if (($pagination = $this->getPagination()) !== false) {
$pagination->totalCount = $this->getTotalCount();

if ($pagination->getPageSize() > 0) {
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
}
}

return $models;
}

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

return $this->_totalCount;
}

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

return $this->_pagination;
}

Expand All @@ -216,15 +217,9 @@ public function setPagination($value)
$config['pageParam'] = $this->id . '-page';
$config['pageSizeParam'] = $this->id . '-per-page';
}
$value = Yii::createObject(array_merge($config, $value));
}
if ($value instanceof Pagination) {
$value->setTotalCount(function () {
return $this->getTotalCount();
});
$this->_pagination = Yii::createObject(array_merge($config, $value));
} elseif ($value instanceof Pagination || $value === false) {
$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: 1 addition & 0 deletions framework/data/SqlDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ protected function prepareModels()
}

if ($pagination !== false) {
$pagination->totalCount = $this->getTotalCount();
$limit = $pagination->getLimit();
$offset = $pagination->getOffset();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/framework/data/ActiveDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ public function testPaginationBeforeModels()
'query' => $query->from('order')->orderBy('id'),
]);
$pagination = $provider->getPagination();
$this->assertEquals(1, $pagination->getPageCount());
$this->assertEquals(0, $pagination->getPageCount());
$this->assertCount(3, $provider->getModels());
$this->assertEquals(1, $pagination->getPageCount());

$provider->getPagination()->pageSize = 2;
$this->assertCount(3, $provider->getModels());
Expand Down
24 changes: 0 additions & 24 deletions tests/framework/rest/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,30 +439,6 @@ 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 41176f8

Please sign in to comment.