Skip to content

Commit

Permalink
Merge pull request #262 from creative-commoners/pulls/4/fix-sorting
Browse files Browse the repository at this point in the history
FIX Linkfield sorting issue
  • Loading branch information
GuySartorelli authored Mar 26, 2024
2 parents 38d2aa4 + 8e8a47a commit bc1f7c9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function onBeforeWrite(): void
// Ensure a Sort value is set and that it's one larger than any other Sort value for
// this owner relation so that newly created Links on MultiLinkField's are properly sorted
if (!$this->Sort) {
$this->Sort = self::get()->filter([
$this->Sort = Link::get()->filter([
'OwnerID' => $this->OwnerID,
'OwnerRelation' => $this->OwnerRelation,
])->max('Sort') + 1;
Expand Down
83 changes: 82 additions & 1 deletion tests/php/Models/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use SilverStripe\LinkField\Services\LinkTypeService;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\LinkField\Tests\Controllers\LinkFieldControllerTest\TestPhoneLink;
use SilverStripe\LinkField\Tests\Models\SiteTreeLinkTest\TestSiteTreeCanView;

class LinkTest extends SapphireTest
{
Expand Down Expand Up @@ -479,9 +480,89 @@ public function provideLinkType(): array
/**
* @dataProvider provideLinkType
*/
public function testGetShortCode($class, $expected): void
public function testGetShortCode(string $class, string $expected): void
{
$linkClass = Injector::inst()->get($class);
$this->assertSame($expected, $linkClass->getShortCode());
}

/**
* Method populates DB with new links of different types that belong to one owner.
* Method returns array of new created links
*/
private function createLinks(): array
{
$linkIds = [];
$linkTypes = [
EmailLink::class,
SiteTreeLink::class,
EmailLink::class,
SiteTreeLink::class,
ExternalLink::class,
PhoneLink::class,
ExternalLink::class,
PhoneLink::class,
SiteTreeLink::class,
SiteTreeLink::class,
ExternalLink::class,
ExternalLink::class,
];

$linkOwner = new LinkOwner();
$linkOwner->write();

$page = new TestSiteTreeCanView(['Title' => 'My test page']);
$page->write();

foreach ($linkTypes as $class) {
$link = new $class();
$link->OwnerID = $linkOwner->ID;
$link->OwnerClass = $linkOwner->ClassName;
$link->OwnerRelation = 'LinkList';

if ($class === SiteTreeLink::class) {
$link->Page = $page->ID;
}

$link->write();
array_push($linkIds, $link->ID);
}

return $linkIds;
}

public function testSortingOneOwner(): void
{
$linkIDs = $this->createLinks();
$expectedSort = 1;

foreach ($linkIDs as $id) {
$link = Link::get()->byID($id);
$this->assertSame($expectedSort, $link->Sort, "Expected sort for {$link->ClassName}: {$expectedSort}");
$expectedSort++;
}
}
public function testSortingTwoOwners(): void
{
$linkOwnerOne = new LinkOwner();
$linkOwnerOne->write();

$linkOwnerTwo = new LinkOwner();
$linkOwnerTwo->write();

$linkOne = new EmailLink();
$linkOne->OwnerID = $linkOwnerOne->ID;
$linkOne->OwnerClass = $linkOwnerOne->ClassName;
$linkOne->OwnerRelation = 'LinkList';
$linkOne->write();

$linkTwo = new EmailLink();
$linkTwo->OwnerID = $linkOwnerTwo->ID;
$linkTwo->OwnerClass = $linkOwnerTwo->ClassName;
$linkTwo->OwnerRelation = 'LinkList';
$linkTwo->write();

$this->assertSame(1, $linkOne->Sort, "Expected sort for {$linkOne->ClassName}: 1");
$this->assertSame(1, $linkTwo->Sort, "Expected sort for {$linkTwo->ClassName}: 1");
}
}

0 comments on commit bc1f7c9

Please sign in to comment.