mirror of
https://github.com/symbiote/silverstripe-gridfieldextensions.git
synced 2024-10-22 17:05:39 +02:00
Corrected a case when sort column is located in a table that belongs to ancestor class of the item.
This commit is contained in:
parent
b93a8f28ac
commit
6b47b6d63a
@ -534,8 +534,20 @@ class GridFieldOrderableRows extends RequestHandler implements
|
|||||||
$isVersioned = false;
|
$isVersioned = false;
|
||||||
$class = $list->dataClass();
|
$class = $list->dataClass();
|
||||||
|
|
||||||
if (DataObject::getSchema()->tableName($class) == $this->getSortTable($list)) {
|
// check if sort column is present on the model provided by dataClass() and if it's versioned
|
||||||
$isVersioned = $class::has_extension(Versioned::class);
|
// cases:
|
||||||
|
// Model has sort column and is versioned - handle as versioned
|
||||||
|
// Model has sort column and is NOT versioned - handle as NOT versioned
|
||||||
|
// Model doesn't have sort column because sort column is on ManyManyList - handle as NOT versioned
|
||||||
|
|
||||||
|
// try to match table name, note that we have to cover the case where the table which has the sort column
|
||||||
|
// belongs to ancestor of the object which is populating the list
|
||||||
|
$classes = ClassInfo::ancestry($class, true);
|
||||||
|
foreach ($classes as $currentClass) {
|
||||||
|
if (DataObject::getSchema()->tableName($currentClass) == $this->getSortTable($list)) {
|
||||||
|
$isVersioned = $class::has_extension(Versioned::class);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through each item, and update the sort values which do not
|
// Loop through each item, and update the sort values which do not
|
||||||
|
@ -9,8 +9,10 @@ use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
|
|||||||
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
|
||||||
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild;
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild;
|
||||||
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered;
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered;
|
||||||
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderedVersioned;
|
||||||
use Symbiote\GridFieldExtensions\Tests\Stub\StubParent;
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubParent;
|
||||||
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclass;
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclass;
|
||||||
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned;
|
||||||
use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;
|
use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,17 +20,22 @@ use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;
|
|||||||
*/
|
*/
|
||||||
class GridFieldOrderableRowsTest extends SapphireTest
|
class GridFieldOrderableRowsTest extends SapphireTest
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
protected $usesDatabase = true;
|
* @var string
|
||||||
|
*/
|
||||||
protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';
|
protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
protected static $extra_dataobjects = [
|
protected static $extra_dataobjects = [
|
||||||
StubParent::class,
|
StubParent::class,
|
||||||
StubOrdered::class,
|
StubOrdered::class,
|
||||||
StubSubclass::class,
|
StubSubclass::class,
|
||||||
StubUnorderable::class,
|
StubUnorderable::class,
|
||||||
StubOrderableChild::class,
|
StubOrderableChild::class,
|
||||||
|
StubOrderedVersioned::class,
|
||||||
|
StubSubclassOrderedVersioned::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
public function testReorderItems()
|
public function testReorderItems()
|
||||||
@ -125,5 +132,80 @@ class GridFieldOrderableRowsTest extends SapphireTest
|
|||||||
'StubParent_MyManyMany',
|
'StubParent_MyManyMany',
|
||||||
$orderable->setSortField('ManyManySort')->getSortTable($parent->MyManyMany())
|
$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()) {
|
||||||
|
$differenceFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertFalse($differenceFound);
|
||||||
|
|
||||||
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,26 @@ Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered:
|
|||||||
item6:
|
item6:
|
||||||
Sort: 6
|
Sort: 6
|
||||||
nestedtest:
|
nestedtest:
|
||||||
Children: =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item1,=>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item2,=>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item3,=>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item4
|
Children:
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item1
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item2
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item3
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild.item4
|
||||||
|
|
||||||
|
Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned:
|
||||||
|
item1:
|
||||||
|
ExtraField: 1
|
||||||
|
Sort: 1
|
||||||
|
item2:
|
||||||
|
ExtraField: 2
|
||||||
|
Sort: 2
|
||||||
|
item3:
|
||||||
|
ExtraField: 3
|
||||||
|
Sort: 3
|
||||||
|
item4:
|
||||||
|
ExtraField: 4
|
||||||
|
Sort: 4
|
||||||
|
|
||||||
Symbiote\GridFieldExtensions\Tests\Stub\StubParent:
|
Symbiote\GridFieldExtensions\Tests\Stub\StubParent:
|
||||||
parent:
|
parent:
|
||||||
MyManyMany:
|
MyManyMany:
|
||||||
@ -37,3 +56,9 @@ Symbiote\GridFieldExtensions\Tests\Stub\StubParent:
|
|||||||
ManyManySort: 108
|
ManyManySort: 108
|
||||||
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered.item6:
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered.item6:
|
||||||
ManyManySort: 108
|
ManyManySort: 108
|
||||||
|
parent-subclass-ordered-versioned:
|
||||||
|
MyHasManySubclassOrderedVersioned:
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned.item1
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned.item2
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned.item3
|
||||||
|
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned.item4
|
||||||
|
33
tests/Stub/StubOrderedVersioned.php
Normal file
33
tests/Stub/StubOrderedVersioned.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symbiote\GridFieldExtensions\Tests\Stub;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class StubOrderedVersioned
|
||||||
|
* @package Symbiote\GridFieldExtensions\Tests\Stub
|
||||||
|
*/
|
||||||
|
class StubOrderedVersioned extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $table_name = 'StubOrderedVersioned';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $extensions = [
|
||||||
|
Versioned::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $db = [
|
||||||
|
'Sort' => 'Int',
|
||||||
|
];
|
||||||
|
}
|
@ -9,7 +9,8 @@ class StubParent extends DataObject implements TestOnly
|
|||||||
{
|
{
|
||||||
private static $has_many = array(
|
private static $has_many = array(
|
||||||
'MyHasMany' => StubOrdered::class,
|
'MyHasMany' => StubOrdered::class,
|
||||||
'MyHasManySubclass' => StubSubclass::class
|
'MyHasManySubclass' => StubSubclass::class,
|
||||||
|
'MyHasManySubclassOrderedVersioned' => StubSubclassOrderedVersioned::class,
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $many_many = array(
|
private static $many_many = array(
|
||||||
|
33
tests/Stub/StubSubclassOrderedVersioned.php
Normal file
33
tests/Stub/StubSubclassOrderedVersioned.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symbiote\GridFieldExtensions\Tests\Stub;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class StubOrderedVersioned
|
||||||
|
* @package Symbiote\GridFieldExtensions\Tests\Stub
|
||||||
|
*/
|
||||||
|
class StubSubclassOrderedVersioned extends StubOrderedVersioned
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $table_name = 'StubSubclassOrderedVersioned';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $db = [
|
||||||
|
'ExtraField' => 'Int',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $has_one = [
|
||||||
|
'Parent' => StubParent::class,
|
||||||
|
];
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user