Merge pull request #329 from creative-commoners/pulls/3/default-sort-unit-test

MNT Add unit tests for default sort
This commit is contained in:
Guy Sartorelli 2022-08-01 11:25:29 +12:00 committed by GitHub
commit acacac2563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 0 deletions

View File

@ -6,6 +6,7 @@ use ReflectionMethod;
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\StubUnorderable;
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 @@ class GridFieldOrderableRowsTest extends SapphireTest
$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;
}
}

View File

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

View File

@ -0,0 +1,19 @@
<?php
namespace Symbiote\GridFieldExtensions\Tests\Stub;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
class TitleSortedObject extends DataObject implements TestOnly
{
private static $db = [
'Title' => 'Varchar',
'Iden' => 'Varchar',
'DefaultSort' => 'Int'
];
private static $default_sort = '"DefaultSort" ASC';
private static $table_name = 'TitleSortedObject';
}