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

Fix #7: Validate that DataReader is not both sortable and limited for GridView #215

Merged
merged 13 commits into from
Nov 10, 2024
10 changes: 5 additions & 5 deletions src/BasePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ final public function render(): string
$html .= Html::openTag($this->itemTag, $attributes);
}

$attributes = $this->linkAttributes;
$linkAttributes = $this->linkAttributes;
if ($item->isDisabled) {
Html::addCssClass($attributes, $this->disabledLinkClass);
Html::addCssClass($linkAttributes, $this->disabledLinkClass);
}
if ($item->isCurrent) {
Html::addCssClass($attributes, $this->currentLinkClass);
Html::addCssClass($linkAttributes, $this->currentLinkClass);
}
$html .= Html::a($item->label, $item->url, $attributes);
$html .= Html::a($item->label, $item->url, $linkAttributes);

if ($this->itemTag !== null) {
$html .= Html::closeTag($this->itemTag);
Expand Down Expand Up @@ -211,7 +211,7 @@ protected function createUrl(PageToken $pageToken): string

$paginator = $this->getPaginator();
$pageSize = $paginator->getPageSize();
$sort = $paginator->getSort()?->getOrderAsString();
$sort = $paginator->isSortable() ? $paginator->getSort()?->getOrderAsString() : null;

return call_user_func_array(
$this->urlCreator,
Expand Down
33 changes: 33 additions & 0 deletions tests/GridView/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use Yiisoft\Yii\DataView\GridView;
use Yiisoft\Yii\DataView\Tests\Support\Assert;
use Yiisoft\Yii\DataView\Tests\Support\TestTrait;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Yii\DataView\Tests\Support\SimplePaginationUrlCreator;

final class BaseTest extends TestCase
{
Expand Down Expand Up @@ -675,4 +679,33 @@ public function testTableAttributes(): void
->render()
);
}

public function testNoSortInUrlWhenLimited(): void
{
$sort = Sort::any()->withOrder(['id' => 'asc', 'name' => 'asc']);
$data = (new IterableDataReader($this->data))
->withSort($sort)
->withLimit(10);

$paginator = (new OffsetPaginator($data))

samdark marked this conversation as resolved.
Show resolved Hide resolved
->withPageSize(1);

$output = GridView::widget()
->columns(
new SerialColumn(),
new DataColumn('id'),
new DataColumn('name'),
new DataColumn('age'),
)
->id('w1-grid')
->dataReader($paginator)
->offsetPaginationConfig([
'urlCreator()' => [new SimplePaginationUrlCreator()],
])
->tableAttributes(['class' => 'table table-striped table-bordered'])
->render();

$this->assertStringNotContainsString('sort=', $output);
}
}
Loading