2013-07-11 04:28:38 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Tests for the {@link GridFieldOrderableRows} component.
|
|
|
|
*/
|
|
|
|
class GridFieldOrderableRowsTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2016-07-21 16:04:37 +02:00
|
|
|
protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';
|
|
|
|
|
2016-07-21 13:55:03 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'GridFieldOrderableRowsTest_Parent',
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
'GridFieldOrderableRowsTest_Subclass',
|
2017-09-22 14:50:25 +02:00
|
|
|
'GridFieldOrderableRowsTest_Unorderable',
|
|
|
|
'GridFieldOrderableRowsTest_OrderableChild',
|
2016-07-21 13:55:03 +02:00
|
|
|
);
|
|
|
|
|
2016-07-21 16:04:37 +02:00
|
|
|
public function testReorderItems() {
|
|
|
|
$orderable = new GridFieldOrderableRows('ManyManySort');
|
|
|
|
$reflection = new ReflectionMethod($orderable, 'executeReorder');
|
|
|
|
$reflection->setAccessible(true);
|
|
|
|
|
|
|
|
$parent = $this->objFromFixture('GridFieldOrderableRowsTest_Parent', 'parent');
|
|
|
|
|
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
|
|
|
|
|
|
|
$grid = new GridField(
|
|
|
|
'MyManyMany',
|
|
|
|
'My Many Many',
|
|
|
|
$parent->MyManyMany()->sort('ManyManySort'),
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
|
|
|
|
$originalOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID');
|
2016-08-18 03:32:18 +02:00
|
|
|
$desiredOrder = array();
|
|
|
|
|
|
|
|
// Make order non-contiguous, and 1-based
|
|
|
|
foreach(array_reverse($originalOrder) as $index => $id) {
|
|
|
|
$desiredOrder[$index * 2 + 1] = $id;
|
|
|
|
}
|
2016-07-21 16:04:37 +02:00
|
|
|
|
|
|
|
$this->assertNotEquals($originalOrder, $desiredOrder);
|
|
|
|
|
|
|
|
$reflection->invoke($orderable, $grid, $desiredOrder);
|
|
|
|
|
2016-08-18 03:32:18 +02:00
|
|
|
$newOrder = $parent->MyManyMany()->sort('ManyManySort')->map('ManyManySort', 'ID')->toArray();
|
2016-07-21 16:04:37 +02:00
|
|
|
|
|
|
|
$this->assertEquals($desiredOrder, $newOrder);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-22 14:50:25 +02:00
|
|
|
public function testSortableChildClass() {
|
|
|
|
$orderable = new GridFieldOrderableRows('Sort');
|
|
|
|
$reflection = new ReflectionMethod($orderable, 'executeReorder');
|
|
|
|
$reflection->setAccessible(true);
|
|
|
|
|
|
|
|
$parent = $this->objFromFixture('GridFieldOrderableRowsTest_Ordered', 'nestedtest');
|
|
|
|
|
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
|
|
|
|
|
|
|
$grid = new GridField(
|
|
|
|
'Children',
|
|
|
|
'Children',
|
|
|
|
$parent->Children(),
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
|
|
|
|
$originalOrder = $parent->Children()->column('ID');
|
|
|
|
$desiredOrder = array_reverse($originalOrder);
|
|
|
|
|
|
|
|
$this->assertNotEquals($originalOrder, $desiredOrder);
|
|
|
|
|
|
|
|
$reflection->invoke($orderable, $grid, $desiredOrder);
|
|
|
|
|
|
|
|
$newOrder = $parent->Children()->column('ID');
|
|
|
|
|
|
|
|
$this->assertEquals($desiredOrder, $newOrder);
|
|
|
|
}
|
|
|
|
|
2013-07-11 04:28:38 +02:00
|
|
|
/**
|
|
|
|
* @covers GridFieldOrderableRows::getSortTable
|
|
|
|
*/
|
|
|
|
public function testGetSortTable() {
|
|
|
|
$orderable = new GridFieldOrderableRows();
|
|
|
|
|
|
|
|
$parent = new GridFieldOrderableRowsTest_Parent();
|
|
|
|
$parent->write();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
2015-10-30 03:06:35 +01:00
|
|
|
$orderable->getSortTable($parent->MyHasMany())
|
2013-07-11 04:28:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
2015-10-30 03:06:35 +01:00
|
|
|
$orderable->getSortTable($parent->MyHasManySubclass())
|
2013-07-11 04:28:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
2015-10-30 03:06:35 +01:00
|
|
|
$orderable->getSortTable($parent->MyManyMany())
|
2013-07-11 04:28:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-10-30 03:06:35 +01:00
|
|
|
'GridFieldOrderableRowsTest_Parent_MyManyMany',
|
|
|
|
$orderable->setSortField('ManyManySort')->getSortTable($parent->MyManyMany())
|
2013-07-11 04:28:38 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
|
2016-07-21 13:55:03 +02:00
|
|
|
class GridFieldOrderableRowsTest_Parent extends DataObject implements TestOnly {
|
2013-07-11 04:28:38 +02:00
|
|
|
|
|
|
|
private static $has_many = array(
|
2015-10-30 03:06:35 +01:00
|
|
|
'MyHasMany' => 'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
'MyHasManySubclass' => 'GridFieldOrderableRowsTest_Subclass'
|
2013-07-11 04:28:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
2015-10-30 03:06:35 +01:00
|
|
|
'MyManyMany' => 'GridFieldOrderableRowsTest_Ordered'
|
2013-07-11 04:28:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many_extraFields = array(
|
2015-10-30 03:06:35 +01:00
|
|
|
'MyManyMany' => array('ManyManySort' => 'Int')
|
2013-07-11 04:28:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-21 13:55:03 +02:00
|
|
|
class GridFieldOrderableRowsTest_Ordered extends DataObject implements TestOnly {
|
2013-07-11 04:28:38 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Sort' => 'Int'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
|
|
|
'Parent' => 'GridFieldOrderableRowsTest_Parent'
|
|
|
|
);
|
|
|
|
|
2017-09-22 14:50:25 +02:00
|
|
|
private static $has_many = array(
|
|
|
|
'Children' => 'GridFieldOrderableRowsTest_OrderableChild',
|
|
|
|
);
|
|
|
|
|
2016-07-21 13:55:03 +02:00
|
|
|
private static $belongs_many_many =array(
|
|
|
|
'MyManyMany' => 'GridFieldOrderableRowsTest_Parent',
|
|
|
|
);
|
|
|
|
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 13:55:03 +02:00
|
|
|
class GridFieldOrderableRowsTest_Subclass extends GridFieldOrderableRowsTest_Ordered implements TestOnly {
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 14:50:25 +02:00
|
|
|
class GridFieldOrderableRowsTest_Unorderable extends DataObject implements TestOnly {
|
|
|
|
}
|
|
|
|
|
|
|
|
class GridFieldOrderableRowsTest_OrderableChild extends GridFieldOrderableRowsTest_Unorderable implements TestOnly {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Sort' => 'Int',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
|
|
|
'Parent' => 'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $default_sort = '"Sort" ASC';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-11 04:28:38 +02:00
|
|
|
/**#@-*/
|