Browse Source

Fix #17631: Fix `yii\widgets\BaseListView` to properly render custom summary

bizley-patch-1
Bizley 4 years ago committed by GitHub
parent
commit
a03fb0c01e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 8
      framework/widgets/BaseListView.php
  3. 109
      tests/framework/widgets/ListViewTest.php

1
framework/CHANGELOG.md

@ -14,6 +14,7 @@ Yii Framework 2 Change Log
- Bug #18557: Fix `yii\data\ActiveDataProvider` to handle DB connection configuration of different type than just `yii\db\Connection` (bizley) - Bug #18557: Fix `yii\data\ActiveDataProvider` to handle DB connection configuration of different type than just `yii\db\Connection` (bizley)
- Bug #18526: Fix `yii\caching\DbCache` to work with MSSQL, add `normalizeTableRowData()` to `yii\db\mssql\QueryBuilder::upsert()` (darkdef) - Bug #18526: Fix `yii\caching\DbCache` to work with MSSQL, add `normalizeTableRowData()` to `yii\db\mssql\QueryBuilder::upsert()` (darkdef)
- Bug #14343: Fix `yii\test\ActiveFixture` to use model's DB connection instead of the default one (margori, bizley) - Bug #14343: Fix `yii\test\ActiveFixture` to use model's DB connection instead of the default one (margori, bizley)
- Bug #17631: Fix `yii\widgets\BaseListView` to properly render custom summary (sjaakp, bizley)
2.0.41.1 March 04, 2021 2.0.41.1 March 04, 2021

8
framework/widgets/BaseListView.php

@ -227,14 +227,18 @@ abstract class BaseListView extends Widget
} }
} }
return Yii::$app->getI18n()->format($summaryContent, [ if ($summaryContent === '') {
return '';
}
return Html::tag($tag, Yii::$app->getI18n()->format($summaryContent, [
'begin' => $begin, 'begin' => $begin,
'end' => $end, 'end' => $end,
'count' => $count, 'count' => $count,
'totalCount' => $totalCount, 'totalCount' => $totalCount,
'page' => $page, 'page' => $page,
'pageCount' => $pageCount, 'pageCount' => $pageCount,
], Yii::$app->language); ], Yii::$app->language), $summaryOptions);
} }
/** /**

109
tests/framework/widgets/ListViewTest.php

@ -7,6 +7,7 @@
namespace yiiunit\framework\widgets; namespace yiiunit\framework\widgets;
use yii\web\Request;
use yii\data\ArrayDataProvider; use yii\data\ArrayDataProvider;
use yii\data\DataProviderInterface; use yii\data\DataProviderInterface;
use yii\widgets\ListView; use yii\widgets\ListView;
@ -79,15 +80,15 @@ HTML
/** /**
* @return DataProviderInterface * @return DataProviderInterface
*/ */
private function getDataProvider() private function getDataProvider($additionalConfig = [])
{ {
return new ArrayDataProvider([ return new ArrayDataProvider(array_merge([
'allModels' => [ 'allModels' => [
['id' => 1, 'login' => 'silverfire'], ['id' => 1, 'login' => 'silverfire'],
['id' => 2, 'login' => 'samdark'], ['id' => 2, 'login' => 'samdark'],
['id' => 3, 'login' => 'cebe'], ['id' => 3, 'login' => 'cebe'],
], ],
]); ], $additionalConfig));
} }
public function testSimplyListView() public function testSimplyListView()
@ -263,4 +264,106 @@ HTML
]); ]);
$this->assertTrue($initTriggered); $this->assertTrue($initTriggered);
} }
public function testNoDataProvider()
{
$this->expectException('yii\base\InvalidConfigException');
$this->expectExceptionMessage('The "dataProvider" property must be set.');
(new ListView())->run();
}
public function providerForNoSorter()
{
return [
'no sort attributes' => [[]],
'sorter false' => [['dataProvider' => $this->getDataProvider(['sort' => false])]],
];
}
/**
* @dataProvider providerForNoSorter
*/
public function testRenderNoSorter($additionalConfig)
{
$config = array_merge(['layout' => '{sorter}'], $additionalConfig);
ob_start();
$this->getListView($config)->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE('<div id="w0" class="list-view"></div>', $out);
}
public function testRenderSorterOnlyWithNoItems()
{
// by default sorter is skipped when there are no items during run()
$out = (new ListView([
'id' => 'w0',
'dataProvider' => $this->getDataProvider(['allModels' => [], 'sort' => ['attributes' => ['id']]]),
]))->renderSorter();
$this->assertEquals('', $out);
}
public function testRenderSorter()
{
\Yii::$app->set('request', new Request(['scriptUrl' => '/']));
ob_start();
$this->getListView([
'layout' => '{sorter}',
'dataProvider' => $this->getDataProvider([
'sort' => [
'attributes' => ['id'],
'route' => 'list/view',
]
])
])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE('<div id="w0" class="list-view"><ul class="sorter">
<li><a href="/?r=list%2Fview&amp;sort=id" data-sort="id">Id</a></li>
</ul></div>', $out);
}
public function testRenderSummaryWhenPaginationIsFalseAndSummaryIsNull()
{
ob_start();
$this->getListView(['dataProvider' => $this->getDataProvider(['pagination' => false])])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE('<div id="w0" class="list-view"><div class="summary">Total <b>3</b> items.</div>
<div data-key="0">0</div>
<div data-key="1">1</div>
<div data-key="2">2</div>
</div>', $out);
}
public function providerForSummary()
{
return [
'empty' => ['', '<div id="w0" class="list-view">
<div data-key="0">0</div>
<div data-key="1">1</div>
<div data-key="2">2</div>
</div>'],
'all tokens' => ['{begin}-{end}-{count}-{totalCount}-{page}-{pageCount}', '<div id="w0" class="list-view"><div class="summary">1-3-3-3-1-1</div>
<div data-key="0">0</div>
<div data-key="1">1</div>
<div data-key="2">2</div>
</div>'],
];
}
/**
* @dataProvider providerForSummary
*/
public function testRenderSummaryWhenSummaryIsCustom($summary, $result)
{
ob_start();
$this->getListView(['summary' => $summary])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE($result, $out);
}
} }

Loading…
Cancel
Save