Skip to content

Commit

Permalink
Add test for sortable logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Sep 15, 2023
1 parent 2af595b commit a0dae15
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Form/ManyAnyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,15 @@ private function shiftRecordByID(array &$data, int $id): ?array
*/
private function buildSortOrder(array $value)
{
// Build the list of IDs ond the sorted order
// Build the list of IDs and the sorted order
$map = array_map(function ($item) {
return $item['ID'];
}, $value);

// We want our first key to be 1, so we don't have a sort value of 0
array_unshift($map, "phoney");
unset($map[0]);

// Flip it so the ID returns its order
return array_flip($map);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/php/Extensions/SortableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SilverStripe\AnyField\Tests\Extensions;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\AnyField\Extensions\Sortable;

class SortableTest extends SapphireTest
{
protected static $required_extensions = [
Link::class => [
Sortable::class,
],
];

public function testGetCMSFields()
{
$link = Link::create();
$fields = $link->getCMSFields();

$this->assertNull($fields->fieldByName('Root.Main.Sort'), 'Sort field has been removed from FieldList');
$this->assertNotNull($fields->fieldByName('Root.Main.Title'));
}
}
114 changes: 114 additions & 0 deletions tests/php/Form/ManyAnyFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use SilverStripe\LinkField\Models\EmailLink;
use SilverStripe\AnyField\Services\AnyService;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\AnyField\Extensions\Sortable;
use LogicException;

class ManyAnyFieldTest extends AllowedClassesTraitTestCase
{
Expand All @@ -24,6 +26,7 @@ class ManyAnyFieldTest extends AllowedClassesTraitTestCase
protected static $required_extensions = [
Link::class => [
AnyFieldTest\ForcePermissionExtension::class,
Sortable::class,
],
];

Expand Down Expand Up @@ -199,6 +202,52 @@ public function testSaveInto()
);
}

public function testSaveIntoWithSort()
{
$owner = $this->objFromFixture(LinkOwner::class, 'link-owner-1');
$externalLinkID = $this->idFromFixture(ExternalLink::class, 'link-2');
$emailLinkID = $this->idFromFixture(EmailLink::class, 'link-3');

$this->assertEquals(
[
$externalLinkID => 'Link2',
$emailLinkID => 'Link3'
],
$owner->Links()->map()->toArray(),
'Initial link list contains 2 elements'
);

$field = new ManyAnyField('Links', 'Links', $owner->Links());
$submittedData = [
[
'Title' => 'Link3',
'Email' => 'hello@silverstripe.org',
'ID' => $emailLinkID,
'dataObjectClassKey' => EmailLink::class,
],
[
'ID' => $externalLinkID,
'Title' => 'Link2',
'ExternalUrl' => 'http://www.google.co.nz',
'dataObjectClassKey' => ExternalLink::class,
]
];
$field->setBaseClass(Link::class);
$field->setSort('Sort');
$field->setValue(json_encode($submittedData), $owner);
$field->saveInto($owner);
$owner->write();

$this->assertEquals(
[
$emailLinkID => 1,
$externalLinkID => 2,
],
$owner->Links()->map('ID', 'Sort')->toArray(),
'Order of link has been reversed'
);
}

public function testCanCreatePermission()
{
$this->expectExceptionCode(403, 'ManyAnyField respects can create permission');
Expand Down Expand Up @@ -299,4 +348,69 @@ public function testInputValue()
'InputValue can convert array to JSON'
);
}

public function testGetProps()
{
$field = $this->getAnyField();
$field->setBaseClass(EmailLink::class);
$props = $field->getProps();
$expected = [
'allowedDataObjectClasses' => AnyService::singleton()->getAllowedDataObjectClasses(
EmailLink::class,
true,
[]
),
'baseDataObjectName' => 'Email Link',
'baseDataObjectIcon' => 'p-mail',
'name' => $field->getName(),
'value' => '',
'extraClass' => $field->extraClass(),
'id' => $field->ID(),
'disabled' => false,
'readOnly' => false,
'autofocus' => false,
'sortable' => false,
'title' => $field->Title(),
];

ksort($expected);
ksort($props);

$this->assertEquals(
$expected,
$props
);
}

public function testSort()
{
$field = $this->getAnyField();
$field->setBaseClass(Link::class);
$this->assertNull($field->getSort(), 'Sort is null by default');
$props = $field->getProps();
$this->assertFalse($props['sortable'], 'ManyAnyField is not sortable by default');


$field->setSort('Sort');
$this->assertEquals('Sort', $field->getSort(), 'Sort value can be set');
$props = $field->getProps();
$this->assertTrue($props['sortable'], 'Setting sort enables sorting');


$field->setSort(null);
$this->assertNull($field->getSort(), 'Can unset sort value');
$props = $field->getProps();
$this->assertFalse($props['sortable'], 'Unsetting sort disable sorting');
}

public function testInvalidSort()
{
$this->expectException(LogicException::class, 'ManyAnyField throws an exception when the sort column is invalid');

$field = $this->getAnyField();
$field->setBaseClass(Link::class);

$field->setSort('NonExistingColumn');
$props = $field->getProps();
}
}

0 comments on commit a0dae15

Please sign in to comment.