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;
|
2022-02-02 03:36:25 +01:00
|
|
|
use SilverStripe\ORM\DataList;
|
2017-06-16 06:07:09 +02:00
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
|
2019-12-11 03:50:13 +01:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MMapper;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MParent;
|
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;
|
2018-04-26 05:17:05 +02:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderedVersioned;
|
2017-09-01 03:33:54 +02:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubParent;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclass;
|
2018-04-26 05:17:05 +02:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned;
|
2018-02-21 12:58:48 +01:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;
|
2018-06-25 02:22:27 +02:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs;
|
2022-02-02 03:36:25 +01:00
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\TitleObject;
|
|
|
|
use Symbiote\GridFieldExtensions\Tests\Stub\TitleSortedObject;
|
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
|
|
|
|
{
|
2018-06-25 02:22:27 +02:00
|
|
|
protected static $fixture_file = [
|
|
|
|
'GridFieldOrderableRowsTest.yml',
|
2019-12-11 03:50:13 +01:00
|
|
|
'OrderableRowsThroughTest.yml',
|
|
|
|
// 'OrderablePolymorphicManyToMany.yml' // TODO: introduce this tests in the next minor release
|
2018-06-25 02:22:27 +02:00
|
|
|
];
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2018-02-21 12:58:48 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2019-12-11 03:50:13 +01:00
|
|
|
// PolymorphM2MChild::class,
|
|
|
|
// PolymorphM2MMapper::class,
|
|
|
|
// PolymorphM2MParent::class,
|
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,
|
2018-04-26 05:17:05 +02:00
|
|
|
StubOrderedVersioned::class,
|
|
|
|
StubSubclassOrderedVersioned::class,
|
2018-06-25 02:22:27 +02:00
|
|
|
ThroughDefiner::class,
|
|
|
|
ThroughIntermediary::class,
|
|
|
|
ThroughBelongs::class,
|
2022-02-02 03:36:25 +01:00
|
|
|
TitleObject::class,
|
|
|
|
TitleSortedObject::class,
|
2018-02-21 12:58:48 +01:00
|
|
|
];
|
2016-07-21 13:55:03 +02:00
|
|
|
|
2018-06-25 02:22:27 +02:00
|
|
|
public function reorderItemsProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[StubParent::class . '.parent', 'MyManyMany', 'ManyManySort'],
|
|
|
|
[ThroughDefiner::class . '.DefinerOne', 'Belongings', 'Sort'],
|
2019-12-11 03:50:13 +01:00
|
|
|
// [PolymorphM2MParent::class . '.ParentOne', 'Children', 'Sort']
|
2018-06-25 02:22:27 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider reorderItemsProvider
|
|
|
|
*/
|
|
|
|
public function testReorderItems($fixtureID, $relationName, $sortName)
|
2016-12-21 03:34:58 +01:00
|
|
|
{
|
2018-06-25 02:22:27 +02:00
|
|
|
$orderable = new GridFieldOrderableRows($sortName);
|
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
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
2016-07-21 16:04:37 +02:00
|
|
|
|
2018-06-25 02:22:27 +02:00
|
|
|
list($parentClass, $parentInstanceID) = explode('.', $fixtureID);
|
|
|
|
$parent = $this->objFromFixture($parentClass, $parentInstanceID);
|
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
$grid = new GridField(
|
2018-06-25 02:22:27 +02:00
|
|
|
$relationName,
|
|
|
|
'Testing Many Many',
|
|
|
|
$parent->$relationName()->sort($sortName),
|
2016-12-21 03:34:58 +01:00
|
|
|
$config
|
|
|
|
);
|
2016-08-18 03:32:18 +02:00
|
|
|
|
2018-06-25 02:22:27 +02:00
|
|
|
$originalOrder = $parent->$relationName()->sort($sortName)->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
|
|
|
|
2018-06-25 02:22:27 +02:00
|
|
|
$newOrder = $parent->$relationName()->sort($sortName)->map($sortName, '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-09-28 17:59:44 +02:00
|
|
|
public function testManyManyThroughListSortOrdersAreUsedForInitialRender()
|
|
|
|
{
|
|
|
|
/** @var ThroughDefiner $record */
|
|
|
|
$record = $this->objFromFixture(ThroughDefiner::class, 'DefinerOne');
|
|
|
|
|
|
|
|
$orderable = new GridFieldOrderableRows('Sort');
|
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
|
|
|
|
|
|
|
$grid = new GridField(
|
|
|
|
'Belongings',
|
|
|
|
'Testing Many Many',
|
|
|
|
$record->Belongings()->sort('Sort'),
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get the first record, which would be the first one to have column contents generated
|
|
|
|
/** @var ThroughIntermediary $expected */
|
|
|
|
$intermediary = $this->objFromFixture(ThroughIntermediary::class, 'One');
|
|
|
|
|
|
|
|
$result = $orderable->getColumnContent($grid, $record, 'irrelevant');
|
|
|
|
|
2021-11-08 23:43:41 +01:00
|
|
|
$this->assertStringContainsString(
|
2018-09-28 17:59:44 +02:00
|
|
|
'Belongings[GridFieldEditableColumns][' . $record->ID . '][Sort]',
|
|
|
|
$result,
|
|
|
|
'The field name is indexed under the record\'s ID'
|
|
|
|
);
|
2021-11-08 23:43:41 +01:00
|
|
|
$this->assertStringContainsString(
|
2018-09-28 17:59:44 +02:00
|
|
|
'value="' . $intermediary->Sort . '"',
|
|
|
|
$result,
|
|
|
|
'The value comes from the MMTL intermediary Sort value'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-11 03:50:13 +01:00
|
|
|
public function testPolymorphicManyManyListSortOrdersAreUsedForInitialRender()
|
|
|
|
{
|
|
|
|
$this->markTestSkipped('TODO: Introduce this test in the next minor release (3.3)');
|
|
|
|
|
|
|
|
$record = $this->objFromFixture(PolymorphM2MParent::class, 'ParentOne');
|
|
|
|
|
|
|
|
$orderable = new GridFieldOrderableRows('Sort');
|
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
|
|
|
|
|
|
|
$grid = new GridField(
|
|
|
|
'Children',
|
|
|
|
'Testing Polymorphic Many Many',
|
|
|
|
$record->Children()->sort('Sort'),
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get the first record, which would be the first one to have column contents generated
|
|
|
|
$intermediary = $this->objFromFixture(PolymorphM2MMapper::class, 'MapP1ToC1');
|
|
|
|
|
|
|
|
$result = $orderable->getColumnContent($grid, $record, 'irrelevant');
|
|
|
|
|
2021-11-08 23:43:41 +01:00
|
|
|
$this->assertStringContainsString(
|
2019-12-11 03:50:13 +01:00
|
|
|
'Children[GridFieldEditableColumns][' . $record->ID . '][Sort]',
|
|
|
|
$result,
|
|
|
|
'The field name is indexed under the record\'s ID'
|
|
|
|
);
|
2021-11-08 23:43:41 +01:00
|
|
|
$this->assertStringContainsString(
|
2019-12-11 03:50:13 +01:00
|
|
|
'value="' . $intermediary->Sort . '"',
|
|
|
|
$result,
|
|
|
|
'The value comes from the MMTL intermediary Sort value'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
);
|
2018-04-26 05:17:05 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'StubOrderedVersioned',
|
|
|
|
$orderable->setSortField('Sort')->getSortTable($parent->MyHasManySubclassOrderedVersioned())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReorderItemsSubclassVersioned()
|
|
|
|
{
|
|
|
|
$orderable = new GridFieldOrderableRows('Sort');
|
|
|
|
$reflection = new ReflectionMethod($orderable, 'executeReorder');
|
|
|
|
$reflection->setAccessible(true);
|
|
|
|
|
|
|
|
$parent = $this->objFromFixture(StubParent::class, 'parent-subclass-ordered-versioned');
|
|
|
|
|
|
|
|
// make sure all items are published
|
|
|
|
foreach ($parent->MyHasManySubclassOrderedVersioned() as $item) {
|
|
|
|
$item->publishRecursive();
|
|
|
|
}
|
|
|
|
|
|
|
|
// there should be no difference between stages at this point
|
|
|
|
$differenceFound = false;
|
|
|
|
foreach ($parent->MyHasManySubclassOrderedVersioned() as $item) {
|
|
|
|
/** @var StubSubclassOrderedVersioned|Versioned $item */
|
|
|
|
if ($item->stagesDiffer()) {
|
2018-06-25 02:22:27 +02:00
|
|
|
$this->fail('Unexpected difference found on stages');
|
2018-04-26 05:17:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reorder items
|
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
|
|
|
|
|
|
|
$grid = new GridField(
|
|
|
|
'TestField',
|
|
|
|
'TestField',
|
|
|
|
$parent->MyHasManySubclassOrderedVersioned()->sort('Sort', 'ASC'),
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
|
|
|
|
$originalOrder = $parent->MyHasManySubclassOrderedVersioned()
|
|
|
|
->sort('Sort', 'ASC')
|
|
|
|
->column('ID');
|
|
|
|
|
|
|
|
$desiredOrder = [];
|
|
|
|
|
|
|
|
// Make order non-contiguous, and 1-based
|
|
|
|
foreach (array_reverse($originalOrder) as $index => $id) {
|
|
|
|
$desiredOrder[$index * 2 + 1] = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertNotEquals($originalOrder, $desiredOrder);
|
|
|
|
|
|
|
|
$reflection->invoke($orderable, $grid, $desiredOrder);
|
|
|
|
|
|
|
|
$newOrder = $parent->MyHasManySubclassOrderedVersioned()
|
|
|
|
->sort('Sort', 'ASC')
|
|
|
|
->map('Sort', 'ID')
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
$this->assertEquals($desiredOrder, $newOrder);
|
|
|
|
|
|
|
|
// reorder should have been handled as versioned - there should be a difference between stages now
|
|
|
|
$differenceFound = false;
|
|
|
|
foreach ($parent->MyHasManySubclassOrderedVersioned() as $item) {
|
|
|
|
if ($item->stagesDiffer()) {
|
|
|
|
$differenceFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($differenceFound);
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
2022-02-02 03:36:25 +01:00
|
|
|
|
|
|
|
public function testGetManipulatedDataWithoutDefaultSort()
|
|
|
|
{
|
|
|
|
$sortedList = $this->getTitleSortedListForManipuatedData(TitleObject::class, [
|
|
|
|
['Title' => 'C'],
|
|
|
|
['Title' => 'A'],
|
|
|
|
['Title' => 'B'],
|
|
|
|
]);
|
|
|
|
$this->assertSame(['A', 'B', 'C'], $sortedList->column('Title'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetManipulatedDataWithDefaultSort()
|
|
|
|
{
|
|
|
|
$sortedList = $this->getTitleSortedListForManipuatedData(TitleSortedObject::class, [
|
|
|
|
['Title' => 'Z', 'Iden' => 'C', 'DefaultSort' => 3],
|
|
|
|
['Title' => 'Z', 'Iden' => 'A', 'DefaultSort' => 2],
|
|
|
|
['Title' => 'Z', 'Iden' => 'B', 'DefaultSort' => 1],
|
|
|
|
]);
|
|
|
|
$this->assertSame(['B', 'A', 'C'], $sortedList->column('Iden'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTitleSortedListForManipuatedData(string $dataClass, array $data): DataList
|
|
|
|
{
|
|
|
|
$list = new DataList($dataClass);
|
|
|
|
foreach ($data as $values) {
|
|
|
|
$item = new $dataClass();
|
|
|
|
$item->update($values);
|
|
|
|
$item->write();
|
|
|
|
$list->add($item);
|
|
|
|
}
|
|
|
|
$orderable = new GridFieldOrderableRows('Title');
|
|
|
|
$config = new GridFieldConfig_RelationEditor();
|
|
|
|
$config->addComponent($orderable);
|
|
|
|
$grid = new GridField('MyName', 'MyTitle', $list, $config);
|
|
|
|
$sortedList = $orderable->getManipulatedData($grid, $list);
|
|
|
|
return $sortedList;
|
|
|
|
}
|
2013-07-11 04:28:38 +02:00
|
|
|
}
|