FIX Revert natural sort

More backwards compatible and more consistent with ORM sorting (fixes #6124)
This commit is contained in:
Jonathon Menz 2016-10-04 11:14:16 -07:00
parent 111ab5f0af
commit 797be6ac82
5 changed files with 119 additions and 46 deletions

View File

@ -448,7 +448,7 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
// First argument is the direction to be sorted,
$multisortArgs[] = &$sortDirection[$column];
if ($firstRun) {
$multisortArgs[] = defined('SORT_NATURAL') ? SORT_NATURAL : SORT_STRING;
$multisortArgs[] = SORT_REGULAR;
}
$firstRun = false;
}

View File

@ -314,67 +314,46 @@ class ArrayListTest extends SapphireTest {
), $list->toArray());
}
public function testNaturalSort() {
//natural sort is only available in 5.4+
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped();
}
$list = new ArrayList(array(
array('Name' => 'Steve'),
public function testMixedCaseSort() {
// Note: Natural sorting is not expected, so if 'bonny10' were included
// below we would expect it to appear between bonny1 and bonny2. That's
// undesirable though so we're not enforcing it in tests.
$original = array(
array('Name' => 'Steve'),
(object) array('Name' => 'Bob'),
array('Name' => 'John'),
array('Name' => 'bonny'),
array('Name' => 'bonny1'),
array('Name' => 'bonny10'),
//array('Name' => 'bonny10'),
array('Name' => 'bonny2'),
));
);
$list = new ArrayList($original);
$expected = array(
(object) array('Name' => 'Bob'),
array('Name' => 'bonny'),
array('Name' => 'bonny1'),
//array('Name' => 'bonny10'),
array('Name' => 'bonny2'),
array('Name' => 'John'),
array('Name' => 'Steve'),
);
// Unquoted name
$list1 = $list->sort('Name');
$this->assertEquals(array(
(object) array('Name' => 'Bob'),
array('Name' => 'bonny'),
array('Name' => 'bonny1'),
array('Name' => 'bonny2'),
array('Name' => 'bonny10'),
array('Name' => 'John'),
array('Name' => 'Steve'),
), $list1->toArray());
$this->assertEquals($expected, $list1->toArray());
// Quoted name name
$list2 = $list->sort('"Name"');
$this->assertEquals(array(
(object) array('Name' => 'Bob'),
array('Name' => 'bonny'),
array('Name' => 'bonny1'),
array('Name' => 'bonny2'),
array('Name' => 'bonny10'),
array('Name' => 'John'),
array('Name' => 'Steve'),
), $list2->toArray());
$this->assertEquals($expected, $list2->toArray());
// Array (non-associative)
$list3 = $list->sort(array('"Name"'));
$this->assertEquals(array(
(object) array('Name' => 'Bob'),
array('Name' => 'bonny'),
array('Name' => 'bonny1'),
array('Name' => 'bonny2'),
array('Name' => 'bonny10'),
array('Name' => 'John'),
array('Name' => 'Steve'),
), $list3->toArray());
$this->assertEquals($expected, $list3->toArray());
// Check original list isn't altered
$this->assertEquals(array(
array('Name' => 'Steve'),
(object) array('Name' => 'Bob'),
array('Name' => 'John'),
array('Name' => 'bonny'),
array('Name' => 'bonny1'),
array('Name' => 'bonny10'),
array('Name' => 'bonny2'),
), $list->toArray());
$this->assertEquals($original, $list->toArray());
}
@ -472,6 +451,42 @@ class ArrayListTest extends SapphireTest {
));
}
public function testSortNumeric() {
$list = new ArrayList(array(
array('Sort' => 0),
array('Sort' => -1),
array('Sort' => 1),
array('Sort' => -2),
array('Sort' => 2),
array('Sort' => -10),
array('Sort' => 10)
));
// Sort descending
$list1 = $list->sort('Sort', 'DESC');
$this->assertEquals(array(
array('Sort' => 10),
array('Sort' => 2),
array('Sort' => 1),
array('Sort' => 0),
array('Sort' => -1),
array('Sort' => -2),
array('Sort' => -10)
), $list1->toArray());
// Sort ascending
$list1 = $list->sort('Sort', 'ASC');
$this->assertEquals(array(
array('Sort' => -10),
array('Sort' => -2),
array('Sort' => -1),
array('Sort' => 0),
array('Sort' => 1),
array('Sort' => 2),
array('Sort' => 10)
), $list1->toArray());
}
public function testReverse() {
$list = new ArrayList(array(
array('Name' => 'John'),

View File

@ -23,6 +23,7 @@ class DataListTest extends SapphireTest {
'DataObjectTest_EquipmentCompany',
'DataObjectTest_SubEquipmentCompany',
'DataObjectTest\NamespacedClass',
'DataObjectTest_Sortable',
'DataObjectTest_Company',
'DataObjectTest_Fan',
'ManyManyListTest_Product',
@ -533,6 +534,34 @@ class DataListTest extends SapphireTest {
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
}
public function testSortNumeric() {
$list = DataObjectTest_Sortable::get();
$list1 = $list->sort('Sort', 'ASC');
$this->assertEquals(array(
-10,
-2,
-1,
0,
1,
2,
10
), $list1->column('Sort'));
}
public function testSortMixedCase() {
$list = DataObjectTest_Sortable::get();
$list1 = $list->sort('Name', 'ASC');
$this->assertEquals(array(
'Bob',
'bonny',
'jane',
'John',
'sam',
'Steve',
'steven'
), $list1->column('Name'));
}
/**
* Test DataList->canFilterBy()
*/

View File

@ -1702,6 +1702,13 @@ class DataObjectTest extends SapphireTest {
}
class DataObjectTest_Sortable extends DataObject implements TestOnly {
private static $db = array(
'Sort' => 'Int',
'Name' => 'Varchar',
);
}
class DataObjectTest_Player extends Member implements TestOnly {
private static $db = array(
'IsRetired' => 'Boolean',

View File

@ -1,3 +1,25 @@
DataObjectTest_Sortable:
numeric1:
Sort: 0
Name: steven
numeric2:
Sort: -1
Name: bonny
numeric3:
Sort: 1
Name: sam
numeric4:
Sort: -2
Name: Bob
numeric5:
Sort: 2
Name: jane
numeric6:
Sort: -10
Name: Steve
numeric7:
Sort: 10
Name: John
DataObjectTest_EquipmentCompany:
equipmentcompany1:
Name: Company corp