From 607a9bcdf91eb5316ee523f8379e6a4107dbca01 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 2 Feb 2022 15:36:25 +1300 Subject: [PATCH] MNT Add unit tests for default sort --- tests/GridFieldOrderableRowsTest.php | 42 ++++++++++++++++++++++++++++ tests/Stub/TitleObject.php | 15 ++++++++++ tests/Stub/TitleSortedObject.php | 19 +++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 tests/Stub/TitleObject.php create mode 100644 tests/Stub/TitleSortedObject.php diff --git a/tests/GridFieldOrderableRowsTest.php b/tests/GridFieldOrderableRowsTest.php index 44e9f1b..5f84e14 100644 --- a/tests/GridFieldOrderableRowsTest.php +++ b/tests/GridFieldOrderableRowsTest.php @@ -6,6 +6,7 @@ use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; +use SilverStripe\ORM\DataList; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MChild; use Symbiote\GridFieldExtensions\Tests\Stub\PolymorphM2MMapper; @@ -20,6 +21,8 @@ use Symbiote\GridFieldExtensions\Tests\Stub\ThroughDefiner; use Symbiote\GridFieldExtensions\Tests\Stub\ThroughIntermediary; use Symbiote\GridFieldExtensions\Tests\Stub\ThroughBelongs; +use Symbiote\GridFieldExtensions\Tests\Stub\TitleObject; +use Symbiote\GridFieldExtensions\Tests\Stub\TitleSortedObject; /** * Tests for the {@link GridFieldOrderableRows} component. @@ -46,6 +49,8 @@ class GridFieldOrderableRowsTest extends SapphireTest ThroughDefiner::class, ThroughIntermediary::class, ThroughBelongs::class, + TitleObject::class, + TitleSortedObject::class, ]; public function reorderItemsProvider() @@ -296,4 +301,41 @@ public function testReorderItemsSubclassVersioned() $this->assertTrue($differenceFound); } + + public function testGetManipulatedDataWithoutDefaultSort() + { + $sortedList = $this->getTitleSortedListForManipuatedData(TitleObject::class, [ + ['Title' => 'C'], + ['Title' => 'A'], + ['Title' => 'B'], + ]); + $this->assertSame(['A', 'B', 'C'], $sortedList->column('Title')); + } + + public function testGetManipulatedDataWithDefaultSort() + { + $sortedList = $this->getTitleSortedListForManipuatedData(TitleSortedObject::class, [ + ['Title' => 'Z', 'Iden' => 'C', 'DefaultSort' => 3], + ['Title' => 'Z', 'Iden' => 'A', 'DefaultSort' => 2], + ['Title' => 'Z', 'Iden' => 'B', 'DefaultSort' => 1], + ]); + $this->assertSame(['B', 'A', 'C'], $sortedList->column('Iden')); + } + + private function getTitleSortedListForManipuatedData(string $dataClass, array $data): DataList + { + $list = new DataList($dataClass); + foreach ($data as $values) { + $item = new $dataClass(); + $item->update($values); + $item->write(); + $list->add($item); + } + $orderable = new GridFieldOrderableRows('Title'); + $config = new GridFieldConfig_RelationEditor(); + $config->addComponent($orderable); + $grid = new GridField('MyName', 'MyTitle', $list, $config); + $sortedList = $orderable->getManipulatedData($grid, $list); + return $sortedList; + } } diff --git a/tests/Stub/TitleObject.php b/tests/Stub/TitleObject.php new file mode 100644 index 0000000..8d993a2 --- /dev/null +++ b/tests/Stub/TitleObject.php @@ -0,0 +1,15 @@ + 'Varchar', + ]; + + private static $table_name = 'TitleObject'; +} diff --git a/tests/Stub/TitleSortedObject.php b/tests/Stub/TitleSortedObject.php new file mode 100644 index 0000000..35e9e18 --- /dev/null +++ b/tests/Stub/TitleSortedObject.php @@ -0,0 +1,19 @@ + 'Varchar', + 'Iden' => 'Varchar', + 'DefaultSort' => 'Int' + ]; + + private static $default_sort = '"DefaultSort" ASC'; + + private static $table_name = 'TitleSortedObject'; +}