From 1f9eb578be3ee912c04d8aec05295faf8259d64d Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 12 Aug 2024 13:27:36 +1200 Subject: [PATCH 1/2] MNT Add extension in finally block --- .../Controllers/LinkFieldControllerTest.php | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/tests/php/Controllers/LinkFieldControllerTest.php b/tests/php/Controllers/LinkFieldControllerTest.php index a4b0691a..24a9b759 100644 --- a/tests/php/Controllers/LinkFieldControllerTest.php +++ b/tests/php/Controllers/LinkFieldControllerTest.php @@ -537,38 +537,41 @@ public function testLinkArchive( int $expectedCode ): void { TestPhoneLink::$fail = $fail; - if ($fail == 'can-delete') { + if ($fail === 'can-delete') { // Remove the Versioned extension because there's logic in LinkFieldController // to use either canArchive() or canDelete() based on the presence of the Versioned extension // Note that you must remove the extension on the base class rather than a TestOnly subclass Link::remove_extension(Versioned::class); } - $owner = $this->getFixtureLinkOwner(); - $ownerID = $owner->ID; - $ownerClass = urlencode($owner->ClassName); - $ownerRelation = 'Link'; - $ownerLinkID = $owner->LinkID; - $id = $this->getID($idType); - $fixtureID = $this->getFixtureLink()->ID; - if ($id === -1) { - $url = "/admin/linkfield/delete?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation"; - } else { - $url = "/admin/linkfield/delete/$id?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation"; - } - $headers = []; - if ($fail !== 'csrf-token') { - $headers = array_merge($headers, $this->csrfTokenheader()); - } - $response = $this->mainSession->sendRequest('DELETE', $url, [], $headers); - $this->assertSame('application/json', $response->getHeader('Content-type')); - $this->assertSame($expectedCode, $response->getStatusCode()); - if ($expectedCode >= 400) { - $this->assertNotNull(TestPhoneLink::get()->byID($fixtureID)); - } else { - $this->assertNull(TestPhoneLink::get()->byID($fixtureID)); - } - if ($fail == 'can-delete') { - Link::add_extension(Versioned::class); + try { + $owner = $this->getFixtureLinkOwner(); + $ownerID = $owner->ID; + $ownerClass = urlencode($owner->ClassName); + $ownerRelation = 'Link'; + $ownerLinkID = $owner->LinkID; + $id = $this->getID($idType); + $fixtureID = $this->getFixtureLink()->ID; + if ($id === -1) { + $url = "/admin/linkfield/delete?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation"; + } else { + $url = "/admin/linkfield/delete/$id?ownerID=$ownerID&ownerClass=$ownerClass&ownerRelation=$ownerRelation"; + } + $headers = []; + if ($fail !== 'csrf-token') { + $headers = array_merge($headers, $this->csrfTokenheader()); + } + $response = $this->mainSession->sendRequest('DELETE', $url, [], $headers); + $this->assertSame('application/json', $response->getHeader('Content-type')); + $this->assertSame($expectedCode, $response->getStatusCode()); + if ($expectedCode >= 400) { + $this->assertNotNull(TestPhoneLink::get()->byID($fixtureID)); + } else { + $this->assertNull(TestPhoneLink::get()->byID($fixtureID)); + } + } finally { + if ($fail === 'can-delete') { + Link::add_extension(Versioned::class); + } } } From c76fdba2b7f0c3a1ece58d702c631f5552eb6073 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:56:19 +1200 Subject: [PATCH 2/2] ENH Don't emit deprecation warnings for unavoidable API calls (#319) --- src/Controllers/LinkFieldController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Controllers/LinkFieldController.php b/src/Controllers/LinkFieldController.php index 2e5bb2bc..840d5c25 100644 --- a/src/Controllers/LinkFieldController.php +++ b/src/Controllers/LinkFieldController.php @@ -16,6 +16,7 @@ use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Dev\Deprecation; use SilverStripe\Forms\HiddenField; use SilverStripe\LinkField\Services\LinkTypeService; use SilverStripe\ORM\DataList; @@ -133,7 +134,8 @@ public function linkDelete(): HTTPResponse } $link = $this->linkFromRequest(); if ($link->hasExtension(Versioned::class)) { - if (!$link->canArchive()) { + $canArchive = Deprecation::withNoReplacement(fn() => $link->canArchive()); + if (!$canArchive) { $this->jsonError(403); } $link->doArchive();