Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Fix #20055: Fix Response header X-Pagination-Total-Count is always 0". #20225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +21 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not related.

- 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
Loading