setAccessible(true); $parent = $this->objFromFixture(StubParent::class, 'parent'); $config = new GridFieldConfig_RelationEditor(); $config->addComponent($orderable); $grid = new GridField( 'MyManyMany', 'My Many Many', $parent->MyManyMany()->sort('ManyManySort'), $config ); $originalOrder = $parent->MyManyMany()->sort('ManyManySort')->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->MyManyMany()->sort('ManyManySort')->map('ManyManySort', 'ID')->toArray(); $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 \Symbiote\GridFieldExtensions\GridFieldOrderableRows::getSortTable */ public function testGetSortTable() { $orderable = new GridFieldOrderableRows(); $parent = new StubParent(); $parent->write(); $this->assertEquals( 'StubOrdered', $orderable->getSortTable($parent->MyHasMany()) ); $this->assertEquals( 'StubOrdered', $orderable->getSortTable($parent->MyHasManySubclass()) ); $this->assertEquals( 'StubOrdered', $orderable->getSortTable($parent->MyManyMany()) ); $this->assertEquals( '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()) { $this->fail('Unexpected diference 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); } }