Merge branch '2' into 3

This commit is contained in:
Daniel Hensby 2018-02-21 11:58:48 +00:00
commit 8c82965e7e
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
6 changed files with 92 additions and 15 deletions

View File

@ -545,10 +545,8 @@ class GridFieldOrderableRows extends RequestHandler implements
$sortTable = $this->getSortTable($list); $sortTable = $this->getSortTable($list);
$now = DBDatetime::now()->Rfc2822(); $now = DBDatetime::now()->Rfc2822();
$additionalSQL = ''; $additionalSQL = '';
$baseTable = $sortTable; $baseTable = DataObject::getSchema()->baseDataTable($list->dataClass());
if (class_exists($sortTable)) {
$baseTable = singleton($sortTable)->baseTable();
}
$isBaseTable = ($baseTable == $sortTable); $isBaseTable = ($baseTable == $sortTable);
if (!$list instanceof ManyManyList && $isBaseTable) { if (!$list instanceof ManyManyList && $isBaseTable) {
$additionalSQL = ", \"LastEdited\" = '$now'"; $additionalSQL = ", \"LastEdited\" = '$now'";
@ -565,7 +563,7 @@ class GridFieldOrderableRows extends RequestHandler implements
$this->getSortTableClauseForIds($list, $id) $this->getSortTableClauseForIds($list, $id)
)); ));
if (!$isBaseTable) { if (!$isBaseTable && !$list instanceof ManyManyList) {
DB::query(sprintf( DB::query(sprintf(
'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s', 'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s',
$baseTable, $baseTable,
@ -600,10 +598,8 @@ class GridFieldOrderableRows extends RequestHandler implements
$clause = sprintf('"%s"."%s" = 0', $table, $this->getSortField()); $clause = sprintf('"%s"."%s" = 0', $table, $this->getSortField());
$now = DBDatetime::now()->Rfc2822(); $now = DBDatetime::now()->Rfc2822();
$additionalSQL = ''; $additionalSQL = '';
$baseTable = $table; $baseTable = DataObject::getSchema()->baseDataTable($list->dataClass());
if (class_exists($table)) {
$baseTable = singleton($table)->baseTable();
}
$isBaseTable = ($baseTable == $table); $isBaseTable = ($baseTable == $table);
if (!$list instanceof ManyManyList && $isBaseTable) { if (!$list instanceof ManyManyList && $isBaseTable) {
$additionalSQL = ", \"LastEdited\" = '$now'"; $additionalSQL = ", \"LastEdited\" = '$now'";
@ -622,7 +618,7 @@ class GridFieldOrderableRows extends RequestHandler implements
$this->getSortTableClauseForIds($list, $id) $this->getSortTableClauseForIds($list, $id)
)); ));
if (!$isBaseTable) { if (!$isBaseTable && !$list instanceof ManyManyList) {
DB::query(sprintf( DB::query(sprintf(
'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s', 'UPDATE "%s" SET "LastEdited" = \'%s\' WHERE %s',
$baseTable, $baseTable,

View File

@ -7,9 +7,11 @@ use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; 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\StubOrdered; use Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered;
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\StubUnorderable;
/** /**
* Tests for the {@link GridFieldOrderableRows} component. * Tests for the {@link GridFieldOrderableRows} component.
@ -21,11 +23,13 @@ class GridFieldOrderableRowsTest extends SapphireTest
protected static $fixture_file = 'GridFieldOrderableRowsTest.yml'; protected static $fixture_file = 'GridFieldOrderableRowsTest.yml';
protected static $extra_dataobjects = array( protected static $extra_dataobjects = [
StubParent::class, StubParent::class,
StubOrdered::class, StubOrdered::class,
StubSubclass::class, StubSubclass::class,
); StubUnorderable::class,
StubOrderableChild::class,
];
public function testReorderItems() public function testReorderItems()
{ {
@ -46,7 +50,7 @@ class GridFieldOrderableRowsTest extends SapphireTest
); );
$originalOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID'); $originalOrder = $parent->MyManyMany()->sort('ManyManySort')->column('ID');
$desiredOrder = array(); $desiredOrder = [];
// Make order non-contiguous, and 1-based // Make order non-contiguous, and 1-based
foreach (array_reverse($originalOrder) as $index => $id) { foreach (array_reverse($originalOrder) as $index => $id) {
@ -62,8 +66,38 @@ class GridFieldOrderableRowsTest extends SapphireTest
$this->assertEquals($desiredOrder, $newOrder); $this->assertEquals($desiredOrder, $newOrder);
} }
public function testSortableChildClass()
{
$orderable = new GridFieldOrderableRows('Sort');
$reflection = new ReflectionMethod($orderable, 'executeReorder');
$reflection->setAccessible(true);
$parent = $this->objFromFixture(StubOrdered::class, 'nestedtest');
$config = new GridFieldConfig_RelationEditor();
$config->addComponent($orderable);
$grid = new GridField(
'Children',
'Children',
$parent->Children(),
$config
);
$originalOrder = $parent->Children()->column('ID');
$desiredOrder = array_reverse($originalOrder);
$this->assertNotEquals($originalOrder, $desiredOrder);
$reflection->invoke($orderable, $grid, $desiredOrder);
$newOrder = $parent->Children()->column('ID');
$this->assertEquals($desiredOrder, $newOrder);
}
/** /**
* @covers GridFieldOrderableRows::getSortTable * @covers \Symbiote\GridFieldExtensions\GridFieldOrderableRows::getSortTable
*/ */
public function testGetSortTable() public function testGetSortTable()
{ {

View File

@ -1,3 +1,12 @@
Symbiote\GridFieldExtensions\Tests\Stub\StubOrderableChild:
item1:
Sort: 1
item2:
Sort: 2
item3:
Sort: 3
item4:
Sort: 4
Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered: Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered:
item1: item1:
Sort: 1 Sort: 1
@ -11,7 +20,8 @@ Symbiote\GridFieldExtensions\Tests\Stub\StubOrdered:
Sort: 5 Sort: 5
item6: item6:
Sort: 6 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
Symbiote\GridFieldExtensions\Tests\Stub\StubParent: Symbiote\GridFieldExtensions\Tests\Stub\StubParent:
parent: parent:
MyManyMany: MyManyMany:

View File

@ -0,0 +1,18 @@
<?php
namespace Symbiote\GridFieldExtensions\Tests\Stub;
class StubOrderableChild extends StubUnorderable
{
private static $db = [
'Sort' => 'Int',
];
private static $has_one = [
'Parent' => StubOrdered::class,
];
private static $default_sort = '"Sort" ASC';
private static $table_name = 'StubOrderableChild';
}

View File

@ -15,6 +15,10 @@ class StubOrdered extends DataObject implements TestOnly
'Parent' => StubParent::class 'Parent' => StubParent::class
); );
private static $has_many = array(
'Children' => StubOrderableChild::class,
);
private static $belongs_many_many =array( private static $belongs_many_many =array(
'MyManyMany' => StubParent::class, 'MyManyMany' => StubParent::class,
); );

View File

@ -0,0 +1,15 @@
<?php
namespace Symbiote\GridFieldExtensions\Tests\Stub;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
class StubUnorderable extends DataObject implements TestOnly
{
private static $db = [
'Title' => 'Varchar',
];
private static $table_name = 'StubUnorderable';
}