-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GridView generating unused label values (issue #19899).
- Loading branch information
1 parent
76d6345
commit 051ce10
Showing
5 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* @link https://www.yiiframework.com/ | ||
* @copyright Copyright (c) 2008 Yii Software LLC | ||
* @license https://www.yiiframework.com/license/ | ||
*/ | ||
|
||
namespace yiiunit\data\ar; | ||
|
||
/** | ||
* @property int $attr1 | ||
* @property int $attr2 | ||
*/ | ||
class NoAutoLabels extends ActiveRecord | ||
{ | ||
public function attributeLabels() | ||
{ | ||
return [ | ||
'attr1' => 'Label for attr1', | ||
]; | ||
} | ||
|
||
public function generateAttributeLabel($name) | ||
{ | ||
throw new \yii\base\InvalidArgumentException('Label not defined!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,12 @@ | |
|
||
namespace yiiunit\framework\grid; | ||
|
||
use Yii; | ||
use yii\data\ArrayDataProvider; | ||
use yii\grid\DataColumn; | ||
use yii\grid\GridView; | ||
use yii\web\View; | ||
use yiiunit\data\ar\NoAutoLabels; | ||
|
||
/** | ||
* @author Evgeniy Tkachenko <[email protected]> | ||
|
@@ -150,4 +152,53 @@ public function testFooter() { | |
|
||
$this->assertTrue(preg_match("/<\/tbody><tfoot>/", $html) === 1); | ||
} | ||
|
||
public function testHeaderLabels() | ||
{ | ||
// Ensure GridView does not call Model::generateAttributeLabel() to generate labels unless the labels are explicitly used. | ||
|
||
$this->mockApplication([ | ||
'components' => [ | ||
'db' => [ | ||
'class' => \yii\db\Connection::className(), | ||
'dsn' => 'sqlite::memory:', | ||
], | ||
], | ||
]); | ||
|
||
NoAutoLabels::$db = Yii::$app->getDb(); | ||
Yii::$app->getDb()->createCommand()->createTable(NoAutoLabels::tableName(), ['attr1' => 'int', 'attr2' => 'int'])->execute(); | ||
|
||
$urlManager = new \yii\web\UrlManager([ | ||
'baseUrl' => '/', | ||
'scriptUrl' => '/index.php', | ||
]); | ||
|
||
$grid = new GridView([ | ||
'dataProvider' => new \yii\data\ActiveDataProvider([ | ||
'query' => NoAutoLabels::find(), | ||
]), | ||
'columns' => [ | ||
'attr1', | ||
'attr2:text:Label for attr2', | ||
], | ||
]); | ||
|
||
// NoAutoLabels::generateAttributeLabel() should not be called. | ||
$grid->dataProvider->setSort([ | ||
'route' => '/', | ||
'urlManager' => $urlManager, | ||
]); | ||
$grid->renderTableHeader(); | ||
|
||
// NoAutoLabels::generateAttributeLabel() should not be called. | ||
$grid->dataProvider->setSort([ | ||
'route' => '/', | ||
'urlManager' => $urlManager, | ||
'attributes' => ['attr1', 'attr2'], | ||
]); | ||
$grid->renderTableHeader(); | ||
|
||
// If NoAutoLabels::generateAttributeLabel() has not been called no exception will be thrown meaning this test passed successfully. | ||
} | ||
} |