Skip to content

Commit

Permalink
Merge pull request #329 from creative-commoners/pulls/3/default-sort-…
Browse files Browse the repository at this point in the history
…unit-test

MNT Add unit tests for default sort
  • Loading branch information
GuySartorelli authored Jul 31, 2022
2 parents dc99a8e + 607a9bc commit acacac2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/GridFieldOrderableRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -46,6 +49,8 @@ class GridFieldOrderableRowsTest extends SapphireTest
ThroughDefiner::class,
ThroughIntermediary::class,
ThroughBelongs::class,
TitleObject::class,
TitleSortedObject::class,
];

public function reorderItemsProvider()
Expand Down Expand Up @@ -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;
}
}
15 changes: 15 additions & 0 deletions tests/Stub/TitleObject.php
Original file line number Diff line number Diff line change
@@ -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';
}
19 changes: 19 additions & 0 deletions tests/Stub/TitleSortedObject.php
Original file line number Diff line number Diff line change
@@ -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';
}

0 comments on commit acacac2

Please sign in to comment.