silverstripe-gridfieldexten.../tests/GridFieldOrderableRowsTest.php

225 lines
7.0 KiB
PHP
Raw Normal View History

<?php
namespace Symbiote\GridFieldExtensions\Tests;
use ReflectionMethod;
use SilverStripe\Dev\SapphireTest;
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;
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;
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner;
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary;
use Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs;
/**
* Tests for the {@link GridFieldOrderableRows} component.
*/
2016-12-21 03:34:58 +01:00
class GridFieldOrderableRowsTest extends SapphireTest
{
/**
* @var string
*/
protected static $fixture_file = [
'GridFieldOrderableRowsTest.yml',
'OrderableRowsThroughTest.yml'
];
2016-07-21 16:04:37 +02:00
2018-02-21 12:58:48 +01:00
protected static $extra_dataobjects = [
StubParent::class,
StubOrdered::class,
StubSubclass::class,
2018-02-21 12:58:48 +01:00
StubUnorderable::class,
StubOrderableChild::class,
StubOrderedVersioned::class,
StubSubclassOrderedVersioned::class,
ThroughDefiner::class,
ThroughIntermediary::class,
ThroughBelongs::class,
2018-02-21 12:58:48 +01:00
];
2016-07-21 13:55:03 +02:00
public function reorderItemsProvider()
2016-12-21 03:34:58 +01:00
{
return [
[StubParent::class . '.parent', 'MyManyMany', 'ManyManySort'],
[ThroughDefiner::class . '.DefinerOne', 'Belongings', 'Sort'],
];
}
/**
* @dataProvider reorderItemsProvider
*/
public function testReorderItems($fixtureID, $relationName, $sortName)
{
$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
list($parentClass, $parentInstanceID) = explode('.', $fixtureID);
$parent = $this->objFromFixture($parentClass, $parentInstanceID);
2016-12-21 03:34:58 +01:00
$grid = new GridField(
$relationName,
'Testing Many Many',
$parent->$relationName()->sort($sortName),
2016-12-21 03:34:58 +01:00
$config
);
$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
$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-02-21 12:58:48 +01:00
public function testSortableChildClass()
{
$orderable = new GridFieldOrderableRows('Sort');
$reflection = new ReflectionMethod($orderable, 'executeReorder');
$reflection->setAccessible(true);
2018-02-21 12:58:48 +01:00
$parent = $this->objFromFixture(StubOrdered::class, 'nestedtest');
2018-02-21 12:58:48 +01:00
$config = new GridFieldConfig_RelationEditor();
$config->addComponent($orderable);
2018-02-21 12:58:48 +01:00
$grid = new GridField(
'Children',
'Children',
$parent->Children(),
$config
);
2018-02-21 12:58:48 +01:00
$originalOrder = $parent->Children()->column('ID');
$desiredOrder = array_reverse($originalOrder);
2018-02-21 12:58:48 +01:00
$this->assertNotEquals($originalOrder, $desiredOrder);
2018-02-21 12:58:48 +01:00
$reflection->invoke($orderable, $grid, $desiredOrder);
2018-02-21 12:58:48 +01:00
$newOrder = $parent->Children()->column('ID');
2018-02-21 12:58:48 +01:00
$this->assertEquals($desiredOrder, $newOrder);
}
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()
{
$orderable = new GridFieldOrderableRows();
$parent = new StubParent();
2016-12-21 03:34:58 +01:00
$parent->write();
2016-12-21 03:34:58 +01:00
$this->assertEquals(
'StubOrdered',
2016-12-21 03:34:58 +01:00
$orderable->getSortTable($parent->MyHasMany())
);
2016-12-21 03:34:58 +01:00
$this->assertEquals(
'StubOrdered',
2016-12-21 03:34:58 +01:00
$orderable->getSortTable($parent->MyHasManySubclass())
);
2016-12-21 03:34:58 +01:00
$this->assertEquals(
'StubOrdered',
2016-12-21 03:34:58 +01:00
$orderable->getSortTable($parent->MyManyMany())
);
2016-12-21 03:34:58 +01:00
$this->assertEquals(
'StubParent_MyManyMany',
2016-12-21 03:34:58 +01:00
$orderable->setSortField('ManyManySort')->getSortTable($parent->MyManyMany())
);
$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()) {
$this->fail('Unexpected difference found on stages');
}
}
// 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
}
}