Skip to content

Commit

Permalink
Grid: setDefaultSort() can contains an undefined "grid" column
Browse files Browse the repository at this point in the history
  • Loading branch information
o5 committed Nov 26, 2013
1 parent 6e76b8a commit 027b674
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
13 changes: 9 additions & 4 deletions Grido/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,16 +753,21 @@ protected function applySorting()
foreach ($this->sort as $column => $dir) {
$component = $this->getColumn($column, FALSE);
if (!$component) {
trigger_error("Column with name '$column' does not exist.", E_USER_NOTICE);
break;
if (!isset($this->defaultSort[$column])) {
trigger_error("Column with name '$column' does not exist.", E_USER_NOTICE);
break;
}

} elseif (!$component->isSortable()) {
if (isset($this->defaultSort[$column])) {
$component->setSortable();
} else {
trigger_error("Column with name '$column' is not sortable.", E_USER_NOTICE);
break;
}
} elseif (!in_array($dir, array(Column::ORDER_ASC, Column::ORDER_DESC))) {
}

if (!in_array($dir, array(Column::ORDER_ASC, Column::ORDER_DESC))) {
if ($dir == '' && isset($this->defaultSort[$column])) {
unset($this->sort[$column]);
break;
Expand All @@ -772,7 +777,7 @@ protected function applySorting()
break;
}

$sort[$component->column] = $dir == Column::ORDER_ASC ? 'ASC' : 'DESC';
$sort[$component ? $component->column : $column] = $dir == Column::ORDER_ASC ? 'ASC' : 'DESC';
}

if ($sort) {
Expand Down
23 changes: 15 additions & 8 deletions tests/Grido/Grid/Grid.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,6 @@ class GridTest extends \Tester\TestCase
$grid->setDefaultSort(array('a' => 'up'));
}, 'InvalidArgumentException', "Dir 'up' for column 'a' is not allowed.");

Assert::error(function() {
$grid = new Grid;
$grid->setModel(array());
$grid->addColumnText('column', 'Column');
$grid->setDefaultSort(array('a' => 'asc'));
$grid->getData();
}, E_USER_NOTICE, "Column with name 'a' does not exist.");

$grid = new Grid;
$data = array(
array('A' => 'A1', 'B' => 'B3'),
Expand All @@ -172,6 +164,21 @@ class GridTest extends \Tester\TestCase

$grid2->sort['B'] = Column::ORDER_DESC;
Assert::same($data, $grid2->data);

$grid = new Grid;
$grid->setModel($data);
$grid->setDefaultSort(array('A' => 'desc'));

$A = array();
foreach ($data as $key => $row) {
$A[$key] = $row['A'];
}
array_multisort($A, SORT_DESC, $data);
Assert::same($data, $grid->data);

Assert::exception(function() use ($grid) {
$grid->setDefaultSort(array('A' => 'up'));
}, 'InvalidArgumentException', "Dir 'up' for column 'A' is not allowed.");
}

function testSetPerPageList()
Expand Down

0 comments on commit 027b674

Please sign in to comment.