2013-07-11 04:28:38 +02:00
|
|
|
<?php
|
2016-09-09 09:00:05 +02:00
|
|
|
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Dev\TestOnly;
|
2016-12-19 05:11:56 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
|
|
|
|
use SilverStripe\GridFieldExtensions\GridFieldOrderableRows;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
|
2013-07-11 04:28:38 +02:00
|
|
|
/**
|
|
|
|
* Tests for the {@link GridFieldOrderableRows} component.
|
|
|
|
*/
|
2016-12-21 03:34:58 +01:00
|
|
|
class GridFieldOrderableRowsTest extends SapphireTest
|
|
|
|
{
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
protected $usesDatabase = true;
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
// protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'GridFieldOrderableRowsTest_Parent',
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
'GridFieldOrderableRowsTest_Subclass',
|
|
|
|
);
|
2016-07-21 13:55:03 +02:00
|
|
|
|
2016-12-19 05:11:56 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->markTestSkipped('Upgrade to 4.0: Needs to be re-implemented.');
|
|
|
|
}
|
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
public function testReorderItems()
|
|
|
|
{
|
2016-12-19 05:11:56 +01:00
|
|
|
$orderable = new GridFieldOrderableRows('ManyManySort');
|
2016-12-21 03:34:58 +01:00
|
|
|
$reflection = new ReflectionMethod($orderable, 'executeReorder');
|
|
|
|
$reflection->setAccessible(true);
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$parent = $this->objFromFixture('GridFieldOrderableRowsTest_Parent', 'parent');
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$grid = new GridField(
|
|
|
|
'MyManyMany',
|
|
|
|
'My Many Many',
|
|
|
|
$parent->MyManyMany()->sort('ManyManySort'),
|
|
|
|
$config
|
|
|
|
);
|
2016-08-18 03:32:18 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$originalOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID');
|
|
|
|
$desiredOrder = array();
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
// 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
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertNotEquals($originalOrder, $desiredOrder);
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$reflection->invoke($orderable, $grid, $desiredOrder);
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$newOrder = $parent->MyManyMany()->sort('ManyManySort')->map('ManyManySort', 'ID')->toArray();
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals($desiredOrder, $newOrder);
|
|
|
|
}
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
/**
|
|
|
|
* @covers GridFieldOrderableRows::getSortTable
|
|
|
|
*/
|
|
|
|
public function testGetSortTable()
|
|
|
|
{
|
2016-12-19 05:11:56 +01:00
|
|
|
$orderable = new GridFieldOrderableRows();
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$parent = new GridFieldOrderableRowsTest_Parent();
|
|
|
|
$parent->write();
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
$orderable->getSortTable($parent->MyHasMany())
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
$orderable->getSortTable($parent->MyHasManySubclass())
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
$orderable->getSortTable($parent->MyManyMany())
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'GridFieldOrderableRowsTest_Parent_MyManyMany',
|
|
|
|
$orderable->setSortField('ManyManySort')->getSortTable($parent->MyManyMany())
|
|
|
|
);
|
|
|
|
}
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
class GridFieldOrderableRowsTest_Parent extends DataObject implements TestOnly
|
|
|
|
{
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
private static $has_many = array(
|
|
|
|
'MyHasMany' => 'GridFieldOrderableRowsTest_Ordered',
|
|
|
|
'MyHasManySubclass' => 'GridFieldOrderableRowsTest_Subclass'
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
private static $many_many = array(
|
|
|
|
'MyManyMany' => 'GridFieldOrderableRowsTest_Ordered'
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
'MyManyMany' => array('ManyManySort' => 'Int')
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
class GridFieldOrderableRowsTest_Ordered extends DataObject implements TestOnly
|
|
|
|
{
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
private static $db = array(
|
|
|
|
'Sort' => 'Int'
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
private static $has_one = array(
|
|
|
|
'Parent' => 'GridFieldOrderableRowsTest_Parent'
|
|
|
|
);
|
2016-07-21 13:55:03 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
private static $belongs_many_many =array(
|
|
|
|
'MyManyMany' => 'GridFieldOrderableRowsTest_Parent',
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
class GridFieldOrderableRowsTest_Subclass extends GridFieldOrderableRowsTest_Ordered implements TestOnly
|
|
|
|
{
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**#@-*/
|