diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 9f49b12..3a24dbc 100644 --- a/framework/CHANGELOG.md +++ b/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 #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 #17631: Fix `yii\widgets\BaseListView` to properly render custom summary (sjaakp, bizley) 2.0.41.1 March 04, 2021 diff --git a/framework/widgets/BaseListView.php b/framework/widgets/BaseListView.php index 88686e8..3b6eddb 100644 --- a/framework/widgets/BaseListView.php +++ b/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, 'end' => $end, 'count' => $count, 'totalCount' => $totalCount, 'page' => $page, 'pageCount' => $pageCount, - ], Yii::$app->language); + ], Yii::$app->language), $summaryOptions); } /** diff --git a/tests/framework/widgets/ListViewTest.php b/tests/framework/widgets/ListViewTest.php index beb5268..c6545a3 100644 --- a/tests/framework/widgets/ListViewTest.php +++ b/tests/framework/widgets/ListViewTest.php @@ -7,6 +7,7 @@ namespace yiiunit\framework\widgets; +use yii\web\Request; use yii\data\ArrayDataProvider; use yii\data\DataProviderInterface; use yii\widgets\ListView; @@ -79,15 +80,15 @@ HTML /** * @return DataProviderInterface */ - private function getDataProvider() + private function getDataProvider($additionalConfig = []) { - return new ArrayDataProvider([ + return new ArrayDataProvider(array_merge([ 'allModels' => [ ['id' => 1, 'login' => 'silverfire'], ['id' => 2, 'login' => 'samdark'], ['id' => 3, 'login' => 'cebe'], ], - ]); + ], $additionalConfig)); } public function testSimplyListView() @@ -263,4 +264,106 @@ HTML ]); $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('
', $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('
', $out); + } + + public function testRenderSummaryWhenPaginationIsFalseAndSummaryIsNull() + { + ob_start(); + $this->getListView(['dataProvider' => $this->getDataProvider(['pagination' => false])])->run(); + $out = ob_get_clean(); + + $this->assertEqualsWithoutLE('
Total 3 items.
+
0
+
1
+
2
+
', $out); + } + + public function providerForSummary() + { + return [ + 'empty' => ['', '
+
0
+
1
+
2
+
'], + 'all tokens' => ['{begin}-{end}-{count}-{totalCount}-{page}-{pageCount}', '
1-3-3-3-1-1
+
0
+
1
+
2
+
'], + ]; + } + + /** + * @dataProvider providerForSummary + */ + public function testRenderSummaryWhenSummaryIsCustom($summary, $result) + { + ob_start(); + $this->getListView(['summary' => $summary])->run(); + $out = ob_get_clean(); + + $this->assertEqualsWithoutLE($result, $out); + } }