|
7 | 7 |
|
8 | 8 | namespace yiiunit\framework\grid; |
9 | 9 |
|
| 10 | +use Yii; |
10 | 11 | use yii\data\ArrayDataProvider; |
11 | 12 | use yii\grid\DataColumn; |
12 | 13 | use yii\grid\GridView; |
13 | 14 | use yii\web\View; |
| 15 | +use yiiunit\data\ar\NoAutoLabels; |
14 | 16 |
|
15 | 17 | /** |
16 | 18 | * @author Evgeniy Tkachenko <[email protected]> |
@@ -150,4 +152,53 @@ public function testFooter() { |
150 | 152 |
|
151 | 153 | $this->assertTrue(preg_match("/<\/tbody><tfoot>/", $html) === 1); |
152 | 154 | } |
| 155 | + |
| 156 | + public function testHeaderLabels() |
| 157 | + { |
| 158 | + // Ensure GridView does not call Model::generateAttributeLabel() to generate labels unless the labels are explicitly used. |
| 159 | + |
| 160 | + $this->mockApplication([ |
| 161 | + 'components' => [ |
| 162 | + 'db' => [ |
| 163 | + 'class' => \yii\db\Connection::className(), |
| 164 | + 'dsn' => 'sqlite::memory:', |
| 165 | + ], |
| 166 | + ], |
| 167 | + ]); |
| 168 | + |
| 169 | + NoAutoLabels::$db = Yii::$app->getDb(); |
| 170 | + Yii::$app->getDb()->createCommand()->createTable(NoAutoLabels::tableName(), ['attr1' => 'int', 'attr2' => 'int'])->execute(); |
| 171 | + |
| 172 | + $urlManager = new \yii\web\UrlManager([ |
| 173 | + 'baseUrl' => '/', |
| 174 | + 'scriptUrl' => '/index.php', |
| 175 | + ]); |
| 176 | + |
| 177 | + $grid = new GridView([ |
| 178 | + 'dataProvider' => new \yii\data\ActiveDataProvider([ |
| 179 | + 'query' => NoAutoLabels::find(), |
| 180 | + ]), |
| 181 | + 'columns' => [ |
| 182 | + 'attr1', |
| 183 | + 'attr2:text:Label for attr2', |
| 184 | + ], |
| 185 | + ]); |
| 186 | + |
| 187 | + // NoAutoLabels::generateAttributeLabel() should not be called. |
| 188 | + $grid->dataProvider->setSort([ |
| 189 | + 'route' => '/', |
| 190 | + 'urlManager' => $urlManager, |
| 191 | + ]); |
| 192 | + $grid->renderTableHeader(); |
| 193 | + |
| 194 | + // NoAutoLabels::generateAttributeLabel() should not be called. |
| 195 | + $grid->dataProvider->setSort([ |
| 196 | + 'route' => '/', |
| 197 | + 'urlManager' => $urlManager, |
| 198 | + 'attributes' => ['attr1', 'attr2'], |
| 199 | + ]); |
| 200 | + $grid->renderTableHeader(); |
| 201 | + |
| 202 | + // If NoAutoLabels::generateAttributeLabel() has not been called no exception will be thrown meaning this test passed successfully. |
| 203 | + } |
153 | 204 | } |
0 commit comments