Skip to content

Commit

Permalink
Corrected a case when sort column is located in a table that belongs …
Browse files Browse the repository at this point in the history
…to ancestor class of the item.
  • Loading branch information
mfendeksilverstripe committed Apr 26, 2018
1 parent b93a8f2 commit 6b47b6d
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 7 deletions.
16 changes: 14 additions & 2 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,20 @@ protected function reorderItems($list, array $values, array $sortedIDs)
$isVersioned = false;
$class = $list->dataClass();

if (DataObject::getSchema()->tableName($class) == $this->getSortTable($list)) {
$isVersioned = $class::has_extension(Versioned::class);
// check if sort column is present on the model provided by dataClass() and if it's versioned
// 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
Expand Down
88 changes: 85 additions & 3 deletions tests/GridFieldOrderableRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild;
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered;
use Symbiote\GridFieldExtensions\Tests\Stub\StubOrderedVersioned;
use Symbiote\GridFieldExtensions\Tests\Stub\StubParent;
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclass;
use Symbiote\GridFieldExtensions\Tests\Stub\StubSubclassOrderedVersioned;
use Symbiote\GridFieldExtensions\Tests\Stub\StubUnorderable;

/**
* Tests for the {@link GridFieldOrderableRows} component.
*/
class GridFieldOrderableRowsTest extends SapphireTest
{

protected $usesDatabase = true;

/**
* @var string
*/
protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';

/**
* @var array
*/
protected static $extra_dataobjects = [
StubParent::class,
StubOrdered::class,
StubSubclass::class,
StubUnorderable::class,
StubOrderableChild::class,
StubOrderedVersioned::class,
StubSubclassOrderedVersioned::class,
];

public function testReorderItems()
Expand Down Expand Up @@ -125,5 +132,80 @@ public function testGetSortTable()
'StubParent_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);
}
}
27 changes: 26 additions & 1 deletion tests/GridFieldOrderableRowsTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,26 @@ Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered:
item6:
Sort: 6
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:
parent:
MyManyMany:
Expand All @@ -37,3 +56,9 @@ Symbiote\GridFieldExtensions\Tests\Stub\StubParent:
ManyManySort: 108
- =>Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered.item6:
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 changes: 33 additions & 0 deletions tests/Stub/StubOrderedVersioned.php
Original file line number Diff line number Diff line change
@@ -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',
];
}
3 changes: 2 additions & 1 deletion tests/Stub/StubParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class StubParent extends DataObject implements TestOnly
{
private static $has_many = array(
'MyHasMany' => StubOrdered::class,
'MyHasManySubclass' => StubSubclass::class
'MyHasManySubclass' => StubSubclass::class,
'MyHasManySubclassOrderedVersioned' => StubSubclassOrderedVersioned::class,
);

private static $many_many = array(
Expand Down
33 changes: 33 additions & 0 deletions tests/Stub/StubSubclassOrderedVersioned.php
Original file line number Diff line number Diff line change
@@ -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,
];
}

0 comments on commit 6b47b6d

Please sign in to comment.