2013-07-11 04:28:38 +02:00
|
|
|
<?php
|
2016-09-09 09:00:05 +02:00
|
|
|
|
2017-09-01 03:33:54 +02:00
|
|
|
namespace Symbiote\GridFieldExtensions\Tests;
|
|
|
|
|
|
|
|
use ReflectionMethod;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-12-19 05:11:56 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
|
2017-06-16 06:07:09 +02:00
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
|
2018-02-21 12:58:48 +01:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild;
|
2017-09-01 03:33:54 +02:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubParent;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclass;
|
2018-02-21 12:58:48 +01:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;
|
2016-09-09 09:00:05 +02:00
|
|
|
|
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
|
|
|
|
2017-09-01 03:33:54 +02:00
|
|
|
protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2017-09-01 03:33:54 +02:00
|
|
|
StubParent::class,
|
|
|
|
StubOrdered::class,
|
|
|
|
StubSubclass::class,
|
2018-02-21 12:58:48 +01:00
|
|
|
StubUnorderable::class,
|
|
|
|
StubOrderableChild::class,
|
|
|
|
];
|
2016-07-21 13:55:03 +02:00
|
|
|
|
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
|
|
|
|
2017-09-01 03:33:54 +02:00
|
|
|
$parent = $this->objFromFixture(StubParent::class, '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');
|
2018-02-21 12:58:48 +01:00
|
|
|
$desiredOrder = [];
|
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
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
public function testSortableChildClass()
|
|
|
|
{
|
|
|
|
$orderable = new GridFieldOrderableRows('Sort');
|
|
|
|
$reflection = new ReflectionMethod($orderable, 'executeReorder');
|
|
|
|
$reflection->setAccessible(true);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$parent = $this->objFromFixture(StubOrdered::class, 'nestedtest');
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$grid = new GridField(
|
|
|
|
'Children',
|
|
|
|
'Children',
|
|
|
|
$parent->Children(),
|
|
|
|
$config
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$originalOrder = $parent->Children()->column('ID');
|
|
|
|
$desiredOrder = array_reverse($originalOrder);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$this->assertNotEquals($originalOrder, $desiredOrder);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$reflection->invoke($orderable, $grid, $desiredOrder);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$newOrder = $parent->Children()->column('ID');
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
$this->assertEquals($desiredOrder, $newOrder);
|
|
|
|
}
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
/**
|
2018-02-21 12:58:48 +01:00
|
|
|
* @covers \Symbiote\GridFieldExtensions\GridFieldOrderableRows::getSortTable
|
2016-12-21 03:34:58 +01:00
|
|
|
*/
|
|
|
|
public function testGetSortTable()
|
|
|
|
{
|
2016-12-19 05:11:56 +01:00
|
|
|
$orderable = new GridFieldOrderableRows();
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2017-09-01 03:33:54 +02:00
|
|
|
$parent = new StubParent();
|
2016-12-21 03:34:58 +01:00
|
|
|
$parent->write();
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
2017-09-01 03:33:54 +02:00
|
|
|
'StubOrdered',
|
2016-12-21 03:34:58 +01:00
|
|
|
$orderable->getSortTable($parent->MyHasMany())
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
2017-09-01 03:33:54 +02:00
|
|
|
'StubOrdered',
|
2016-12-21 03:34:58 +01:00
|
|
|
$orderable->getSortTable($parent->MyHasManySubclass())
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
2017-09-01 03:33:54 +02:00
|
|
|
'StubOrdered',
|
2016-12-21 03:34:58 +01:00
|
|
|
$orderable->getSortTable($parent->MyManyMany())
|
|
|
|
);
|
2013-07-11 04:28:38 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->assertEquals(
|
2017-09-01 03:33:54 +02:00
|
|
|
'StubParent_MyManyMany',
|
2016-12-21 03:34:58 +01:00
|
|
|
$orderable->setSortField('ManyManySort')->getSortTable($parent->MyManyMany())
|
|
|
|
);
|
|
|
|
}
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|