From 8e8a47aeba0f2ed3331f5344c38bae2036f2ec0a Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Tue, 26 Mar 2024 14:28:21 +1300 Subject: [PATCH] FIX Linkfield sorting issue --- src/Models/Link.php | 2 +- tests/php/Models/LinkTest.php | 83 ++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/Models/Link.php b/src/Models/Link.php index 79729b11..616c8909 100644 --- a/src/Models/Link.php +++ b/src/Models/Link.php @@ -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; diff --git a/tests/php/Models/LinkTest.php b/tests/php/Models/LinkTest.php index 9de1670a..2ba5a9f0 100644 --- a/tests/php/Models/LinkTest.php +++ b/tests/php/Models/LinkTest.php @@ -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 { @@ -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"); + } }